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



132
133
134
# File 'lib/protocol/http2/stream.rb', line 132

def children
  @children
end

#connectionObject (readonly)

The connection this stream belongs to.



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

def connection
  @connection
end

#dependent_idObject

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

#idObject (readonly)

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

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

Returns the value of attribute remote_window.



150
151
152
# File 'lib/protocol/http2/stream.rb', line 150

def remote_window
  @remote_window
end

#stateObject

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



141
142
143
# File 'lib/protocol/http2/stream.rb', line 141

def state
  @state
end

#weightObject

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



467
468
469
# File 'lib/protocol/http2/stream.rb', line 467

def accept_push_promise_stream(stream_id, headers)
	@connection.accept_push_promise_stream(stream_id)
end

#active?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/protocol/http2/stream.rb', line 217

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.



206
207
# File 'lib/protocol/http2/stream.rb', line 206

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.



308
309
310
311
312
313
314
315
316
# File 'lib/protocol/http2/stream.rb', line 308

def close!(error_code = nil)
	@state = :closed
	
	if error_code
		error = StreamError.new("Stream closed!", error_code)
	end
	
	self.close(error)
end

#closed?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/protocol/http2/stream.rb', line 221

def closed?
	@state == :closed
end

#consume_remote_window(frame) ⇒ Object



271
272
273
274
275
# File 'lib/protocol/http2/stream.rb', line 271

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.



447
448
449
# File 'lib/protocol/http2/stream.rb', line 447

def create_push_promise_stream(headers)
	@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 do |child|
		child.dependent_id = stream.id
	end
	
	@children = {stream.id => stream}
	
	stream.dependent_id = @id
end

#inspectObject



482
483
484
# File 'lib/protocol/http2/stream.rb', line 482

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

#maximum_frame_sizeObject



209
210
211
# File 'lib/protocol/http2/stream.rb', line 209

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=(priority) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/protocol/http2/stream.rb', line 185

def priority= priority
	dependent_id = priority.stream_dependency
	
	if dependent_id == @id
		raise ProtocolError, "Stream priority for stream id #{@id} cannot depend on itself!"
	end
	
	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

#process_data(frame) ⇒ String

Returns the data that was received.

Returns:

  • (String)

    the data that was received.



373
374
375
# File 'lib/protocol/http2/stream.rb', line 373

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.



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/protocol/http2/stream.rb', line 378

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

#receive_headers(frame) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/protocol/http2/stream.rb', line 342

def receive_headers(frame)
	if @state == :idle
		if frame.end_stream?
			@state = :half_closed_remote
		else
			@state = :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)
	else
		raise ProtocolError, "Cannot receive headers in state: #{@state}"
	end
end

#receive_priority(frame) ⇒ Object



400
401
402
# File 'lib/protocol/http2/stream.rb', line 400

def receive_priority(frame)
	self.priority = frame.unpack
end

#receive_push_promise(frame) ⇒ Object



471
472
473
474
475
476
477
478
479
480
# File 'lib/protocol/http2/stream.rb', line 471

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



404
405
406
407
408
409
410
411
412
413
414
# File 'lib/protocol/http2/stream.rb', line 404

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



430
431
432
433
434
435
436
# File 'lib/protocol/http2/stream.rb', line 430

def reserved_local!
	if @state == :idle
		@state = :reserved_local
	else
		raise ProtocolError, "Cannot reserve stream in state: #{@state}"
	end
end

#reserved_remote!Object



438
439
440
441
442
443
444
# File 'lib/protocol/http2/stream.rb', line 438

def reserved_remote!
	if @state == :idle
		@state = :reserved_remote
	else
		raise ProtocolError, "Cannot reserve stream in state: #{@state}"
	end
end

#send_data(*args) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/protocol/http2/stream.rb', line 288

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

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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/protocol/http2/stream.rb', line 241

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


225
226
227
# File 'lib/protocol/http2/stream.rb', line 225

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

Parameters:

  • headers (Hash)

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



453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/protocol/http2/stream.rb', line 453

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



318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/protocol/http2/stream.rb', line 318

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

#write_frame(frame) ⇒ Object



213
214
215
# File 'lib/protocol/http2/stream.rb', line 213

def write_frame(frame)
	@connection.write_frame(frame)
end