Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add types to the server code and remove unused parameter #7813

Merged
merged 9 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/7813.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to the http server code and remove an unused parameter.
71 changes: 41 additions & 30 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def render(self, request):
return NOT_DONE_YET

@wrap_async_request_handler
async def _async_render_wrapper(self, request):
async def _async_render_wrapper(self, request: Request):
clokep marked this conversation as resolved.
Show resolved Hide resolved
"""This is a wrapper that delegates to `_async_render` and handles
exceptions, return values, metrics, etc.
"""
Expand All @@ -237,7 +237,7 @@ async def _async_render_wrapper(self, request):
f = failure.Failure()
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
no appropriate method exists. Can be overriden in sub classes for
different routing.
Expand Down Expand Up @@ -278,7 +278,7 @@ class DirectServeJsonResource(_AsyncResource):
"""

def _send_response(
self, request, code, response_object,
self, request: Request, code: int, response_object: Any,
):
"""Implements _AsyncResource._send_response
"""
Expand Down Expand Up @@ -507,22 +507,37 @@ class RootOptionsRedirectResource(OptionsResource, RootRedirect):


def respond_with_json(
request,
code,
json_object,
send_cors=False,
response_code_message=None,
pretty_print=False,
canonical_json=True,
request: Request,
code: int,
json_object: Any,
send_cors: bool = False,
pretty_print: bool = False,
canonical_json: bool = 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
# the Deferred fires, but since the flag is RIGHT THERE it seems like
# a waste.
if request._disconnected:
logger.warning(
"Not sending response to request %s, already disconnected.", request
)
return
return None

if pretty_print:
json_bytes = encode_pretty_printed_json(json_object) + b"\n"
Expand All @@ -533,30 +548,26 @@ def respond_with_json(
else:
json_bytes = json.dumps(json_object).encode("utf-8")

return respond_with_json_bytes(
request,
code,
json_bytes,
send_cors=send_cors,
response_code_message=response_code_message,
)
return respond_with_json_bytes(request, code, json_bytes, send_cors=send_cors)


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.

Args:
request (twisted.web.http.Request): The http request to respond to.
code (int): The HTTP response code.
json_bytes (bytes): The json bytes to use as the response body.
send_cors (bool): Whether to send Cross-Origin Resource Sharing headers
request: The http request to respond to.
code: The HTTP response code.
json_bytes: The json bytes to use as the response body.
send_cors: Whether to send Cross-Origin Resource Sharing headers
https://fetch.spec.whatwg.org/#http-cors-protocol

Returns:
twisted.web.server.NOT_DONE_YET"""
twisted.web.server.NOT_DONE_YET if the request is still active.
"""

request.setResponseCode(code, message=response_code_message)
request.setResponseCode(code)
request.setHeader(b"Content-Type", b"application/json")
request.setHeader(b"Content-Length", b"%d" % (len(json_bytes),))
request.setHeader(b"Cache-Control", b"no-cache, no-store, must-revalidate")
Expand All @@ -573,12 +584,12 @@ def respond_with_json_bytes(
return NOT_DONE_YET


def set_cors_headers(request):
"""Set the CORs headers so that javascript running in a web browsers can
def set_cors_headers(request: Request):
"""Set the CORS headers so that javascript running in a web browsers can
use this API

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(
Expand Down Expand Up @@ -643,7 +654,7 @@ def set_clickjacking_protection_headers(request: Request):
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.

Twisted throws a RuntimeException if the connection closed before the
Expand All @@ -662,7 +673,7 @@ def finish_request(request):
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=[])
for user_agent in user_agents:
if b"curl" in user_agent:
Expand Down