servlet.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """ This module contains base REST classes for constructing REST servlets. """
  16. import logging
  17. from canonicaljson import json
  18. from synapse.api.errors import Codes, SynapseError
  19. logger = logging.getLogger(__name__)
  20. def parse_integer(request, name, default=None, required=False):
  21. """Parse an integer parameter from the request string
  22. Args:
  23. request: the twisted HTTP request.
  24. name (bytes/unicode): the name of the query parameter.
  25. default (int|None): value to use if the parameter is absent, defaults
  26. to None.
  27. required (bool): whether to raise a 400 SynapseError if the
  28. parameter is absent, defaults to False.
  29. Returns:
  30. int|None: An int value or the default.
  31. Raises:
  32. SynapseError: if the parameter is absent and required, or if the
  33. parameter is present and not an integer.
  34. """
  35. return parse_integer_from_args(request.args, name, default, required)
  36. def parse_integer_from_args(args, name, default=None, required=False):
  37. if not isinstance(name, bytes):
  38. name = name.encode('ascii')
  39. if name in args:
  40. try:
  41. return int(args[name][0])
  42. except Exception:
  43. message = "Query parameter %r must be an integer" % (name,)
  44. raise SynapseError(400, message)
  45. else:
  46. if required:
  47. message = "Missing integer query parameter %r" % (name,)
  48. raise SynapseError(400, message, errcode=Codes.MISSING_PARAM)
  49. else:
  50. return default
  51. def parse_boolean(request, name, default=None, required=False):
  52. """Parse a boolean parameter from the request query string
  53. Args:
  54. request: the twisted HTTP request.
  55. name (bytes/unicode): the name of the query parameter.
  56. default (bool|None): value to use if the parameter is absent, defaults
  57. to None.
  58. required (bool): whether to raise a 400 SynapseError if the
  59. parameter is absent, defaults to False.
  60. Returns:
  61. bool|None: A bool value or the default.
  62. Raises:
  63. SynapseError: if the parameter is absent and required, or if the
  64. parameter is present and not one of "true" or "false".
  65. """
  66. return parse_boolean_from_args(request.args, name, default, required)
  67. def parse_boolean_from_args(args, name, default=None, required=False):
  68. if not isinstance(name, bytes):
  69. name = name.encode('ascii')
  70. if name in args:
  71. try:
  72. return {
  73. b"true": True,
  74. b"false": False,
  75. }[args[name][0]]
  76. except Exception:
  77. message = (
  78. "Boolean query parameter %r must be one of"
  79. " ['true', 'false']"
  80. ) % (name,)
  81. raise SynapseError(400, message)
  82. else:
  83. if required:
  84. message = "Missing boolean query parameter %r" % (name,)
  85. raise SynapseError(400, message, errcode=Codes.MISSING_PARAM)
  86. else:
  87. return default
  88. def parse_string(request, name, default=None, required=False,
  89. allowed_values=None, param_type="string", encoding='ascii'):
  90. """
  91. Parse a string parameter from the request query string.
  92. If encoding is not None, the content of the query param will be
  93. decoded to Unicode using the encoding, otherwise it will be encoded
  94. Args:
  95. request: the twisted HTTP request.
  96. name (bytes|unicode): the name of the query parameter.
  97. default (bytes|unicode|None): value to use if the parameter is absent,
  98. defaults to None. Must be bytes if encoding is None.
  99. required (bool): whether to raise a 400 SynapseError if the
  100. parameter is absent, defaults to False.
  101. allowed_values (list[bytes|unicode]): List of allowed values for the
  102. string, or None if any value is allowed, defaults to None. Must be
  103. the same type as name, if given.
  104. encoding (str|None): The encoding to decode the string content with.
  105. Returns:
  106. bytes/unicode|None: A string value or the default. Unicode if encoding
  107. was given, bytes otherwise.
  108. Raises:
  109. SynapseError if the parameter is absent and required, or if the
  110. parameter is present, must be one of a list of allowed values and
  111. is not one of those allowed values.
  112. """
  113. return parse_string_from_args(
  114. request.args, name, default, required, allowed_values, param_type, encoding
  115. )
  116. def parse_string_from_args(args, name, default=None, required=False,
  117. allowed_values=None, param_type="string", encoding='ascii'):
  118. if not isinstance(name, bytes):
  119. name = name.encode('ascii')
  120. if name in args:
  121. value = args[name][0]
  122. if encoding:
  123. value = value.decode(encoding)
  124. if allowed_values is not None and value not in allowed_values:
  125. message = "Query parameter %r must be one of [%s]" % (
  126. name, ", ".join(repr(v) for v in allowed_values)
  127. )
  128. raise SynapseError(400, message)
  129. else:
  130. return value
  131. else:
  132. if required:
  133. message = "Missing %s query parameter %r" % (param_type, name)
  134. raise SynapseError(400, message, errcode=Codes.MISSING_PARAM)
  135. else:
  136. if encoding and isinstance(default, bytes):
  137. return default.decode(encoding)
  138. return default
  139. def parse_json_value_from_request(request, allow_empty_body=False):
  140. """Parse a JSON value from the body of a twisted HTTP request.
  141. Args:
  142. request: the twisted HTTP request.
  143. allow_empty_body (bool): if True, an empty body will be accepted and
  144. turned into None
  145. Returns:
  146. The JSON value.
  147. Raises:
  148. SynapseError if the request body couldn't be decoded as JSON.
  149. """
  150. try:
  151. content_bytes = request.content.read()
  152. except Exception:
  153. raise SynapseError(400, "Error reading JSON content.")
  154. if not content_bytes and allow_empty_body:
  155. return None
  156. # Decode to Unicode so that simplejson will return Unicode strings on
  157. # Python 2
  158. try:
  159. content_unicode = content_bytes.decode('utf8')
  160. except UnicodeDecodeError:
  161. logger.warn("Unable to decode UTF-8")
  162. raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
  163. try:
  164. content = json.loads(content_unicode)
  165. except Exception as e:
  166. logger.warn("Unable to parse JSON: %s", e)
  167. raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
  168. return content
  169. def parse_json_object_from_request(request, allow_empty_body=False):
  170. """Parse a JSON object from the body of a twisted HTTP request.
  171. Args:
  172. request: the twisted HTTP request.
  173. allow_empty_body (bool): if True, an empty body will be accepted and
  174. turned into an empty dict.
  175. Raises:
  176. SynapseError if the request body couldn't be decoded as JSON or
  177. if it wasn't a JSON object.
  178. """
  179. content = parse_json_value_from_request(
  180. request, allow_empty_body=allow_empty_body,
  181. )
  182. if allow_empty_body and content is None:
  183. return {}
  184. if type(content) != dict:
  185. message = "Content must be a JSON object."
  186. raise SynapseError(400, message, errcode=Codes.BAD_JSON)
  187. return content
  188. def assert_params_in_dict(body, required):
  189. absent = []
  190. for k in required:
  191. if k not in body:
  192. absent.append(k)
  193. if len(absent) > 0:
  194. raise SynapseError(400, "Missing params: %r" % absent, Codes.MISSING_PARAM)
  195. class RestServlet(object):
  196. """ A Synapse REST Servlet.
  197. An implementing class can either provide its own custom 'register' method,
  198. or use the automatic pattern handling provided by the base class.
  199. To use this latter, the implementing class instead provides a `PATTERN`
  200. class attribute containing a pre-compiled regular expression. The automatic
  201. register method will then use this method to register any of the following
  202. instance methods associated with the corresponding HTTP method:
  203. on_GET
  204. on_PUT
  205. on_POST
  206. on_DELETE
  207. on_OPTIONS
  208. Automatically handles turning CodeMessageExceptions thrown by these methods
  209. into the appropriate HTTP response.
  210. """
  211. def register(self, http_server):
  212. """ Register this servlet with the given HTTP server. """
  213. if hasattr(self, "PATTERNS"):
  214. patterns = self.PATTERNS
  215. for method in ("GET", "PUT", "POST", "OPTIONS", "DELETE"):
  216. if hasattr(self, "on_%s" % (method,)):
  217. method_handler = getattr(self, "on_%s" % (method,))
  218. http_server.register_paths(method, patterns, method_handler)
  219. else:
  220. raise NotImplementedError("RestServlet must register something.")