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

Inherits:
Protocol::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

Constructor Details

#initialize(stream) ⇒ Request

Initialize the request from an HTTP/2 stream.



125
126
127
128
129
# File 'lib/async/http/protocol/http2/request.rb', line 125

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.



131
132
133
# File 'lib/async/http/protocol/http2/request.rb', line 131

def stream
  @stream
end

Instance Method Details

#connectionObject



134
135
136
# File 'lib/async/http/protocol/http2/request.rb', line 134

def connection
	@stream.connection
end

#hijack?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/async/http/protocol/http2/request.rb', line 144

def hijack?
	false
end

#send_response(response) ⇒ Object

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/async/http/protocol/http2/request.rb', line 154

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)


139
140
141
# File 'lib/async/http/protocol/http2/request.rb', line 139

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.



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/async/http/protocol/http2/request.rb', line 190

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