mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-12-26 13:19:22 -05:00
Add types to the server code and remove unused parameter (#7813)
This commit is contained in:
parent
1bca21e1da
commit
d9e47af617
1
changelog.d/7813.misc
Normal file
1
changelog.d/7813.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Add type hints to the http server code and remove an unused parameter.
|
@ -217,7 +217,7 @@ class _AsyncResource(resource.Resource, metaclass=abc.ABCMeta):
|
|||||||
return NOT_DONE_YET
|
return NOT_DONE_YET
|
||||||
|
|
||||||
@wrap_async_request_handler
|
@wrap_async_request_handler
|
||||||
async def _async_render_wrapper(self, request):
|
async def _async_render_wrapper(self, request: SynapseRequest):
|
||||||
"""This is a wrapper that delegates to `_async_render` and handles
|
"""This is a wrapper that delegates to `_async_render` and handles
|
||||||
exceptions, return values, metrics, etc.
|
exceptions, return values, metrics, etc.
|
||||||
"""
|
"""
|
||||||
@ -237,7 +237,7 @@ class _AsyncResource(resource.Resource, metaclass=abc.ABCMeta):
|
|||||||
f = failure.Failure()
|
f = failure.Failure()
|
||||||
self._send_error_response(f, request)
|
self._send_error_response(f, request)
|
||||||
|
|
||||||
async def _async_render(self, request):
|
async def _async_render(self, request: Request):
|
||||||
"""Delegates to `_async_render_<METHOD>` methods, or returns a 400 if
|
"""Delegates to `_async_render_<METHOD>` methods, or returns a 400 if
|
||||||
no appropriate method exists. Can be overriden in sub classes for
|
no appropriate method exists. Can be overriden in sub classes for
|
||||||
different routing.
|
different routing.
|
||||||
@ -278,7 +278,7 @@ class DirectServeJsonResource(_AsyncResource):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def _send_response(
|
def _send_response(
|
||||||
self, request, code, response_object,
|
self, request: Request, code: int, response_object: Any,
|
||||||
):
|
):
|
||||||
"""Implements _AsyncResource._send_response
|
"""Implements _AsyncResource._send_response
|
||||||
"""
|
"""
|
||||||
@ -507,14 +507,29 @@ class RootOptionsRedirectResource(OptionsResource, RootRedirect):
|
|||||||
|
|
||||||
|
|
||||||
def respond_with_json(
|
def respond_with_json(
|
||||||
request,
|
request: Request,
|
||||||
code,
|
code: int,
|
||||||
json_object,
|
json_object: Any,
|
||||||
send_cors=False,
|
send_cors: bool = False,
|
||||||
response_code_message=None,
|
pretty_print: bool = False,
|
||||||
pretty_print=False,
|
canonical_json: bool = True,
|
||||||
canonical_json=True,
|
|
||||||
):
|
):
|
||||||
|
"""Sends encoded JSON in response to the given request.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request: The http request to respond to.
|
||||||
|
code: The HTTP response code.
|
||||||
|
json_object: The object to serialize to JSON.
|
||||||
|
send_cors: Whether to send Cross-Origin Resource Sharing headers
|
||||||
|
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
|
||||||
|
the JSON bytes.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
twisted.web.server.NOT_DONE_YET if the request is still active.
|
||||||
|
"""
|
||||||
# could alternatively use request.notifyFinish() and flip a flag when
|
# could alternatively use request.notifyFinish() and flip a flag when
|
||||||
# the Deferred fires, but since the flag is RIGHT THERE it seems like
|
# the Deferred fires, but since the flag is RIGHT THERE it seems like
|
||||||
# a waste.
|
# a waste.
|
||||||
@ -522,7 +537,7 @@ def respond_with_json(
|
|||||||
logger.warning(
|
logger.warning(
|
||||||
"Not sending response to request %s, already disconnected.", request
|
"Not sending response to request %s, already disconnected.", request
|
||||||
)
|
)
|
||||||
return
|
return None
|
||||||
|
|
||||||
if pretty_print:
|
if pretty_print:
|
||||||
json_bytes = encode_pretty_printed_json(json_object) + b"\n"
|
json_bytes = encode_pretty_printed_json(json_object) + b"\n"
|
||||||
@ -533,30 +548,26 @@ def respond_with_json(
|
|||||||
else:
|
else:
|
||||||
json_bytes = json.dumps(json_object).encode("utf-8")
|
json_bytes = json.dumps(json_object).encode("utf-8")
|
||||||
|
|
||||||
return respond_with_json_bytes(
|
return respond_with_json_bytes(request, code, json_bytes, send_cors=send_cors)
|
||||||
request,
|
|
||||||
code,
|
|
||||||
json_bytes,
|
|
||||||
send_cors=send_cors,
|
|
||||||
response_code_message=response_code_message,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def respond_with_json_bytes(
|
def respond_with_json_bytes(
|
||||||
request, code, json_bytes, send_cors=False, response_code_message=None
|
request: Request, code: int, json_bytes: bytes, send_cors: bool = False,
|
||||||
):
|
):
|
||||||
"""Sends encoded JSON in response to the given request.
|
"""Sends encoded JSON in response to the given request.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
request (twisted.web.http.Request): The http request to respond to.
|
request: The http request to respond to.
|
||||||
code (int): The HTTP response code.
|
code: The HTTP response code.
|
||||||
json_bytes (bytes): The json bytes to use as the response body.
|
json_bytes: The json bytes to use as the response body.
|
||||||
send_cors (bool): 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
|
||||||
Returns:
|
|
||||||
twisted.web.server.NOT_DONE_YET"""
|
|
||||||
|
|
||||||
request.setResponseCode(code, message=response_code_message)
|
Returns:
|
||||||
|
twisted.web.server.NOT_DONE_YET if the request is still active.
|
||||||
|
"""
|
||||||
|
|
||||||
|
request.setResponseCode(code)
|
||||||
request.setHeader(b"Content-Type", b"application/json")
|
request.setHeader(b"Content-Type", b"application/json")
|
||||||
request.setHeader(b"Content-Length", b"%d" % (len(json_bytes),))
|
request.setHeader(b"Content-Length", b"%d" % (len(json_bytes),))
|
||||||
request.setHeader(b"Cache-Control", b"no-cache, no-store, must-revalidate")
|
request.setHeader(b"Cache-Control", b"no-cache, no-store, must-revalidate")
|
||||||
@ -573,12 +584,12 @@ def respond_with_json_bytes(
|
|||||||
return NOT_DONE_YET
|
return NOT_DONE_YET
|
||||||
|
|
||||||
|
|
||||||
def set_cors_headers(request):
|
def set_cors_headers(request: Request):
|
||||||
"""Set the CORs headers so that javascript running in a web browsers can
|
"""Set the CORS headers so that javascript running in a web browsers can
|
||||||
use this API
|
use this API
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
request (twisted.web.http.Request): The http request to add CORs to.
|
request: The http request to add CORS to.
|
||||||
"""
|
"""
|
||||||
request.setHeader(b"Access-Control-Allow-Origin", b"*")
|
request.setHeader(b"Access-Control-Allow-Origin", b"*")
|
||||||
request.setHeader(
|
request.setHeader(
|
||||||
@ -643,7 +654,7 @@ def set_clickjacking_protection_headers(request: Request):
|
|||||||
request.setHeader(b"Content-Security-Policy", b"frame-ancestors 'none';")
|
request.setHeader(b"Content-Security-Policy", b"frame-ancestors 'none';")
|
||||||
|
|
||||||
|
|
||||||
def finish_request(request):
|
def finish_request(request: Request):
|
||||||
""" Finish writing the response to the request.
|
""" Finish writing the response to the request.
|
||||||
|
|
||||||
Twisted throws a RuntimeException if the connection closed before the
|
Twisted throws a RuntimeException if the connection closed before the
|
||||||
@ -662,7 +673,7 @@ def finish_request(request):
|
|||||||
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):
|
def _request_user_agent_is_curl(request: Request) -> bool:
|
||||||
user_agents = request.requestHeaders.getRawHeaders(b"User-Agent", default=[])
|
user_agents = request.requestHeaders.getRawHeaders(b"User-Agent", default=[])
|
||||||
for user_agent in user_agents:
|
for user_agent in user_agents:
|
||||||
if b"curl" in user_agent:
|
if b"curl" in user_agent:
|
||||||
|
Loading…
Reference in New Issue
Block a user