|
41 | 41 |
|
42 | 42 |
|
43 | 43 | #
|
44 |
| -# `get_headers` comes from `werkzeug.datastructures.EnvironHeaders` |
45 |
| -# https://github.com/pallets/werkzeug/blob/0.14.1/werkzeug/datastructures.py#L1361 |
| 44 | +# `get_headers` comes from `werkzeug.datastructures.headers.__iter__` |
| 45 | +# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/datastructures/headers.py#L644 |
46 | 46 | #
|
47 | 47 | # We need this function because Django does not give us a "pure" http header
|
48 | 48 | # dict. So we might as well use it for all WSGI integrations.
|
49 | 49 | #
|
50 | 50 | def _get_headers(environ):
|
51 | 51 | # type: (Dict[str, str]) -> Iterator[Tuple[str, str]]
|
52 |
| - """ |
53 |
| - Returns only proper HTTP headers. |
54 |
| - """ |
55 | 52 | for key, value in environ.items():
|
56 |
| - key = str(key) |
57 |
| - if key.startswith("HTTP_") and key not in ( |
| 53 | + if key.startswith("HTTP_") and key not in { |
58 | 54 | "HTTP_CONTENT_TYPE",
|
59 | 55 | "HTTP_CONTENT_LENGTH",
|
60 |
| - ): |
| 56 | + }: |
61 | 57 | yield key[5:].replace("_", "-").title(), value
|
62 |
| - elif key in ("CONTENT_TYPE", "CONTENT_LENGTH"): |
| 58 | + elif key in {"CONTENT_TYPE", "CONTENT_LENGTH"} and value: |
63 | 59 | yield key.replace("_", "-").title(), value
|
64 | 60 |
|
65 | 61 |
|
66 | 62 | #
|
67 | 63 | # `get_host` comes from `werkzeug.wsgi.get_host`
|
68 |
| -# https://github.com/pallets/werkzeug/blob/1.0.1/src/werkzeug/wsgi.py#L145 |
| 64 | +# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/wsgi.py#L86 |
69 | 65 | #
|
70 | 66 | def get_host(environ, use_x_forwarded_for=False):
|
71 | 67 | # type: (Dict[str, str], bool) -> str
|
72 | 68 | """
|
73 | 69 | Return the host for the given WSGI environment.
|
74 | 70 | """
|
75 |
| - if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ: |
76 |
| - rv = environ["HTTP_X_FORWARDED_HOST"] |
77 |
| - if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"): |
78 |
| - rv = rv[:-3] |
79 |
| - elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"): |
80 |
| - rv = rv[:-4] |
81 |
| - elif environ.get("HTTP_HOST"): |
82 |
| - rv = environ["HTTP_HOST"] |
83 |
| - if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"): |
84 |
| - rv = rv[:-3] |
85 |
| - elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"): |
86 |
| - rv = rv[:-4] |
87 |
| - elif environ.get("SERVER_NAME"): |
88 |
| - rv = environ["SERVER_NAME"] |
89 |
| - if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in ( |
90 |
| - ("https", "443"), |
91 |
| - ("http", "80"), |
92 |
| - ): |
93 |
| - rv += ":" + environ["SERVER_PORT"] |
94 |
| - else: |
95 |
| - # In spite of the WSGI spec, SERVER_NAME might not be present. |
96 |
| - rv = "unknown" |
97 |
| - |
98 |
| - return rv |
| 71 | + return _get_host( |
| 72 | + environ["wsgi.url_scheme"], |
| 73 | + environ.get("HTTP_HOST"), |
| 74 | + _get_server(environ), |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +# `_get_host` comes from `werkzeug.sansio.utils` |
| 79 | +# https://github.com/pallets/werkzeug/blob/3.1.3/src/werkzeug/sansio/utils.py#L49 |
| 80 | +def _get_host( |
| 81 | + scheme, |
| 82 | + host_header, |
| 83 | + server=None, |
| 84 | +): |
| 85 | + # type: (str, str | None, Tuple[str, int | None] | None) -> str |
| 86 | + """ |
| 87 | + Return the host for the given parameters. |
| 88 | + """ |
| 89 | + host = "" |
| 90 | + |
| 91 | + if host_header is not None: |
| 92 | + host = host_header |
| 93 | + elif server is not None: |
| 94 | + host = server[0] |
| 95 | + |
| 96 | + # If SERVER_NAME is IPv6, wrap it in [] to match Host header. |
| 97 | + # Check for : because domain or IPv4 can't have that. |
| 98 | + if ":" in host and host[0] != "[": |
| 99 | + host = f"[{host}]" |
| 100 | + |
| 101 | + if server[1] is not None: |
| 102 | + host = f"{host}:{server[1]}" # noqa: E231 |
| 103 | + |
| 104 | + if scheme in {"http", "ws"} and host.endswith(":80"): |
| 105 | + host = host[:-3] |
| 106 | + elif scheme in {"https", "wss"} and host.endswith(":443"): |
| 107 | + host = host[:-4] |
| 108 | + |
| 109 | + return host |
| 110 | + |
| 111 | + |
| 112 | +def _get_server(environ): |
| 113 | + # type: (Dict[str, str]) -> Tuple[str, int | None] | None |
| 114 | + name = environ.get("SERVER_NAME") |
| 115 | + |
| 116 | + if name is None: |
| 117 | + return None |
| 118 | + |
| 119 | + try: |
| 120 | + port = int(environ.get("SERVER_PORT", None)) |
| 121 | + except (TypeError, ValueError): |
| 122 | + # unix socket |
| 123 | + port = None |
| 124 | + |
| 125 | + return name, port |
0 commit comments