Class: Protocol::HTTP2::Stream
- Inherits:
-
Object
- Object
- Protocol::HTTP2::Stream
- Includes:
- FlowControl
- Defined in:
- lib/protocol/http2/stream.rb
Overview
A single HTTP 2.0 connection can multiplex multiple streams in parallel: multiple requests and responses can be in flight simultaneously and stream data can be interleaved and prioritized.
This class encapsulates all of the state, transition, flow-control, and error management as defined by the HTTP 2.0 specification. All you have to do is subscribe to appropriate events (marked with “:” prefix in diagram below) and provide your application logic to handle request and response processing.
+--------+
send PP | | recv PP
,--------| idle |--------.
/ | | \
v +--------+ v
+----------+ | +----------+
| | | send H / | |
,——| reserved | | recv H | reserved |——. | | (local) | | | (remote) | | | ---------- v ---------- | | | -------- | | | | recv ES | | send ES | | | send H | ,——-| open |——-. | recv H | | | / | | \ | | | v v -------- v v | | ---------- | ---------- | | | half | | | half | | | | closed | | send R / | closed | | | | (remote) | | recv R | (local) | | | ---------- | ---------- | | | | | | | | send ES / | recv ES / | | | | send R / v send R / | | | | recv R -------- recv R | | | send R / ‘———–>| |<———–’ send R / | | recv R | closed | recv R | ‘———————–>| |<———————-’
+--------+
send: endpoint sends this frame
recv: endpoint receives this frame
H: HEADERS frame (with implied CONTINUATIONs)
PP: PUSH_PROMISE frame (with implied CONTINUATIONs)
ES: END_STREAM flag
R: RST_STREAM frame
State transition methods use a trailing “!”.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#id ⇒ Object
readonly
Stream ID (odd for client initiated streams, even otherwise).
-
#local_window ⇒ Object
readonly
Returns the value of attribute local_window.
-
#remote_window ⇒ Object
readonly
Returns the value of attribute remote_window.
-
#state ⇒ Object
readonly
Stream state as defined by HTTP 2.0.
Instance Method Summary collapse
-
#accept_push_promise_stream(stream_id, headers) ⇒ Object
Override this function to implement your own push promise logic.
-
#close! ⇒ Object
This is not the same as a ‘close` method.
- #closed? ⇒ Boolean
- #consume_remote_window(frame) ⇒ Object
-
#create_push_promise_stream(headers) ⇒ Object
Override this function to implement your own push promise logic.
-
#initialize(connection, id = connection.next_stream_id) ⇒ Stream
constructor
A new instance of Stream.
- #inspect ⇒ Object
- #maximum_frame_size ⇒ Object
- #priority=(priority) ⇒ Object
-
#receive_data(frame) ⇒ Object
DATA frames are subject to flow control and can only be sent when a stream is in the “open” or “half-closed (remote)” state.
- #receive_headers(frame) ⇒ Object
- #receive_priority(frame) ⇒ Object
- #receive_push_promise(frame) ⇒ Object
- #receive_reset_stream(frame) ⇒ Object
- #reserved_local! ⇒ Object
- #reserved_remote! ⇒ Object
- #send_data(*args) ⇒ Object
- #send_failure(status, reason) ⇒ Object
-
#send_headers(*args) ⇒ Object
The HEADERS frame is used to open a stream, and additionally carries a header block fragment.
- #send_headers? ⇒ Boolean
-
#send_push_promise(headers) ⇒ Object
Server push is semantically equivalent to a server responding to a request; however, in this case, that request is also sent by the server, as a PUSH_PROMISE frame.
- #send_reset_stream(error_code = 0) ⇒ Object
- #write_frame(frame) ⇒ Object
Methods included from FlowControl
#available_frame_size, #consume_local_window, #receive_window_update, #send_window_update, #window_updated
Constructor Details
#initialize(connection, id = connection.next_stream_id) ⇒ Stream
Returns a new instance of Stream.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/protocol/http2/stream.rb', line 77 def initialize(connection, id = connection.next_stream_id) @connection = connection @id = id @state = :idle @priority = nil @local_window = Window.new(connection.local_settings.initial_window_size) @remote_window = Window.new(connection.remote_settings.initial_window_size) @headers = nil @data = nil @connection.streams[@id] = self end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
100 101 102 |
# File 'lib/protocol/http2/stream.rb', line 100 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
99 100 101 |
# File 'lib/protocol/http2/stream.rb', line 99 def headers @headers end |
#id ⇒ Object (readonly)
Stream ID (odd for client initiated streams, even otherwise).
94 95 96 |
# File 'lib/protocol/http2/stream.rb', line 94 def id @id end |
#local_window ⇒ Object (readonly)
Returns the value of attribute local_window.
102 103 104 |
# File 'lib/protocol/http2/stream.rb', line 102 def local_window @local_window end |
#remote_window ⇒ Object (readonly)
Returns the value of attribute remote_window.
103 104 105 |
# File 'lib/protocol/http2/stream.rb', line 103 def remote_window @remote_window end |
#state ⇒ Object (readonly)
Stream state as defined by HTTP 2.0.
97 98 99 |
# File 'lib/protocol/http2/stream.rb', line 97 def state @state end |
Instance Method Details
#accept_push_promise_stream(stream_id, headers) ⇒ Object
Override this function to implement your own push promise logic.
367 368 369 |
# File 'lib/protocol/http2/stream.rb', line 367 def accept_push_promise_stream(stream_id, headers) @connection.accept_push_promise_stream(stream_id) end |
#close! ⇒ Object
This is not the same as a ‘close` method. If you are looking for that, use `send_stream_reset`.
221 222 223 |
# File 'lib/protocol/http2/stream.rb', line 221 def close! @state = :closed end |
#closed? ⇒ Boolean
121 122 123 |
# File 'lib/protocol/http2/stream.rb', line 121 def closed? @state == :closed end |
#consume_remote_window(frame) ⇒ Object
184 185 186 187 188 |
# File 'lib/protocol/http2/stream.rb', line 184 def consume_remote_window(frame) super @connection.consume_remote_window(frame) end |
#create_push_promise_stream(headers) ⇒ Object
Override this function to implement your own push promise logic.
347 348 349 |
# File 'lib/protocol/http2/stream.rb', line 347 def create_push_promise_stream(headers) @connection.create_push_promise_stream end |
#inspect ⇒ Object
381 382 383 |
# File 'lib/protocol/http2/stream.rb', line 381 def inspect "\#<#{self.class} id=#{@id} state=#{@state}>" end |
#maximum_frame_size ⇒ Object
113 114 115 |
# File 'lib/protocol/http2/stream.rb', line 113 def maximum_frame_size @connection.available_frame_size end |
#priority=(priority) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/protocol/http2/stream.rb', line 105 def priority= priority if priority.stream_id == @id raise ProtocolError, "Stream #{@id} cannot depend on itself!" end @priority = priority end |
#receive_data(frame) ⇒ Object
DATA frames are subject to flow control and can only be sent when a stream is in the “open” or “half-closed (remote)” state. The entire DATA frame payload is included in flow control, including the Pad Length and Padding fields if present. If a DATA frame is received whose stream is not in “open” or “half-closed (local)” state, the recipient MUST respond with a stream error of type STREAM_CLOSED.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/protocol/http2/stream.rb', line 280 def receive_data(frame) if @state == :open consume_local_window(frame) if frame.end_stream? @state = :half_closed_remote end @data = frame.unpack elsif @state == :half_closed_local consume_local_window(frame) if frame.end_stream? close! end @data = frame.unpack else raise ProtocolError, "Cannot receive data in state: #{@state}" end end |
#receive_headers(frame) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/protocol/http2/stream.rb', line 249 def receive_headers(frame) if @state == :idle if frame.end_stream? @state = :half_closed_remote else @state = :open end @headers = process_headers(frame) elsif @state == :reserved_remote @state = :half_closed_local @headers = process_headers(frame) elsif @state == :open if frame.end_stream? @state = :half_closed_remote end @headers = process_headers(frame) elsif @state == :half_closed_local if frame.end_stream? close! end @headers = process_headers(frame) else raise ProtocolError, "Cannot receive headers in state: #{@state}" end end |
#receive_priority(frame) ⇒ Object
302 303 304 |
# File 'lib/protocol/http2/stream.rb', line 302 def receive_priority(frame) self.priority = frame.unpack end |
#receive_push_promise(frame) ⇒ Object
371 372 373 374 375 376 377 378 379 |
# File 'lib/protocol/http2/stream.rb', line 371 def receive_push_promise(frame) promised_stream_id, data = frame.unpack headers = @connection.decode_headers(data) stream = self.accept_push_promise_stream(promised_stream_id, headers) stream.reserved_remote! return stream, headers end |
#receive_reset_stream(frame) ⇒ Object
306 307 308 309 310 311 312 313 314 |
# File 'lib/protocol/http2/stream.rb', line 306 def receive_reset_stream(frame) if @state != :idle and @state != :closed close! return frame.unpack else raise ProtocolError, "Cannot reset stream in state: #{@state}" end end |
#reserved_local! ⇒ Object
330 331 332 333 334 335 336 |
# File 'lib/protocol/http2/stream.rb', line 330 def reserved_local! if @state == :idle @state = :reserved_local else raise ProtocolError, "Cannot reserve stream in state: #{@state}" end end |
#reserved_remote! ⇒ Object
338 339 340 341 342 343 344 |
# File 'lib/protocol/http2/stream.rb', line 338 def reserved_remote! if @state == :idle @state = :reserved_remote else raise ProtocolError, "Cannot reserve stream in state: #{@state}" end end |
#send_data(*args) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/protocol/http2/stream.rb', line 202 def send_data(*args) if @state == :open frame = write_data(*args) if frame.end_stream? @state = :half_closed_local end elsif @state == :half_closed_remote frame = write_data(*args) if frame.end_stream? close! end else raise ProtocolError, "Cannot send data in state: #{@state}" end end |
#send_failure(status, reason) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/protocol/http2/stream.rb', line 129 def send_failure(status, reason) if send_headers? send_headers(nil, [ [':status', status.to_s], ['reason', reason] ], END_STREAM) else send_reset_stream(PROTOCOL_ERROR) end return nil end |
#send_headers(*args) ⇒ Object
The HEADERS frame is used to open a stream, and additionally carries a header block fragment. HEADERS frames can be sent on a stream in the “idle”, “reserved (local)”, “open”, or “half-closed (remote)” state.
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 |
# File 'lib/protocol/http2/stream.rb', line 154 def send_headers(*args) if @state == :idle frame = write_headers(*args) if frame.end_stream? @state = :half_closed_local else @state = :open end elsif @state == :reserved_local frame = write_headers(*args) @state = :half_closed_remote elsif @state == :open frame = write_headers(*args) if frame.end_stream? @state = :half_closed_local end elsif @state == :half_closed_remote frame = write_headers(*args) if frame.end_stream? close! end else raise ProtocolError, "Cannot send headers in state: #{@state}" end end |
#send_headers? ⇒ Boolean
125 126 127 |
# File 'lib/protocol/http2/stream.rb', line 125 def send_headers? @state == :idle or @state == :reserved_local or @state == :open or @state == :half_closed_remote end |
#send_push_promise(headers) ⇒ Object
Server push is semantically equivalent to a server responding to a request; however, in this case, that request is also sent by the server, as a PUSH_PROMISE frame.
353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/protocol/http2/stream.rb', line 353 def send_push_promise(headers) if @state == :open or @state == :half_closed_remote promised_stream = self.create_push_promise_stream(headers) promised_stream.reserved_local! write_push_promise(promised_stream.id, headers) return promised_stream else raise ProtocolError, "Cannot send push promise in state: #{@state}" end end |
#send_reset_stream(error_code = 0) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/protocol/http2/stream.rb', line 225 def send_reset_stream(error_code = 0) if @state != :idle and @state != :closed frame = ResetStreamFrame.new(@id) frame.pack(error_code) write_frame(frame) close! else raise ProtocolError, "Cannot reset stream in state: #{@state}" end end |
#write_frame(frame) ⇒ Object
117 118 119 |
# File 'lib/protocol/http2/stream.rb', line 117 def write_frame(frame) @connection.write_frame(frame) end |