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

#peer, #remote_address, #remote_address=

Constructor Details

#initialize(stream) ⇒ Request

Returns a new instance of Request.



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

def initialize(stream)
	super(nil, nil, nil, nil, VERSION, nil)
	
	@stream = stream
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



110
111
112
# File 'lib/async/http/protocol/http2/request.rb', line 110

def stream
  @stream
end

Instance Method Details

#connectionObject



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

def connection
	@stream.connection
end

#hijack?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/async/http/protocol/http2/request.rb', line 120

def hijack?
	false
end

#send_response(response) ⇒ Object



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
159
160
# File 'lib/async/http/protocol/http2/request.rb', line 128

def send_response(response)
	if response.nil?
		return @stream.send_headers(nil, NO_RESPONSE, ::Protocol::HTTP2::END_STREAM)
	end
	
	protocol_headers = [
		[STATUS, response.status],
	]
	
	if protocol = response.protocol
		protocol_headers << [PROTOCOL, protocol]
	end
	
	if length = response.body&.length
		protocol_headers << [CONTENT_LENGTH, length]
	end
	
	headers = ::Protocol::HTTP::Headers::Merged.new(protocol_headers, response.headers)
	
	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(nil, headers)
		
		@stream.send_body(body, trailer)
	else
		# Ensure the response body is closed if we are ending the stream:
		response.close
		
		@stream.send_headers(nil, headers, ::Protocol::HTTP2::END_STREAM)
	end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
	@scheme and @method and @path
end