lib/httpx/plugins/proxy.rb in httpx-1.1.5 vs lib/httpx/plugins/proxy.rb in httpx-1.2.0
- old
+ new
@@ -1,9 +1,9 @@
# frozen_string_literal: true
module HTTPX
- class HTTPProxyError < Error; end
+ class HTTPProxyError < ConnectionError; end
module Plugins
#
# This plugin adds support for proxies. It ships with support for:
#
@@ -141,46 +141,36 @@
end
proxy = Parameters.new(**proxy_opts)
proxy_options = options.merge(proxy: proxy)
- connection = pool.find_connection(uri, proxy_options) || build_connection(uri, proxy_options)
+ 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 build_connection(uri, options)
- proxy = options.proxy
- return super unless proxy
-
- init_connection("tcp", uri, options)
- end
-
def fetch_response(request, connections, options)
response = super
- if response.is_a?(ErrorResponse) &&
- __proxy_error?(response) && !@_proxy_uris.empty?
+ if response.is_a?(ErrorResponse) && proxy_error?(request, response)
@_proxy_uris.shift
+
+ # return last error response if no more proxies to try
+ return response if @_proxy_uris.empty?
+
log { "failed connecting to proxy, trying next..." }
request.transition(:idle)
send_request(request, connections, options)
return
end
response
end
- def build_altsvc_connection(_, _, _, _, _, options)
- return if options.proxy
-
- super
- end
-
- def __proxy_error?(response)
+ def proxy_error?(_request, response)
error = response.error
case error
when NativeResolveError
return false unless @_proxy_uris && !@_proxy_uris.empty?
@@ -233,18 +223,10 @@
else
@origin == connection.origin
end
end
- def send(request)
- return super unless (
- @options.proxy && @state != :idle && connecting?
- )
-
- (@proxy_pending ||= []) << request
- end
-
def connecting?
return super unless @options.proxy
super || @state == :connecting || @state == :connected
end
@@ -269,22 +251,22 @@
emit(:close)
end
private
+ def initialize_type(uri, options)
+ return super unless options.proxy
+
+ "tcp"
+ end
+
def connect
return super unless @options.proxy
case @state
when :idle
transition(:connecting)
when :connected
- if @proxy_pending
- while (req = @proxy_pendind.shift)
- send(req)
- end
- end
-
transition(:open)
end
end
def handle_transition(nextstate)