Skip to content
Merged
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
5 changes: 3 additions & 2 deletions lib/async/http/protocol/http1/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative "connection"
require_relative "finishable"

require "async/promise"
require "console/event/failure"

module Async
Expand All @@ -22,14 +23,14 @@ class Server < Connection
def initialize(...)
super

@ready = Async::Notification.new
@ready = Async::Promise.new
end

# Called when the connection is closed, signalling any waiting tasks.
def closed(error = nil)
super

@ready.signal
@ready.resolve(nil)
end

# Write a failure response with the given status code.
Expand Down
30 changes: 10 additions & 20 deletions lib/async/http/protocol/http2/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require_relative "../response"
require_relative "stream"

require "async/promise"

module Async
module HTTP
module Protocol
Expand All @@ -20,8 +22,7 @@ def initialize(*)

@response = Response.new(self)

@notification = Async::Notification.new
@exception = nil
@ready = Async::Promise.new
end

attr :response
Expand Down Expand Up @@ -88,7 +89,7 @@ def receive_initial_headers(headers, end_stream)
send_reset_stream(::Protocol::HTTP2::Error::PROTOCOL_ERROR)
end

self.notify!
@ready.resolve(nil)

return headers
end
Expand All @@ -106,22 +107,9 @@ def receive_interim_headers(status, headers)
@response.request.send_interim_response(status, headers)
end

# Notify anyone waiting on the response headers to be received (or failure).
def notify!
if notification = @notification
@notification = nil
notification.signal
end
end

# Wait for the headers to be received or for stream reset.
def wait
# If you call wait after the headers were already received, it should return immediately:
@notification&.wait

if @exception
raise @exception
end
@ready.wait
rescue ::Protocol::HTTP2::StreamError => error
if error.code == ::Protocol::HTTP2::Error::INTERNAL_ERROR
raise ::Protocol::HTTP::RemoteError, error.message
Expand All @@ -139,9 +127,11 @@ def closed(error)
@response = nil
end

@exception = error

self.notify!
if error
@ready.reject(error)
else
@ready.resolve(nil)
end
end
end

Expand Down
Loading