Async::HTTPSourceAsyncHTTPProtocolHTTP2Request

class Request

Typically used on the server side to represent an incoming request, and write the response.

Nested

Definitions

def initialize(stream)

Initialize the request from an HTTP/2 stream.

Signature

parameter stream Stream

The HTTP/2 stream for this request.

Implementation

def initialize(stream)
	super(nil, nil, nil, nil, VERSION, nil, nil, nil, self.public_method(:write_interim_response))
	
	@stream = stream
end

def connection

Signature

returns Connection

The underlying HTTP/2 connection.

Implementation

def connection
	@stream.connection
end

def valid?

Signature

returns Boolean

Whether the request has the required pseudo-headers.

Implementation

def valid?
	@scheme and @method and (@path or @method == ::Protocol::HTTP::Methods::CONNECT)
end

def hijack?

Signature

returns Boolean

Whether connection hijacking is supported (not available for HTTP/2).

Implementation

def hijack?
	false
end

def send_response(response)

Send a response back to the client via the HTTP/2 stream.

Signature

parameter response Protocol::HTTP::Response | Nil

The response to send.

Implementation

def send_response(response)
	if response.nil?
		return @stream.send_headers(NO_RESPONSE, ::Protocol::HTTP2::END_STREAM)
	end
	
	protocol_headers = [
		[STATUS, response.status],
	]
	
	if length = response.body&.length
		protocol_headers << [CONTENT_LENGTH, length]
	end
	
	headers = ::Protocol::HTTP::Headers::Merged.new(
		protocol_headers,
		response.headers.header
	)
	
	if body = response.body and !self.head?
		# This function informs the headers object that any subsequent headers are going to be trailer. Therefore, it must be called *before* sending the headers, to avoid any race conditions.
		trailer = response.headers.trailer!
		
		@stream.send_headers(headers)
		
		@stream.send_body(body, trailer)
	else
		# Ensure the response body is closed if we are ending the stream:
		response.close
		
		@stream.send_headers(headers, ::Protocol::HTTP2::END_STREAM)
	end
end

def write_interim_response(status, headers = nil)

Write an interim (1xx) response to the client.

Signature

parameter status Integer

The interim HTTP status code.

parameter headers Hash | Nil

Optional interim response headers.

Implementation

def write_interim_response(status, headers = nil)
	interim_response_headers = [
		[STATUS, status]
	]
	
	if headers
		interim_response_headers = ::Protocol::HTTP::Headers::Merged.new(interim_response_headers, headers)
	end
	
	@stream.send_headers(interim_response_headers)
end