Class: Async::HTTP::Protocol::HTTP2::Response
- Inherits:
-
Response
- Object
- Protocol::HTTP::Response
- Response
- Async::HTTP::Protocol::HTTP2::Response
- Defined in:
- lib/async/http/protocol/http2/response.rb
Overview
Typically used on the client side to represent a request and the incoming response.
Defined Under Namespace
Classes: Stream
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Attributes inherited from Response
Instance Method Summary collapse
- #build_request(headers) ⇒ Object
-
#initialize(stream) ⇒ Response
constructor
A new instance of Response.
- #promises ⇒ Object
-
#send_request(request, task: Async::Task.current) ⇒ Object
Send a request and read it into this response.
- #valid? ⇒ Boolean
- #wait ⇒ Object
Methods inherited from Response
#hijack?, #peer, #remote_address, #remote_address=
Constructor Details
#initialize(stream) ⇒ Response
Returns a new instance of Response.
112 113 114 115 116 117 118 |
# File 'lib/async/http/protocol/http2/response.rb', line 112 def initialize(stream) super(stream.connection.version, nil, nil) @stream = stream @request = nil @promises = nil end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
122 123 124 |
# File 'lib/async/http/protocol/http2/response.rb', line 122 def request @request end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
120 121 122 |
# File 'lib/async/http/protocol/http2/response.rb', line 120 def stream @stream end |
Instance Method Details
#build_request(headers) ⇒ Object
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 161 162 163 164 165 166 |
# File 'lib/async/http/protocol/http2/response.rb', line 136 def build_request(headers) request = ::Protocol::HTTP::Request.new request.headers = ::Protocol::HTTP::Headers.new headers.each do |key, value| if key == SCHEME raise ::Protocol::HTTP2::HeaderError, "Request scheme already specified!" if request.scheme request.scheme = value elsif key == AUTHORITY raise ::Protocol::HTTP2::HeaderError, "Request authority already specified!" if request. request. = value elsif key == METHOD raise ::Protocol::HTTP2::HeaderError, "Request method already specified!" if request.method request.method = value elsif key == PATH raise ::Protocol::HTTP2::HeaderError, "Request path is empty!" if value.empty? raise ::Protocol::HTTP2::HeaderError, "Request path already specified!" if request.path request.path = value elsif key.start_with? ':' raise ::Protocol::HTTP2::HeaderError, "Invalid pseudo-header #{key}!" else request.headers[key] = value end end @request = request end |
#promises ⇒ Object
132 133 134 |
# File 'lib/async/http/protocol/http2/response.rb', line 132 def promises @promises ||= Async::Queue.new end |
#send_request(request, task: Async::Task.current) ⇒ Object
Send a request and read it into this response.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/async/http/protocol/http2/response.rb', line 169 def send_request(request, task: Async::Task.current) @request = request # https://http2.github.io/http2-spec/#rfc.section.8.1.2.3 # All HTTP/2 requests MUST include exactly one valid value for the :method, :scheme, and :path pseudo-header fields, unless it is a CONNECT request (Section 8.3). An HTTP request that omits mandatory pseudo-header fields is malformed (Section 8.1.2.6). pseudo_headers = [ [SCHEME, request.scheme], [METHOD, request.method], [PATH, request.path], ] # To ensure that the HTTP/1.1 request line can be reproduced accurately, this pseudo-header field MUST be omitted when translating from an HTTP/1.1 request that has a request target in origin or asterisk form (see [RFC7230], Section 5.3). Clients that generate HTTP/2 requests directly SHOULD use the :authority pseudo-header field instead of the Host header field. if = request. pseudo_headers << [AUTHORITY, ] end if protocol = request.protocol pseudo_headers << [PROTOCOL, protocol] end headers = ::Protocol::HTTP::Headers::Merged.new( pseudo_headers, request.headers ) if request.body.nil? @stream.send_headers(nil, headers, ::Protocol::HTTP2::END_STREAM) else begin @stream.send_headers(nil, headers) rescue raise RequestFailed end @stream.send_body(request.body) end end |
#valid? ⇒ Boolean
128 129 130 |
# File 'lib/async/http/protocol/http2/response.rb', line 128 def valid? !!@status end |
#wait ⇒ Object
124 125 126 |
# File 'lib/async/http/protocol/http2/response.rb', line 124 def wait @stream.wait end |