Class: Protocol::HTTP2::Connection

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

Direct Known Subclasses

Client, Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FlowControlled

#available_frame_size, #available_size, #consume_local_window, #consume_remote_window, #request_window_update, #send_window_update, #update_local_window, #window_updated

Constructor Details

#initialize(framer, local_stream_id) ⇒ Connection



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/protocol/http2/connection.rb', line 32

def initialize(framer, local_stream_id)
  super()
  
  @state = :new
  
  # Hash(Integer, Stream)
  @streams = {}
  
  # Hash(Integer, Dependency)
  @dependency = Dependency.new(self, 0)
  @dependencies = {0 => @dependency}
  
  @framer = framer
  
  # The next stream id to use:
  @local_stream_id = local_stream_id
  
  # The biggest remote stream id seen thus far:
  @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

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



148
149
150
# File 'lib/protocol/http2/connection.rb', line 148

def dependencies
  @dependencies
end

#framerObject (readonly)

Returns the value of attribute framer.



91
92
93
# File 'lib/protocol/http2/connection.rb', line 91

def framer
  @framer
end

#local_settingsObject

Current settings value for local and peer



97
98
99
# File 'lib/protocol/http2/connection.rb', line 97

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.



102
103
104
# File 'lib/protocol/http2/connection.rb', line 102

def local_window
  @local_window
end

#remote_settingsObject

Returns the value of attribute remote_settings.



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

def remote_settings
  @remote_settings
end

#remote_stream_idObject (readonly)

The highest stream_id that has been successfully accepted by this connection.



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

def remote_stream_id
  @remote_stream_id
end

#remote_windowObject (readonly)

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



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

def remote_window
  @remote_window
end

#stateObject

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



94
95
96
# File 'lib/protocol/http2/connection.rb', line 94

def state
  @state
end

#streamsObject (readonly)

Returns the value of attribute streams.



147
148
149
# File 'lib/protocol/http2/connection.rb', line 147

def streams
  @streams
end

Instance Method Details

#[](id) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/protocol/http2/connection.rb', line 74

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.



366
367
368
# File 'lib/protocol/http2/connection.rb', line 366

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.



355
356
357
358
359
360
361
# File 'lib/protocol/http2/connection.rb', line 355

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

#childrenObject



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

def children
  @dependency.streams
end

#client_stream_id?(id) ⇒ Boolean



428
429
430
# File 'lib/protocol/http2/connection.rb', line 428

def client_stream_id?(id)
  id.odd?
end

#close(error = nil) ⇒ Object

Close the underlying framer and all streams.



123
124
125
126
127
128
# File 'lib/protocol/http2/connection.rb', line 123

def close(error = nil)
  @framer.close
  # @framer = nil
  
  @streams.each_value{|stream| stream.close(error)}
end

#close!Object

Transition the connection into the closed state.



217
218
219
220
221
# File 'lib/protocol/http2/connection.rb', line 217

def close!
  @state = :closed
  
  return self
end

#closed?Boolean



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

def closed?
  @state == :closed
end

#closed_stream_id?(id) ⇒ Boolean



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/protocol/http2/connection.rb', line 436

def closed_stream_id?(id)
  if id.zero?
    # The connection "stream id" can never be closed:
    false
  elsif id.even?
    # Server-initiated streams are even.
    if @local_stream_id.even?
      id < @local_stream_id
    else
      id <= @remote_stream_id
    end
  elsif id.odd?
    # Client-initiated streams are odd.
    if @local_stream_id.odd?
      id < @local_stream_id
    else
      id <= @remote_stream_id
    end
  end
end

#consume_window(size = self.available_size) ⇒ Object

Traverse active streams in order of priority and allow them to consume the available flow-control window.



471
472
473
474
475
476
# File 'lib/protocol/http2/connection.rb', line 471

def consume_window(size = self.available_size)
  # Return if there is no window to consume:
  return unless size > 0
  
  @dependency.consume_window(size)
end

#create_push_promise_stream(&block) ⇒ Object



381
382
383
# File 'lib/protocol/http2/connection.rb', line 381

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.



373
374
375
376
377
378
379
# File 'lib/protocol/http2/connection.rb', line 373

def create_stream(id = next_stream_id, &block)
  if block_given?
    return yield(self, id)
  else
    return Stream.create(self, id)
  end
end

#decode_headers(data) ⇒ Object



134
135
136
# File 'lib/protocol/http2/connection.rb', line 134

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

#delete(id) ⇒ Object



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

def delete(id)
  @streams.delete(id)
  
  if dependency = @dependencies[id]
    dependency.delete! if dependency.irrelevant?
  end
end

#dependency_for(id) ⇒ Object

Fetch (or create) the flow control windows for the specified stream id.



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

def dependency_for(id)
  @dependencies.fetch(id) do
    dependency = Dependency.new(self, id)
    
    # TODO this might be irrelevant, if initially processing priority frame.
    @dependency.add_child(dependency)
    
    @dependencies[id] = dependency
  end
end

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



130
131
132
# File 'lib/protocol/http2/connection.rb', line 130

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

#idObject



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

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.



166
167
168
169
170
171
172
173
# File 'lib/protocol/http2/connection.rb', line 166

def ignore_frame?(frame)
  if self.closed?
    # puts "ignore_frame? #{frame.stream_id} -> #{valid_remote_stream_id?(frame.stream_id)} > #{@remote_stream_id}"
    if valid_remote_stream_id?(frame.stream_id)
      return frame.stream_id > @remote_stream_id
    end
  end
