fix: do not generate new connection on 407 check for proxies

instead, look for the correct conn in-session. this does not leak connections with usage
This commit is contained in:
HoneyryderChuck 2024-07-22 14:50:13 +01:00
parent 1ee39870da
commit 002459b9b6
3 changed files with 29 additions and 15 deletions

View File

@ -107,16 +107,29 @@ module HTTPX
def find_connection(request, connections, options)
return super unless options.respond_to?(:proxy)
uri = URI(request.uri)
uri = request.uri
proxy_opts = if (next_proxy = uri.find_proxy)
proxy_options = proxy_options(uri, options)
return super(request, connections, proxy_options) unless proxy_options.proxy
connection = pool.find_connection(uri, proxy_options) || init_connection(uri, proxy_options)
unless connections.nil? || connections.include?(connection)
connections << connection
set_connection_callbacks(connection, connections, options)
end
connection
end
def proxy_options(request_uri, options)
proxy_opts = if (next_proxy = request_uri.find_proxy)
{ uri: next_proxy }
else
proxy = options.proxy
return super unless proxy
return options unless proxy
return super(request, connections, options.merge(proxy: nil)) unless proxy.key?(:uri)
return options.merge(proxy: nil) unless proxy.key?(:uri)
@_proxy_uris ||= Array(proxy[:uri])
@ -133,8 +146,8 @@ module HTTPX
no_proxy = proxy[:no_proxy]
no_proxy = no_proxy.join(",") if no_proxy.is_a?(Array)
return super(request, connections, options.merge(proxy: nil)) unless URI::Generic.use_proxy?(uri.host, next_proxy.host,
next_proxy.port, no_proxy)
return options.merge(proxy: nil) unless URI::Generic.use_proxy?(request_uri.host, next_proxy.host,
next_proxy.port, no_proxy)
end
proxy.merge(uri: next_proxy)
@ -142,13 +155,7 @@ module HTTPX
proxy = Parameters.new(**proxy_opts)
proxy_options = options.merge(proxy: proxy)
connection = pool.find_connection(uri, proxy_options) || init_connection(uri, proxy_options)
unless connections.nil? || connections.include?(connection)
connections << connection
set_connection_callbacks(connection, connections, options)
end
connection
options.merge(proxy: proxy)
end
def fetch_response(request, connections, options)

View File

@ -32,9 +32,14 @@ module HTTPX
!request.headers.key?("proxy-authorization") &&
response.headers.key?("proxy-authenticate")
connection = find_connection(request, connections, options)
uri = request.uri
if connection.options.proxy.can_authenticate?(response.headers["proxy-authenticate"])
proxy_options = proxy_options(uri, options)
connection = connections.find do |conn|
conn.match?(uri, proxy_options)
end
if connection && connection.options.proxy.can_authenticate?(response.headers["proxy-authenticate"])
request.transition(:idle)
request.headers["proxy-authorization"] =
connection.options.proxy.authenticate(request, response.headers["proxy-authenticate"])

View File

@ -43,6 +43,8 @@ module HTTPX
private
def proxy_error?: (Request request, response) -> bool
def proxy_options: (http_uri request_uri, Options & _ProxyOptions options) -> (Options & _ProxyOptions)
end
module ConnectionMethods