MCPcopy
hub / github.com/pallets/flask / create_url_adapter

Method create_url_adapter

src/flask/app.py:425–476  ·  view source on GitHub ↗

Creates a URL adapter for the given request. The URL adapter is created at a point where the request context is not yet set up so the request is passed explicitly. .. versionchanged:: 3.1 If :data:`SERVER_NAME` is set, it does not restrict requests to

(self, request: Request | None)

Source from the content-addressed store, hash-verified

423 return rv
424
425 def create_url_adapter(self, request: Request | None) -> MapAdapter | None:
426 """Creates a URL adapter for the given request. The URL adapter
427 is created at a point where the request context is not yet set
428 up so the request is passed explicitly.
429
430 .. versionchanged:: 3.1
431 If :data:`SERVER_NAME` is set, it does not restrict requests to
432 only that domain, for both ``subdomain_matching`` and
433 ``host_matching``.
434
435 .. versionchanged:: 1.0
436 :data:`SERVER_NAME` no longer implicitly enables subdomain
437 matching. Use :attr:`subdomain_matching` instead.
438
439 .. versionchanged:: 0.9
440 This can be called outside a request when the URL adapter is created
441 for an application context.
442
443 .. versionadded:: 0.6
444 """
445 if request is not None:
446 if (trusted_hosts := self.config["TRUSTED_HOSTS"]) is not None:
447 request.trusted_hosts = trusted_hosts
448
449 # Check trusted_hosts here until bind_to_environ does.
450 request.host = get_host(request.environ, request.trusted_hosts) # pyright: ignore
451 subdomain = None
452 server_name = self.config["SERVER_NAME"]
453
454 if self.url_map.host_matching:
455 # Don't pass SERVER_NAME, otherwise it's used and the actual
456 # host is ignored, which breaks host matching.
457 server_name = None
458 elif not self.subdomain_matching:
459 # Werkzeug doesn't implement subdomain matching yet. Until then,
460 # disable it by forcing the current subdomain to the default, or
461 # the empty string.
462 subdomain = self.url_map.default_subdomain or ""
463
464 return self.url_map.bind_to_environ(
465 request.environ, server_name=server_name, subdomain=subdomain
466 )
467
468 # Need at least SERVER_NAME to match/build outside a request.
469 if self.config["SERVER_NAME"] is not None:
470 return self.url_map.bind(
471 self.config["SERVER_NAME"],
472 script_name=self.config["APPLICATION_ROOT"],
473 url_scheme=self.config["PREFERRED_URL_SCHEME"],
474 )
475
476 return None
477
478 def raise_routing_exception(self, request: Request) -> t.NoReturn:
479 """Intercept routing exceptions and possibly do something else.

Callers 3

url_forMethod · 0.95
__init__Method · 0.80
__init__Method · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected