]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/werkzeug/sansio/utils.py
1 from __future__
import annotations
4 from urllib
.parse
import quote
6 from .._internal
import _plain_int
7 from ..exceptions
import SecurityError
8 from ..urls
import uri_to_iri
11 def host_is_trusted(hostname
: str, trusted_list
: t
.Iterable
[str]) -> bool:
12 """Check if a host matches a list of trusted names.
14 :param hostname: The name to check.
15 :param trusted_list: A list of valid names to match. If a name
16 starts with a dot it will match all subdomains.
24 hostname
= hostname
.partition(":")[0].encode("idna").decode("ascii")
25 except UnicodeEncodeError:
28 if isinstance(trusted_list
, str):
29 trusted_list
= [trusted_list
]
31 for ref
in trusted_list
:
32 if ref
.startswith("."):
39 ref
= ref
.partition(":")[0].encode("idna").decode("ascii")
40 except UnicodeEncodeError:
43 if ref
== hostname
or (suffix_match
and hostname
.endswith(f
".{ref}")):
51 host_header
: str |
None,
52 server
: tuple[str, int |
None] |
None = None,
53 trusted_hosts
: t
.Iterable
[str] |
None = None,
55 """Return the host for the given parameters.
57 This first checks the ``host_header``. If it's not present, then
58 ``server`` is used. The host will only contain the port if it is
59 different than the standard port for the protocol.
61 Optionally, verify that the host is trusted using
62 :func:`host_is_trusted` and raise a
63 :exc:`~werkzeug.exceptions.SecurityError` if it is not.
65 :param scheme: The protocol the request used, like ``"https"``.
66 :param host_header: The ``Host`` header value.
67 :param server: Address of the server. ``(host, port)``, or
68 ``(path, None)`` for unix sockets.
69 :param trusted_hosts: A list of trusted host names.
71 :return: Host, with port if necessary.
72 :raise ~werkzeug.exceptions.SecurityError: If the host is not
77 if host_header
is not None:
79 elif server
is not None:
82 if server
[1] is not None:
83 host
= f
"{host}:{server[1]}"
85 if scheme
in {"http", "ws"}
and host
.endswith(":80"):
87 elif scheme
in {"https", "wss"}
and host
.endswith(":443"):
90 if trusted_hosts
is not None:
91 if not host_is_trusted(host
, trusted_hosts
):
92 raise SecurityError(f
"Host {host!r} is not trusted.")
100 root_path
: str |
None = None,
101 path
: str |
None = None,
102 query_string
: bytes |
None = None,
104 """Recreate the URL for a request. If an optional part isn't
105 provided, it and subsequent parts are not included in the URL.
107 The URL is an IRI, not a URI, so it may contain Unicode characters.
108 Use :func:`~werkzeug.urls.iri_to_uri` to convert it to ASCII.
110 :param scheme: The protocol the request used, like ``"https"``.
111 :param host: The host the request was made to. See :func:`get_host`.
112 :param root_path: Prefix that the application is mounted under. This
113 is prepended to ``path``.
114 :param path: The path part of the URL after ``root_path``.
115 :param query_string: The portion of the URL after the "?".
117 url
= [scheme
, "://", host
]
119 if root_path
is None:
121 return uri_to_iri("".join(url
))
123 # safe = https://url.spec.whatwg.org/#url-path-segment-string
124 # as well as percent for things that are already quoted
125 url
.append(quote(root_path
.rstrip("/"), safe
="!$&'()*+,/:;=@%"))
129 return uri_to_iri("".join(url
))
131 url
.append(quote(path
.lstrip("/"), safe
="!$&'()*+,/:;=@%"))
135 url
.append(quote(query_string
, safe
="!$&'()*+,/:;=?@%"))
137 return uri_to_iri("".join(url
))
140 def get_content_length(
141 http_content_length
: str |
None = None,
142 http_transfer_encoding
: str |
None = None,
144 """Return the ``Content-Length`` header value as an int. If the header is not given
145 or the ``Transfer-Encoding`` header is ``chunked``, ``None`` is returned to indicate
146 a streaming request. If the value is not an integer, or negative, 0 is returned.
148 :param http_content_length: The Content-Length HTTP header.
149 :param http_transfer_encoding: The Transfer-Encoding HTTP header.
151 .. versionadded:: 2.2
153 if http_transfer_encoding
== "chunked" or http_content_length
is None:
157 return max(0, _plain_int(http_content_length
))