httputils.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  6. #
  7. # This library 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 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library 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 Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Helper functions for HTTP.
  21. """
  22. from http import client
  23. from radicale.log import logger
  24. NOT_ALLOWED = (
  25. client.FORBIDDEN, (("Content-Type", "text/plain"),),
  26. "Access to the requested resource forbidden.")
  27. FORBIDDEN = (
  28. client.FORBIDDEN, (("Content-Type", "text/plain"),),
  29. "Action on the requested resource refused.")
  30. BAD_REQUEST = (
  31. client.BAD_REQUEST, (("Content-Type", "text/plain"),), "Bad Request")
  32. NOT_FOUND = (
  33. client.NOT_FOUND, (("Content-Type", "text/plain"),),
  34. "The requested resource could not be found.")
  35. CONFLICT = (
  36. client.CONFLICT, (("Content-Type", "text/plain"),),
  37. "Conflict in the request.")
  38. METHOD_NOT_ALLOWED = (
  39. client.METHOD_NOT_ALLOWED, (("Content-Type", "text/plain"),),
  40. "The method is not allowed on the requested resource.")
  41. PRECONDITION_FAILED = (
  42. client.PRECONDITION_FAILED,
  43. (("Content-Type", "text/plain"),), "Precondition failed.")
  44. REQUEST_TIMEOUT = (
  45. client.REQUEST_TIMEOUT, (("Content-Type", "text/plain"),),
  46. "Connection timed out.")
  47. REQUEST_ENTITY_TOO_LARGE = (
  48. client.REQUEST_ENTITY_TOO_LARGE, (("Content-Type", "text/plain"),),
  49. "Request body too large.")
  50. REMOTE_DESTINATION = (
  51. client.BAD_GATEWAY, (("Content-Type", "text/plain"),),
  52. "Remote destination not supported.")
  53. DIRECTORY_LISTING = (
  54. client.FORBIDDEN, (("Content-Type", "text/plain"),),
  55. "Directory listings are not supported.")
  56. INTERNAL_SERVER_ERROR = (
  57. client.INTERNAL_SERVER_ERROR, (("Content-Type", "text/plain"),),
  58. "A server error occurred. Please contact the administrator.")
  59. DAV_HEADERS = "1, 2, 3, calendar-access, addressbook, extended-mkcol"
  60. def decode_request(configuration, environ, text):
  61. """Try to magically decode ``text`` according to given ``environ``."""
  62. # List of charsets to try
  63. charsets = []
  64. # First append content charset given in the request
  65. content_type = environ.get("CONTENT_TYPE")
  66. if content_type and "charset=" in content_type:
  67. charsets.append(
  68. content_type.split("charset=")[1].split(";")[0].strip())
  69. # Then append default Radicale charset
  70. charsets.append(configuration.get("encoding", "request"))
  71. # Then append various fallbacks
  72. charsets.append("utf-8")
  73. charsets.append("iso8859-1")
  74. # Remove duplicates
  75. for i, s in reversed(list(enumerate(charsets))):
  76. if s in charsets[:i]:
  77. del charsets[i]
  78. # Try to decode
  79. for charset in charsets:
  80. try:
  81. return text.decode(charset)
  82. except UnicodeDecodeError:
  83. pass
  84. raise UnicodeDecodeError("decode_request", text, 0, len(text),
  85. "all codecs failed [%s]" % ", ".join(charsets))
  86. def read_raw_request_body(configuration, environ):
  87. content_length = int(environ.get("CONTENT_LENGTH") or 0)
  88. if not content_length:
  89. return b""
  90. content = environ["wsgi.input"].read(content_length)
  91. if len(content) < content_length:
  92. raise RuntimeError("Request body too short: %d" % len(content))
  93. return content
  94. def read_request_body(configuration, environ):
  95. content = decode_request(
  96. configuration, environ, read_raw_request_body(configuration, environ))
  97. logger.debug("Request content:\n%s", content)
  98. return content