Skip to content

Commit 42b6e45

Browse files
author
Yeray Diaz Diaz
authored
Fix forward proxy adding connection to pool (#70)
* Use _add_to_pool in forward proxy request * Update changelog
1 parent be8735b commit 42b6e45

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

7+
## Unreleased
8+
9+
- Fix connections using proxy forwarding requests not being added to the
10+
connection pool properly.
11+
712
## 0.8.1 (April 30th, 2020)
813

914
### Changed

httpcore/_async/http_proxy.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ async def _forward_request(
9696
connection = AsyncHTTPConnection(
9797
origin=origin, http2=False, ssl_context=self._ssl_context,
9898
)
99-
async with self._thread_lock:
100-
self._connections.setdefault(origin, set())
101-
self._connections[origin].add(connection)
99+
await self._add_to_pool(connection)
102100

103101
# Issue a forwarded proxy request...
104102

httpcore/_sync/http_proxy.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ def _forward_request(
9696
connection = SyncHTTPConnection(
9797
origin=origin, http2=False, ssl_context=self._ssl_context,
9898
)
99-
with self._thread_lock:
100-
self._connections.setdefault(origin, set())
101-
self._connections[origin].add(connection)
99+
self._add_to_pool(connection)
102100

103101
# Issue a forwarded proxy request...
104102

tests/async_tests/test_interfaces.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,17 @@ async def test_http_proxy(
186186
method = b"GET"
187187
url = (b"http", b"example.org", 80, b"/")
188188
headers = [(b"host", b"example.org")]
189+
max_connections = 1
190+
max_keepalive = 2
189191
# Tunnel requires the host header to be present,
190192
# Forwarding will use the request headers
191193
proxy_headers = headers if proxy_mode == "TUNNEL_ONLY" else None
192194
async with httpcore.AsyncHTTPProxy(
193-
proxy_server, proxy_headers=proxy_headers, proxy_mode=proxy_mode
195+
proxy_server,
196+
proxy_headers=proxy_headers,
197+
proxy_mode=proxy_mode,
198+
max_connections=max_connections,
199+
max_keepalive=max_keepalive,
194200
) as http:
195201
http_version, status_code, reason, headers, stream = await http.request(
196202
method, url, headers
@@ -213,11 +219,15 @@ async def test_proxy_https_requests(
213219
method = b"GET"
214220
url = (b"https", b"example.org", 443, b"/")
215221
headers = proxy_headers = [(b"host", b"example.org")]
222+
max_connections = 1
223+
max_keepalive = 2
216224
async with httpcore.AsyncHTTPProxy(
217225
proxy_server,
218226
proxy_headers=proxy_headers,
219227
proxy_mode=proxy_mode,
220228
ssl_context=ca_ssl_context,
229+
max_connections=max_connections,
230+
max_keepalive=max_keepalive,
221231
) as http:
222232
http_version, status_code, reason, headers, stream = await http.request(
223233
method, url, headers

tests/sync_tests/test_interfaces.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,17 @@ def test_http_proxy(
186186
method = b"GET"
187187
url = (b"http", b"example.org", 80, b"/")
188188
headers = [(b"host", b"example.org")]
189+
max_connections = 1
190+
max_keepalive = 2
189191
# Tunnel requires the host header to be present,
190192
# Forwarding will use the request headers
191193
proxy_headers = headers if proxy_mode == "TUNNEL_ONLY" else None
192194
with httpcore.SyncHTTPProxy(
193-
proxy_server, proxy_headers=proxy_headers, proxy_mode=proxy_mode
195+
proxy_server,
196+
proxy_headers=proxy_headers,
197+
proxy_mode=proxy_mode,
198+
max_connections=max_connections,
199+
max_keepalive=max_keepalive,
194200
) as http:
195201
http_version, status_code, reason, headers, stream = http.request(
196202
method, url, headers
@@ -213,11 +219,15 @@ def test_proxy_https_requests(
213219
method = b"GET"
214220
url = (b"https", b"example.org", 443, b"/")
215221
headers = proxy_headers = [(b"host", b"example.org")]
222+
max_connections = 1
223+
max_keepalive = 2
216224
with httpcore.SyncHTTPProxy(
217225
proxy_server,
218226
proxy_headers=proxy_headers,
219227
proxy_mode=proxy_mode,
220228
ssl_context=ca_ssl_context,
229+
max_connections=max_connections,
230+
max_keepalive=max_keepalive,
221231
) as http:
222232
http_version, status_code, reason, headers, stream = http.request(
223233
method, url, headers

0 commit comments

Comments
 (0)