lib/httpx/plugins/proxy.rb in httpx-0.1.0 vs lib/httpx/plugins/proxy.rb in httpx-0.2.0
- old
+ new
@@ -1,7 +1,9 @@
# frozen_string_literal: true
+require "resolv"
+require "ipaddr"
require "forwardable"
module HTTPX
module Plugins
module Proxy
@@ -47,25 +49,26 @@
def find_channel(request, **options)
uri = URI(request.uri)
proxy = proxy_params(uri)
return super unless proxy
- @connection.find_channel(proxy) || begin
- channel = build_proxy_channel(proxy, **options)
- set_channel_callbacks(channel)
- channel
- end
+ @connection.find_channel(proxy) || build_channel(proxy, options)
end
+ def build_channel(proxy, options)
+ channel = build_proxy_channel(proxy, **options)
+ set_channel_callbacks(channel, options)
+ channel
+ end
+
def build_proxy_channel(proxy, **options)
parameters = Parameters.new(**proxy)
uri = parameters.uri
log { "proxy: #{uri}" }
- io = TCP.new(uri, @options)
proxy_type = Parameters.registry(parameters.uri.scheme)
- channel = proxy_type.new(io, parameters, @options.merge(options), &method(:on_response))
- @connection.__send__(:register_channel, channel)
+ channel = proxy_type.new("tcp", uri, parameters, @options.merge(options), &method(:on_response))
+ @connection.__send__(:resolve_channel, channel)
channel
end
def fetch_response(request)
response = super
@@ -100,12 +103,12 @@
end
register_plugin :proxy, Proxy
end
class ProxyChannel < Channel
- def initialize(io, parameters, options, &blk)
- super(io, options, &blk)
+ def initialize(type, uri, parameters, options, &blk)
+ super(type, uri, options, &blk)
@parameters = parameters
end
def match?(*)
true
@@ -142,10 +145,10 @@
end
class ProxySSL < SSL
def initialize(tcp, request_uri, options)
@io = tcp.to_io
- super(tcp, options)
+ super(request_uri, tcp.addresses, options)
@hostname = request_uri.host
@state = :connected
end
end
end