Class: Async::HTTP::Proxy
- Inherits:
-
Object
- Object
- Async::HTTP::Proxy
- 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 Classes: ConnectFailure
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
-
.endpoint(client, endpoint, headers = nil) ⇒ Object
Construct a endpoint that will use the given client as a proxy for HTTP requests.
-
.tcp(client, host, port, headers = nil) ⇒ Object
Prepare and endpoint which can establish a TCP connection to the remote system.
Instance Method Summary collapse
-
#close ⇒ Object
Close the underlying client connection.
-
#connect(&block) ⇒ Socket
Establish a TCP connection to the specified host.
-
#initialize(client, address, headers = nil) ⇒ Proxy
constructor
A new instance of Proxy.
-
#wrap_endpoint(endpoint) ⇒ Async::HTTP::Endpoint
An endpoint that connects via the specified proxy.
Constructor Details
#initialize(client, address, headers = nil) ⇒ Proxy
Returns a new instance of Proxy.
79 80 81 82 83 |
# File 'lib/async/http/proxy.rb', line 79 def initialize(client, address, headers = nil) @client = client @address = address @headers = ::Protocol::HTTP::Headers[headers].freeze end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
85 86 87 |
# File 'lib/async/http/proxy.rb', line 85 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.
70 71 72 73 74 |
# File 'lib/async/http/proxy.rb', line 70 def self.endpoint(client, endpoint, headers = nil) proxy = self.new(client, endpoint.(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.
62 63 64 |
# File 'lib/async/http/proxy.rb', line 62 def self.tcp(client, host, port, headers = nil) self.new(client, "#{host}:#{port}", headers) end |
Instance Method Details
#close ⇒ Object
Close the underlying client connection.
88 89 90 |
# File 'lib/async/http/proxy.rb', line 88 def close @client.close end |
#connect(&block) ⇒ Socket
Establish a TCP connection to the specified host.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/async/http/proxy.rb', line 94 def connect(&block) input = Body::Writable.new response = @client.connect(authority: @address, headers: @headers, body: 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 |
#wrap_endpoint(endpoint) ⇒ Async::HTTP::Endpoint
Returns an endpoint that connects via the specified proxy.
119 120 121 |
# File 'lib/async/http/proxy.rb', line 119 def wrap_endpoint(endpoint) Endpoint.new(endpoint.url, self, **endpoint.) end |