Class: Async::HTTP::Protocol::HTTP2::Request

Inherits:
Request
  • Object
show all
Defined in:
lib/async/http/protocol/http2/request.rb

Overview

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

Defined Under Namespace

Classes: Stream

Constant Summary collapse

NO_RESPONSE =
[
	[STATUS, "500"],
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Request

#inspect, #peer, #remote_address

Constructor Details

#initialize(stream) ⇒ Request

Initialize the request from an HTTP/2 stream.



98
99
100
101
102
# File 'lib/async/http/protocol/http2/request.rb', line 98

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

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



104
105
106
# File 'lib/async/http/protocol/http2/request.rb', line 104

def stream
  @stream
end

Instance Method Details

#connectionObject



107
108
109
# File 'lib/async/http/protocol/http2/request.rb', line 107

def connection
	@stream.connection
end

#hijack?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/async/http/protocol/http2/request.rb', line 117

def hijack?
	false
end

#send_response(response) ⇒ Object

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/async/http/protocol/http2/request.rb', line 127

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

#valid?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/async/http/protocol/http2/request.rb', line 112

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

#write_interim_response(status, headers = nil) ⇒ Object

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



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/async/http/protocol/http2/request.rb', line 163

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