Class: HTTP2::Client

Inherits:
Connection show all
Defined in:
lib/http/2/client.rb

Overview

HTTP 2.0 client connection class that implements appropriate header compression / decompression algorithms and stream management logic.

Your code is responsible for driving the client object, which in turn performs all of the necessary HTTP 2.0 encoding / decoding, state management, and the rest. A simple example:

Examples:

socket = YourTransport.new

conn = HTTP2::Client.new
conn.on(:frame) {|bytes| socket << bytes }

while bytes = socket.read
  conn << bytes
end

Constant Summary

Constants included from FlowBuffer

FlowBuffer::MAX_WINDOW_SIZE

Instance Attribute Summary

Attributes inherited from Connection

#active_stream_count, #local_settings, #local_window, #pending_settings, #remote_settings, #remote_window, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#<<, #closed?, #goaway, #new_stream, #ping, #settings, #window_update

Methods included from Emitter

#emit, #on, #once

Methods included from FlowBuffer

#buffered_amount, #flush

Constructor Details

#initialize(settings = {}) ⇒ Client

Initialize new HTTP 2.0 client object.



23
24
25
26
27
28
29
30
31
# File 'lib/http/2/client.rb', line 23

def initialize(settings = {})
  @stream_id    = 1
  @state        = :waiting_connection_preface

  @local_role   = :client
  @remote_role  = :server

  super
end

Class Method Details

.settings_header(settings) ⇒ Object



70
71
72
73
# File 'lib/http/2/client.rb', line 70

def self.settings_header(settings)
  frame = Framer.new.generate(type: :settings, stream: 0, payload: settings)
  Base64.urlsafe_encode64(frame[9..-1])
end

Instance Method Details

#receive(frame) ⇒ Object



43
44
45
46
# File 'lib/http/2/client.rb', line 43

def receive(frame)
  send_connection_preface
  super
end

#send(frame) ⇒ Object

Send an outgoing frame. Connection and stream flow control is managed by Connection class.

Parameters:

  • frame (Hash)

See Also:



38
39
40
41
# File 'lib/http/2/client.rb', line 38

def send(frame)
  send_connection_preface
  super
end

#send_connection_prefaceObject

Emit the connection preface if not yet



60
61
62
63
64
65
66
67
68
# File 'lib/http/2/client.rb', line 60

def send_connection_preface
  return unless @state == :waiting_connection_preface

  @state = :connected
  emit(:frame, CONNECTION_PREFACE_MAGIC)

  payload = @local_settings.reject { |k, v| v == SPEC_DEFAULT_CONNECTION_SETTINGS[k] }
  settings(payload)
end

#upgradeObject

sends the preface and initializes the first stream in half-closed state

Raises:



49
50
51
52
53
54
55
56
57
# File 'lib/http/2/client.rb', line 49

def upgrade
  @h2c_upgrade = :start
  raise ProtocolError unless @stream_id == 1

  send_connection_preface
  stream = new_stream(state: :half_closed_local)
  @h2c_upgrade = :finished
  stream
end