-
Notifications
You must be signed in to change notification settings - Fork 561
ref(wsgi): Update _werkzeug vendor to newer version #4793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,58 +41,89 @@ | |
|
||
|
||
# | ||
# `get_headers` comes from `werkzeug.datastructures.EnvironHeaders` | ||
# https://github.com/pallets/werkzeug/blob/0.14.1/werkzeug/datastructures.py#L1361 | ||
# `get_headers` comes from `werkzeug.datastructures.headers.__iter__` | ||
# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/datastructures/headers.py#L644 | ||
# | ||
# We need this function because Django does not give us a "pure" http header | ||
# dict. So we might as well use it for all WSGI integrations. | ||
# | ||
def _get_headers(environ): | ||
# type: (Dict[str, str]) -> Iterator[Tuple[str, str]] | ||
""" | ||
Returns only proper HTTP headers. | ||
""" | ||
for key, value in environ.items(): | ||
key = str(key) | ||
if key.startswith("HTTP_") and key not in ( | ||
if key.startswith("HTTP_") and key not in { | ||
"HTTP_CONTENT_TYPE", | ||
"HTTP_CONTENT_LENGTH", | ||
): | ||
}: | ||
yield key[5:].replace("_", "-").title(), value | ||
elif key in ("CONTENT_TYPE", "CONTENT_LENGTH"): | ||
elif key in {"CONTENT_TYPE", "CONTENT_LENGTH"} and value: | ||
yield key.replace("_", "-").title(), value | ||
|
||
|
||
# | ||
# `get_host` comes from `werkzeug.wsgi.get_host` | ||
# https://github.com/pallets/werkzeug/blob/1.0.1/src/werkzeug/wsgi.py#L145 | ||
# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/wsgi.py#L86 | ||
# | ||
def get_host(environ, use_x_forwarded_for=False): | ||
# type: (Dict[str, str], bool) -> str | ||
""" | ||
Return the host for the given WSGI environment. | ||
""" | ||
if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ: | ||
rv = environ["HTTP_X_FORWARDED_HOST"] | ||
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"): | ||
rv = rv[:-3] | ||
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"): | ||
rv = rv[:-4] | ||
elif environ.get("HTTP_HOST"): | ||
rv = environ["HTTP_HOST"] | ||
if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"): | ||
rv = rv[:-3] | ||
elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"): | ||
rv = rv[:-4] | ||
elif environ.get("SERVER_NAME"): | ||
rv = environ["SERVER_NAME"] | ||
if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in ( | ||
("https", "443"), | ||
("http", "80"), | ||
): | ||
rv += ":" + environ["SERVER_PORT"] | ||
else: | ||
# In spite of the WSGI spec, SERVER_NAME might not be present. | ||
rv = "unknown" | ||
|
||
return rv | ||
return _get_host( | ||
environ["wsgi.url_scheme"], | ||
( | ||
environ["HTTP_X_FORWARDED_HOST"] | ||
if use_x_forwarded_for and environ.get("HTTP_X_FORWARDED_HOST") | ||
else environ.get("HTTP_HOST") | ||
), | ||
_get_server(environ), | ||
) | ||
|
||
|
||
# `_get_host` comes from `werkzeug.sansio.utils` | ||
# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/sansio/utils.py#L49 | ||
def _get_host( | ||
scheme, | ||
host_header, | ||
server=None, | ||
): | ||
# type: (str, str | None, Tuple[str, int | None] | None) -> str | ||
""" | ||
Return the host for the given parameters. | ||
""" | ||
host = "" | ||
|
||
if host_header is not None: | ||
host = host_header | ||
elif server is not None: | ||
host = server[0] | ||
|
||
# If SERVER_NAME is IPv6, wrap it in [] to match Host header. | ||
# Check for : because domain or IPv4 can't have that. | ||
if ":" in host and host[0] != "[": | ||
host = f"[{host}]" | ||
|
||
if server[1] is not None: | ||
host = f"{host}:{server[1]}" # noqa: E231 | ||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if scheme in {"http", "ws"} and host.endswith(":80"): | ||
host = host[:-3] | ||
elif scheme in {"https", "wss"} and host.endswith(":443"): | ||
host = host[:-4] | ||
|
||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return host | ||
|
||
|
||
def _get_server(environ): | ||
# type: (Dict[str, str]) -> Tuple[str, int | None] | None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Type Annotations Break Compatibility and Change BehaviorThe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding And I don't think |
||
name = environ.get("SERVER_NAME") | ||
|
||
if name is None: | ||
return None | ||
|
||
try: | ||
port = int(environ.get("SERVER_PORT", None)) # type: ignore[arg-type] | ||
sentrivana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except (TypeError, ValueError): | ||
# unix socket | ||
port = None | ||
|
||
return name, port |
Uh oh!
There was an error while loading. Please reload this page.