Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/async/http/body/pipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def to_io
@tail
end

# Close the socket and stop reading from the input. Unlike {close}, the writer forwards any data already written to the socket before closing the output normally.
def finish
@reader&.stop
@tail.close
end

# Close the pipe and stop the reader and writer tasks.
def close
@reader&.stop
Expand Down
22 changes: 21 additions & 1 deletion lib/async/http/protocol/http2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,31 @@ def call(request)

response = create_response
write_request(response, request)
read_response(response)

begin
read_response(response)
received = true
ensure
# If the caller stopped waiting for the response (e.g. its timeout expired) while the request body is still being sent, cancel the stream. Otherwise the task sending the body would keep running, and for a body which is never finished, such as a CONNECT tunnel, forever.
cancel_stream(response.stream) unless received
end

return response
end

# Cancel a stream whose request body is still being sent. The caller has stopped waiting, and sending the reset can block (e.g. the remote peer is not reading), so it is sent by a separate task.
# @parameter stream [Stream] The stream to cancel.
def cancel_stream(stream)
return unless stream.sending?

Async(transient: true, annotation: "Cancelling #{stream}.") do
stream.send_reset_stream(::Protocol::HTTP2::Error::CANCEL) if stream.sending?
rescue => error
# The stream or the connection was closed in the meantime:
Console.debug(self, "Failed to cancel stream!", error)
end
end

# Write a request to the remote server via the given response stream.
# @parameter response [Response] The response stream to write through.
# @parameter request [Protocol::HTTP::Request] The request to send.
Expand Down
2 changes: 1 addition & 1 deletion lib/async/http/protocol/http2/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def passthrough(task)
# chunk.clear unless chunk.frozen?
# GC.start
end
rescue Async::Cancel, StandardError => error
rescue Async::Stop, StandardError => error

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 This comment was written by Claude (Claude Code) on behalf of the PR author.

Why Async::Stop and not Async::Cancel: the gemspec allows async >= 2.35.1, and 2.35.1 defines Async::Stop but has no Async::Cancel constant. With rescue Async::Cancel, evaluating this clause raised NameError: uninitialized constant Async::Cancel whenever the task writing the body was stopped or failed, so the task ended with that NameError.

In current async (checked on 2.46.0) Async::Stop is an alias (Stop = Cancel), so this rescues the same exception there.

raise
ensure
# Ensure the body we are reading from is fully closed:
Expand Down
4 changes: 2 additions & 2 deletions lib/async/http/protocol/http2/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ def wait
@stream.wait
end

# Close this response as quickly as possible. If the response body is still active, cancel the HTTP/2 exchange rather than draining it.
# Close this response as quickly as possible. If the response body is still active, cancel the HTTP/2 exchange rather than draining it. An error also cancels the exchange if the response has no body (left) but the request is still being sent.
# @parameter error [Exception | Nil] The error which closed the response.
def close(error = nil)
if @body && !@stream.closed?
if (@body || error) && !@stream.closed?
code = error ? ::Protocol::HTTP2::Error::INTERNAL_ERROR : ::Protocol::HTTP2::Error::CANCEL
@stream.send_reset_stream(code)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/async/http/protocol/http2/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ def send_body(body, trailer = nil)
@output.start
end

# @returns [Boolean] Whether the body given to {send_body} is still being sent.
def sending?
!@output.nil?
end

# Called when the output terminates normally.
def finish_output(error = nil)
return if self.closed?
Expand Down
222 changes: 211 additions & 11 deletions lib/async/http/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,171 @@ module HTTP
class Proxy
# Raised when a CONNECT tunnel through a proxy cannot be established.
class ConnectFailure < StandardError
# The maximum number of bytes of the response body to read, see {body}.
MAXIMUM_BODY_SIZE = 1024 * 8

# Initialize the failure with the unsuccessful response.
# @parameter response [Protocol::HTTP::Response] The failed response from the proxy.
def initialize(response)
# @parameter body [String | Nil] The beginning of the response body, if any.
def initialize(response, body = nil)
super "Failed to connect: #{response.status}"
@response = response
@body = body
end

# @attribute [Protocol::HTTP::Response] The failed response from the proxy. It has already been closed.
attr :response

# @attribute [String | Nil] Up to {MAXIMUM_BODY_SIZE} bytes of the response body, which may explain why the proxy rejected the connection.
#
# The body is read before this error is raised, so a proxy which neither finishes the body nor closes the connection (e.g. an HTTP/1 response without `content-length` on a persistent connection) will delay the failure until the caller's own timeout, if any.
attr :body
end

# A CONNECT tunnel, which is closed once its socket has been closed.
#
# Closing the socket of a {Body::Pipe} only ends the CONNECT request body, so the CONNECT response would remain open (and the proxy connection busy) until the proxy closes its end of the tunnel. Instead, the CONNECT response is closed as soon as the socket is closed, and the CONNECT request is closed once any data already written to the socket has been sent. HTTP/2 then resets the stream and HTTP/1 closes the connection, neither of which can be reused after a tunnel anyway.
class Tunnel
# The default time in seconds to wait for data already written to the socket to be sent to the proxy, after the socket is closed.
CLOSE_TIMEOUT = 10

