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: 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
Class Method Summary
collapse
Instance Method Summary
collapse
-
#accept_push_promise_stream(stream_id, headers) ⇒ Object
Override this function to implement your own push promise logic.
-
#active? ⇒ Boolean
-
#add_child(stream) ⇒ Object
-
#close(error = nil) ⇒ Object
The stream is being closed because the connection is being closed.
-
#close!(error_code = nil) ⇒ Object
Transition the stream into the closed state.
-
#closed? ⇒ Boolean
-
#consume_remote_window(frame) ⇒ Object
-
#create_push_promise_stream(headers) ⇒ Object
Override this function to implement your own push promise logic.
-
#exclusive_child(stream) ⇒ Object
-
#ignore_data(frame) ⇒ Object
-
#ignore_reset_stream(frame) ⇒ Object
-
#initialize(connection, id, local_window, remote_window, state = :idle, dependent_id = 0, weight = 16, children = nil) ⇒ Stream
constructor
A new instance of Stream.
-
#inspect ⇒ Object
-
#maximum_frame_size ⇒ Object
-
#open! ⇒ Object
-
#parent(id = @dependent_id) ⇒ Object
-
#parent=(stream) ⇒ Object
-
#priority(exclusive = false) ⇒ Object
The current local priority of the stream.
-
#priority=(priority) ⇒ Object
Change the priority of the stream both locally and remotely.
-
#process_data(frame) ⇒ String
The data that was received.
-
#process_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
-
#remove_child(stream) ⇒ Object
-
#reserved_local! ⇒ Object
-
#reserved_remote! ⇒ Object
-
#send_data(*arguments, **options) ⇒ 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_priority(priority) ⇒ Object
-
#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
-
#to_s ⇒ Object
-
#write_frame(frame) ⇒ Object
#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, local_window, remote_window, state = :idle, dependent_id = 0, weight = 16, children = nil) ⇒ Stream
Returns a new instance of Stream.
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/protocol/http2/stream.rb', line 114
def initialize(connection, id, local_window, remote_window, state = :idle, dependent_id = 0, weight = 16, children = nil)
@connection = connection
@id = id
@local_window = local_window
@remote_window = remote_window
@state = state
if active?
connnection.active(self)
end
@dependent_id = dependent_id
@weight = weight
@children = children
end
|
Instance Attribute Details
#children ⇒ Object
Cache of dependent children.
136
137
138
|
# File 'lib/protocol/http2/stream.rb', line 136
def children
@children
end
|
#connection ⇒ Object
The connection this stream belongs to.
139
140
141
|
# File 'lib/protocol/http2/stream.rb', line 139
def connection
@connection
end
|
#dependent_id ⇒ Object
The stream id that this stream depends on, according to the priority.
148
149
150
|
# File 'lib/protocol/http2/stream.rb', line 148
def dependent_id
@dependent_id
end
|
#id ⇒ Object
Stream ID (odd for client initiated streams, even otherwise).
142
143
144
|
# File 'lib/protocol/http2/stream.rb', line 142
def id
@id
end
|
#local_window ⇒ Object
Returns the value of attribute local_window.
153
154
155
|
# File 'lib/protocol/http2/stream.rb', line 153
def local_window
@local_window
end
|
#remote_window ⇒ Object
Returns the value of attribute remote_window.
154
155
156
|
# File 'lib/protocol/http2/stream.rb', line 154
def remote_window
@remote_window
end
|
#state ⇒ Object
Stream state, e.g. ‘idle`, `closed`.
145
146
147
|
# File 'lib/protocol/http2/stream.rb', line 145
def state
@state
end
|
#weight ⇒ Object
The weight of the stream relative to other siblings.
151
152
153
|
# File 'lib/protocol/http2/stream.rb', line 151
def weight
@weight
end
|
Class Method Details
.accept(connection, id) ⇒ Object
106
107
108
109
110
111
112
|
# File 'lib/protocol/http2/stream.rb', line 106
def self.accept(connection, id)
if stream = connection.streams[id]
self.replace(stream)
else
self.create(connection, id)
end
end
|
.create(connection, id = connection.next_stream_id) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/protocol/http2/stream.rb', line 77
def self.create(connection, id = connection.next_stream_id)
local_window = Window.new(connection.local_settings.initial_window_size)
remote_window = Window.new(connection.remote_settings.initial_window_size)
stream = self.new(connection, id, local_window, remote_window)
connection.streams[id] = stream
stream.parent.add_child(stream)
return stream
end
|
.replace(stream) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/protocol/http2/stream.rb', line 90
def self.replace(stream)
connection = stream.connection
stream.parent.remove_child(stream)
stream = self.new(
connection, stream.id, stream.local_window, stream.remote_window,
stream.state, stream.dependent_id, stream.weight, stream.children
)
connection.streams[stream.id] = stream
stream.parent.add_child(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.
532
533
534
|
# File 'lib/protocol/http2/stream.rb', line 532
def accept_push_promise_stream(stream_id, )
@connection.accept_push_promise_stream(stream_id)
end
|
#active? ⇒ Boolean
223
224
225
|
# File 'lib/protocol/http2/stream.rb', line 223
def active?
@state != :closed && @state != :idle
end
|
#add_child(stream) ⇒ Object
156
157
158
159
|
# File 'lib/protocol/http2/stream.rb', line 156
def add_child(stream)
@children ||= {}
@children[stream.id] = stream
end
|
#close(error = nil) ⇒ Object
The stream is being closed because the connection is being closed.
212
213
|
# File 'lib/protocol/http2/stream.rb', line 212
def close(error = nil)
end
|
#close!(error_code = nil) ⇒ Object
Transition the stream into the closed state.
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/protocol/http2/stream.rb', line 328
def close!(error_code = nil)
@state = :closed
@connection.deactivate(self)
self.parent&.remove_child(self)
if error_code
error = StreamError.new("Stream closed!", error_code)
end
self.close(error)
return self
end
|
#closed? ⇒ Boolean
227
228
229
|
# File 'lib/protocol/http2/stream.rb', line 227
def closed?
@state == :closed
end
|
#consume_remote_window(frame) ⇒ Object
280
281
282
283
284
|
# File 'lib/protocol/http2/stream.rb', line 280
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.
512
513
514
|
# File 'lib/protocol/http2/stream.rb', line 512
def create_push_promise_stream()
@connection.create_push_promise_stream
end
|
#exclusive_child(stream) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/protocol/http2/stream.rb', line 165
def exclusive_child(stream)
stream.children = @children
@children.each_value do |child|
child.dependent_id = stream.id
end
@children = {stream.id => stream}
stream.dependent_id = @id
end
|
#ignore_data(frame) ⇒ Object
408
409
410
|
# File 'lib/protocol/http2/stream.rb', line 408
def ignore_data(frame)
end
|
#ignore_reset_stream(frame) ⇒ Object
457
458
459
|
# File 'lib/protocol/http2/stream.rb', line 457
def ignore_reset_stream(frame)
end
|
#inspect ⇒ Object
547
548
549
|
# File 'lib/protocol/http2/stream.rb', line 547
def inspect
"\#<#{self.class} id=#{@id} state=#{@state}>"
end
|
#maximum_frame_size ⇒ Object
215
216
217
|
# File 'lib/protocol/http2/stream.rb', line 215
def maximum_frame_size
@connection.available_frame_size
end
|
#open! ⇒ Object
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/protocol/http2/stream.rb', line 315
def open!
if @state == :idle
@state = :open
@connection.activate(self)
else
raise ProtocolError, "Cannot open stream in state: #{@state}"
end
return self
end
|
#parent(id = @dependent_id) ⇒ Object
177
178
179
|
# File 'lib/protocol/http2/stream.rb', line 177
def parent(id = @dependent_id)
@connection[id] || @connection.accept_stream(id)
end
|
#parent=(stream) ⇒ Object
181
182
183
184
185
186
187
|
# File 'lib/protocol/http2/stream.rb', line 181
def parent= stream
self.parent&.remove_child(self)
@dependent_id = stream.id
stream.add_child(self)
end
|
#priority(exclusive = false) ⇒ Object
The current local priority of the stream.
445
446
447
|
# File 'lib/protocol/http2/stream.rb', line 445
def priority(exclusive = false)
Priority.new(exclusive, @dependent_id, @weight)
end
|
#priority=(priority) ⇒ Object
Change the priority of the stream both locally and remotely.
439
440
441
442
|
# File 'lib/protocol/http2/stream.rb', line 439
def priority= priority
send_priority(priority)
process_priority(priority)
end
|
#process_data(frame) ⇒ String
Returns the data that was received.
404
405
406
|
# File 'lib/protocol/http2/stream.rb', line 404
def process_data(frame)
frame.unpack
end
|
#process_priority(priority) ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/protocol/http2/stream.rb', line 189
def process_priority priority
dependent_id = priority.stream_dependency
if dependent_id == @id
raise ProtocolError, "Stream priority for stream id #{@id} cannot depend on itself!"
end
@weight = priority.weight
if priority.exclusive
self.parent&.remove_child(self)
self.parent(dependent_id).exclusive_child(self)
elsif dependent_id != @dependent_id
self.parent&.remove_child(self)
@dependent_id = dependent_id
self.parent.add_child(self)
end
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.
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
# File 'lib/protocol/http2/stream.rb', line 413
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
|
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/protocol/http2/stream.rb', line 371
def (frame)
if @state == :idle
if frame.end_stream?
@state = :half_closed_remote
else
open!
end
return (frame)
elsif @state == :reserved_remote
@state = :half_closed_local
return (frame)
elsif @state == :open
if frame.end_stream?
@state = :half_closed_remote
end
return (frame)
elsif @state == :half_closed_local
if frame.end_stream?
close!
end
return (frame)
elsif self.closed?
(frame)
else
self.send_reset_stream(Error::STREAM_CLOSED)
end
end
|
#receive_priority(frame) ⇒ Object
453
454
455
|
# File 'lib/protocol/http2/stream.rb', line 453
def receive_priority(frame)
self.process_priority(frame.unpack)
end
|
#receive_push_promise(frame) ⇒ Object
536
537
538
539
540
541
542
543
544
545
|
# File 'lib/protocol/http2/stream.rb', line 536
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
# File 'lib/protocol/http2/stream.rb', line 461
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
|
#remove_child(stream) ⇒ Object
161
162
163
|
# File 'lib/protocol/http2/stream.rb', line 161
def remove_child(stream)
@children.delete(stream.id)
end
|
#reserved_local! ⇒ Object
493
494
495
496
497
498
499
500
|
# File 'lib/protocol/http2/stream.rb', line 493
def reserved_local!
if @state == :idle
@state = :reserved_local
@connection.activate(self)
else
raise ProtocolError, "Cannot reserve stream in state: #{@state}"
end
end
|
#reserved_remote! ⇒ Object
502
503
504
505
506
507
508
509
|
# File 'lib/protocol/http2/stream.rb', line 502
def reserved_remote!
if @state == :idle
@state = :reserved_remote
@connection.activate(self)
else
raise ProtocolError, "Cannot reserve stream in state: #{@state}"
end
end
|
#send_data(*arguments, **options) ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/protocol/http2/stream.rb', line 297
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.
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
278
|
# File 'lib/protocol/http2/stream.rb', line 250
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
|
231
232
233
|
# File 'lib/protocol/http2/stream.rb', line 231
def
@state == :idle or @state == :reserved_local or @state == :open or @state == :half_closed_remote
end
|
#send_priority(priority) ⇒ Object
449
450
451
|
# File 'lib/protocol/http2/stream.rb', line 449
def send_priority(priority)
@connection.send_priority(@id, priority)
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.
518
519
520
521
522
523
524
525
526
527
528
529
|
# File 'lib/protocol/http2/stream.rb', line 518
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
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'lib/protocol/http2/stream.rb', line 343
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
551
552
553
|
# File 'lib/protocol/http2/stream.rb', line 551
def to_s
inspect
end
|
#write_frame(frame) ⇒ Object
219
220
221
|
# File 'lib/protocol/http2/stream.rb', line 219
def write_frame(frame)
@connection.write_frame(frame)
end
|