Like :meth:`bind` but you can pass it an WSGI environment and it will fetch the information from that dictionary. Note that because of limitations in the protocol there is no way to get the current subdomain and real `server_name` from the environment. If you don't
(
self,
environ: WSGIEnvironment | Request,
server_name: str | None = None,
subdomain: str | None = None,
)
| 250 | ) |
| 251 | |
| 252 | def bind_to_environ( |
| 253 | self, |
| 254 | environ: WSGIEnvironment | Request, |
| 255 | server_name: str | None = None, |
| 256 | subdomain: str | None = None, |
| 257 | ) -> MapAdapter: |
| 258 | """Like :meth:`bind` but you can pass it an WSGI environment and it |
| 259 | will fetch the information from that dictionary. Note that because of |
| 260 | limitations in the protocol there is no way to get the current |
| 261 | subdomain and real `server_name` from the environment. If you don't |
| 262 | provide it, Werkzeug will use `SERVER_NAME` and `SERVER_PORT` (or |
| 263 | `HTTP_HOST` if provided) as used `server_name` with disabled subdomain |
| 264 | feature. |
| 265 | |
| 266 | If `subdomain` is `None` but an environment and a server name is |
| 267 | provided it will calculate the current subdomain automatically. |
| 268 | Example: `server_name` is ``'example.com'`` and the `SERVER_NAME` |
| 269 | in the wsgi `environ` is ``'staging.dev.example.com'`` the calculated |
| 270 | subdomain will be ``'staging.dev'``. |
| 271 | |
| 272 | If the object passed as environ has an environ attribute, the value of |
| 273 | this attribute is used instead. This allows you to pass request |
| 274 | objects. Additionally `PATH_INFO` added as a default of the |
| 275 | :class:`MapAdapter` so that you don't have to pass the path info to |
| 276 | the match method. |
| 277 | |
| 278 | .. versionchanged:: 1.0.0 |
| 279 | If the passed server name specifies port 443, it will match |
| 280 | if the incoming scheme is ``https`` without a port. |
| 281 | |
| 282 | .. versionchanged:: 1.0.0 |
| 283 | A warning is shown when the passed server name does not |
| 284 | match the incoming WSGI server name. |
| 285 | |
| 286 | .. versionchanged:: 0.8 |
| 287 | This will no longer raise a ValueError when an unexpected server |
| 288 | name was passed. |
| 289 | |
| 290 | .. versionchanged:: 0.5 |
| 291 | previously this method accepted a bogus `calculate_subdomain` |
| 292 | parameter that did not have any effect. It was removed because |
| 293 | of that. |
| 294 | |
| 295 | :param environ: a WSGI environment. |
| 296 | :param server_name: an optional server name hint (see above). |
| 297 | :param subdomain: optionally the current subdomain (see above). |
| 298 | """ |
| 299 | env = _get_environ(environ) |
| 300 | wsgi_server_name = get_host(env).lower() |
| 301 | scheme = env["wsgi.url_scheme"] |
| 302 | upgrade = any( |
| 303 | v.strip() == "upgrade" |
| 304 | for v in env.get("HTTP_CONNECTION", "").lower().split(",") |
| 305 | ) |
| 306 | |
| 307 | if upgrade and env.get("HTTP_UPGRADE", "").lower() == "websocket": |
| 308 | scheme = "wss" if scheme == "https" else "ws" |
| 309 |