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
|
# 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
@dependent_id = dependent_id
@weight = weight
@children = children
end
|
Instance Attribute Details
#children ⇒ Object
Cache of dependent children.
132
133
134
|
# File 'lib/protocol/http2/stream.rb', line 132
def children
@children
end
|
#connection ⇒ Object
The connection this stream belongs to.
135
136
137
|
# File 'lib/protocol/http2/stream.rb', line 135
def connection
@connection
end
|
#dependent_id ⇒ Object
The stream id that this stream depends on, according to the priority.
144
145
146
|
# File 'lib/protocol/http2/stream.rb', line 144
def dependent_id
@dependent_id
end
|
#id ⇒ Object
Stream ID (odd for client initiated streams, even otherwise).
138
139
140
|
# File 'lib/protocol/http2/stream.rb', line 138
def id
@id
end
|
#local_window ⇒ Object
Returns the value of attribute local_window.
149
150
151
|
# File 'lib/protocol/http2/stream.rb', line 149
def local_window
@local_window
end
|
#remote_window ⇒ Object
Returns the value of attribute remote_window.
150
151
152
|
# File 'lib/protocol/http2/stream.rb', line 150
def remote_window
@remote_window
end
|
#state ⇒ Object
Stream state, e.g. ‘idle`, `closed`.
141
142
143
|
# File 'lib/protocol/http2/stream.rb', line 141
def state
@state
end
|
#weight ⇒ Object
The weight of the stream relative to other siblings.
147
148
149
|
# File 'lib/protocol/http2/stream.rb', line 147
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.
518
519
520
|
# File 'lib/protocol/http2/stream.rb', line 518
def accept_push_promise_stream(stream_id, )
@connection.accept_push_promise_stream(stream_id)
end
|
#active? ⇒ Boolean
219
220
221
|
# File 'lib/protocol/http2/stream.rb', line 219
def active?
@state != :closed && @state != :idle
end
|
#add_child(stream) ⇒ Object
152
153
154
155
|
# File 'lib/protocol/http2/stream.rb', line 152
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.
208
209
|
# File 'lib/protocol/http2/stream.rb', line 208
def close(error = nil)
end
|
#close!(error_code = nil) ⇒ Object
Transition the stream into the closed state.
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/protocol/http2/stream.rb', line 319
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
223
224
225
|
# File 'lib/protocol/http2/stream.rb', line 223
def closed?
@state == :closed
end
|
#consume_remote_window(frame) ⇒ Object
276
277
278
279
280
|
# File 'lib/protocol/http2/stream.rb', line 276
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.
498
499
500
|
# File 'lib/protocol/http2/stream.rb', line 498
def create_push_promise_stream()
@connection.create_push_promise_stream
end
|
#exclusive_child(stream) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/protocol/http2/stream.rb', line 161
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
396
397
398
|
# File 'lib/protocol/http2/stream.rb', line 396
def ignore_data(frame)
end
|
#ignore_reset_stream(frame) ⇒ Object
445
446
447
|
# File 'lib/protocol/http2/stream.rb', line 445
def ignore_reset_stream(frame)
end
|
#inspect ⇒ Object
533
534
535
|
# File 'lib/protocol/http2/stream.rb', line 533
def inspect
"\#<#{self.class} id=#{@id} state=#{@state}>"
end
|
#maximum_frame_size ⇒ Object
211
212
213
|
# File 'lib/protocol/http2/stream.rb', line 211
def maximum_frame_size
@connection.available_frame_size
end
|
#open! ⇒ Object
311
312
313
314
315
|
# File 'lib/protocol/http2/stream.rb', line 311
def open!
@state = :open
return self
end
|
#parent(id = @dependent_id) ⇒ Object
173
174
175
|
# File 'lib/protocol/http2/stream.rb', line 173
def parent(id = @dependent_id)
@connection[id] || @connection.accept_stream(id)
end
|
#parent=(stream) ⇒ Object
177
178
179
180
181
182
183
|
# File 'lib/protocol/http2/stream.rb', line 177
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.
433
434
435
|
# File 'lib/protocol/http2/stream.rb', line 433
def priority(exclusive = false)
Priority.new(exclusive, @dependent_id, @weight)
end
|
#priority=(priority) ⇒ Object
Change the priority of the stream both locally and remotely.
427
428
429
430
|
# File 'lib/protocol/http2/stream.rb', line 427
def priority= priority
send_priority(priority)
process_priority(priority)
end
|
#process_data(frame) ⇒ String
Returns the data that was received.
392
393
394
|
# File 'lib/protocol/http2/stream.rb', line 392
def process_data(frame)
frame.unpack
end
|
#process_priority(priority) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/protocol/http2/stream.rb', line 185
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.
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/protocol/http2/stream.rb', line 401
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
|
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# File 'lib/protocol/http2/stream.rb', line 359
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
441
442
443
|
# File 'lib/protocol/http2/stream.rb', line 441
def receive_priority(frame)
self.process_priority(frame.unpack)
end
|
#receive_push_promise(frame) ⇒ Object
522
523
524
525
526
527
528
529
530
531
|
# File 'lib/protocol/http2/stream.rb', line 522
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
# File 'lib/protocol/http2/stream.rb', line 449
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
157
158
159
|
# File 'lib/protocol/http2/stream.rb', line 157
def remove_child(stream)
@children.delete(stream.id)
end
|
#reserved_local! ⇒ Object
481
482
483
484
485
486
487
|
# File 'lib/protocol/http2/stream.rb', line 481
def reserved_local!
if @state == :idle
@state = :reserved_local
else
raise ProtocolError, "Cannot reserve stream in state: #{@state}"
end
end
|
#reserved_remote! ⇒ Object
489
490
491
492
493
494
495
|
# File 'lib/protocol/http2/stream.rb', line 489
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
# File 'lib/protocol/http2/stream.rb', line 293
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.
246
247
248
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
|
# File 'lib/protocol/http2/stream.rb', line 246
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
|
227
228
229
|
# File 'lib/protocol/http2/stream.rb', line 227
def
@state == :idle or @state == :reserved_local or @state == :open or @state == :half_closed_remote
end
|
#send_priority(priority) ⇒ Object
437
438
439
|
# File 'lib/protocol/http2/stream.rb', line 437
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.
504
505
506
507
508
509
510
511
512
513
514
515
|
# File 'lib/protocol/http2/stream.rb', line 504
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
331
332
333
334
335
336
337
338
339
340
341
342
|
# File 'lib/protocol/http2/stream.rb', line 331
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
537
538
539
|
# File 'lib/protocol/http2/stream.rb', line 537
def to_s
inspect
end
|
#write_frame(frame) ⇒ Object
215
216
217
|
# File 'lib/protocol/http2/stream.rb', line 215
def write_frame(frame)
@connection.write_frame(frame)
end
|