class Async::HTTP::Protocol::HTTP2::Response::Stream

Attributes

response[R]

Public Class Methods

new(*) click to toggle source
# File lib/async/http/protocol/http2/response.rb, line 33
def initialize(*)
        super
        
        @response = Response.new(self)
        
        @notification = Async::Notification.new
        @exception = nil
end

Public Instance Methods

accept_push_promise_stream(promised_stream_id, headers) click to toggle source
# File lib/async/http/protocol/http2/response.rb, line 52
def accept_push_promise_stream(promised_stream_id, headers)
        raise ProtocolError, "Cannot accept push promise stream!"
end
closed(error) click to toggle source
# File lib/async/http/protocol/http2/response.rb, line 106
def closed(error)
        super
        
        if @response
                @response = nil
        end
        
        @exception = error
        
        notify!
end
notify!() click to toggle source

Notify anyone waiting on the response headers to be received (or failure).

# File lib/async/http/protocol/http2/response.rb, line 89
def notify!
        if notification = @notification
                @notification = nil
                notification.signal
        end
end
receive_initial_headers(headers, end_stream) click to toggle source

This should be invoked from the background reader, and notifies the task waiting for the headers that we are done.

# File lib/async/http/protocol/http2/response.rb, line 57
def receive_initial_headers(headers, end_stream)
        headers.each do |key, value|
                if key == STATUS
                        @response.status = Integer(value)
                elsif key == PROTOCOL
                        @response.protocol = value
                elsif key == CONTENT_LENGTH
                        @length = Integer(value)
                else
                        add_header(key, value)
                end
        end
        
        @response.headers = @headers
        
        if @response.valid?
                if !end_stream
                        # We only construct the input/body if data is coming.
                        @response.body = prepare_input(@length)
                elsif @response.head?
                        @response.body = ::Protocol::HTTP::Body::Head.new(@length)
                end
        else
                send_reset_stream(::Protocol::HTTP2::Error::PROTOCOL_ERROR)
        end
        
        self.notify!
        
        return headers
end
wait() click to toggle source

Wait for the headers to be received or for stream reset.

# File lib/async/http/protocol/http2/response.rb, line 97
def wait
        # If you call wait after the headers were already received, it should return immediately:
        @notification&.wait
        
        if @exception
                raise @exception
        end
end
wait_for_input() click to toggle source
# File lib/async/http/protocol/http2/response.rb, line 44
def wait_for_input
        # The input isn't ready until the response headers have been received:
        @response.wait
        
        # There is a possible race condition if you try to access @input - it might already be closed and nil.
        return @response.body
end