end

#maximum_concurrent_streamsObject



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

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.



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

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.



139
140
141
142
143
144
145
# File 'lib/protocol/http2/connection.rb', line 139

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

#open!Object



295
296
297
298
299
# File 'lib/protocol/http2/connection.rb', line 295

def open!
  @state = :open
  
  return self
end

#parentObject



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

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.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/protocol/http2/connection.rb', line 272

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

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.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/protocol/http2/connection.rb', line 176

def read_frame
  frame = @framer.read_frame(@local_settings.maximum_frame_size)
  # puts "#{self.class} #{@state} read_frame: class=#{frame.class} stream_id=#{frame.stream_id} flags=#{frame.flags} length=#{frame.length} (remote_stream_id=#{@remote_stream_id})"
  # puts "Windows: local_window=#{@local_window.inspect}; remote_window=#{@remote_window.inspect}"
  
  return if ignore_frame?(frame)
  
  yield frame if block_given?
  frame.apply(self)
  
  return frame
rescue GoawayError => error
  # Go directly to jail. Do not pass go, do not collect $200.
  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

Raises:



497
498
499
# File 'lib/protocol/http2/connection.rb', line 497

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

#receive_data(frame) ⇒ Object



339
340
341
342
343
344
345
346
347
# File 'lib/protocol/http2/connection.rb', line 339

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

#receive_frame(frame) ⇒ Object



501
502
503
# File 'lib/protocol/http2/connection.rb', line 501

def receive_frame(frame)
  # ignore.
end

#receive_goaway(frame) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/protocol/http2/connection.rb', line 233

def receive_goaway(frame)
  # We capture the last stream that was processed.
  @remote_stream_id, error_code, message = frame.unpack
  
  self.close!
  
  if error_code != 0
    # Shut down immediately.
    raise GoawayError.new(message, error_code)
  end
end

#receive_headers(frame) ⇒ Object

On the server side, starts a new request.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/protocol/http2/connection.rb', line 386

def receive_headers(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.receive_headers(frame)
  else
    if stream_id <= @remote_stream_id
      raise ProtocolError, "Invalid stream id: #{stream_id} <= #{@remote_stream_id}!"
    end
    
    if @streams.size < self.maximum_concurrent_streams
      stream = accept_stream(stream_id)
      @remote_stream_id = stream_id
      
      stream.receive_headers(frame)
    else
      raise ProtocolError, "Exceeded maximum concurrent streams"
    end
  end
end

#receive_ping(frame) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/protocol/http2/connection.rb', line 323

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.



419
420
421
422
# File 'lib/protocol/http2/connection.rb', line 419

def receive_priority(frame)
  dependency = dependency_for(frame.stream_id)
  dependency.receive_priority(frame)
end

#receive_push_promise(frame) ⇒ Object

Raises:



424
425
426
# File 'lib/protocol/http2/connection.rb', line 424

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

#receive_reset_stream(frame) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
# File 'lib/protocol/http2/connection.rb', line 457

def receive_reset_stream(frame)
  if frame.connection?
    raise ProtocolError, "Cannot reset connection!"
  elsif stream = @streams[frame.stream_id]
    stream.receive_reset_stream(frame)
  elsif closed_stream_id?(frame.stream_id)
    # Ignore.
  else
    raise StreamClosed, "Cannot reset stream #{frame.stream_id}"
  end
end

#receive_settings(frame) ⇒ Object



301
302
303
304
305
306
307
308
309
310
# File 'lib/protocol/http2/connection.rb', line 301

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



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/protocol/http2/connection.rb', line 478

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
  elsif closed_stream_id?(frame.stream_id)
    # Ignore.
  else
    # Receiving any frame other than HEADERS or PRIORITY on a stream in this state (idle) 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

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.



224
225
226
227
228
229
230
231
# File 'lib/protocol/http2/connection.rb', line 224

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



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

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



411
412
413
414
415
416
# File 'lib/protocol/http2/connection.rb', line 411

def send_priority(stream_id, priority)
  frame = PriorityFrame.new(stream_id)
  frame.pack(priority)
  
  write_frame(frame)
end

#send_settings(changes) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/protocol/http2/connection.rb', line 200

def send_settings(changes)
  @local_settings.append(changes)
  
  frame = SettingsFrame.new
  frame.pack(changes)
  
  write_frame(frame)
  
  # If the initial window size is set to something bigger than the default, we want to increase it.
  difference = @local_settings.pending.initial_window_size - @local_window.capacity
  
  if difference > 0
    send_window_update(difference)
  end
end

#server_stream_id?(id) ⇒ Boolean



432
433
434
# File 'lib/protocol/http2/connection.rb', line 432

def server_stream_id?(id)
  id.even?
end

#update_local_settings(changes) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/protocol/http2/connection.rb', line 253

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



261
262
263
264
265
266
267
# File 'lib/protocol/http2/connection.rb', line 261

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



349
350
351
# File 'lib/protocol/http2/connection.rb', line 349

def valid_remote_stream_id?
  false
end

#write_frame(frame) ⇒ Object



245
246
247
# File 'lib/protocol/http2/connection.rb', line 245

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

#write_frames {|@framer| ... } ⇒ Object

Yields:



249
250
251
# File 'lib/protocol/http2/connection.rb', line 249

def write_frames
  yield @framer
end