We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Enable x_host and x_proto by default through deprecated proxyfix?
When #1314 refactored ProxyFix, it aliased num_proxies to x_for only (as that was the only one which previously supported multiple proxies).

However in doing so it disabled forwarding of host and scheme, [which were forwarded by default up to 0.14](https://github.com/pallets/werkzeug/blob/f1d15a2c7f35dcde0e52787c9fdf9ea6f6405308/werkzeug/contrib/fixers.py#L146-L151), which breaks systems relying on this.

I'd like to set x_host=1 and x_proto=1 in 0.15 IFF the user goes through the deprecated wrapper (`werkzeug.contrib.fixers.ProxyFix`) in order to restore the old behaviour, and maybe add a small warning about this behavioural change to the deprecation message (or possibly in one of the `versionchanged` blocks of the new version). Would that be acceptable / interesting or a waste of time?

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/werkzeug/middleware/proxy_fix.py`
Content:
```
1 """
2 X-Forwarded-For Proxy Fix
3 =========================
4 
5 This module provides a middleware that adjusts the WSGI environ based on
6 ``X-Forwarded-`` headers that proxies in front of an application may
7 set.
8 
9 When an application is running behind a proxy server, WSGI may see the
10 request as coming from that server rather than the real client. Proxies
11 set various headers to track where the request actually came from.
12 
13 This middleware should only be applied if the application is actually
14 behind such a proxy, and should be configured with the number of proxies
15 that are chained in front of it. Not all proxies set all the headers.
16 Since incoming headers can be faked, you must set how many proxies are
17 setting each header so the middleware knows what to trust.
18 
19 .. autoclass:: ProxyFix
20 
21 :copyright: 2007 Pallets
22 :license: BSD-3-Clause
23 """
24 import warnings
25 
26 
27 class ProxyFix(object):
28     """Adjust the WSGI environ based on ``X-Forwarded-`` that proxies in
29     front of the application may set.
30 
31     -   ``X-Forwarded-For`` sets ``REMOTE_ADDR``.
32     -   ``X-Forwarded-Proto`` sets ``wsgi.url_scheme``.
33     -   ``X-Forwarded-Host`` sets ``HTTP_HOST``, ``SERVER_NAME``, and
34         ``SERVER_PORT``.
35     -   ``X-Forwarded-Port`` sets ``HTTP_HOST`` and ``SERVER_PORT``.
36     -   ``X-Forwarded-Prefix`` sets ``SCRIPT_NAME``.
37 
38     You must tell the middleware how many proxies set each header so it
39     knows what values to trust. It is a security issue to trust values
40     that came from the client rather than a proxy.
41 
42     The original values of the headers are stored in the WSGI
43     environ as ``werkzeug.proxy_fix.orig``, a dict.
44 
45     :param app: The WSGI application to wrap.
46     :param x_for: Number of values to trust for ``X-Forwarded-For``.
47     :param x_proto: Number of values to trust for ``X-Forwarded-Proto``.
48     :param x_host: Number of values to trust for ``X-Forwarded-Host``.
49     :param x_port: Number of values to trust for ``X-Forwarded-Port``.
50     :param x_prefix: Number of values to trust for
51         ``X-Forwarded-Prefix``.
52     :param num_proxies: Deprecated, use ``x_for`` instead.
53 
54     .. code-block:: python
55 
56         from werkzeug.middleware.proxy_fix import ProxyFix
57         # App is behind one proxy that sets the -For and -Host headers.
58         app = ProxyFix(app, x_for=1, x_host=1)
59 
60     .. versionchanged:: 0.15
61         All headers support multiple values. The ``num_proxies``
62         argument is deprecated. Each header is configured with a
63         separate number of trusted proxies.
64 
65     .. versionchanged:: 0.15
66         Original WSGI environ values are stored in the
67         ``werkzeug.proxy_fix.orig`` dict. ``orig_remote_addr``,
68         ``orig_wsgi_url_scheme``, and ``orig_http_host`` are deprecated
69         and will be removed in 1.0.
70 
71     .. versionchanged:: 0.15
72         Support ``X-Forwarded-Port`` and ``X-Forwarded-Prefix``.
73 
74     .. versionchanged:: 0.15
75         ``X-Fowarded-Host`` and ``X-Forwarded-Port`` modify
76         ``SERVER_NAME`` and ``SERVER_PORT``.
77     """
78 
79     def __init__(
80         self, app, num_proxies=None, x_for=1, x_proto=0, x_host=0, x_port=0, x_prefix=0
81     ):
82         self.app = app
83         self.x_for = x_for
84         self.x_proto = x_proto
85         self.x_host = x_host
86         self.x_port = x_port
87         self.x_prefix = x_prefix
88         self.num_proxies = num_proxies
89 
90     @property
91     def num_proxies(self):
92         """The number of proxies setting ``X-Forwarded-For`` in front
93         of the application.
94 
95         .. deprecated:: 0.15
96             A separate number of trusted proxies is configured for each
97             header. ``num_proxies`` maps to ``x_for``. This method will
98             be removed in 1.0.
99 
100         :internal:
101         """
102         warnings.warn(
103             "'num_proxies' is deprecated as of version 0.15 and will be"
104             " removed in version 1.0. Use 'x_for' instead.",
105             DeprecationWarning,
106             stacklevel=2,
107         )
108         return self.x_for
109 
110     @num_proxies.setter
111     def num_proxies(self, value):
112         if value is not None:
113             warnings.warn(
114                 "'num_proxies' is deprecated as of version 0.15 and"
115                 " will be removed in version 1.0. Use 'x_for' instead.",
116                 DeprecationWarning,
117                 stacklevel=2,
118             )
119             self.x_for = value
120 
121     def get_remote_addr(self, forwarded_for):
122         """Get the real ``remote_addr`` by looking backwards ``x_for``
123         number of values in the ``X-Forwarded-For`` header.
124 
125         :param forwarded_for: List of values parsed from the
126             ``X-Forwarded-For`` header.
127         :return: The real ``remote_addr``, or ``None`` if there were not
128             at least ``x_for`` values.
129 
130         .. deprecated:: 0.15
131             This is handled internally for each header. This method will
132             be removed in 1.0.
133 
134         .. versionchanged:: 0.9
135             Use ``num_proxies`` instead of always picking the first
136             value.
137 
138         .. versionadded:: 0.8
139         """
140         warnings.warn(
141             "'get_remote_addr' is deprecated as of version 0.15 and"
142             " will be removed in version 1.0. It is now handled"
143             " internally for each header.",
144             DeprecationWarning,
145         )
146         return self._get_trusted_comma(self.x_for, ",".join(forwarded_for))
147 
148     def _get_trusted_comma(self, trusted, value):
149         """Get the real value from a comma-separated header based on the
150         configured number of trusted proxies.
151 
152         :param trusted: Number of values to trust in the header.
153         :param value: Header value to parse.
154         :return: The real value, or ``None`` if there are fewer values
155             than the number of trusted proxies.
156 
157         .. versionadded:: 0.15
158         """
159         if not (trusted and value):
160             return
161         values = [x.strip() for x in value.split(",")]
162         if len(values) >= trusted:
163             return values[-trusted]
164 
165     def __call__(self, environ, start_response):
166         """Modify the WSGI environ based on the various ``Forwarded``
167         headers before calling the wrapped application. Store the
168         original environ values in ``werkzeug.proxy_fix.orig_{key}``.
169         """
170         environ_get = environ.get
171         orig_remote_addr = environ_get("REMOTE_ADDR")
172         orig_wsgi_url_scheme = environ_get("wsgi.url_scheme")
173         orig_http_host = environ_get("HTTP_HOST")
174         environ.update(
175             {
176                 "werkzeug.proxy_fix.orig": {
177                     "REMOTE_ADDR": orig_remote_addr,
178                     "wsgi.url_scheme": orig_wsgi_url_scheme,
179                     "HTTP_HOST": orig_http_host,
180                     "SERVER_NAME": environ_get("SERVER_NAME"),
181                     "SERVER_PORT": environ_get("SERVER_PORT"),
182                     "SCRIPT_NAME": environ_get("SCRIPT_NAME"),
183                 },
184                 # todo: remove deprecated keys
185                 "werkzeug.proxy_fix.orig_remote_addr": orig_remote_addr,
186                 "werkzeug.proxy_fix.orig_wsgi_url_scheme": orig_wsgi_url_scheme,
187                 "werkzeug.proxy_fix.orig_http_host": orig_http_host,
188             }
189         )
190 
191         x_for = self._get_trusted_comma(self.x_for, environ_get("HTTP_X_FORWARDED_FOR"))
192         if x_for:
193             environ["REMOTE_ADDR"] = x_for
194 
195         x_proto = self._get_trusted_comma(
196             self.x_proto, environ_get("HTTP_X_FORWARDED_PROTO")
197         )
198         if x_proto:
199             environ["wsgi.url_scheme"] = x_proto
200 
201         x_host = self._get_trusted_comma(
202             self.x_host, environ_get("HTTP_X_FORWARDED_HOST")
203         )
204         if x_host:
205             environ["HTTP_HOST"] = x_host
206             parts = x_host.split(":", 1)
207             environ["SERVER_NAME"] = parts[0]
208             if len(parts) == 2:
209                 environ["SERVER_PORT"] = parts[1]
210 
211         x_port = self._get_trusted_comma(
212             self.x_port, environ_get("HTTP_X_FORWARDED_PORT")
213         )
214         if x_port:
215             host = environ.get("HTTP_HOST")
216             if host:
217                 parts = host.split(":", 1)
218                 host = parts[0] if len(parts) == 2 else host
219                 environ["HTTP_HOST"] = "%s:%s" % (host, x_port)
220             environ["SERVER_PORT"] = x_port
221 
222         x_prefix = self._get_trusted_comma(
223             self.x_prefix, environ_get("HTTP_X_FORWARDED_PREFIX")
224         )
225         if x_prefix:
226             environ["SCRIPT_NAME"] = x_prefix
227 
228         return self.app(environ, start_response)
229
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/werkzeug/middleware/proxy_fix.py b/src/werkzeug/middleware/proxy_fix.py
--- a/src/werkzeug/middleware/proxy_fix.py
+++ b/src/werkzeug/middleware/proxy_fix.py
@@ -77,7 +77,7 @@
     """
 
     def __init__(
-        self, app, num_proxies=None, x_for=1, x_proto=0, x_host=0, x_port=0, x_prefix=0
+        self, app, num_proxies=None, x_for=1, x_proto=1, x_host=0, x_port=0, x_prefix=0
     ):
         self.app = app
         self.x_for = x_for
@@ -112,11 +112,15 @@
         if value is not None:
             warnings.warn(
                 "'num_proxies' is deprecated as of version 0.15 and"
-                " will be removed in version 1.0. Use 'x_for' instead.",
+                " will be removed in version 1.0. Use"
+                " 'x_for={value}, x_proto={value}, x_host={value}'"
+                " instead.".format(value=value),
                 DeprecationWarning,
                 stacklevel=2,
             )
             self.x_for = value
+            self.x_proto = value
+            self.x_host = value
 
     def get_remote_addr(self, forwarded_for):
         """Get the real ``remote_addr`` by looking backwards ``x_for``

{"golden_diff": "diff --git a/src/werkzeug/middleware/proxy_fix.py b/src/werkzeug/middleware/proxy_fix.py\n--- a/src/werkzeug/middleware/proxy_fix.py\n+++ b/src/werkzeug/middleware/proxy_fix.py\n@@ -77,7 +77,7 @@\n     \"\"\"\n \n     def __init__(\n-        self, app, num_proxies=None, x_for=1, x_proto=0, x_host=0, x_port=0, x_prefix=0\n+        self, app, num_proxies=None, x_for=1, x_proto=1, x_host=0, x_port=0, x_prefix=0\n     ):\n         self.app = app\n         self.x_for = x_for\n@@ -112,11 +112,15 @@\n         if value is not None:\n             warnings.warn(\n                 \"'num_proxies' is deprecated as of version 0.15 and\"\n-                \" will be removed in version 1.0. Use 'x_for' instead.\",\n+                \" will be removed in version 1.0. Use\"\n+                \" 'x_for={value}, x_proto={value}, x_host={value}'\"\n+                \" instead.\".format(value=value),\n                 DeprecationWarning,\n                 stacklevel=2,\n             )\n             self.x_for = value\n+            self.x_proto = value\n+            self.x_host = value\n \n     def get_remote_addr(self, forwarded_for):\n         \"\"\"Get the real ``remote_addr`` by looking backwards ``x_for``\n", "issue": "Enable x_host and x_proto by default through deprecated proxyfix?\nWhen #1314 refactored ProxyFix, it aliased num_proxies to x_for only (as that was the only one which previously supported multiple proxies).\r\n\r\nHowever in doing so it disabled forwarding of host and scheme, [which were forwarded by default up to 0.14](https://github.com/pallets/werkzeug/blob/f1d15a2c7f35dcde0e52787c9fdf9ea6f6405308/werkzeug/contrib/fixers.py#L146-L151), which breaks systems relying on this.\r\n\r\nI'd like to set x_host=1 and x_proto=1 in 0.15 IFF the user goes through the deprecated wrapper (`werkzeug.contrib.fixers.ProxyFix`) in order to restore the old behaviour, and maybe add a small warning about this behavioural change to the deprecation message (or possibly in one of the `versionchanged` blocks of the new version). Would that be acceptable / interesting or a waste of time?\n", "before_files": [{"content": "\"\"\"\nX-Forwarded-For Proxy Fix\n=========================\n\nThis module provides a middleware that adjusts the WSGI environ based on\n``X-Forwarded-`` headers that proxies in front of an application may\nset.\n\nWhen an application is running behind a proxy server, WSGI may see the\nrequest as coming from that server rather than the real client. Proxies\nset various headers to track where the request actually came from.\n\nThis middleware should only be applied if the application is actually\nbehind such a proxy, and should be configured with the number of proxies\nthat are chained in front of it. Not all proxies set all the headers.\nSince incoming headers can be faked, you must set how many proxies are\nsetting each header so the middleware knows what to trust.\n\n.. autoclass:: ProxyFix\n\n:copyright: 2007 Pallets\n:license: BSD-3-Clause\n\"\"\"\nimport warnings\n\n\nclass ProxyFix(object):\n    \"\"\"Adjust the WSGI environ based on ``X-Forwarded-`` that proxies in\n    front of the application may set.\n\n    -   ``X-Forwarded-For`` sets ``REMOTE_ADDR``.\n    -   ``X-Forwarded-Proto`` sets ``wsgi.url_scheme``.\n    -   ``X-Forwarded-Host`` sets ``HTTP_HOST``, ``SERVER_NAME``, and\n        ``SERVER_PORT``.\n    -   ``X-Forwarded-Port`` sets ``HTTP_HOST`` and ``SERVER_PORT``.\n    -   ``X-Forwarded-Prefix`` sets ``SCRIPT_NAME``.\n\n    You must tell the middleware how many proxies set each header so it\n    knows what values to trust. It is a security issue to trust values\n    that came from the client rather than a proxy.\n\n    The original values of the headers are stored in the WSGI\n    environ as ``werkzeug.proxy_fix.orig``, a dict.\n\n    :param app: The WSGI application to wrap.\n    :param x_for: Number of values to trust for ``X-Forwarded-For``.\n    :param x_proto: Number of values to trust for ``X-Forwarded-Proto``.\n    :param x_host: Number of values to trust for ``X-Forwarded-Host``.\n    :param x_port: Number of values to trust for ``X-Forwarded-Port``.\n    :param x_prefix: Number of values to trust for\n        ``X-Forwarded-Prefix``.\n    :param num_proxies: Deprecated, use ``x_for`` instead.\n\n    .. code-block:: python\n\n        from werkzeug.middleware.proxy_fix import ProxyFix\n        # App is behind one proxy that sets the -For and -Host headers.\n        app = ProxyFix(app, x_for=1, x_host=1)\n\n    .. versionchanged:: 0.15\n        All headers support multiple values. The ``num_proxies``\n        argument is deprecated. Each header is configured with a\n        separate number of trusted proxies.\n\n    .. versionchanged:: 0.15\n        Original WSGI environ values are stored in the\n        ``werkzeug.proxy_fix.orig`` dict. ``orig_remote_addr``,\n        ``orig_wsgi_url_scheme``, and ``orig_http_host`` are deprecated\n        and will be removed in 1.0.\n\n    .. versionchanged:: 0.15\n        Support ``X-Forwarded-Port`` and ``X-Forwarded-Prefix``.\n\n    .. versionchanged:: 0.15\n        ``X-Fowarded-Host`` and ``X-Forwarded-Port`` modify\n        ``SERVER_NAME`` and ``SERVER_PORT``.\n    \"\"\"\n\n    def __init__(\n        self, app, num_proxies=None, x_for=1, x_proto=0, x_host=0, x_port=0, x_prefix=0\n    ):\n        self.app = app\n        self.x_for = x_for\n        self.x_proto = x_proto\n        self.x_host = x_host\n        self.x_port = x_port\n        self.x_prefix = x_prefix\n        self.num_proxies = num_proxies\n\n    @property\n    def num_proxies(self):\n        \"\"\"The number of proxies setting ``X-Forwarded-For`` in front\n        of the application.\n\n        .. deprecated:: 0.15\n            A separate number of trusted proxies is configured for each\n            header. ``num_proxies`` maps to ``x_for``. This method will\n            be removed in 1.0.\n\n        :internal:\n        \"\"\"\n        warnings.warn(\n            \"'num_proxies' is deprecated as of version 0.15 and will be\"\n            \" removed in version 1.0. Use 'x_for' instead.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return self.x_for\n\n    @num_proxies.setter\n    def num_proxies(self, value):\n        if value is not None:\n            warnings.warn(\n                \"'num_proxies' is deprecated as of version 0.15 and\"\n                \" will be removed in version 1.0. Use 'x_for' instead.\",\n                DeprecationWarning,\n                stacklevel=2,\n            )\n            self.x_for = value\n\n    def get_remote_addr(self, forwarded_for):\n        \"\"\"Get the real ``remote_addr`` by looking backwards ``x_for``\n        number of values in the ``X-Forwarded-For`` header.\n\n        :param forwarded_for: List of values parsed from the\n            ``X-Forwarded-For`` header.\n        :return: The real ``remote_addr``, or ``None`` if there were not\n            at least ``x_for`` values.\n\n        .. deprecated:: 0.15\n            This is handled internally for each header. This method will\n            be removed in 1.0.\n\n        .. versionchanged:: 0.9\n            Use ``num_proxies`` instead of always picking the first\n            value.\n\n        .. versionadded:: 0.8\n        \"\"\"\n        warnings.warn(\n            \"'get_remote_addr' is deprecated as of version 0.15 and\"\n            \" will be removed in version 1.0. It is now handled\"\n            \" internally for each header.\",\n            DeprecationWarning,\n        )\n        return self._get_trusted_comma(self.x_for, \",\".join(forwarded_for))\n\n    def _get_trusted_comma(self, trusted, value):\n        \"\"\"Get the real value from a comma-separated header based on the\n        configured number of trusted proxies.\n\n        :param trusted: Number of values to trust in the header.\n        :param value: Header value to parse.\n        :return: The real value, or ``None`` if there are fewer values\n            than the number of trusted proxies.\n\n        .. versionadded:: 0.15\n        \"\"\"\n        if not (trusted and value):\n            return\n        values = [x.strip() for x in value.split(\",\")]\n        if len(values) >= trusted:\n            return values[-trusted]\n\n    def __call__(self, environ, start_response):\n        \"\"\"Modify the WSGI environ based on the various ``Forwarded``\n        headers before calling the wrapped application. Store the\n        original environ values in ``werkzeug.proxy_fix.orig_{key}``.\n        \"\"\"\n        environ_get = environ.get\n        orig_remote_addr = environ_get(\"REMOTE_ADDR\")\n        orig_wsgi_url_scheme = environ_get(\"wsgi.url_scheme\")\n        orig_http_host = environ_get(\"HTTP_HOST\")\n        environ.update(\n            {\n                \"werkzeug.proxy_fix.orig\": {\n                    \"REMOTE_ADDR\": orig_remote_addr,\n                    \"wsgi.url_scheme\": orig_wsgi_url_scheme,\n                    \"HTTP_HOST\": orig_http_host,\n                    \"SERVER_NAME\": environ_get(\"SERVER_NAME\"),\n                    \"SERVER_PORT\": environ_get(\"SERVER_PORT\"),\n                    \"SCRIPT_NAME\": environ_get(\"SCRIPT_NAME\"),\n                },\n                # todo: remove deprecated keys\n                \"werkzeug.proxy_fix.orig_remote_addr\": orig_remote_addr,\n                \"werkzeug.proxy_fix.orig_wsgi_url_scheme\": orig_wsgi_url_scheme,\n                \"werkzeug.proxy_fix.orig_http_host\": orig_http_host,\n            }\n        )\n\n        x_for = self._get_trusted_comma(self.x_for, environ_get(\"HTTP_X_FORWARDED_FOR\"))\n        if x_for:\n            environ[\"REMOTE_ADDR\"] = x_for\n\n        x_proto = self._get_trusted_comma(\n            self.x_proto, environ_get(\"HTTP_X_FORWARDED_PROTO\")\n        )\n        if x_proto:\n            environ[\"wsgi.url_scheme\"] = x_proto\n\n        x_host = self._get_trusted_comma(\n            self.x_host, environ_get(\"HTTP_X_FORWARDED_HOST\")\n        )\n        if x_host:\n            environ[\"HTTP_HOST\"] = x_host\n            parts = x_host.split(\":\", 1)\n            environ[\"SERVER_NAME\"] = parts[0]\n            if len(parts) == 2:\n                environ[\"SERVER_PORT\"] = parts[1]\n\n        x_port = self._get_trusted_comma(\n            self.x_port, environ_get(\"HTTP_X_FORWARDED_PORT\")\n        )\n        if x_port:\n            host = environ.get(\"HTTP_HOST\")\n            if host:\n                parts = host.split(\":\", 1)\n                host = parts[0] if len(parts) == 2 else host\n                environ[\"HTTP_HOST\"] = \"%s:%s\" % (host, x_port)\n            environ[\"SERVER_PORT\"] = x_port\n\n        x_prefix = self._get_trusted_comma(\n            self.x_prefix, environ_get(\"HTTP_X_FORWARDED_PREFIX\")\n        )\n        if x_prefix:\n            environ[\"SCRIPT_NAME\"] = x_prefix\n\n        return self.app(environ, start_response)\n", "path": "src/werkzeug/middleware/proxy_fix.py"}], "after_files": [{"content": "\"\"\"\nX-Forwarded-For Proxy Fix\n=========================\n\nThis module provides a middleware that adjusts the WSGI environ based on\n``X-Forwarded-`` headers that proxies in front of an application may\nset.\n\nWhen an application is running behind a proxy server, WSGI may see the\nrequest as coming from that server rather than the real client. Proxies\nset various headers to track where the request actually came from.\n\nThis middleware should only be applied if the application is actually\nbehind such a proxy, and should be configured with the number of proxies\nthat are chained in front of it. Not all proxies set all the headers.\nSince incoming headers can be faked, you must set how many proxies are\nsetting each header so the middleware knows what to trust.\n\n.. autoclass:: ProxyFix\n\n:copyright: 2007 Pallets\n:license: BSD-3-Clause\n\"\"\"\nimport warnings\n\n\nclass ProxyFix(object):\n    \"\"\"Adjust the WSGI environ based on ``X-Forwarded-`` that proxies in\n    front of the application may set.\n\n    -   ``X-Forwarded-For`` sets ``REMOTE_ADDR``.\n    -   ``X-Forwarded-Proto`` sets ``wsgi.url_scheme``.\n    -   ``X-Forwarded-Host`` sets ``HTTP_HOST``, ``SERVER_NAME``, and\n        ``SERVER_PORT``.\n    -   ``X-Forwarded-Port`` sets ``HTTP_HOST`` and ``SERVER_PORT``.\n    -   ``X-Forwarded-Prefix`` sets ``SCRIPT_NAME``.\n\n    You must tell the middleware how many proxies set each header so it\n    knows what values to trust. It is a security issue to trust values\n    that came from the client rather than a proxy.\n\n    The original values of the headers are stored in the WSGI\n    environ as ``werkzeug.proxy_fix.orig``, a dict.\n\n    :param app: The WSGI application to wrap.\n    :param x_for: Number of values to trust for ``X-Forwarded-For``.\n    :param x_proto: Number of values to trust for ``X-Forwarded-Proto``.\n    :param x_host: Number of values to trust for ``X-Forwarded-Host``.\n    :param x_port: Number of values to trust for ``X-Forwarded-Port``.\n    :param x_prefix: Number of values to trust for\n        ``X-Forwarded-Prefix``.\n    :param num_proxies: Deprecated, use ``x_for`` instead.\n\n    .. code-block:: python\n\n        from werkzeug.middleware.proxy_fix import ProxyFix\n        # App is behind one proxy that sets the -For and -Host headers.\n        app = ProxyFix(app, x_for=1, x_host=1)\n\n    .. versionchanged:: 0.15\n        All headers support multiple values. The ``num_proxies``\n        argument is deprecated. Each header is configured with a\n        separate number of trusted proxies.\n\n    .. versionchanged:: 0.15\n        Original WSGI environ values are stored in the\n        ``werkzeug.proxy_fix.orig`` dict. ``orig_remote_addr``,\n        ``orig_wsgi_url_scheme``, and ``orig_http_host`` are deprecated\n        and will be removed in 1.0.\n\n    .. versionchanged:: 0.15\n        Support ``X-Forwarded-Port`` and ``X-Forwarded-Prefix``.\n\n    .. versionchanged:: 0.15\n        ``X-Fowarded-Host`` and ``X-Forwarded-Port`` modify\n        ``SERVER_NAME`` and ``SERVER_PORT``.\n    \"\"\"\n\n    def __init__(\n        self, app, num_proxies=None, x_for=1, x_proto=1, x_host=0, x_port=0, x_prefix=0\n    ):\n        self.app = app\n        self.x_for = x_for\n        self.x_proto = x_proto\n        self.x_host = x_host\n        self.x_port = x_port\n        self.x_prefix = x_prefix\n        self.num_proxies = num_proxies\n\n    @property\n    def num_proxies(self):\n        \"\"\"The number of proxies setting ``X-Forwarded-For`` in front\n        of the application.\n\n        .. deprecated:: 0.15\n            A separate number of trusted proxies is configured for each\n            header. ``num_proxies`` maps to ``x_for``. This method will\n            be removed in 1.0.\n\n        :internal:\n        \"\"\"\n        warnings.warn(\n            \"'num_proxies' is deprecated as of version 0.15 and will be\"\n            \" removed in version 1.0. Use 'x_for' instead.\",\n            DeprecationWarning,\n            stacklevel=2,\n        )\n        return self.x_for\n\n    @num_proxies.setter\n    def num_proxies(self, value):\n        if value is not None:\n            warnings.warn(\n                \"'num_proxies' is deprecated as of version 0.15 and\"\n                \" will be removed in version 1.0. Use\"\n                \" 'x_for={value}, x_proto={value}, x_host={value}'\"\n                \" instead.\".format(value=value),\n                DeprecationWarning,\n                stacklevel=2,\n            )\n            self.x_for = value\n            self.x_proto = value\n            self.x_host = value\n\n    def get_remote_addr(self, forwarded_for):\n        \"\"\"Get the real ``remote_addr`` by looking backwards ``x_for``\n        number of values in the ``X-Forwarded-For`` header.\n\n        :param forwarded_for: List of values parsed from the\n            ``X-Forwarded-For`` header.\n        :return: The real ``remote_addr``, or ``None`` if there were not\n            at least ``x_for`` values.\n\n        .. deprecated:: 0.15\n            This is handled internally for each header. This method will\n            be removed in 1.0.\n\n        .. versionchanged:: 0.9\n            Use ``num_proxies`` instead of always picking the first\n            value.\n\n        .. versionadded:: 0.8\n        \"\"\"\n        warnings.warn(\n            \"'get_remote_addr' is deprecated as of version 0.15 and\"\n            \" will be removed in version 1.0. It is now handled\"\n            \" internally for each header.\",\n            DeprecationWarning,\n        )\n        return self._get_trusted_comma(self.x_for, \",\".join(forwarded_for))\n\n    def _get_trusted_comma(self, trusted, value):\n        \"\"\"Get the real value from a comma-separated header based on the\n        configured number of trusted proxies.\n\n        :param trusted: Number of values to trust in the header.\n        :param value: Header value to parse.\n        :return: The real value, or ``None`` if there are fewer values\n            than the number of trusted proxies.\n\n        .. versionadded:: 0.15\n        \"\"\"\n        if not (trusted and value):\n            return\n        values = [x.strip() for x in value.split(\",\")]\n        if len(values) >= trusted:\n            return values[-trusted]\n\n    def __call__(self, environ, start_response):\n        \"\"\"Modify the WSGI environ based on the various ``Forwarded``\n        headers before calling the wrapped application. Store the\n        original environ values in ``werkzeug.proxy_fix.orig_{key}``.\n        \"\"\"\n        environ_get = environ.get\n        orig_remote_addr = environ_get(\"REMOTE_ADDR\")\n        orig_wsgi_url_scheme = environ_get(\"wsgi.url_scheme\")\n        orig_http_host = environ_get(\"HTTP_HOST\")\n        environ.update(\n            {\n                \"werkzeug.proxy_fix.orig\": {\n                    \"REMOTE_ADDR\": orig_remote_addr,\n                    \"wsgi.url_scheme\": orig_wsgi_url_scheme,\n                    \"HTTP_HOST\": orig_http_host,\n                    \"SERVER_NAME\": environ_get(\"SERVER_NAME\"),\n                    \"SERVER_PORT\": environ_get(\"SERVER_PORT\"),\n                    \"SCRIPT_NAME\": environ_get(\"SCRIPT_NAME\"),\n                },\n                # todo: remove deprecated keys\n                \"werkzeug.proxy_fix.orig_remote_addr\": orig_remote_addr,\n                \"werkzeug.proxy_fix.orig_wsgi_url_scheme\": orig_wsgi_url_scheme,\n                \"werkzeug.proxy_fix.orig_http_host\": orig_http_host,\n            }\n        )\n\n        x_for = self._get_trusted_comma(self.x_for, environ_get(\"HTTP_X_FORWARDED_FOR\"))\n        if x_for:\n            environ[\"REMOTE_ADDR\"] = x_for\n\n        x_proto = self._get_trusted_comma(\n            self.x_proto, environ_get(\"HTTP_X_FORWARDED_PROTO\")\n        )\n        if x_proto:\n            environ[\"wsgi.url_scheme\"] = x_proto\n\n        x_host = self._get_trusted_comma(\n            self.x_host, environ_get(\"HTTP_X_FORWARDED_HOST\")\n        )\n        if x_host:\n            environ[\"HTTP_HOST\"] = x_host\n            parts = x_host.split(\":\", 1)\n            environ[\"SERVER_NAME\"] = parts[0]\n            if len(parts) == 2:\n                environ[\"SERVER_PORT\"] = parts[1]\n\n        x_port = self._get_trusted_comma(\n            self.x_port, environ_get(\"HTTP_X_FORWARDED_PORT\")\n        )\n        if x_port:\n            host = environ.get(\"HTTP_HOST\")\n            if host:\n                parts = host.split(\":\", 1)\n                host = parts[0] if len(parts) == 2 else host\n                environ[\"HTTP_HOST\"] = \"%s:%s\" % (host, x_port)\n            environ[\"SERVER_PORT\"] = x_port\n\n        x_prefix = self._get_trusted_comma(\n            self.x_prefix, environ_get(\"HTTP_X_FORWARDED_PREFIX\")\n        )\n        if x_prefix:\n            environ[\"SCRIPT_NAME\"] = x_prefix\n\n        return self.app(environ, start_response)\n", "path": "src/werkzeug/middleware/proxy_fix.py"}]}