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

Commit b25a494

Browse files
authored
Add types to http.site (#10867)
1 parent ebd8baf commit b25a494

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

changelog.d/10867.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints to `synapse.http.site`.

synapse/http/site.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from twisted.internet.interfaces import IAddress, IReactorTime
2323
from twisted.python.failure import Failure
24-
from twisted.web.resource import IResource
24+
from twisted.web.resource import IResource, Resource
2525
from twisted.web.server import Request, Site
2626

2727
from synapse.config.server import ListenerConfig
@@ -61,7 +61,7 @@ class SynapseRequest(Request):
6161
logcontext: the log context for this request
6262
"""
6363

64-
def __init__(self, channel, *args, max_request_body_size=1024, **kw):
64+
def __init__(self, channel, *args, max_request_body_size: int = 1024, **kw):
6565
Request.__init__(self, channel, *args, **kw)
6666
self._max_request_body_size = max_request_body_size
6767
self.site: SynapseSite = channel.site
@@ -83,13 +83,13 @@ def __init__(self, channel, *args, max_request_body_size=1024, **kw):
8383
self._is_processing = False
8484

8585
# the time when the asynchronous request handler completed its processing
86-
self._processing_finished_time = None
86+
self._processing_finished_time: Optional[float] = None
8787

8888
# what time we finished sending the response to the client (or the connection
8989
# dropped)
90-
self.finish_time = None
90+
self.finish_time: Optional[float] = None
9191

92-
def __repr__(self):
92+
def __repr__(self) -> str:
9393
# We overwrite this so that we don't log ``access_token``
9494
return "<%s at 0x%x method=%r uri=%r clientproto=%r site=%r>" % (
9595
self.__class__.__name__,
@@ -100,7 +100,7 @@ def __repr__(self):
100100
self.site.site_tag,
101101
)
102102

103-
def handleContentChunk(self, data):
103+
def handleContentChunk(self, data: bytes) -> None:
104104
# we should have a `content` by now.
105105
assert self.content, "handleContentChunk() called before gotLength()"
106106
if self.content.tell() + len(data) > self._max_request_body_size:
@@ -139,7 +139,7 @@ def requester(self, value: Union[Requester, str]) -> None:
139139
# If there's no authenticated entity, it was the requester.
140140
self.logcontext.request.authenticated_entity = authenticated_entity or requester
141141

142-
def get_request_id(self):
142+
def get_request_id(self) -> str:
143143
return "%s-%i" % (self.get_method(), self.request_seq)
144144

145145
def get_redacted_uri(self) -> str:
@@ -205,7 +205,7 @@ def get_authenticated_entity(self) -> Tuple[Optional[str], Optional[str]]:
205205

206206
return None, None
207207

208-
def render(self, resrc):
208+
def render(self, resrc: Resource) -> None:
209209
# this is called once a Resource has been found to serve the request; in our
210210
# case the Resource in question will normally be a JsonResource.
211211

@@ -282,7 +282,7 @@ async def handle_request(request):
282282
if self.finish_time is not None:
283283
self._finished_processing()
284284

285-
def finish(self):
285+
def finish(self) -> None:
286286
"""Called when all response data has been written to this Request.
287287
288288
Overrides twisted.web.server.Request.finish to record the finish time and do
@@ -295,7 +295,7 @@ def finish(self):
295295
with PreserveLoggingContext(self.logcontext):
296296
self._finished_processing()
297297

298-
def connectionLost(self, reason):
298+
def connectionLost(self, reason: Union[Failure, Exception]) -> None:
299299
"""Called when the client connection is closed before the response is written.
300300
301301
Overrides twisted.web.server.Request.connectionLost to record the finish time and
@@ -327,7 +327,7 @@ def connectionLost(self, reason):
327327
if not self._is_processing:
328328
self._finished_processing()
329329

330-
def _started_processing(self, servlet_name):
330+
def _started_processing(self, servlet_name: str) -> None:
331331
"""Record the fact that we are processing this request.
332332
333333
This will log the request's arrival. Once the request completes,
@@ -354,9 +354,11 @@ def _started_processing(self, servlet_name):
354354
self.get_redacted_uri(),
355355
)
356356

357-
def _finished_processing(self):
357+
def _finished_processing(self) -> None:
358358
"""Log the completion of this request and update the metrics"""
359359
assert self.logcontext is not None
360+
assert self.finish_time is not None
361+
360362
usage = self.logcontext.get_resource_usage()
361363

362364
if self._processing_finished_time is None:
@@ -437,15 +439,15 @@ class XForwardedForRequest(SynapseRequest):
437439
_forwarded_for: "Optional[_XForwardedForAddress]" = None
438440
_forwarded_https: bool = False
439441

440-
def requestReceived(self, command, path, version):
442+
def requestReceived(self, command: bytes, path: bytes, version: bytes) -> None:
441443
# this method is called by the Channel once the full request has been
442444
# received, to dispatch the request to a resource.
443445
# We can use it to set the IP address and protocol according to the
444446
# headers.
445447
self._process_forwarded_headers()
446448
return super().requestReceived(command, path, version)
447449

448-
def _process_forwarded_headers(self):
450+
def _process_forwarded_headers(self) -> None:
449451
headers = self.requestHeaders.getRawHeaders(b"x-forwarded-for")
450452
if not headers:
451453
return
@@ -470,7 +472,7 @@ def _process_forwarded_headers(self):
470472
)
471473
self._forwarded_https = True
472474

473-
def isSecure(self):
475+
def isSecure(self) -> bool:
474476
if self._forwarded_https:
475477
return True
476478
return super().isSecure()
@@ -545,14 +547,16 @@ def __init__(
545547
proxied = config.http_options.x_forwarded
546548
request_class = XForwardedForRequest if proxied else SynapseRequest
547549

548-
def request_factory(channel, queued) -> Request:
550+
def request_factory(channel, queued: bool) -> Request:
549551
return request_class(
550-
channel, max_request_body_size=max_request_body_size, queued=queued
552+
channel,
553+
max_request_body_size=max_request_body_size,
554+
queued=queued,
551555
)
552556

553557
self.requestFactory = request_factory # type: ignore
554558
self.access_logger = logging.getLogger(logger_name)
555559
self.server_version_string = server_version_string.encode("ascii")
556560

557-
def log(self, request):
561+
def log(self, request: SynapseRequest) -> None:
558562
pass

0 commit comments

Comments
 (0)