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
-
#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
-
#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.
-
#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(*args) ⇒ 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
-
#update_priority(priority) ⇒ Object
-
#write_frame(frame) ⇒ Object
#available_frame_size, #available_size, #consume_local_window, #consume_window, #receive_window_update, #send_window_update, #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.
490
491
492
|
# File 'lib/protocol/http2/stream.rb', line 490
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.
313
314
315
316
317
318
319
320
321
|
# File 'lib/protocol/http2/stream.rb', line 313
def close!(error_code = nil)
@state = :closed
if error_code
error = StreamError.new("Stream closed!", error_code)
end
self.close(error)
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.
470
471
472
|
# File 'lib/protocol/http2/stream.rb', line 470
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
|
#inspect ⇒ Object
505
506
507
|
# File 'lib/protocol/http2/stream.rb', line 505
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
|
#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.
412
413
414
|
# File 'lib/protocol/http2/stream.rb', line 412
def priority(exclusive = false)
Priority.new(exclusive, @dependent_id, @weight)
end
|
#priority=(priority) ⇒ Object
Change the priority of the stream both locally and remotely.
406
407
408
409
|
# File 'lib/protocol/http2/stream.rb', line 406
def priority= priority
send_priority(priority)
update_priority(priority)
end
|
#process_data(frame) ⇒ String
Returns the data that was received.
378
379
380
|
# File 'lib/protocol/http2/stream.rb', line 378
def process_data(frame)
frame.unpack
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.
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/protocol/http2/stream.rb', line 383
def receive_data(frame)
if @state == :open
consume_local_window(frame)
if frame.end_stream?
@state = :half_closed_remote
end
process_data(frame)
elsif @state == :half_closed_local
consume_local_window(frame)
process_data(frame)
if frame.end_stream?
close!
end
else
raise ProtocolError, "Cannot receive data in state: #{@state}"
end
end
|
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
# File 'lib/protocol/http2/stream.rb', line 347
def (frame)
if @state == :idle
if frame.end_stream?
@state = :half_closed_remote
else
@state = :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)
else
raise ProtocolError, "Cannot receive headers in state: #{@state}"
end
end
|
#receive_priority(frame) ⇒ Object
420
421
422
|
# File 'lib/protocol/http2/stream.rb', line 420
def receive_priority(frame)
self.update_priority(frame.unpack)
end
|
#receive_push_promise(frame) ⇒ Object
494
495
496
497
498
499
500
501
502
503
|
# File 'lib/protocol/http2/stream.rb', line 494
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
424
425
426
427
428
429
430
431
432
433
434
|
# File 'lib/protocol/http2/stream.rb', line 424
def receive_reset_stream(frame)
if @state != :idle and @state != :closed
error_code = frame.unpack
close!(error_code)
return error_code
else
raise ProtocolError, "Cannot reset stream in state: #{@state}"
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
453
454
455
456
457
458
459
|
# File 'lib/protocol/http2/stream.rb', line 453
def reserved_local!
if @state == :idle
@state = :reserved_local
else
raise ProtocolError, "Cannot reserve stream in state: #{@state}"
end
end
|
#reserved_remote! ⇒ Object
461
462
463
464
465
466
467
|
# File 'lib/protocol/http2/stream.rb', line 461
def reserved_remote!
if @state == :idle
@state = :reserved_remote
else
raise ProtocolError, "Cannot reserve stream in state: #{@state}"
end
end
|
#send_data(*args) ⇒ 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(*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
|
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
@state = :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
416
417
418
|
# File 'lib/protocol/http2/stream.rb', line 416
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.
476
477
478
479
480
481
482
483
484
485
486
487
|
# File 'lib/protocol/http2/stream.rb', line 476
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
323
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/protocol/http2/stream.rb', line 323
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
|
#update_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 update_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
|
#write_frame(frame) ⇒ Object
215
216
217
|
# File 'lib/protocol/http2/stream.rb', line 215
def write_frame(frame)
@connection.write_frame(frame)
end
|