Class: Protocol::HTTP2::Connection
- Inherits:
-
Object
- Object
- Protocol::HTTP2::Connection
show all
- Includes:
- FlowControl
- Defined in:
- lib/protocol/http2/connection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#[](id) ⇒ Object
-
#accept_push_promise_stream(stream_id, &block) ⇒ Object
Accept an incoming push promise from the other side of the connection.
-
#accept_stream(stream_id, &block) ⇒ Object
Accept an incoming stream from the other side of the connnection.
-
#active_streams ⇒ Object
-
#add_child(stream) ⇒ Object
-
#close(error = nil) ⇒ Object
Close the underlying framer and all streams.
-
#close! ⇒ Object
Transition the connection into the closed state.
-
#closed? ⇒ Boolean
-
#create_push_promise_stream(&block) ⇒ Object
-
#create_stream(id = next_stream_id, &block) ⇒ Stream
Create a stream, defaults to an outgoing stream.
-
#decode_headers(data) ⇒ Object
-
#encode_headers(headers, buffer = String.new.b) ⇒ Object
-
#exclusive_child(stream) ⇒ Object
-
#id ⇒ Object
-
#ignore_frame?(frame) ⇒ Boolean
-
#initialize(framer, local_stream_id) ⇒ Connection
constructor
A new instance of Connection.
-
#maximum_concurrent_streams ⇒ Object
-
#maximum_frame_size ⇒ Object
The size of a frame payload is limited by the maximum size that a receiver advertises in the SETTINGS_MAX_FRAME_SIZE setting.
-
#next_stream_id ⇒ Object
Streams are identified with an unsigned 31-bit integer.
-
#open! ⇒ Object
-
#parent ⇒ Object
-
#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).
-
#read_frame ⇒ Object
Reads one frame from the network and processes.
-
#receive_continuation(frame) ⇒ Object
-
#receive_data(frame) ⇒ Object
-
#receive_frame(frame) ⇒ Object
-
#receive_goaway(frame) ⇒ Object
-
#receive_headers(frame) ⇒ Object
On the server side, starts a new request.
-
#receive_ping(frame) ⇒ Object
-
#receive_priority(frame) ⇒ Object
Sets the priority for an incoming stream.
-
#receive_push_promise(frame) ⇒ Object
-
#receive_reset_stream(frame) ⇒ Object
-
#receive_settings(frame) ⇒ Object
-
#receive_window_update(frame) ⇒ Object
-
#remove_child(stream) ⇒ Object
-
#send_goaway(error_code = 0, message = "") ⇒ Object
Tell the remote end that the connection is being shut down.
-
#send_ping(data) ⇒ Object
-
#send_priority(stream_id, priority) ⇒ Object
-
#send_settings(changes) ⇒ Object
-
#update_local_settings(changes) ⇒ Object
-
#update_remote_settings(changes) ⇒ Object
-
#valid_remote_stream_id? ⇒ Boolean
-
#write_frame(frame) ⇒ Object
-
#write_frames {|@framer| ... } ⇒ Object
#available_frame_size, #available_size, #consume_local_window, #consume_remote_window, #consume_window, #send_window_update, #window_updated
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
49
50
|
# File 'lib/protocol/http2/connection.rb', line 31
def initialize(framer, local_stream_id)
super()
@state = :new
@streams = {}
@children = {}
@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
#children ⇒ Object
Returns the value of attribute children.
130
131
132
|
# File 'lib/protocol/http2/connection.rb', line 130
def children
@children
end
|
#framer ⇒ Object
Returns the value of attribute framer.
77
78
79
|
# File 'lib/protocol/http2/connection.rb', line 77
def framer
@framer
end
|
#local_settings ⇒ Object
Current settings value for local and peer
83
84
85
|
# File 'lib/protocol/http2/connection.rb', line 83
def local_settings
@local_settings
end
|
#local_window ⇒ Object
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.
88
89
90
|
# File 'lib/protocol/http2/connection.rb', line 88
def local_window
@local_window
end
|
#remote_settings ⇒ Object
Returns the value of attribute remote_settings.
84
85
86
|
# File 'lib/protocol/http2/connection.rb', line 84
def remote_settings
@remote_settings
end
|
#remote_stream_id ⇒ Object
The highest stream_id that has been successfully accepted by this connection.
94
95
96
|
# File 'lib/protocol/http2/connection.rb', line 94
def remote_stream_id
@remote_stream_id
end
|
#remote_window ⇒ Object
Our window for sending data. When we send data, it reduces this window.
91
92
93
|
# File 'lib/protocol/http2/connection.rb', line 91
def remote_window
@remote_window
end
|
#state ⇒ Object
Connection state (:new, :open, :closed).
80
81
82
|
# File 'lib/protocol/http2/connection.rb', line 80
def state
@state
end
|
#streams ⇒ Object
Returns the value of attribute streams.
129
130
131
|
# File 'lib/protocol/http2/connection.rb', line 129
def streams
@streams
end
|
Instance Method Details
#[](id) ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/protocol/http2/connection.rb', line 60
def [] id
if id.zero?
self
else
@streams[id]
end
end
|
#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.
348
349
350
|
# File 'lib/protocol/http2/connection.rb', line 348
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.
337
338
339
340
341
342
343
|
# File 'lib/protocol/http2/connection.rb', line 337
def accept_stream(stream_id, &block)
unless valid_remote_stream_id?(stream_id)
raise ProtocolError, "Invalid stream id: #{stream_id}"
end
create_stream(stream_id, &block)
end
|
#active_streams ⇒ Object
100
101
102
103
|
# File 'lib/protocol/http2/connection.rb', line 100
def active_streams
@streams.each_value.select(&:active?)
end
|
#add_child(stream) ⇒ Object
132
133
134
|
# File 'lib/protocol/http2/connection.rb', line 132
def add_child(stream)
@children[stream.id] = stream
end
|
#close(error = nil) ⇒ Object
Close the underlying framer and all streams.
106
107
108
109
110
|
# File 'lib/protocol/http2/connection.rb', line 106
def close(error = nil)
@framer.close
@streams.each_value{|stream| stream.close(error)}
end
|
#close! ⇒ Object
Transition the connection into the closed state.
199
200
201
202
203
|
# File 'lib/protocol/http2/connection.rb', line 199
def close!
@state = :closed
return self
end
|
#closed? ⇒ Boolean
96
97
98
|
# File 'lib/protocol/http2/connection.rb', line 96
def closed?
@state == :closed
end
|
#create_push_promise_stream(&block) ⇒ Object
363
364
365
|
# File 'lib/protocol/http2/connection.rb', line 363
def create_push_promise_stream(&block)
create_stream(&block)
end
|
#create_stream(id = next_stream_id, &block) ⇒ Stream
Create a stream, defaults to an outgoing stream. On the client side, we create requests.
355
356
357
358
359
360
361
|
# File 'lib/protocol/http2/connection.rb', line 355
def create_stream(id = next_stream_id, &block)
if block_given?
return yield(self, id)
else
return Stream.create(self, id)
end
end
|
116
117
118
|
# File 'lib/protocol/http2/connection.rb', line 116
def (data)
HPACK::Decompressor.new(data, @decoder, table_size_limit: @local_settings.).decode
end
|
112
113
114
|
# File 'lib/protocol/http2/connection.rb', line 112
def (, buffer = String.new.b)
HPACK::Compressor.new(buffer, @encoder, table_size_limit: @remote_settings.).encode()
end
|
#exclusive_child(stream) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/protocol/http2/connection.rb', line 140
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 = 0
end
|
#id ⇒ Object
52
53
54
|
# File 'lib/protocol/http2/connection.rb', line 52
def id
0
end
|
#ignore_frame?(frame) ⇒ Boolean
6.8. GOAWAY There is an inherent race condition between an endpoint starting new streams and the remote sending a GOAWAY frame. To deal with this case, the GOAWAY contains the stream identifier of the last peer-initiated stream that was or might be processed on the sending endpoint in this connection. For instance, if the server sends a GOAWAY frame, the identified stream is the highest-numbered stream initiated by the client. Once sent, the sender will ignore frames sent on streams initiated by the receiver if the stream has an identifier higher than the included last stream identifier. Receivers of a GOAWAY frame MUST NOT open additional streams on the connection, although a new connection can be established for new streams.
155
156
157
158
159
160
161
162
|
# File 'lib/protocol/http2/connection.rb', line 155
def ignore_frame?(frame)
if self.closed?
if valid_remote_stream_id?(frame.stream_id)
return frame.stream_id > @remote_stream_id
end
end
end
|
#maximum_concurrent_streams ⇒ Object
73
74
75
|
# File 'lib/protocol/http2/connection.rb', line 73
def maximum_concurrent_streams
[@local_settings.maximum_concurrent_streams, @remote_settings.maximum_concurrent_streams].min
end
|
#maximum_frame_size ⇒ Object
The size of a frame payload is limited by the maximum size that a receiver advertises in the SETTINGS_MAX_FRAME_SIZE setting.
69
70
71
|
# File 'lib/protocol/http2/connection.rb', line 69
def maximum_frame_size
@remote_settings.maximum_frame_size
end
|
#next_stream_id ⇒ Object
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.
121
122
123
124
125
126
127
|
# File 'lib/protocol/http2/connection.rb', line 121
def next_stream_id
id = @local_stream_id
@local_stream_id += 2
return id
end
|
#open! ⇒ Object
277
278
279
280
281
|
# File 'lib/protocol/http2/connection.rb', line 277
def open!
@state = :open
return self
end
|
#parent ⇒ Object
56
57
58
|
# File 'lib/protocol/http2/connection.rb', line 56
def parent
nil
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.
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/protocol/http2/connection.rb', line 254
def process_settings(frame)
if frame.acknowledgement?
changes = @local_settings.acknowledge
update_local_settings(changes)
return true
else
reply = frame.acknowledge
write_frame(reply)
changes = frame.unpack
@remote_settings.update(changes)
update_remote_settings(changes)
return false
end
end
|
#read_frame ⇒ Object
Reads one frame from the network and processes. Processing the frame updates the state of the connection and related streams. If the frame triggers an error, e.g. a protocol error, the connection will typically emit a goaway frame and re-raise the exception. You should continue processing frames until the underlying connection is closed.
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/protocol/http2/connection.rb', line 165
def read_frame
frame = @framer.read_frame(@local_settings.maximum_frame_size)
return if ignore_frame?(frame)
yield frame if block_given?
frame.apply(self)
return frame
rescue GoawayError => error
raise
rescue ProtocolError => error
send_goaway(error.code || PROTOCOL_ERROR, error.message)
raise
rescue HPACK::Error => error
send_goaway(COMPRESSION_ERROR, error.message)
raise
end
|
#receive_continuation(frame) ⇒ Object
440
441
442
|
# File 'lib/protocol/http2/connection.rb', line 440
def receive_continuation(frame)
raise ProtocolError, "Received unexpected continuation: #{frame.class}"
end
|
#receive_data(frame) ⇒ Object
321
322
323
324
325
326
327
328
329
|
# File 'lib/protocol/http2/connection.rb', line 321
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
444
445
446
|
# File 'lib/protocol/http2/connection.rb', line 444
def receive_frame(frame)
end
|
#receive_goaway(frame) ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/protocol/http2/connection.rb', line 215
def receive_goaway(frame)
@remote_stream_id, error_code, message = frame.unpack
self.close!
if error_code != 0
raise GoawayError.new(message, error_code)
end
end
|
On the server side, starts a new request.
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
# File 'lib/protocol/http2/connection.rb', line 368
def (frame)
stream_id = frame.stream_id
if stream_id.zero?
raise ProtocolError, "Cannot receive headers for stream 0!"
end
if stream = @streams[stream_id]
stream.(frame)
else
if stream_id <= @remote_stream_id
raise ProtocolError, "Invalid stream id: #{stream_id} <= #{@remote_stream_id}!"
end
if self.active_streams.count < self.maximum_concurrent_streams
stream = accept_stream(stream_id)
@remote_stream_id = stream_id
stream.(frame)
else
raise ProtocolError, "Exceeded maximum concurrent streams"
end
end
end
|
#receive_ping(frame) ⇒ Object
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/protocol/http2/connection.rb', line 305
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
Sets the priority for an incoming stream.
401
402
403
404
405
406
407
408
409
|
# File 'lib/protocol/http2/connection.rb', line 401
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
411
412
413
|
# File 'lib/protocol/http2/connection.rb', line 411
def receive_push_promise(frame)
raise ProtocolError, "Unable to receive push promise!"
end
|
#receive_reset_stream(frame) ⇒ Object
415
416
417
418
419
420
421
|
# File 'lib/protocol/http2/connection.rb', line 415
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
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/protocol/http2/connection.rb', line 283
def receive_settings(frame)
if @state == :new
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
# File 'lib/protocol/http2/connection.rb', line 423
def receive_window_update(frame)
if frame.connection?
super
self.consume_window
elsif stream = @streams[frame.stream_id]
begin
stream.receive_window_update(frame)
rescue ProtocolError => error
stream.send_reset_stream(error.code)
end
else
raise ProtocolError, "Cannot update window of idle stream #{frame.stream_id}"
end
end
|
#remove_child(stream) ⇒ Object
136
137
138
|
# File 'lib/protocol/http2/connection.rb', line 136
def remove_child(stream)
@children.delete(stream.id)
end
|
#send_goaway(error_code = 0, message = "") ⇒ Object
Tell the remote end that the connection is being shut down. If the ‘error_code` is 0, this is a graceful shutdown. The other end of the connection should not make any new streams, but existing streams may be completed.
206
207
208
209
210
211
212
213
|
# File 'lib/protocol/http2/connection.rb', line 206
def send_goaway(error_code = 0, message = "")
frame = GoawayFrame.new
frame.pack @remote_stream_id, error_code, message
write_frame(frame)
ensure
self.close!
end
|
#send_ping(data) ⇒ Object
294
295
296
297
298
299
300
301
302
303
|
# File 'lib/protocol/http2/connection.rb', line 294
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_priority(stream_id, priority) ⇒ Object
393
394
395
396
397
398
|
# File 'lib/protocol/http2/connection.rb', line 393
def send_priority(stream_id, priority)
frame = PriorityFrame.new(stream_id)
frame.pack(priority)
write_frame(frame)
end
|
#send_settings(changes) ⇒ Object
189
190
191
192
193
194
195
196
|
# File 'lib/protocol/http2/connection.rb', line 189
def send_settings(changes)
@local_settings.append(changes)
frame = SettingsFrame.new
frame.pack(changes)
write_frame(frame)
end
|
#update_local_settings(changes) ⇒ Object
235
236
237
238
239
240
241
|
# File 'lib/protocol/http2/connection.rb', line 235
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
243
244
245
246
247
248
249
|
# File 'lib/protocol/http2/connection.rb', line 243
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
331
332
333
|
# File 'lib/protocol/http2/connection.rb', line 331
def valid_remote_stream_id?
false
end
|
#write_frame(frame) ⇒ Object
227
228
229
|
# File 'lib/protocol/http2/connection.rb', line 227
def write_frame(frame)
@framer.write_frame(frame)
end
|
#write_frames {|@framer| ... } ⇒ Object
231
232
233
|
# File 'lib/protocol/http2/connection.rb', line 231
def write_frames
yield @framer
end
|