Class: Protocol::HTTP2::Stream

Inherits:
Object
  • Object
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

Methods included from FlowControl

#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
	
	# Stream priority:
	@dependent_id = dependent_id
	@weight = weight
	
	# A cache of streams that have child.dependent_id = self.id
	@children = children
end

Instance Attribute Details

#childrenObject

Cache of dependent children.



136
137
138
# File 'lib/protocol/http2/stream.rb', line 136

def children
  @children
end

#connectionObject (readonly)

The connection this stream belongs to.



139
140
141
# File 'lib/protocol/http2/stream.rb', line 139

def connection
  @connection
end

#dependent_idObject

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

#idObject (readonly)

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_windowObject (readonly)

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_windowObject (readonly)

Returns the value of attribute remote_window.



154
155
156
# File 'lib/protocol/http2/stream.rb', line 154

def remote_window
  @remote_window
end

#stateObject

Stream state, e.g. ‘idle`, `closed`.



145
146
147
# File 'lib/protocol/http2/stream.rb', line 145

def state
  @state
end

#weightObject

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)
	
	# Create new stream:
	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
	)
	
	# Replace existing stream:
	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, headers)
	@connection.accept_push_promise_stream(stream_id)
end

#active?Boolean

Returns:

  • (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.

Parameters:

  • error_code (Integer) (defaults to: nil)

    the error code if the stream was closed due to a stream reset.



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

Returns:

  • (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(headers)
	@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)
	# Async.logger.warn(self) {"Received headers in state: #{@state}!"}
end

#ignore_reset_stream(frame) ⇒ Object



457
458
459
# File 'lib/protocol/http2/stream.rb', line 457

def ignore_reset_stream(frame)
	# Async.logger.warn(self) {"Received reset stream (#{error_code}) in state: #{@state}!"}
end

#inspectObject



547
548
549
# File 'lib/protocol/http2/stream.rb', line 547

def inspect
	"\#<#{self.class} id=#{@id} state=#{@state}>"
end

#maximum_frame_sizeObject



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.

Returns:

  • (String)

    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
		# 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 (Section 5.4.2) of type STREAM_CLOSED.
		self.send_reset_stream(Error::STREAM_CLOSED)
	end
end

#receive_headers(frame) ⇒ Object



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 receive_headers(frame)
	if @state == :idle
		if frame.end_stream?
			@state = :half_closed_remote
		else
			open!
		end
		
		return process_headers(frame)
	elsif @state == :reserved_remote
		@state = :half_closed_local
		
		return process_headers(frame)
	elsif @state == :open
		if frame.end_stream?
			@state = :half_closed_remote
		end
		
		return process_headers(frame)
	elsif @state == :half_closed_local
		if frame.end_stream?
			close!
		end
		
		return process_headers(frame)
	elsif self.closed?
		ignore_headers(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
	headers = @connection.decode_headers(data)
	
	stream = self.accept_push_promise_stream(promised_stream_id, headers)
	stream.parent = self
	stream.reserved_remote!
	
	return stream, headers
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
		# If a RST_STREAM frame identifying an idle stream is received, the recipient MUST treat this as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
		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

#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.



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 send_headers(*args)
	if @state == :idle
		frame = write_headers(*args)
		
		if frame.end_stream?
			@state = :half_closed_local
		else
			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

Returns:

  • (Boolean)


231
232
233
# File 'lib/protocol/http2/stream.rb', line 231

def send_headers?
	@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.

Parameters:

  • headers (Hash)

    contains a complete set of request header fields that the server attributes to the request.



518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/protocol/http2/stream.rb', line 518

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



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_sObject



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