Code comments for URL model (#1602)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent e67b0dd · Browse files · 1 file·+63 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+63 added−10 removed
- other:+63 added−10 removed
Provenance
- Commit
c927f3e- Tree
0549c04- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
e67b0dd
1 file changed+63 added−10 removed
modified:httpx/_models.py+63 added-10 removed
260 unmodified lines261 def netloc(self) -> bytes:262 """263 Either `<host>` or `<host>:<port>` as bytes.264 Always normalized to lowercase, and IDNA encoded.265+266+ The port component is not included if it is the default for an267+ "http://" or "https://" URL.268+269+ This property may be used for generating the value of a request270+ "Host" header.271+272+ See: https://tools.ietf.org/html/rfc3986#section-3.2.3273 """274 host = self._uri_reference.host or ""275 port = self._uri_reference.port276 netloc = host.encode("ascii")277 if port:270- netloc = netloc + b":" + str(port).encode("ascii")278+ netloc = netloc + b":" + port.encode("ascii")279 return netloc280 281 @property282 def path(self) -> str:55 unmodified lines338 """339 The URL fragments, as used in HTML anchors.340 As a string, without the leading '#'.341 """334- return self._uri_reference.fragment or ""342+ return unquote(self._uri_reference.fragment or "")343 344 @property345 def raw(self) -> RawURL:346 """347 The URL in the raw representation used by the low level340- transport API. For example, see `httpcore`.348+ transport API. See `BaseTransport.handle_request`.349 350 Provides the (scheme, host, port, target) for the outgoing request.351 """352 return (47 unmodified lines400 "raw_path": bytes,401 "fragment": str,402 "params": object,403 }404+405+ # Step 1406+ # ======407+ #408+ # Perform type checking for all supported keyword arguments.409 for key, value in kwargs.items():410 if key not in allowed:411 message = f"{key!r} is an invalid keyword argument for copy_with()"412 raise TypeError(message)2 unmodified lines415 seen = type(value).__name__416 message = f"Argument {key!r} must be {expected} but got {seen}"417 raise TypeError(message)418 406- # Replace username, password, userinfo, host, port, netloc with "authority" for rfc3986419+ # Step 2420+ # ======421+ #422+ # Consolidate "username", "password", "userinfo", "host", "port" and "netloc"423+ # into a single "authority" keyword, for `rfc3986`.424 if "username" in kwargs or "password" in kwargs:408- # Consolidate username and password into userinfo.425+ # Consolidate "username" and "password" into "userinfo".426 username = quote(kwargs.pop("username", self.username) or "")427 password = quote(kwargs.pop("password", self.password) or "")428 userinfo = f"{username}:{password}" if password else username429 kwargs["userinfo"] = userinfo.encode("ascii")430 431 if "host" in kwargs or "port" in kwargs:415- # Consolidate host and port into netloc.432+ # Consolidate "host" and "port" into "netloc".433 host = kwargs.pop("host", self.host) or ""434 port = kwargs.pop("port", self.port)435 436 if host and ":" in host and host[0] != "[":420- # it's an IPv6 address, so it should be hidden under bracket437+ # IPv6 addresses need to be escaped within sqaure brackets.438 host = f"[{host}]"439 440 kwargs["netloc"] = (441 f"{host}:{port}".encode("ascii")1 unmodified line443 else host.encode("ascii")444 )445 446 if "userinfo" in kwargs or "netloc" in kwargs:430- # Consolidate userinfo and netloc into authority.447+ # Consolidate "userinfo" and "netloc" into authority.448 userinfo = (kwargs.pop("userinfo", self.userinfo) or b"").decode("ascii")449 netloc = (kwargs.pop("netloc", self.netloc) or b"").decode("ascii")450 authority = f"{userinfo}@{netloc}" if userinfo else netloc451 kwargs["authority"] = authority452 453+ # Step 3454+ # ======455+ #456+ # Wrangle any "path", "query", "raw_path" and "params" keywords into457+ # "query" and "path" keywords for `rfc3986`.458 if "raw_path" in kwargs:459+ # If "raw_path" is included, then split it into "path" and "query" components.460 raw_path = kwargs.pop("raw_path") or b""461 path, has_query, query = raw_path.decode("ascii").partition("?")462 kwargs["path"] = path463 kwargs["query"] = query if has_query else None464 465 else:443- # Ensure path=<url quoted str> for rfc3986466 if kwargs.get("path") is not None:467+ # Ensure `kwargs["path"] = <url quoted str>` for `rfc3986`.468 kwargs["path"] = quote(kwargs["path"])469 470 if kwargs.get("query") is not None:448- # Ensure query=<str> for rfc3986471+ # Ensure `kwargs["query"] = <str>` for `rfc3986`.472+ #473+ # Note that `.copy_with(query=None)` and `.copy_with(query=b"")`474+ # are subtly different. The `None` style will not include an empty475+ # trailing "?" character.476 kwargs["query"] = kwargs["query"].decode("ascii")477 478 if "params" in kwargs:479+ # Replace any "params" keyword with the raw "query" instead.480+ #481+ # Ensure that empty params use `kwargs["query"] = None` rather482+ # than `kwargs["query"] = ""`, so that generated URLs do not483+ # include an empty trailing "?".484 params = kwargs.pop("params")485 kwargs["query"] = None if not params else str(QueryParams(params))486 487+ # Step 4488+ # ======489+ #490+ # Ensure any fragment component is quoted.491+ if kwargs.get("fragment") is not None:492+ kwargs["fragment"] = quote(kwargs["fragment"])493+494+ # Step 5495+ # ======496+ #497+ # At this point kwargs may include keys for "scheme", "authority", "path",498+ # "query" and "fragment". Together these constitute the entire URL.499+ #500+ # See https://tools.ietf.org/html/rfc3986#section-3501+ #502+ # foo://example.com:8042/over/there?name=ferret#nose503+ # \_/ \______________/\_________/ \_________/ \__/504+ # | | | | |505+ # scheme authority path query fragment506 return URL(self._uri_reference.copy_with(**kwargs).unsplit())507 508 def copy_set_param(self, key: str, value: typing.Any = None) -> "URL":509 return self.copy_with(params=self.params.set(key, value))45 unmodified lines555 def __repr__(self) -> str:556 class_name = self.__class__.__name__557 url_str = str(self)558 if self._uri_reference.userinfo:559+ # Mask any password component in the URL representation, to lower the560+ # risk of unintended leakage, such as in debug information and logging.561 username = quote(self.username)562 url_str = (563 rfc3986.urlparse(url_str)564 .copy_with(userinfo=f"{username}:[secure]")1255 unmodified lines