_werkzeug.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. Copyright (c) 2007 by the Pallets team.
  3. Some rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the copyright holder nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  17. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. THIS SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. SUCH DAMAGE.
  27. """
  28. from typing import TYPE_CHECKING
  29. if TYPE_CHECKING:
  30. from typing import Dict
  31. from typing import Iterator
  32. from typing import Tuple
  33. from typing import Optional
  34. #
  35. # `get_headers` comes from `werkzeug.datastructures.EnvironHeaders`
  36. # https://github.com/pallets/werkzeug/blob/0.14.1/werkzeug/datastructures.py#L1361
  37. #
  38. # We need this function because Django does not give us a "pure" http header
  39. # dict. So we might as well use it for all WSGI integrations.
  40. #
  41. def _get_headers(environ: "Dict[str, str]") -> "Iterator[Tuple[str, str]]":
  42. """
  43. Returns only proper HTTP headers.
  44. """
  45. for key, value in environ.items():
  46. key = str(key)
  47. if key.startswith("HTTP_") and key not in (
  48. "HTTP_CONTENT_TYPE",
  49. "HTTP_CONTENT_LENGTH",
  50. ):
  51. yield key[5:].replace("_", "-").title(), value
  52. elif key in ("CONTENT_TYPE", "CONTENT_LENGTH"):
  53. yield key.replace("_", "-").title(), value
  54. def _strip_default_port(host: str, scheme: "Optional[str]") -> str:
  55. """Strip the port from the host if it's the default for the scheme."""
  56. if scheme == "http" and host.endswith(":80"):
  57. return host[:-3]
  58. if scheme == "https" and host.endswith(":443"):
  59. return host[:-4]
  60. return host
  61. # `get_host` comes from `werkzeug.wsgi.get_host`
  62. # https://github.com/pallets/werkzeug/blob/1.0.1/src/werkzeug/wsgi.py#L145
  63. def get_host(environ: "Dict[str, str]", use_x_forwarded_for: bool = False) -> str:
  64. """
  65. Return the host for the given WSGI environment.
  66. """
  67. scheme = environ.get("wsgi.url_scheme")
  68. if use_x_forwarded_for:
  69. scheme = environ.get("HTTP_X_FORWARDED_PROTO", scheme)
  70. if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ:
  71. return _strip_default_port(environ["HTTP_X_FORWARDED_HOST"], scheme)
  72. elif environ.get("HTTP_HOST"):
  73. return _strip_default_port(environ["HTTP_HOST"], scheme)
  74. elif environ.get("SERVER_NAME"):
  75. # SERVER_NAME/SERVER_PORT describe the internal server, so use
  76. # wsgi.url_scheme (not the forwarded scheme) for port decisions.
  77. rv = environ["SERVER_NAME"]
  78. if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in (
  79. ("https", "443"),
  80. ("http", "80"),
  81. ):
  82. rv += ":" + environ["SERVER_PORT"]
  83. return rv
  84. else:
  85. # In spite of the WSGI spec, SERVER_NAME might not be present.
  86. return "unknown"