-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathclient.rb
81 lines (68 loc) · 2.06 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
module HTTP2
# 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:
#
# @example
# socket = YourTransport.new
#
# conn = HTTP2::Client.new
# conn.on(:frame) {|bytes| socket << bytes }
#
# while bytes = socket.read
# conn << bytes
# end
#
class Client < Connection
# Initialize new HTTP 2.0 client object.
def initialize(settings = {})
@stream_id = 1
@state = :waiting_connection_preface
@local_role = :client
@remote_role = :server
super
end
# Send an outgoing frame. Connection and stream flow control is managed
# by Connection class.
#
# @see Connection
# @param frame [Hash]
def send(frame)
send_connection_preface
super
end
def receive(frame)
send_connection_preface
super
end
# sends the preface and initializes the first stream in half-closed state
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
# Emit the connection preface if not yet
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
def self.settings_header(settings)
frame = Framer.new.generate(type: :settings, stream: 0, payload: settings)
Base64.urlsafe_encode64(frame[9..-1])
end
private
def verify_pseudo_headers(frame)
_verify_pseudo_headers(frame, RESPONSE_MANDATORY_HEADERS)
end
end
end