Skip to content

Commit 253e73c

Browse files
committed
Correct matching condition when finding server key in the test framework
1 parent 418148b commit 253e73c

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

test/support/protocol_test_case.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -800,18 +800,10 @@ def initialize s
800800
def handshake port, path
801801
key = SecureRandom.hex(11)
802802
@sock.print "GET #{path} HTTP/1.1\r\nHost: 127.0.0.1:#{port}\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: #{key}==\r\n\r\n"
803-
res = nil
804-
loop do
805-
res = @sock.readpartial 4092
806-
break unless res.match?(/out|input/)
807-
end
803+
server_key = get_server_key
808804

809-
if res.match(/^Sec-WebSocket-Accept: (.*)\r\n/)
810-
correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
811-
raise "The Sec-WebSocket-Accept value: #{$1} is not valid" unless $1 == correct_key
812-
else
813-
raise "Unknown response: #{res}"
814-
end
805+
correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
806+
raise "The Sec-WebSocket-Accept value: #{$1} is not valid" unless server_key == correct_key
815807
end
816808

817809
def send msg
@@ -882,6 +874,19 @@ def send_close_connection
882874
def close
883875
@sock.close
884876
end
877+
878+
private
879+
880+
def get_server_key
881+
Timeout.timeout(ProtocolTestCase::TIMEOUT_SEC) do
882+
loop do
883+
res = @sock.readpartial 4092
884+
if res.match(/^Sec-WebSocket-Accept: (.*)\r\n/)
885+
return $1
886+
end
887+
end
888+
end
889+
end
885890
end
886891

887892
# When constant variables are referred from modules, they have to be defined outside the class.

0 commit comments

Comments
 (0)