Skip to content

Commit 58b1b73

Browse files
authored
Merge pull request #9498 from NoahGorny/cache-http-requests
session: Cache http requests of trusted hosts
2 parents f6665f3 + b0bb352 commit 58b1b73

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

news/9498.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

src/pip/_internal/network/session.py

+7
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,15 @@ def add_trusted_host(
358358
if host_port not in self.pip_trusted_origins:
359359
self.pip_trusted_origins.append(host_port)
360360

361+
self.mount(
362+
build_url_from_netloc(host, scheme="http") + "/", self._trusted_host_adapter
363+
)
361364
self.mount(build_url_from_netloc(host) + "/", self._trusted_host_adapter)
362365
if not host_port[1]:
366+
self.mount(
367+
build_url_from_netloc(host, scheme="http") + ":",
368+
self._trusted_host_adapter,
369+
)
363370
# Mount wildcard ports for the same host.
364371
self.mount(build_url_from_netloc(host) + ":", self._trusted_host_adapter)
365372

tests/unit/test_network_session.py

+12
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def test_trusted_hosts_adapter(self, tmpdir):
8282
# Check that the "port wildcard" is present.
8383
assert "https://example.com:" in session.adapters
8484
# Check that the cache is enabled.
85+
assert hasattr(session.adapters["http://example.com/"], "cache")
8586
assert hasattr(session.adapters["https://example.com/"], "cache")
8687

8788
def test_add_trusted_host(self):
@@ -93,12 +94,20 @@ def test_add_trusted_host(self):
9394
prefix3 = "https://host3/"
9495
prefix3_wildcard = "https://host3:"
9596

97+
prefix2_http = "http://host2/"
98+
prefix3_http = "http://host3/"
99+
prefix3_wildcard_http = "http://host3:"
100+
96101
# Confirm some initial conditions as a baseline.
97102
assert session.pip_trusted_origins == [("host1", None), ("host3", None)]
98103
assert session.adapters[prefix3] is trusted_host_adapter
99104
assert session.adapters[prefix3_wildcard] is trusted_host_adapter
100105

106+
assert session.adapters[prefix3_http] is trusted_host_adapter
107+
assert session.adapters[prefix3_wildcard_http] is trusted_host_adapter
108+
101109
assert prefix2 not in session.adapters
110+
assert prefix2_http not in session.adapters
102111

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

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

122132
session.add_trusted_host("host4:8080")
123133
prefix4 = "https://host4:8080/"
134+
prefix4_http = "http://host4:8080/"
124135
assert session.pip_trusted_origins == [
125136
("host1", None),
126137
("host3", None),
127138
("host2", None),
128139
("host4", 8080),
129140
]
130141
assert session.adapters[prefix4] is trusted_host_adapter
142+
assert session.adapters[prefix4_http] is trusted_host_adapter
131143

132144
def test_add_trusted_host__logging(self, caplog):
133145
"""

0 commit comments

Comments
 (0)