Class: Protocol::HTTP2::Stream
- Inherits:
-
Object
- Object
- Protocol::HTTP2::Stream
show all
- 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: 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
Class Method Summary
collapse
Instance Method Summary
collapse
#available_frame_size, #available_size, #consume_local_window, #consume_window, #receive_window_update, #request_window_update, #send_window_update, #update_local_window, #window_updated
Constructor Details
#initialize(connection, id, state = :idle) ⇒ Stream
Returns a new instance of Stream.
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/protocol/http2/stream.rb', line 85
def initialize(connection, id, state = :idle)
@connection = connection
@id = id
@state = state
@local_window = Window.new(@connection.local_settings.initial_window_size)
@remote_window = Window.new(@connection.remote_settings.initial_window_size)
@dependency = @connection.dependency_for(@id)
end
|
Instance Attribute Details
#connection ⇒ Object
The connection this stream belongs to.
98
99
100
|
# File 'lib/protocol/http2/stream.rb', line 98
def connection
@connection
end
|
#dependency ⇒ Object
Returns the value of attribute dependency.
106
107
108
|
# File 'lib/protocol/http2/stream.rb', line 106
def dependency
@dependency
end
|
#id ⇒ Object
Stream ID (odd for client initiated streams, even otherwise).
101
102
103
|
# File 'lib/protocol/http2/stream.rb', line 101
def id
@id
end
|
#local_window ⇒ Object
Returns the value of attribute local_window.
108
109
110
|
# File 'lib/protocol/http2/stream.rb', line 108
def local_window
@local_window
end
|
#remote_window ⇒ Object
Returns the value of attribute remote_window.
109
110
111
|
# File 'lib/protocol/http2/stream.rb', line 109
def remote_window
@remote_window
end
|
#state ⇒ Object
Stream state, e.g. ‘idle`, `closed`.
104
105
106
|
# File 'lib/protocol/http2/stream.rb', line 104
def state
@state
end
|
Class Method Details
.create(connection, id) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/protocol/http2/stream.rb', line 77
def self.create(connection, id)
stream = self.new(connection, id)
connection.streams[id] = stream
return stream
end
|
Instance Method Details
#accept_push_promise_stream(stream_id, headers) ⇒ Object
Override this function to implement your own push promise logic.
428
429
430
|
# File 'lib/protocol/http2/stream.rb', line 428
def accept_push_promise_stream(stream_id, )
@connection.accept_push_promise_stream(stream_id)
end
|
#active? ⇒ Boolean
144
145
146
|
# File 'lib/protocol/http2/stream.rb', line 144
def active?
@state != :closed && @state != :idle
end
|
#children ⇒ Object
127
128
129
|
# File 'lib/protocol/http2/stream.rb', line 127
def children
@dependency&.streams
end
|
#close(error = nil) ⇒ Object
The stream is being closed because the connection is being closed.
132
133
134
|
# File 'lib/protocol/http2/stream.rb', line 132
def close(error = nil)
@connection.delete(@id)
end
|
#close!(error_code = nil) ⇒ Object
Transition the stream into the closed state.
248
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/protocol/http2/stream.rb', line 248
def close!(error_code = nil)
@state = :closed
if error_code
error = StreamError.new("Stream closed!", error_code)
end
self.close(error)
return self
end
|
#closed? ⇒ Boolean
148
149
150
|
# File 'lib/protocol/http2/stream.rb', line 148
def closed?
@state == :closed
end
|
#consume_remote_window(frame) ⇒ Object
201
202
203
204
205
|
# File 'lib/protocol/http2/stream.rb', line 201
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.
408
409
410
|
# File 'lib/protocol/http2/stream.rb', line 408
def create_push_promise_stream()
@connection.create_push_promise_stream
end
|
#ignore_data(frame) ⇒ Object
325
326
327
|
# File 'lib/protocol/http2/stream.rb', line 325
def ignore_data(frame)
end
|
#ignore_reset_stream(frame) ⇒ Object
355
356
357
|
# File 'lib/protocol/http2/stream.rb', line 355
def ignore_reset_stream(frame)
end
|
#inspect ⇒ Object
443
444
445
|
# File 'lib/protocol/http2/stream.rb', line 443
def inspect
"\#<#{self.class} id=#{@id} state=#{@state}>"
end
|
#maximum_frame_size ⇒ Object
136
137
138
|
# File 'lib/protocol/http2/stream.rb', line 136
def maximum_frame_size
@connection.available_frame_size
end
|
#open! ⇒ Object
236
237
238
239
240
241
242
243
244
|
# File 'lib/protocol/http2/stream.rb', line 236
def open!
if @state == :idle
@state = :open
else
raise ProtocolError, "Cannot open stream in state: #{@state}"
end
return self
end
|
#parent=(stream) ⇒ Object
123
124
125
|
# File 'lib/protocol/http2/stream.rb', line 123
def parent=(stream)
@dependency.parent = stream.dependency
end
|
#priority ⇒ Object
115
116
117
|
# File 'lib/protocol/http2/stream.rb', line 115
def priority
@dependency.priority
end
|
#priority=(priority) ⇒ Object
119
120
121
|
# File 'lib/protocol/http2/stream.rb', line 119
def priority= priority
@dependency.priority = priority
end
|
#process_data(frame) ⇒ String
Returns the data that was received.
321
322
323
|
# File 'lib/protocol/http2/stream.rb', line 321
def process_data(frame)
frame.unpack
end
|
273
274
275
276
277
278
279
280
281
282
|
# File 'lib/protocol/http2/stream.rb', line 273
def (frame)
priority, data = frame.unpack
if priority
@dependency.process_priority(priority)
end
@connection.(data)
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.
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
# File 'lib/protocol/http2/stream.rb', line 330
def receive_data(frame)
if @state == :open
update_local_window(frame)
if frame.end_stream?
@state = :half_closed_remote
end
process_data(frame)
elsif @state == :half_closed_local
update_local_window(frame)
process_data(frame)
if frame.end_stream?
close!
end
elsif self.closed?
ignore_data(frame)
else
self.send_reset_stream(Error::STREAM_CLOSED)
end
end
|
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/protocol/http2/stream.rb', line 288
def (frame)
if @state == :idle
if frame.end_stream?
@state = :half_closed_remote
else
open!
end
(frame)
elsif @state == :reserved_remote
(frame)
@state = :half_closed_local
elsif @state == :open
(frame)
if frame.end_stream?
@state = :half_closed_remote
end
elsif @state == :half_closed_local
(frame)
if frame.end_stream?
close!
end
elsif self.closed?
(frame)
else
self.send_reset_stream(Error::STREAM_CLOSED)
end
end
|
#receive_push_promise(frame) ⇒ Object
432
433
434
435
436
437
438
439
440
441
|
# File 'lib/protocol/http2/stream.rb', line 432
def receive_push_promise(frame)
promised_stream_id, data = frame.unpack
= @connection.(data)
stream = self.accept_push_promise_stream(promised_stream_id, )
stream.parent = self
stream.reserved_remote!
return stream,
end
|
#receive_reset_stream(frame) ⇒ Object
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
# File 'lib/protocol/http2/stream.rb', line 359
def receive_reset_stream(frame)
if @state == :idle
raise ProtocolError, "Cannot receive reset stream in state: #{@state}!"
elsif self.closed?
ignore_reset_stream(frame)
else
error_code = frame.unpack
close!(error_code)
return error_code
end
end
|
#reserved_local! ⇒ Object
391
392
393
394
395
396
397
|
# File 'lib/protocol/http2/stream.rb', line 391
def reserved_local!
if @state == :idle
@state = :reserved_local
else
raise ProtocolError, "Cannot reserve stream in state: #{@state}"
end
end
|
#reserved_remote! ⇒ Object
399
400
401
402
403
404
405
|
# File 'lib/protocol/http2/stream.rb', line 399
def reserved_remote!
if @state == :idle
@state = :reserved_remote
else
raise ProtocolError, "Cannot reserve stream in state: #{@state}"
end
end
|
#send_data(*arguments, **options) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/protocol/http2/stream.rb', line 218
def send_data(*arguments, **options)
if @state == :open
frame = write_data(*arguments, **options)
if frame.end_stream?
@state = :half_closed_local
end
elsif @state == :half_closed_remote
frame = write_data(*arguments, **options)
if frame.end_stream?
close!
end
else
raise ProtocolError, "Cannot send data in state: #{@state}"
end
end
|
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.
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
|
# File 'lib/protocol/http2/stream.rb', line 171
def (*args)
if @state == :idle
frame = (*args)
if frame.end_stream?
@state = :half_closed_local
else
open!
end
elsif @state == :reserved_local
frame = (*args)
@state = :half_closed_remote
elsif @state == :open
frame = (*args)
if frame.end_stream?
@state = :half_closed_local
end
elsif @state == :half_closed_remote
frame = (*args)
if frame.end_stream?
close!
end
else
raise ProtocolError, "Cannot send headers in state: #{@state}"
end
end
|
152
153
154
|
# File 'lib/protocol/http2/stream.rb', line 152
def
@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.
414
415
416
417
418
419
420
421
422
423
424
425
|
# File 'lib/protocol/http2/stream.rb', line 414
def send_push_promise()
if @state == :open or @state == :half_closed_remote
promised_stream = self.create_push_promise_stream()
promised_stream.reserved_local!
write_push_promise(promised_stream.id, )
return promised_stream
else
raise ProtocolError, "Cannot send push promise in state: #{@state}"
end
end
|
#send_reset_stream(error_code = 0) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/protocol/http2/stream.rb', line 260
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 send reset stream (#{error_code}) in state: #{@state}"
end
end
|
#to_s ⇒ Object
447
448
449
|
# File 'lib/protocol/http2/stream.rb', line 447
def to_s
inspect
end
|
#weight ⇒ Object
111
112
113
|
# File 'lib/protocol/http2/stream.rb', line 111
def weight
@dependency.weight
end
|
#write_frame(frame) ⇒ Object
140
141
142
|
# File 'lib/protocol/http2/stream.rb', line 140
def write_frame(frame)
@connection.write_frame(frame)
end
|