Skip to content

Commit

Permalink
send connection preface on receive (#142)
Browse files Browse the repository at this point in the history
* send connection on preface on receive, in case the client hasn't sent his own settings (currently this is being done only if the first message is from the client)
* do not set stream id in the test, as streams already have a proper stream id, and it will interfere with inferring if it's a connection frame (like, when you set a data frame with 0 as id)
* added test for the client initialization
  • Loading branch information
HoneyryderChuck authored and igrigorik committed Nov 17, 2018
1 parent 7ca33d2 commit 70a8a2e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/http/2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def send(frame)
super(frame)
end

def receive(frame)
send_connection_preface
super(frame)
end

# sends the preface and initializes the first stream in half-closed state
def upgrade
fail ProtocolError unless @stream_id == 1
Expand Down
5 changes: 4 additions & 1 deletion lib/http/2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ def receive(data)
raise if e.is_a?(Error::Error)
connection_error(e: e)
end
alias << receive

def <<(*args)
receive(*args)
end

private

Expand Down
12 changes: 12 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
expect(frame[:type]).to eq :settings
expect(frame[:payload]).to include([:settings_max_concurrent_streams, 200])
end

it 'should initialize client when receiving server settings before sending ack' do
frames = []
@client.on(:frame) { |bytes| frames << bytes }
@client << f.generate(SETTINGS.dup)

expect(frames[0]).to eq CONNECTION_PREFACE_MAGIC
expect(f.parse(frames[1])[:type]).to eq :settings
ack_frame = f.parse(frames[2])
expect(ack_frame[:type]).to eq :settings
expect(ack_frame[:flags]).to include(:ack)
end
end

context 'upgrade' do
Expand Down
1 change: 0 additions & 1 deletion spec/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
context 'initialization and settings' do
(FRAME_TYPES - [SETTINGS]).each do |frame|
it "should raise error if first frame is #{frame[:type]}" do
frame = set_stream_id(f.generate(frame.deep_dup), 0x0)
expect { @conn << frame }.to raise_error(ProtocolError)
expect(@conn).to be_closed
end
Expand Down

0 comments on commit 70a8a2e

Please sign in to comment.