_werkzeug.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #
  34. # `get_headers` comes from `werkzeug.datastructures.EnvironHeaders`
  35. # https://github.com/pallets/werkzeug/blob/0.14.1/werkzeug/datastructures.py#L1361
  36. #
  37. # We need this function because Django does not give us a "pure" http header
  38. # dict. So we might as well use it for all WSGI integrations.
  39. #
  40. def _get_headers(environ):
  41. # type: (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. #
  55. # `get_host` comes from `werkzeug.wsgi.get_host`
  56. # https://github.com/pallets/werkzeug/blob/1.0.1/src/werkzeug/wsgi.py#L145
  57. #
  58. def get_host(environ, use_x_forwarded_for=False):
  59. # type: (Dict[str, str], bool) -> str
  60. """
  61. Return the host for the given WSGI environment.
  62. """
  63. if use_x_forwarded_for and "HTTP_X_FORWARDED_HOST" in environ:
  64. rv = environ["HTTP_X_FORWARDED_HOST"]
  65. if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
  66. rv = rv[:-3]
  67. elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
  68. rv = rv[:-4]
  69. elif environ.get("HTTP_HOST"):
  70. rv = environ["HTTP_HOST"]
  71. if environ["wsgi.url_scheme"] == "http" and rv.endswith(":80"):
  72. rv = rv[:-3]
  73. elif environ["wsgi.url_scheme"] == "https" and rv.endswith(":443"):
  74. rv = rv[:-4]
  75. elif environ.get("SERVER_NAME"):
  76. rv = environ["SERVER_NAME"]
  77. if (environ["wsgi.url_scheme"], environ["SERVER_PORT"]) not in (
  78. ("https", "443"),
  79. ("http", "80"),
  80. ):
  81. rv += ":" + environ["SERVER_PORT"]
  82. else:
  83. # In spite of the WSGI spec, SERVER_NAME might not be present.
  84. rv = "unknown"
  85. return rv