MCPcopy Create free account
hub / github.com/encode/starlette / parse_host_header

Function parse_host_header

starlette/_utils.py:118–137  ·  view source on GitHub ↗

Parse `host_header` into its host and port components. The host preserves brackets around IP literals. Invalid headers produce `None`.

(host_header: str | None)

Source from the content-addressed store, hash-verified

116
117
118def parse_host_header(host_header: str | None) -> ParsedHost | None:
119 """Parse `host_header` into its host and port components.
120
121 The host preserves brackets around IP literals. Invalid headers produce `None`.
122 """
123 if host_header is None:
124 return None
125
126 match = _HOST_RE.fullmatch(host_header)
127 if match is None:
128 return None
129
130 ipv6 = match["ipv6"]
131 if ipv6 is not None:
132 try:
133 IPv6Address(ipv6)
134 except AddressValueError:
135 return None
136
137 return ParsedHost(match["host"], match["port"])
138
139
140def get_route_path(scope: Scope) -> str:

Callers 3

matchesMethod · 0.90
__init__Method · 0.90
__call__Method · 0.90

Calls 1

ParsedHostClass · 0.85

Tested by

no test coverage detected