# Extends the socket of a tunnel so that closing it closes the entire tunnel. These methods can be called from any thread.
module Closable
# Close the socket, and then the tunnel.
def close
if @tunnel
# Any other descriptor for the socket (e.g. from `clone`) would keep it open, and the tunnel is about to be closed, so make sure they observe the end of the stream:
begin
self.shutdown(::Socket::SHUT_RDWR)
rescue IOError, SystemCallError
# The socket is already closed or shut down.
end
end

super
ensure
close_tunnel
end

# Close the reading side of the socket, and then the tunnel if the writing side is also closed.
def close_read
super
ensure
close_tunnel if closed?
end

# Close the writing side of the socket, and then the tunnel if the reading side is also closed.
def close_write
super
ensure
close_tunnel if closed?
end

# A copy has its own file descriptor, so closing it must not close the tunnel.
def initialize_copy(other)
super

@tunnel = nil
end

private

def close_tunnel
if tunnel = @tunnel
@tunnel = nil
tunnel.close
end
end
end

# @parameter timeout [Numeric] The time in seconds to wait for data already written to the socket to be sent to the proxy, after the socket is closed.
def initialize(timeout = CLOSE_TIMEOUT)
@timeout = timeout

# What has happened to the tunnel: `:sent`, `:received` and `:closed`. A queue can be pushed to from any thread, and its timeout doesn't interrupt the waiting task with an exception, which Ruby 3.3.6 doesn't handle while waiting on a condition variable.
@events = ::Thread::Queue.new

@input = Body::Writable.new

# The connection closes the request body once it has finished sending it:
@request_body = ::Protocol::HTTP::Body::Completable.new(@input, proc{@events.push(:sent)})

@response = nil
@pipe = nil
end

# @attribute [Protocol::HTTP::Body::Readable] The body of the CONNECT request, which is everything written to the socket.
attr :request_body

# Open the tunnel using a successful CONNECT response, and start a task which closes the tunnel once its socket is closed.
# @parameter response [Protocol::HTTP::Response] The CONNECT response.
# @returns [Socket] The socket for the tunnel, which closes the tunnel when closed.
def open(response, parent: Task.current)
@response = response

# The response has no body if the proxy has already finished it. The pipe closes the response body once it has finished receiving it:
response_body = response.body || ::Protocol::HTTP::Body::Readable.new
response_body = ::Protocol::HTTP::Body::Completable.new(response_body, proc{@events.push(:received)})

@pipe = Body::Pipe.new(response_body, @input, task: parent)

parent.async(transient: true, annotation: "#{self.class} closer.", &self.method(:closer))

socket = @pipe.to_io
socket.extend(Closable)
socket.instance_variable_set(:@tunnel, self)

return socket
end

# Close the tunnel. The tunnel is closed by its own task, so unlike stopping the tasks of the pipe, this can be called from any thread.
def close
@events.push(:closed)
end

# Close the request body of a tunnel which was not opened, so that the connection stops waiting for it.
def abort
@input.close
end

private

# Wait until one of the given events has happened.
# @returns [Symbol | Nil] The event which happened, or `nil` if the timeout expired first.
def wait_for(*events, timeout: nil)
deadline = Clock.now + timeout if timeout

until event = events.find{|event| @happened.include?(event)}
if deadline
remaining = deadline - Clock.now
return nil unless remaining > 0 and happened = @events.pop(timeout: remaining)
else
happened = @events.pop
end

@happened << happened

# Once both directions have finished, there is nothing left to close:
@happened << :finished if @happened.include?(:sent) and @happened.include?(:received)
end

return event
end

def closer(task)
@happened = []

# If the tunnel finishes without the socket being closed (e.g. each side of the socket was closed separately), there is nothing to do:
return unless wait_for(:closed, :finished) == :closed

# Close the CONNECT response and the socket. The pipe forwards any data already written to the socket, and then ends the CONNECT request:
@pipe.finish

unless wait_for(:sent, timeout: @timeout)
# The proxy has stopped reading, so give up, which resets an HTTP/2 stream. An HTTP/1 connection which is blocked writing to the proxy is not interrupted:
@response.close(::IO::TimeoutError.new("Timed out while closing the tunnel!"))
end
end
end

# Extends {Async::HTTP::Client} with proxy capabilities.
Expand Down Expand Up @@ -76,10 +233,19 @@ def self.endpoint(client, endpoint, headers = nil)
# @param client [Async::HTTP::Client] the client which will be used as a proxy server.
# @param address [String] the address to connect to.
# @param headers [Array] an optional list of headers to use when establishing the connection.
def initialize(client, address, headers = nil)
# @param close_timeout [Numeric] the time in seconds to wait for data already written to a tunnel to be sent to the proxy, after the tunnel is closed.
def initialize(client, address, headers = nil, close_timeout: Tunnel::CLOSE_TIMEOUT, **options)
# Headers given as a trailing hash, e.g. `Proxy.new(client, address, "proxy-authorization" => token)`, are received as keyword arguments:
if headers.nil?
headers = options unless options.empty?
elsif !options.empty?
raise ArgumentError, "unknown keywords: #{options.keys.map(&:inspect).join(', ')}"
end

