Skip to content
Myr

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

4 files+18 added−19 removed

modified:httpx/client.py+10 added-2 removed
1import inspect2import typing3from types import TracebackType45import hstspreload67from .auth import HTTPBasicAuth8from .concurrency import AsyncioBackend9from .config import (10 DEFAULT_MAX_REDIRECTS,
95 unmodified lines
106 self.dispatch = async_dispatch107 self.concurrency_backend = backend108 self.trust_env = trust_env109110 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 url115116 def merge_cookies(117 self, cookies: CookieTypes = None118 ) -> typing.Optional[CookieTypes]:119 if cookies or self.cookies:
451 unmodified lines
571 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 lines
655 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 lines
modified:httpx/models.py-9 removed
6 unmodified lines
7from http.cookiejar import Cookie, CookieJar8from urllib.parse import parse_qsl, urlencode910import chardet11import hstspreload11import rfc39861213from .config import USER_AGENT14from .decoders import (
92 unmodified lines
107 raise InvalidURL("No scheme included in URL.")108 if not self.host:109 raise InvalidURL("No host included in URL.")110112 # 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")119111 @property112 def scheme(self) -> str:113 return self._uri_reference.scheme or ""114
1056 unmodified lines
modified:tests/client/test_client.py+8 added
148 unmodified lines
149 with httpx.Client(base_url=base_url) as http:150 response = http.get("/")151 assert response.status_code == 200152 assert str(response.url) == base_url153154155def test_merge_url():156 client = httpx.Client(base_url="https://www.paypal.com/")157 url = client.merge_url("http://www.paypal.com")158159 assert url.scheme == "https"160 assert url.is_ssl
modified:tests/models/test_url.py-8 removed
174 unmodified lines
175176 url_set = set(urls)177178 assert all(url in urls for url in url_set)179180181def test_hsts_preload_converted_to_https():182 url = URL("http://www.paypal.com")183184 assert url.is_ssl185 assert url.scheme == "https"186 assert url == "https://www.paypal.com"