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.



87
88
89
90
91
# File 'lib/async/http/protocol/http2/request.rb', line 87

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

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



93
94
95
# File 'lib/async/http/protocol/http2/request.rb', line 93

def stream
  @stream
end

Instance Method Details

#connectionObject



95
96
97
# File 'lib/async/http/protocol/http2/request.rb', line 95

def connection
	@stream.connection
end

#hijack?Boolean

Returns:

  • (Boolean)


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

def hijack?
	false
end

#send_response(response) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/async/http/protocol/http2/request.rb', line 111

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)


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

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

#write_interim_response(response) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/async/http/protocol/http2/request.rb', line 145

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