Skip to content

Commit fd57e55

Browse files
authored
improve checks in stream? for http1 (#58)
1 parent 06ff17d commit fd57e55

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Juan Antonio Martín Lucas <dev@jaml.pro>
22
Aurora Nockert <aurora@aventine.se>
3+
Thomas Morgan <tm@iprog.com>

lib/async/websocket/upgrade_request.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ class UpgradeRequest < ::Protocol::HTTP::Request
2020
include ::Protocol::WebSocket::Headers
2121

2222
class Wrapper
23-
def initialize(response)
23+
def initialize(response, verified:)
2424
@response = response
2525
@stream = nil
26+
@verified = verified
2627
end
2728

2829
def close
@@ -32,7 +33,7 @@ def close
3233
attr_accessor :response
3334

3435
def stream?
35-
@response.status == 101
36+
@response.status == 101 && @verified
3637
end
3738

3839
def status
@@ -74,7 +75,7 @@ def call(connection)
7475
end
7576
end
7677

77-
return Wrapper.new(response)
78+
return Wrapper.new(response, verified: !!accept_digest)
7879
end
7980
end
8081
end

test/async/websocket/client.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,34 @@
110110
with 'http/1' do
111111
let(:protocol) {Async::HTTP::Protocol::HTTP1}
112112
it_behaves_like ClientExamples
113+
114+
with 'invalid sec-websocket-accept header' do
115+
let(:app) do
116+
Protocol::HTTP::Middleware.for do |request|
117+
Protocol::HTTP::Response[101, {'sec-websocket-accept'=>'wrong-digest'}, []]
118+
end
119+
end
120+
121+
it 'raises an error' do
122+
expect do
123+
Async::WebSocket::Client.connect(client_endpoint) {}
124+
end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Invalid accept digest/)
125+
end
126+
end
127+
128+
with 'missing sec-websocket-accept header' do
129+
let(:app) do
130+
Protocol::HTTP::Middleware.for do |request|
131+
Protocol::HTTP::Response[101, {}, []]
132+
end
133+
end
134+
135+
it 'raises an error' do
136+
expect do
137+
Async::WebSocket::Client.connect(client_endpoint) {}
138+
end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Failed to negotiate connection/)
139+
end
140+
end
113141
end
114142

115143
with 'http/2' do

0 commit comments

Comments
 (0)