Move HSTS preload checking to client (#184)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Seth Michael Larson · committed by Tom Christie · parent 919e8d3 · Browse files · 4 files·+18 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+18 added−19 removed
- tests:+8 added−8 removed
- other:+10 added−11 removed
Provenance
- Commit
9142a89- Tree
06732cb- Author
Seth Michael Larson <sethmichaellarson@gmail.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
919e8d3
4 files changed+18 added−19 removed
modified:httpx/client.py+10 added-2 removed
1 import inspect2 import typing3 from types import TracebackType4 5+import hstspreload6+7 from .auth import HTTPBasicAuth8 from .concurrency import AsyncioBackend9 from .config import (10 DEFAULT_MAX_REDIRECTS,95 unmodified lines106 self.dispatch = async_dispatch107 self.concurrency_backend = backend108 self.trust_env = trust_env109 110+ def merge_url(self, url: URLTypes) -> URL:111+ url = self.base_url.join(relative_url=url)112+ if url.scheme == "http" and hstspreload.in_hsts_preload(url.host):113+ url = url.copy_with(scheme="https")114+ return url115+116 def merge_cookies(117 self, cookies: CookieTypes = None118 ) -> typing.Optional[CookieTypes]:119 if cookies or self.cookies:451 unmodified lines571 verify: VerifyTypes = None,572 timeout: TimeoutTypes = None,573 trust_env: bool = True,574 ) -> AsyncResponse:567- url = self.base_url.join(url)575+ url = self.merge_url(url)576 headers = self.merge_headers(headers)577 cookies = self.merge_cookies(cookies)578 request = AsyncRequest(579 method,75 unmodified lines655 verify: VerifyTypes = None,656 timeout: TimeoutTypes = None,657 trust_env: bool = True,658 ) -> Response:651- url = self.base_url.join(url)659+ url = self.merge_url(url)660 headers = self.merge_headers(headers)661 cookies = self.merge_cookies(cookies)662 request = AsyncRequest(663 method,297 unmodified linesmodified:httpx/models.py-9 removed
6 unmodified lines7 from http.cookiejar import Cookie, CookieJar8 from urllib.parse import parse_qsl, urlencode9 10 import chardet11-import hstspreload11 import rfc398612 13 from .config import USER_AGENT14 from .decoders import (92 unmodified lines107 raise InvalidURL("No scheme included in URL.")108 if not self.host:109 raise InvalidURL("No host included in URL.")110 112- # If the URL is HTTP but the host is on the HSTS preload list switch to HTTPS.113- if (114- self.scheme == "http"115- and self.host116- and hstspreload.in_hsts_preload(self.host)117- ):118- self._uri_reference = self._uri_reference.copy_with(scheme="https")119-111 @property112 def scheme(self) -> str:113 return self._uri_reference.scheme or ""114 1056 unmodified linesmodified:tests/client/test_client.py+8 added
148 unmodified lines149 with httpx.Client(base_url=base_url) as http:150 response = http.get("/")151 assert response.status_code == 200152 assert str(response.url) == base_url153+154+155+def test_merge_url():156+ client = httpx.Client(base_url="https://www.paypal.com/")157+ url = client.merge_url("http://www.paypal.com")158+159+ assert url.scheme == "https"160+ assert url.is_sslmodified:tests/models/test_url.py-8 removed
174 unmodified lines175 176 url_set = set(urls)177 178 assert all(url in urls for url in url_set)179-180-181-def test_hsts_preload_converted_to_https():182- url = URL("http://www.paypal.com")183-184- assert url.is_ssl185- assert url.scheme == "https"186- assert url == "https://www.paypal.com"