mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-12-25 20:39:24 -05:00
Improve RawHeaders
type hints (#14303)
This commit is contained in:
parent
81815e0561
commit
730b13dbc9
1
changelog.d/14303.misc
Normal file
1
changelog.d/14303.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Improve type hinting of `RawHeaders`.
|
@ -178,13 +178,13 @@ class KeyUploadServlet(RestServlet):
|
|||||||
# Proxy headers from the original request, such as the auth headers
|
# Proxy headers from the original request, such as the auth headers
|
||||||
# (in case the access token is there) and the original IP /
|
# (in case the access token is there) and the original IP /
|
||||||
# User-Agent of the request.
|
# User-Agent of the request.
|
||||||
headers = {
|
headers: Dict[bytes, List[bytes]] = {
|
||||||
header: request.requestHeaders.getRawHeaders(header, [])
|
header: list(request.requestHeaders.getRawHeaders(header, []))
|
||||||
for header in (b"Authorization", b"User-Agent")
|
for header in (b"Authorization", b"User-Agent")
|
||||||
}
|
}
|
||||||
# Add the previous hop to the X-Forwarded-For header.
|
# Add the previous hop to the X-Forwarded-For header.
|
||||||
x_forwarded_for = request.requestHeaders.getRawHeaders(
|
x_forwarded_for = list(
|
||||||
b"X-Forwarded-For", []
|
request.requestHeaders.getRawHeaders(b"X-Forwarded-For", [])
|
||||||
)
|
)
|
||||||
# we use request.client here, since we want the previous hop, not the
|
# we use request.client here, since we want the previous hop, not the
|
||||||
# original client (as returned by request.getClientAddress()).
|
# original client (as returned by request.getClientAddress()).
|
||||||
|
@ -25,7 +25,6 @@ from typing import (
|
|||||||
List,
|
List,
|
||||||
Mapping,
|
Mapping,
|
||||||
Optional,
|
Optional,
|
||||||
Sequence,
|
|
||||||
Tuple,
|
Tuple,
|
||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
@ -90,14 +89,29 @@ incoming_responses_counter = Counter(
|
|||||||
"synapse_http_client_responses", "", ["method", "code"]
|
"synapse_http_client_responses", "", ["method", "code"]
|
||||||
)
|
)
|
||||||
|
|
||||||
# the type of the headers list, to be passed to the t.w.h.Headers.
|
# the type of the headers map, to be passed to the t.w.h.Headers.
|
||||||
# Actually we can mix str and bytes keys, but Mapping treats 'key' as invariant so
|
#
|
||||||
# we simplify.
|
# The actual type accepted by Twisted is
|
||||||
|
# Mapping[Union[str, bytes], Sequence[Union[str, bytes]] ,
|
||||||
|
# allowing us to mix and match str and bytes freely. However: any str is also a
|
||||||
|
# Sequence[str]; passing a header string value which is a
|
||||||
|
# standalone str is interpreted as a sequence of 1-codepoint strings. This is a disastrous footgun.
|
||||||
|
# We use a narrower value type (RawHeaderValue) to avoid this footgun.
|
||||||
|
#
|
||||||
|
# We also simplify the keys to be either all str or all bytes. This helps because
|
||||||
|
# Dict[K, V] is invariant in K (and indeed V).
|
||||||
RawHeaders = Union[Mapping[str, "RawHeaderValue"], Mapping[bytes, "RawHeaderValue"]]
|
RawHeaders = Union[Mapping[str, "RawHeaderValue"], Mapping[bytes, "RawHeaderValue"]]
|
||||||
|
|
||||||
# the value actually has to be a List, but List is invariant so we can't specify that
|
# the value actually has to be a List, but List is invariant so we can't specify that
|
||||||
# the entries can either be Lists or bytes.
|
# the entries can either be Lists or bytes.
|
||||||
RawHeaderValue = Sequence[Union[str, bytes]]
|
RawHeaderValue = Union[
|
||||||
|
List[str],
|
||||||
|
List[bytes],
|
||||||
|
List[Union[str, bytes]],
|
||||||
|
Tuple[str, ...],
|
||||||
|
Tuple[bytes, ...],
|
||||||
|
Tuple[Union[str, bytes], ...],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def check_against_blacklist(
|
def check_against_blacklist(
|
||||||
|
Loading…
Reference in New Issue
Block a user