Skip to content
Myr

Handle URL quoting username and password components. (#1159)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Handle URL quoting username and password components * Tweak userinfo quoting

authored by Tom Christie · committed by GitHub · parent 557ad70 · Browse files · 2 files+18 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−6 removed

  • tests:+12 added−0 removed
  • other:+6 added−6 removed

2 files+18 added−6 removed

modified:httpx/_models.py+6 added-6 removed
5 unmodified lines
6import urllib.request7import warnings8from collections.abc import MutableMapping9from http.cookiejar import Cookie, CookieJar10from urllib.parse import parse_qsl, urlencode10from urllib.parse import parse_qsl, quote, unquote, urlencode1112import chardet13import rfc398614import rfc3986.exceptions
84 unmodified lines
99100 @property101 def username(self) -> str:102 userinfo = self._uri_reference.userinfo or ""103 return userinfo.partition(":")[0]103 return unquote(userinfo.partition(":")[0])104105 @property106 def password(self) -> str:107 userinfo = self._uri_reference.userinfo or ""108 return userinfo.partition(":")[2]108 return unquote(userinfo.partition(":")[2])109110 @property111 def host(self) -> str:112 return self._uri_reference.host or ""
61 unmodified lines
174 or "port" in kwargs175 ):176 host = kwargs.pop("host", self.host)177 port = kwargs.pop("port", self.port)178 username = kwargs.pop("username", self.username)179 password = kwargs.pop("password", self.password)178 username = quote(kwargs.pop("username", self.username) or "")179 password = quote(kwargs.pop("password", self.password) or "")180181 authority = host182 if port is not None:183 authority += f":{port}"
8 unmodified lines
192 return URL(self._uri_reference.copy_with(**kwargs).unsplit())193194 def join(self, url: URLTypes) -> "URL":195 """196 Return an absolute URL, using given this URL as the base.196 Return an absolute URL, using this URL as the base.197 """198 if self.is_relative_url:199 return URL(url)200
1013 unmodified lines
modified:tests/models/test_url.py+12 added
185 unmodified lines
186 assert getattr(new, k) == v187 assert str(new) == "https://username:password@example.net:444"188189190def test_url_copywith_for_userinfo():191 copy_with_kwargs = {192 "username": "tom@example.org",193 "password": "abc123@ %",194 }195 url = URL("https://example.org")196 new = url.copy_with(**copy_with_kwargs)197 assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"198 assert new.username == "tom@example.org"199 assert new.password == "abc123@ %"200201202def test_url_invalid():203 with pytest.raises(InvalidURL):204 URL("https://😇/")