Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session: Cache http requests of trusted hosts #9498

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/9498.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If a host is explicitly specified as trusted by the user (via the --trusted-host option), cache HTTP responses from it in addition to HTTPS ones.
7 changes: 7 additions & 0 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,15 @@ def add_trusted_host(
if host_port not in self.pip_trusted_origins:
self.pip_trusted_origins.append(host_port)

self.mount(
build_url_from_netloc(host, scheme="http") + "/", self._trusted_host_adapter
)
self.mount(build_url_from_netloc(host) + "/", self._trusted_host_adapter)
if not host_port[1]:
self.mount(
build_url_from_netloc(host, scheme="http") + ":",
self._trusted_host_adapter,
)
# Mount wildcard ports for the same host.
self.mount(build_url_from_netloc(host) + ":", self._trusted_host_adapter)

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_network_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_trusted_hosts_adapter(self, tmpdir):
# Check that the "port wildcard" is present.
assert "https://example.com:" in session.adapters
# Check that the cache is enabled.
assert hasattr(session.adapters["http://example.com/"], "cache")
assert hasattr(session.adapters["https://example.com/"], "cache")

def test_add_trusted_host(self):
Expand All @@ -93,12 +94,20 @@ def test_add_trusted_host(self):
prefix3 = "https://host3/"
prefix3_wildcard = "https://host3:"

prefix2_http = "http://host2/"
prefix3_http = "http://host3/"
prefix3_wildcard_http = "http://host3:"

# Confirm some initial conditions as a baseline.
assert session.pip_trusted_origins == [("host1", None), ("host3", None)]
assert session.adapters[prefix3] is trusted_host_adapter
assert session.adapters[prefix3_wildcard] is trusted_host_adapter

assert session.adapters[prefix3_http] is trusted_host_adapter
assert session.adapters[prefix3_wildcard_http] is trusted_host_adapter

assert prefix2 not in session.adapters
assert prefix2_http not in session.adapters

# Test adding a new host.
session.add_trusted_host("host2")
Expand All @@ -110,6 +119,7 @@ def test_add_trusted_host(self):
# Check that prefix3 is still present.
assert session.adapters[prefix3] is trusted_host_adapter
assert session.adapters[prefix2] is trusted_host_adapter
assert session.adapters[prefix2_http] is trusted_host_adapter

# Test that adding the same host doesn't create a duplicate.
session.add_trusted_host("host3")
Expand All @@ -121,13 +131,15 @@ def test_add_trusted_host(self):

session.add_trusted_host("host4:8080")
prefix4 = "https://host4:8080/"
prefix4_http = "http://host4:8080/"
assert session.pip_trusted_origins == [
("host1", None),
("host3", None),
("host2", None),
("host4", 8080),
]
assert session.adapters[prefix4] is trusted_host_adapter
assert session.adapters[prefix4_http] is trusted_host_adapter

def test_add_trusted_host__logging(self, caplog):
"""
Expand Down