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

Method __init__

starlette/datastructures.py:26–67  ·  view source on GitHub ↗
(
        self,
        url: str = "",
        scope: Scope | None = None,
        **components: Any,
    )

Source from the content-addressed store, hash-verified

24
25class URL:
26 def __init__(
27 self,
28 url: str = "",
29 scope: Scope | None = None,
30 **components: Any,
31 ) -> None:
32 if scope is not None:
33 assert not url, 'Cannot set both "url" and "scope".'
34 assert not components, 'Cannot set both "scope" and "**components".'
35 scheme = scope.get("scheme", "http")
36 server = scope.get("server", None)
37 path = scope["path"]
38 query_string = scope.get("query_string", b"")
39
40 host_header = None
41 for key, value in scope["headers"]:
42 if key == b"host":
43 host_header = value.decode("latin-1")
44 break
45
46 parsed_host = parse_host_header(host_header)
47 if parsed_host is not None and parsed_host.is_valid_port:
48 netloc = parsed_host.authority
49 elif server is not None:
50 host, port = server
51 if ":" in host and not host.startswith("["):
52 host = f"[{host}]"
53 default_port = {"http": 80, "https": 443, "ws": 80, "wss": 443}[scheme]
54 netloc = host if port == default_port else f"{host}:{port}"
55 else:
56 netloc = None
57
58 query = query_string.decode()
59 if netloc is not None:
60 url = SplitResult(scheme=scheme, netloc=netloc, path=path, query=query, fragment="").geturl()
61 else:
62 url = f"{path}?{query}" if query else path
63 elif components:
64 assert not url, 'Cannot set both "url" and "**components".'
65 url = URL("").replace(**components).components.geturl()
66
67 self._url = url
68
69 @property
70 def components(self) -> SplitResult:

Callers 2

__init__Method · 0.45
__init__Method · 0.45

Calls 5

parse_host_headerFunction · 0.90
URLClass · 0.85
decodeMethod · 0.80
replaceMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected