Class: Protocol::HTTP2::Connection

Inherits:
Object
  • Object
show all
Includes:
FlowControl
Defined in:
lib/protocol/http2/connection.rb

Direct Known Subclasses

Client, Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FlowControl

#available_frame_size, #consume_local_window, #consume_remote_window, #send_window_update

Constructor Details

#initialize(framer, local_stream_id) ⇒ Connection

Returns a new instance of Connection.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/protocol/http2/connection.rb', line 31

def initialize(framer, local_stream_id)
	@state = :new
	@streams = {}
	@closed = 0
	
	@framer = framer
	@local_stream_id = local_stream_id
	@remote_stream_id = 0
	
	@local_settings = PendingSettings.new
	@remote_settings = Settings.new
	
	@decoder = HPACK::Context.new
	@encoder = HPACK::Context.new
	
	@local_window = Window.new(@local_settings.initial_window_size)
	@remote_window = Window.new(@remote_settings.initial_window_size)
end

Instance Attribute Details

#framerObject (readonly)

Returns the value of attribute framer.



63
64
65
# File 'lib/protocol/http2/connection.rb', line 63

def framer
  @framer
end

#local_settingsObject

Current settings value for local and peer



69
70
71
# File 'lib/protocol/http2/connection.rb', line 69

def local_settings
  @local_settings
end

#local_windowObject (readonly)

Our window for receiving data. When we receive data, it reduces this window. If the window gets too small, we must send a window update.



74
75
76
# File 'lib/protocol/http2/connection.rb', line 74

def local_window
  @local_window
end

#remote_settingsObject

Returns the value of attribute remote_settings.



70
71
72
# File 'lib/protocol/http2/connection.rb', line 70

def remote_settings
  @remote_settings
end

#remote_windowObject (readonly)

Our window for sending data. When we send data, it reduces this window.



77
78
79
# File 'lib/protocol/http2/connection.rb', line 77

def remote_window
  @remote_window
end

#stateObject

Connection state (:new, :open, :closed).



66
67
68
# File 'lib/protocol/http2/connection.rb', line 66

def state
  @state
end

#streamsObject (readonly)

Returns the value of attribute streams.



122
123
124
# File 'lib/protocol/http2/connection.rb', line 122

def streams
  @streams
end

Instance Method Details

#accept_push_promise_stream(stream_id, &block) ⇒ Object

Accept an incoming push promise from the other side of the connection. On the client side, we accept push promise streams. On the server side, existing streams create push promise streams.



305
306
307
# File 'lib/protocol/http2/connection.rb', line 305

def accept_push_promise_stream(stream_id, &block)
	accept_stream(stream_id, &block)
end

#accept_stream(stream_id, &block) ⇒ Object

Accept an incoming stream from the other side of the connnection. On the server side, we accept requests.



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

def accept_stream(stream_id, &block)
	unless valid_remote_stream_id?(stream_id)
		raise ProtocolError, "Invalid remote stream id: #{stream_id}"
	end
	
	if stream_id <= @remote_stream_id
		raise ProtocolError, "Invalid stream id: #{stream_id} <= #{@remote_stream_id}!"
	end
	
	@remote_stream_id = stream_id
	
	create_stream(stream_id, &block)
end

#active_stream_countObject



87
88
89
# File 'lib/protocol/http2/connection.rb', line 87

def active_stream_count
	@streams.size - @closed
end

#closeObject



99
100
101
# File 'lib/protocol/http2/connection.rb', line 99

def close
	@framer.close
end

#closed?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/protocol/http2/connection.rb', line 79

def closed?
	@state == :closed
end

#connection_error!(error, message) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/protocol/http2/connection.rb', line 91

def connection_error!(error, message)
	return if @framer.closed?
	
	send_goaway(error, message)
	
	self.close
end

#create_push_promise_stream(&block) ⇒ Object



326
327
328
# File 'lib/protocol/http2/connection.rb', line 326

def create_push_promise_stream(&block)
	create_stream(&block)
end

#create_stream(stream_id = next_stream_id, &block) ⇒ Stream

Create a stream, defaults to an outgoing stream. On the client side, we create requests.

Returns:

  • (Stream)

    the created stream.



312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/protocol/http2/connection.rb', line 312

def create_stream(stream_id = next_stream_id, &block)
	if block_given?
		yield(stream_id)
	else
		return Stream.new(self, stream_id)
	end
	
	if stream = @streams[stream_id]
		return stream
	else
		raise ProtocolError, "Stream creation failed!"
	end
end

#decode_headers(data) ⇒ Object



109
110
111
# File 'lib/protocol/http2/connection.rb', line 109

def decode_headers(data)
	HPACK::Decompressor.new(data, @decoder).decode
end

#encode_headers(headers, buffer = String.new.b) ⇒ Object



103
104
105
106
107
# File 'lib/protocol/http2/connection.rb', line 103

def encode_headers(headers, buffer = String.new.b)
	HPACK::Compressor.new(buffer, @encoder).encode(headers)
	
	return buffer
end

#idObject



50
51
52
# File 'lib/protocol/http2/connection.rb', line 50

def id
	0
end

#maximum_concurrent_streamsObject



59
60
61
# File 'lib/protocol/http2/connection.rb', line 59

def maximum_concurrent_streams
	[@local_settings.maximum_concurrent_streams, @remote_settings.maximum_concurrent_streams].min
end

#maximum_frame_sizeObject

The size of a frame payload is limited by the maximum size that a receiver advertises in the SETTINGS_MAX_FRAME_SIZE setting.



55
56
57
# File 'lib/protocol/http2/connection.rb', line 55

def maximum_frame_size
	@remote_settings.maximum_frame_size
end

#next_stream_idObject

Streams are identified with an unsigned 31-bit integer. Streams initiated by a client MUST use odd-numbered stream identifiers; those initiated by the server MUST use even-numbered stream identifiers. A stream identifier of zero (0x0) is used for connection control messages; the stream identifier of zero cannot be used to establish a new stream.



114
115
116
117
118
119
120
# File 'lib/protocol/http2/connection.rb', line 114

def next_stream_id
	id = @local_stream_id
	
	@local_stream_id += 2
	
	return id
end

#open!Object



239
240
241
242
243
# File 'lib/protocol/http2/connection.rb', line 239

def open!
	@state = :open
	
	return self
end

#process_settings(frame) ⇒ Boolean

In addition to changing the flow-control window for streams that are not yet active, a SETTINGS frame can alter the initial flow-control window size for streams with active flow-control windows (that is, streams in the “open” or “half-closed (remote)” state). When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST adjust the size of all stream flow-control windows that it maintains by the difference between the new value and the old value.

Returns:

  • (Boolean)

    whether the frame was an acknowledgement



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/protocol/http2/connection.rb', line 216

def process_settings(frame)
	if frame.acknowledgement?
		# The remote end has confirmed the settings have been received:
		changes = @local_settings.acknowledge
		
		update_local_settings(changes)
		
		return true
	else
		# The remote end is updating the settings, we reply with acknowledgement:
		reply = frame.acknowledge
		
		write_frame(reply)
		
		changes = frame.unpack
		@remote_settings.update(changes)
		
		update_remote_settings(changes)
		
		return false
	end
end

#read_frameObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/protocol/http2/connection.rb', line 124

def read_frame
	frame = @framer.read_frame(@local_settings.maximum_frame_size)
	# puts "#{self.class} #{@state} read_frame: class=#{frame.class} flags=#{frame.flags} length=#{frame.length}"
	# puts "Windows: local_window=#{@local_window.inspect}; remote_window=#{@remote_window.inspect}"
	
	yield frame if block_given?
	
	frame.apply(self)
	
	return frame
rescue GoawayError
	# This is not a connection error. We are done.
	self.close
	
	raise
rescue ProtocolError => error
	connection_error!(error.code || PROTOCOL_ERROR, error.message)
	
	raise
rescue HPACK::CompressionError => error
	connection_error!(COMPRESSION_ERROR, error.message)
	
	raise
rescue
	connection_error!(PROTOCOL_ERROR, $!.message)
	
	raise
end

#receive_continuation(frame) ⇒ Object

Raises:



392
393
394
# File 'lib/protocol/http2/connection.rb', line 392

def receive_continuation(frame)
	raise ProtocolError, "Received unexpected continuation: #{frame.class}"
end

#receive_data(frame) ⇒ Object



272
273
274
275
276
277
278
279
280
# File 'lib/protocol/http2/connection.rb', line 272

def receive_data(frame)
	consume_local_window(frame)
	
	if stream = @streams[frame.stream_id]
		stream.receive_data(frame)
	else
		raise ProtocolError, "Cannot receive data for idle stream #{frame.stream_id}"
	end
end

#receive_frame(frame) ⇒ Object



396
397
398
# File 'lib/protocol/http2/connection.rb', line 396

def receive_frame(frame)
	# ignore.
end

#receive_goaway(frame) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/protocol/http2/connection.rb', line 171

def receive_goaway(frame)
	@state = :closed
	
	last_stream_id, error_code, message = frame.unpack
	
	if error_code != 0
		raise GoawayError.new(message, error_code)
	end
end

#receive_headers(frame) ⇒ Object

On the server side, starts a new request.



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/protocol/http2/connection.rb', line 331

def receive_headers(frame)
	if frame.stream_id == 0
		raise ProtocolError, "Cannot receive headers for stream 0!"
	end
	
	if stream = @streams[frame.stream_id]
		stream.receive_headers(frame)
	else
		if self.active_stream_count < self.maximum_concurrent_streams
			stream = accept_stream(frame.stream_id)
			stream.receive_headers(frame)
		else
			raise ProtocolError, "Exceeded maximum concurrent streams"
		end
	end
end

#receive_ping(frame) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/protocol/http2/connection.rb', line 256

def receive_ping(frame)
	if @state != :closed
		if frame.stream_id != 0
			raise ProtocolError, "Ping received for non-zero stream!"
		end
		
		unless frame.acknowledgement?
			reply = frame.acknowledge
			
			write_frame(reply)
		end
	else
		raise ProtocolError, "Cannot receive ping in state #{@state}"
	end
end

#receive_priority(frame) ⇒ Object

On the client and server side, sets the priority for an incoming stream.



349
350
351
352
353
354
355
356
# File 'lib/protocol/http2/connection.rb', line 349

def receive_priority(frame)
	if stream = @streams[frame.stream_id]
		stream.receive_priority(frame)
	else
		stream = accept_stream(frame.stream_id)
		stream.receive_priority(frame)
	end
end

#receive_push_promise(frame) ⇒ Object

Raises:



358
359
360
# File 'lib/protocol/http2/connection.rb', line 358

def receive_push_promise(frame)
	raise ProtocolError, "Unable to receive push promise!"
end

#receive_reset_stream(frame) ⇒ Object



362
363
364
365
366
367
368
# File 'lib/protocol/http2/connection.rb', line 362

def receive_reset_stream(frame)
	if stream = @streams[frame.stream_id]
		stream.receive_reset_stream(frame)
	else
		raise StreamClosed, "Cannot reset stream #{frame.stream_id}"
	end
end

#receive_settings(frame) ⇒ Object



245
246
247
248
249
250
251
252
253
254
# File 'lib/protocol/http2/connection.rb', line 245

def receive_settings(frame)
	if @state == :new
		# We transition to :open when we receive acknowledgement of first settings frame:
		open! if process_settings(frame)
	elsif @state != :closed
		process_settings(frame)
	else
		raise ProtocolError, "Cannot receive settings in state #{@state}"
	end
end

#receive_window_update(frame) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/protocol/http2/connection.rb', line 370

def receive_window_update(frame)
	if frame.connection?
		super
	elsif stream = @streams[frame.stream_id]
		begin
			stream.receive_window_update(frame)
		rescue ProtocolError => error
			stream.send_reset_stream(error.code)
		end
	else
		# Receiving any frame other than HEADERS or PRIORITY on a stream in this state MUST be treated as a connection error of type PROTOCOL_ERROR.
		raise ProtocolError, "Cannot update window of idle stream #{frame.stream_id}"
	end
end

#send_goaway(error_code = 0, message = "") ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/protocol/http2/connection.rb', line 162

def send_goaway(error_code = 0, message = "")
	frame = GoawayFrame.new
	frame.pack @remote_stream_id, error_code, message
	
	write_frame(frame)
	
	@state = :closed
end

#send_ping(data) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/protocol/http2/connection.rb', line 186

def send_ping(data)
	if @state != :closed
		frame = PingFrame.new
		frame.pack data
		
		write_frame(frame)
	else
		raise ProtocolError, "Cannot send ping in state #{@state}"
	end
end

#send_settings(changes) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/protocol/http2/connection.rb', line 153

def send_settings(changes)
	@local_settings.append(changes)
	
	frame = SettingsFrame.new
	frame.pack(changes)
	
	write_frame(frame)
end

#stream_closed(stream) ⇒ Object



83
84
85
# File 'lib/protocol/http2/connection.rb', line 83

def stream_closed(stream)
	@closed += 1
end

#update_local_settings(changes) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/protocol/http2/connection.rb', line 197

def update_local_settings(changes)
	capacity = @local_settings.initial_window_size
	
	@streams.each_value do |stream|
		stream.local_window.capacity = capacity
	end
end

#update_remote_settings(changes) ⇒ Object



205
206
207
208
209
210
211
# File 'lib/protocol/http2/connection.rb', line 205

def update_remote_settings(changes)
	capacity = @remote_settings.initial_window_size
	
	@streams.each_value do |stream|
		stream.remote_window.capacity = capacity
	end
end

#valid_remote_stream_id?Boolean

Returns:

  • (Boolean)


282
283
284
# File 'lib/protocol/http2/connection.rb', line 282

def valid_remote_stream_id?
	false
end

#window_updatedObject



385
386
387
388
389
390
# File 'lib/protocol/http2/connection.rb', line 385

def window_updated
	# This is very inefficient, but workable.
	@streams.each_value do |stream|
		stream.window_updated unless stream.closed?
	end
end

#write_frame(frame) ⇒ Object



181
182
183
184
# File 'lib/protocol/http2/connection.rb', line 181

def write_frame(frame)
	# puts "#{self.class} #{@state} write_frame: class=#{frame.class} flags=#{frame.flags} length=#{frame.length}"
	@framer.write_frame(frame)
end