diff options
author | Richard van der Hoff <github@rvanderhoff.org.uk> | 2018-02-09 14:11:00 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-09 14:11:00 +0000 |
commit | 10b34dbb9abcc0800db84dae74a244caaea54e2d (patch) | |
tree | aedd171b7d6973ea2f4c76dfb6e8610737da9e4c /synapse/http/servlet.py | |
parent | Merge pull request #2856 from matrix-org/erikj/remove_ratelimit (diff) | |
parent | purge: move room_depth update to end (diff) | |
download | synapse-10b34dbb9abcc0800db84dae74a244caaea54e2d.tar.xz |
Merge pull request #2858 from matrix-org/rav/purge_updates
delete_local_events for purge_room_history
Diffstat (limited to 'synapse/http/servlet.py')
-rw-r--r-- | synapse/http/servlet.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/synapse/http/servlet.py b/synapse/http/servlet.py index 71420e54db..ef8e62901b 100644 --- a/synapse/http/servlet.py +++ b/synapse/http/servlet.py @@ -148,11 +148,13 @@ def parse_string_from_args(args, name, default=None, required=False, return default -def parse_json_value_from_request(request): +def parse_json_value_from_request(request, allow_empty_body=False): """Parse a JSON value from the body of a twisted HTTP request. Args: request: the twisted HTTP request. + allow_empty_body (bool): if True, an empty body will be accepted and + turned into None Returns: The JSON value. @@ -165,6 +167,9 @@ def parse_json_value_from_request(request): except Exception: raise SynapseError(400, "Error reading JSON content.") + if not content_bytes and allow_empty_body: + return None + try: content = simplejson.loads(content_bytes) except Exception as e: @@ -174,17 +179,24 @@ def parse_json_value_from_request(request): return content -def parse_json_object_from_request(request): +def parse_json_object_from_request(request, allow_empty_body=False): """Parse a JSON object from the body of a twisted HTTP request. Args: request: the twisted HTTP request. + allow_empty_body (bool): if True, an empty body will be accepted and + turned into an empty dict. Raises: SynapseError if the request body couldn't be decoded as JSON or if it wasn't a JSON object. """ - content = parse_json_value_from_request(request) + content = parse_json_value_from_request( + request, allow_empty_body=allow_empty_body, + ) + + if allow_empty_body and content is None: + return {} if type(content) != dict: message = "Content must be a JSON object." |