lib/httpx/plugins/proxy.rb in httpx-0.24.0 vs lib/httpx/plugins/proxy.rb in httpx-0.24.1
- old
+ new
@@ -23,10 +23,14 @@
klass.plugin(:"proxy/http")
klass.plugin(:"proxy/socks4")
klass.plugin(:"proxy/socks5")
end
+ def extra_options(options)
+ options.merge(supported_proxy_protocols: [])
+ end
+
if URI::Generic.methods.include?(:use_proxy?)
def use_proxy?(*args)
URI::Generic.use_proxy?(*args)
end
else
@@ -116,10 +120,16 @@
module OptionsMethods
def option_proxy(value)
value.is_a?(Parameters) ? value : Hash[value]
end
+
+ def option_supported_proxy_protocols(value)
+ raise TypeError, ":supported_proxy_protocols must be an Array" unless value.is_a?(Array)
+
+ value.map(&:to_s)
+ end
end
module InstanceMethods
private
@@ -140,12 +150,16 @@
@_proxy_uris ||= Array(proxy[:uri])
next_proxy = @_proxy_uris.first
raise Error, "Failed to connect to proxy" unless next_proxy
+ next_proxy = URI(next_proxy)
+
+ raise Error,
+ "#{next_proxy.scheme}: unsupported proxy protocol" unless options.supported_proxy_protocols.include?(next_proxy.scheme)
+
if proxy.key?(:no_proxy)
- next_proxy = URI(next_proxy)
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 Proxy.use_proxy?(uri.host, next_proxy.host,
@@ -251,14 +265,15 @@
@origin == connection.origin
end
end
def send(request)
- return super unless @options.proxy
- return super unless connecting?
+ return super unless (
+ @options.proxy && @state != :idle && connecting?
+ )
- @pending << request
+ (@proxy_pending ||= []) << request
end
def connecting?
return super unless @options.proxy
@@ -292,9 +307,15 @@
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)