forked from MirrorHub/synapse
Remove special case of pretty printing JSON responses for curl (#8833)
* Remove special case of pretty printing JSON responses for curl Signed-off-by: Tulir Asokan <tulir@maunium.net>
This commit is contained in:
parent
5cbe8d93fe
commit
856eab606b
2 changed files with 6 additions and 24 deletions
1
changelog.d/8833.removal
Normal file
1
changelog.d/8833.removal
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Disable pretty printing JSON responses for curl. Users who want pretty-printed output should use [jq](https://stedolan.github.io/jq/) in combination with curl. Contributed by @tulir.
|
|
@ -25,7 +25,7 @@ from io import BytesIO
|
||||||
from typing import Any, Callable, Dict, Iterator, List, Tuple, Union
|
from typing import Any, Callable, Dict, Iterator, List, Tuple, Union
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
from canonicaljson import iterencode_canonical_json, iterencode_pretty_printed_json
|
from canonicaljson import iterencode_canonical_json
|
||||||
from zope.interface import implementer
|
from zope.interface import implementer
|
||||||
|
|
||||||
from twisted.internet import defer, interfaces
|
from twisted.internet import defer, interfaces
|
||||||
|
@ -94,11 +94,7 @@ def return_json_error(f: failure.Failure, request: SynapseRequest) -> None:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
respond_with_json(
|
respond_with_json(
|
||||||
request,
|
request, error_code, error_dict, send_cors=True,
|
||||||
error_code,
|
|
||||||
error_dict,
|
|
||||||
send_cors=True,
|
|
||||||
pretty_print=_request_user_agent_is_curl(request),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -290,7 +286,6 @@ class DirectServeJsonResource(_AsyncResource):
|
||||||
code,
|
code,
|
||||||
response_object,
|
response_object,
|
||||||
send_cors=True,
|
send_cors=True,
|
||||||
pretty_print=_request_user_agent_is_curl(request),
|
|
||||||
canonical_json=self.canonical_json,
|
canonical_json=self.canonical_json,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -587,7 +582,6 @@ def respond_with_json(
|
||||||
code: int,
|
code: int,
|
||||||
json_object: Any,
|
json_object: Any,
|
||||||
send_cors: bool = False,
|
send_cors: bool = False,
|
||||||
pretty_print: bool = False,
|
|
||||||
canonical_json: bool = True,
|
canonical_json: bool = True,
|
||||||
):
|
):
|
||||||
"""Sends encoded JSON in response to the given request.
|
"""Sends encoded JSON in response to the given request.
|
||||||
|
@ -598,8 +592,6 @@ def respond_with_json(
|
||||||
json_object: The object to serialize to JSON.
|
json_object: The object to serialize to JSON.
|
||||||
send_cors: Whether to send Cross-Origin Resource Sharing headers
|
send_cors: Whether to send Cross-Origin Resource Sharing headers
|
||||||
https://fetch.spec.whatwg.org/#http-cors-protocol
|
https://fetch.spec.whatwg.org/#http-cors-protocol
|
||||||
pretty_print: Whether to include indentation and line-breaks in the
|
|
||||||
resulting JSON bytes.
|
|
||||||
canonical_json: Whether to use the canonicaljson algorithm when encoding
|
canonical_json: Whether to use the canonicaljson algorithm when encoding
|
||||||
the JSON bytes.
|
the JSON bytes.
|
||||||
|
|
||||||
|
@ -615,13 +607,10 @@ def respond_with_json(
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if pretty_print:
|
if canonical_json:
|
||||||
encoder = iterencode_pretty_printed_json
|
encoder = iterencode_canonical_json
|
||||||
else:
|
else:
|
||||||
if canonical_json:
|
encoder = _encode_json_bytes
|
||||||
encoder = iterencode_canonical_json
|
|
||||||
else:
|
|
||||||
encoder = _encode_json_bytes
|
|
||||||
|
|
||||||
request.setResponseCode(code)
|
request.setResponseCode(code)
|
||||||
request.setHeader(b"Content-Type", b"application/json")
|
request.setHeader(b"Content-Type", b"application/json")
|
||||||
|
@ -759,11 +748,3 @@ def finish_request(request: Request):
|
||||||
request.finish()
|
request.finish()
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
logger.info("Connection disconnected before response was written: %r", e)
|
logger.info("Connection disconnected before response was written: %r", e)
|
||||||
|
|
||||||
|
|
||||||
def _request_user_agent_is_curl(request: Request) -> bool:
|
|
||||||
user_agents = request.requestHeaders.getRawHeaders(b"User-Agent", default=[])
|
|
||||||
for user_agent in user_agents:
|
|
||||||
if b"curl" in user_agent:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
Loading…
Reference in a new issue