formfields.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. #
  3. # cms.py - simple WSGI/Python based CMS script
  4. #
  5. # Copyright (C) 2011-2022 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #from cms.cython_support cimport * #@cy
  20. from cms.exception import *
  21. from cms.util import * #+cimport
  22. import cms.multipart as multipart
  23. __all__ = [
  24. "CMSFormFields",
  25. ]
  26. class CMSFormFields(object):
  27. """Form field parser.
  28. """
  29. __slots__ = (
  30. "__forms",
  31. )
  32. defaultCharset = LOWERCASE + UPPERCASE + NUMBERS + "-_. \t"
  33. defaultCharsetBool = LOWERCASE + UPPERCASE + NUMBERS + " \t"
  34. defaultCharsetInt = NUMBERS + "xXabcdefABCDEF- \t"
  35. def __init__(self, body, bodyType):
  36. try:
  37. forms, files = multipart.parse_form_data(
  38. environ={
  39. "wsgi.input" : BytesIO(body),
  40. "CONTENT_LENGTH" : str(len(body)),
  41. "CONTENT_TYPE" : bodyType,
  42. "REQUEST_METHOD" : "POST",
  43. }
  44. )
  45. self.__forms = forms
  46. except Exception as e:
  47. raise CMSException(400, "Cannot parse form data.")
  48. def items(self):
  49. return {
  50. k: v.encode("UTF-8", "strict")
  51. for k, v in self.__forms.iterallitems()
  52. }.items()
  53. def getStr(self, name, default="", maxlen=32, charset=defaultCharset):
  54. """Get a form field.
  55. Returns a str.
  56. """
  57. field = self.__forms.get(name, default)
  58. if field is None:
  59. return None
  60. if maxlen is not None and len(field) > maxlen:
  61. raise CMSException(400, "Form data is too long.")
  62. if charset is not None and [ c for c in field if c not in charset ]:
  63. raise CMSException(400, "Invalid character in form data")
  64. return field
  65. def getBool(self, name, default=False, maxlen=32, charset=defaultCharsetBool):
  66. """Get a form field.
  67. Returns a bool.
  68. """
  69. field = self.getStr(name, None, maxlen, charset)
  70. if field is None:
  71. return default
  72. return stringBool(field, default)
  73. def getInt(self, name, default=0, maxlen=32, charset=defaultCharsetInt):
  74. """Get a form field.
  75. Returns an int.
  76. """
  77. field = self.getStr(name, None, maxlen, charset)
  78. if field is None:
  79. return default
  80. try:
  81. field = field.lower().strip()
  82. if field.startswith("0x"):
  83. return int(field[2:], 16)
  84. return int(field)
  85. except ValueError:
  86. return default