utils.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import annotations
  2. import typing as t
  3. from urllib.parse import quote
  4. from .._internal import _plain_int
  5. from ..exceptions import SecurityError
  6. from ..urls import uri_to_iri
  7. def host_is_trusted(hostname: str | None, trusted_list: t.Iterable[str]) -> bool:
  8. """Check if a host matches a list of trusted names.
  9. :param hostname: The name to check.
  10. :param trusted_list: A list of valid names to match. If a name
  11. starts with a dot it will match all subdomains.
  12. .. versionadded:: 0.9
  13. """
  14. if not hostname:
  15. return False
  16. try:
  17. hostname = hostname.partition(":")[0].encode("idna").decode("ascii")
  18. except UnicodeEncodeError:
  19. return False
  20. if isinstance(trusted_list, str):
  21. trusted_list = [trusted_list]
  22. for ref in trusted_list:
  23. if ref.startswith("."):
  24. ref = ref[1:]
  25. suffix_match = True
  26. else:
  27. suffix_match = False
  28. try:
  29. ref = ref.partition(":")[0].encode("idna").decode("ascii")
  30. except UnicodeEncodeError:
  31. return False
  32. if ref == hostname or (suffix_match and hostname.endswith(f".{ref}")):
  33. return True
  34. return False
  35. def get_host(
  36. scheme: str,
  37. host_header: str | None,
  38. server: tuple[str, int | None] | None = None,
  39. trusted_hosts: t.Iterable[str] | None = None,
  40. ) -> str:
  41. """Return the host for the given parameters.
  42. This first checks the ``host_header``. If it's not present, then
  43. ``server`` is used. The host will only contain the port if it is
  44. different than the standard port for the protocol.
  45. Optionally, verify that the host is trusted using
  46. :func:`host_is_trusted` and raise a
  47. :exc:`~werkzeug.exceptions.SecurityError` if it is not.
  48. :param scheme: The protocol the request used, like ``"https"``.
  49. :param host_header: The ``Host`` header value.
  50. :param server: Address of the server. ``(host, port)``, or
  51. ``(path, None)`` for unix sockets.
  52. :param trusted_hosts: A list of trusted host names.
  53. :return: Host, with port if necessary.
  54. :raise ~werkzeug.exceptions.SecurityError: If the host is not
  55. trusted.
  56. .. versionchanged:: 3.1.3
  57. If ``SERVER_NAME`` is IPv6, it is wrapped in ``[]``.
  58. """
  59. host = ""
  60. if host_header is not None:
  61. host = host_header
  62. elif server is not None:
  63. host = server[0]
  64. # If SERVER_NAME is IPv6, wrap it in [] to match Host header.
  65. # Check for : because domain or IPv4 can't have that.
  66. if ":" in host and host[0] != "[":
  67. host = f"[{host}]"
  68. if server[1] is not None:
  69. host = f"{host}:{server[1]}"
  70. if scheme in {"http", "ws"} and host.endswith(":80"):
  71. host = host[:-3]
  72. elif scheme in {"https", "wss"} and host.endswith(":443"):
  73. host = host[:-4]
  74. if trusted_hosts is not None:
  75. if not host_is_trusted(host, trusted_hosts):
  76. raise SecurityError(f"Host {host!r} is not trusted.")
  77. return host
  78. def get_current_url(
  79. scheme: str,
  80. host: str,
  81. root_path: str | None = None,
  82. path: str | None = None,
  83. query_string: bytes | None = None,
  84. ) -> str:
  85. """Recreate the URL for a request. If an optional part isn't
  86. provided, it and subsequent parts are not included in the URL.
  87. The URL is an IRI, not a URI, so it may contain Unicode characters.
  88. Use :func:`~werkzeug.urls.iri_to_uri` to convert it to ASCII.
  89. :param scheme: The protocol the request used, like ``"https"``.
  90. :param host: The host the request was made to. See :func:`get_host`.
  91. :param root_path: Prefix that the application is mounted under. This
  92. is prepended to ``path``.
  93. :param path: The path part of the URL after ``root_path``.
  94. :param query_string: The portion of the URL after the "?".
  95. """
  96. url = [scheme, "://", host]
  97. if root_path is None:
  98. url.append("/")
  99. return uri_to_iri("".join(url))
  100. # safe = https://url.spec.whatwg.org/#url-path-segment-string
  101. # as well as percent for things that are already quoted
  102. url.append(quote(root_path.rstrip("/"), safe="!$&'()*+,/:;=@%"))
  103. url.append("/")
  104. if path is None:
  105. return uri_to_iri("".join(url))
  106. url.append(quote(path.lstrip("/"), safe="!$&'()*+,/:;=@%"))
  107. if query_string:
  108. url.append("?")
  109. url.append(quote(query_string, safe="!$&'()*+,/:;=?@%"))
  110. return uri_to_iri("".join(url))
  111. def get_content_length(
  112. http_content_length: str | None = None,
  113. http_transfer_encoding: str | None = None,
  114. ) -> int | None:
  115. """Return the ``Content-Length`` header value as an int. If the header is not given
  116. or the ``Transfer-Encoding`` header is ``chunked``, ``None`` is returned to indicate
  117. a streaming request. If the value is not an integer, or negative, 0 is returned.
  118. :param http_content_length: The Content-Length HTTP header.
  119. :param http_transfer_encoding: The Transfer-Encoding HTTP header.
  120. .. versionadded:: 2.2
  121. """
  122. if http_transfer_encoding == "chunked" or http_content_length is None:
  123. return None
  124. try:
  125. return max(0, _plain_int(http_content_length))
  126. except ValueError:
  127. return 0