Class: Async::HTTP::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/proxy.rb

Overview

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.

Defined Under Namespace

Modules: Client

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, address, headers = nil) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • client (Async::HTTP::Client)

    the client which will be used as a proxy server.

  • address (String)

    the address to connect to.

  • headers (Array) (defaults to: nil)

    an optional list of headers to use when establishing the connection.



75
76
77
78
79
# File 'lib/async/http/proxy.rb', line 75

def initialize(client, address, headers = nil)
	@client = client
	@address = address
	@headers = ::Protocol::HTTP::Headers[headers].freeze
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



81
82
83
# File 'lib/async/http/proxy.rb', line 81

def client
  @client
end

Class Method Details

.endpoint(client, endpoint, headers = nil) ⇒ Object

Construct a endpoint that will use the given client as a proxy for HTTP requests.

Parameters:

  • client (Async::HTTP::Client)

    the client which will be used as a proxy server.

  • endpoint (Async::HTTP::Endpoint)

    the endpoint to connect to.

  • headers (Array) (defaults to: nil)

    an optional list of headers to use when establishing the connection.



66
67
68
69
70
# File 'lib/async/http/proxy.rb', line 66

def self.endpoint(client, endpoint, headers = nil)
	proxy = self.new(client, endpoint.authority(false), headers)
	
	return proxy.endpoint(endpoint.url)
end

.tcp(client, host, port, headers = nil) ⇒ Object

Prepare and endpoint which can establish a TCP connection to the remote system.

Parameters:

  • client (Async::HTTP::Client)

    the client which will be used as a proxy server.

  • host (String)

    the hostname or address to connect to.

  • port (String)

    the port number to connect to.

  • headers (Array) (defaults to: nil)

    an optional list of headers to use when establishing the connection.

See Also:

  • IO::Endpoint#tcp


58
59
60
# File 'lib/async/http/proxy.rb', line 58

def self.tcp(client, host, port, headers = nil)
	self.new(client, "#{host}:#{port}", headers)
end

Instance Method Details

#closeObject

Close the underlying client connection.



84
85
86
# File 'lib/async/http/proxy.rb', line 84

def close
	@client.close
end

#connect(&block) ⇒ Socket

Establish a TCP connection to the specified host.

Returns:

  • (Socket)

    a connected bi-directional socket.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/async/http/proxy.rb', line 90

def connect(&block)
	input = Body::Writable.new
	
	response = @client.connect(@address.to_s, @headers, input)
	
	pipe = Body::Pipe.new(response.body, input)
	
	return pipe.to_io unless block_given?
	
	begin
		yield pipe.to_io
	ensure
		pipe.close
	end
end

#wrap_endpoint(endpoint) ⇒ Async::HTTP::Endpoint

Returns an endpoint that connects via the specified proxy.

Returns:



107
108
109
# File 'lib/async/http/proxy.rb', line 107

def wrap_endpoint(endpoint)
	Endpoint.new(endpoint.url, self, **endpoint.options)
end