class Proxy
Wraps a client, address and headers required to initiate a connectio to a remote host using the CONNECT verb. Behaves like a TCP endpoint for the purposes of connecting to a remote host.
Nested
Definitions
def self.tcp(client, host, port, headers = nil)
Prepare and endpoint which can establish a TCP connection to the remote system.
Implementation
def self.tcp(client, host, port, headers = nil)
self.new(client, "#{host}:#{port}", headers)
end
def self.endpoint(client, endpoint, headers = nil)
Construct a endpoint that will use the given client as a proxy for HTTP requests.
Implementation
def self.endpoint(client, endpoint, headers = nil)
proxy = self.new(client, endpoint.authority(false), headers)
return proxy.endpoint(endpoint.url)
end
def initialize(client, address, headers = nil)
Implementation
def initialize(client, address, headers = nil)
@client = client
@address = address
@headers = ::Protocol::HTTP::Headers[headers].freeze
end
def close
Close the underlying client connection.
Implementation
def close
@client.close
end
def connect(&block)
Establish a TCP connection to the specified host.
Implementation
def connect(&block)
input = Body::Writable.new
response = @client.connect(@address.to_s, @headers, input)
if response.success?
pipe = Body::Pipe.new(response.body, input)
return pipe.to_io unless block_given?
begin
yield pipe.to_io
ensure
pipe.close
end
else
# This ensures we don't leave a response dangling:
input.close
response.close
raise ConnectFailure, response
end
end
def wrap_endpoint(endpoint)
Implementation
def wrap_endpoint(endpoint)
Endpoint.new(endpoint.url, self, **endpoint.options)
end