From 6a13d5141893b36d08e2139834ff20d3936b34af Mon Sep 17 00:00:00 2001 From: Naoto Ono Date: Mon, 24 Oct 2022 20:14:20 +0900 Subject: [PATCH] Correct matching condition when finding a server key --- test/support/protocol_test_case.rb | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/test/support/protocol_test_case.rb b/test/support/protocol_test_case.rb index 0e654e881..a041c17e1 100644 --- a/test/support/protocol_test_case.rb +++ b/test/support/protocol_test_case.rb @@ -800,18 +800,10 @@ def initialize s def handshake port, path key = SecureRandom.hex(11) @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" - res = nil - loop do - res = @sock.readpartial 4092 - break unless res.match?(/out|input/) - end + server_key = get_server_key - if res.match(/^Sec-WebSocket-Accept: (.*)\r\n/) - correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11" - raise "The Sec-WebSocket-Accept value: #{$1} is not valid" unless $1 == correct_key - else - raise "Unknown response: #{res}" - end + correct_key = Base64.strict_encode64 Digest::SHA1.digest "#{key}==258EAFA5-E914-47DA-95CA-C5AB0DC85B11" + raise "The Sec-WebSocket-Accept value: #{$1} is not valid" unless server_key == correct_key end def send msg @@ -882,6 +874,19 @@ def send_close_connection def close @sock.close end + + private + + def get_server_key + Timeout.timeout(ProtocolTestCase::TIMEOUT_SEC) do + loop do + res = @sock.readpartial 4092 + if res.match(/^Sec-WebSocket-Accept: (.*)\r\n/) + return $1 + end + end + end + end end # When constant variables are referred from modules, they have to be defined outside the class.