@client = client
@address = address
@headers = ::Protocol::HTTP::Headers[headers].freeze
@close_timeout = close_timeout
end

attr :client
Expand All @@ -90,35 +256,69 @@ def close
end

# Establish a TCP connection to the specified host.
#
# The tunnel is closed when the returned socket is closed, or when the block exits. Any data already written to the socket is sent to the proxy first, unless that takes longer than the close timeout.
# @return [Socket] a connected bi-directional socket.
def connect(&block)
input = Body::Writable.new
tunnel = Tunnel.new(@close_timeout)

response = @client.connect(authority: @address, headers: @headers, body: input)
begin
response = @client.connect(authority: @address, headers: @headers, body: tunnel.request_body)
ensure
# If the request failed, e.g. the proxy closed the connection or the caller's timeout expired, a task may still be waiting to read the request body, and would never finish:
tunnel.abort unless response
end

if response.success?
pipe = Body::Pipe.new(response.body, input)
socket = tunnel.open(response)

return pipe.to_io unless block_given?
return socket unless block_given?

begin
yield pipe.to_io
yield socket
ensure
pipe.close
socket.close
end
else
# This ensures we don't leave a response dangling:
input.close
response.close
tunnel.abort

begin
body = read_failure_body(response)
ensure
response.close
end

raise ConnectFailure, response
raise ConnectFailure.new(response, body)
end
end

# @return [Async::HTTP::Endpoint] an endpoint that connects via the specified proxy.
def wrap_endpoint(endpoint)
Endpoint.new(endpoint.url, self, **endpoint.options)
end

private

# Proxies often explain why they rejected a connection in the response body, so read the beginning of it. Failing to do so shouldn't hide the original failure.
def read_failure_body(response)
body = response.body
return nil unless body

buffer = String.new

begin
# A single chunk can be much larger than the limit, so only append what is still needed:
while (remaining = ConnectFailure::MAXIMUM_BODY_SIZE - buffer.bytesize) > 0 and chunk = body.read
buffer << chunk.byteslice(0, remaining)
end
rescue IOError, SystemCallError, ::Protocol::HTTP::Error => error
# Anything else, e.g. `Async::TimeoutError`, should propagate to the caller:
Console.debug(self, "Failed to read proxy response body!", error)
end

return buffer
end
end

Client.prepend(Proxy::Client)
Expand Down
11 changes: 11 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Releases

## Unreleased

- Closing the socket returned by `Async::HTTP::Proxy#connect` (without a block) now closes the entire CONNECT tunnel, rather than only ending the CONNECT request body. The CONNECT response is closed immediately, and the CONNECT request is closed once any data already written to the socket has been forwarded, after which HTTP/2 proxies receive `RST_STREAM(NO_ERROR)` and HTTP/1 proxy connections are closed. `Async::HTTP::Client#close` for the proxy client no longer waits for the proxy to close its end of the tunnel. The block form of `Async::HTTP::Proxy#connect` closes the tunnel the same way when the block exits, and no longer discards data written immediately before the block exits.
- The tunnel is closed by a background task, so the socket can be closed from any thread. Closing each side of the socket separately (`close_write` and `close_read`) also closes the tunnel. Closing a copy of the socket (e.g. `Socket#clone`) only closes the copy, and closing the socket itself ends the stream for any copies.
- Added the `close_timeout:` option to `Async::HTTP::Proxy.new` (10 seconds by default), which limits how long a closed tunnel waits for data already written to the socket to be sent. After that, an HTTP/2 proxy which has stopped reading receives `RST_STREAM`. An HTTP/1 proxy connection which is blocked writing is not interrupted.
- `Async::HTTP::Proxy::ConnectFailure#body` exposes up to 8 KiB of the proxy's response body, which often explains why the connection was rejected. I/O and protocol errors while reading it are ignored, but other errors, such as `Async::TimeoutError`, propagate to the caller.
- `Async::HTTP::Proxy#connect` now closes the CONNECT request body when the request fails without a response, e.g. the proxy closes the connection or the caller's timeout expires. Previously, the task writing the request body was left waiting forever on HTTP/1 proxy connections.
- HTTP/2 clients now send `RST_STREAM(CANCEL)` when the caller stops waiting for the response headers (e.g. its timeout expires) while the request body is still being sent. Previously, the task sending the body kept running, and never finished if the body was never finished, as for a CONNECT tunnel. The reset is sent by a separate task, so the caller is not delayed if the connection can't be written to.
- Closing an HTTP/2 response with an error now resets the stream even if the response has no body (left), as the request may still be being sent.
- Fixed `uninitialized constant Async::Cancel` when the task sending an HTTP/2 request or response body is stopped or fails with async versions before `Async::Cancel` was introduced.

## v0.105.0

- Respond with `400 Bad Request` for any `Protocol::HTTP::BadRequest` raised while parsing HTTP/1 or HTTP/2 requests, include the exception class name without reflecting request data, and avoid reporting them as unhandled server errors.
Expand Down
Loading
Loading