From bd349db82252a98f0ba943b24ed063be074b4e0c Mon Sep 17 00:00:00 2001 From: Anton Zhuravsky Date: Thu, 4 Apr 2024 19:46:22 +0100 Subject: [PATCH 1/2] Adding #hijacked? method to connection --- lib/protocol/http1/connection.rb | 9 +++++++++ test/protocol/http1/hijack.rb | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/protocol/http1/connection.rb b/lib/protocol/http1/connection.rb index 7cfb0ba..366e8a4 100644 --- a/lib/protocol/http1/connection.rb +++ b/lib/protocol/http1/connection.rb @@ -53,6 +53,7 @@ def initialize(stream, persistent = true) @stream = stream @persistent = persistent + @hijacked = false @count = 0 end @@ -104,11 +105,19 @@ def write_connection_header(version) def write_upgrade_header(upgrade) @stream.write("connection: upgrade\r\nupgrade: #{upgrade}\r\n") end + + # Indicates whether the connection has been hijacked meaning its + # IO has been handed over and is not usable anymore. + # @return [Boolean] hijack status + def hijacked? + @hijacked + end # Effectively close the connection and return the underlying IO. # @return [IO] the underlying non-blocking IO. def hijack! @persistent = false + @hijacked = true stream = @stream @stream.flush diff --git a/test/protocol/http1/hijack.rb b/test/protocol/http1/hijack.rb index cb29384..e91595f 100644 --- a/test/protocol/http1/hijack.rb +++ b/test/protocol/http1/hijack.rb @@ -19,11 +19,17 @@ server_wrapper = server.hijack! expect(server.persistent).to be == false end + + it "should repord itself as #hijacked? after the hijack" do + expect(server.hijacked?).to be == false + server.hijack! + expect(server.hijacked?).to be == true + end it "should use non-chunked output" do expect(body).to receive(:ready?).and_return(false) expect(body).to receive(:empty?).and_return(false) - expect(body).to receive(:length).and_return(nil) + expect(body).to receive(:length).twice.and_return(nil) expect(body).to receive(:each).and_return(nil) expect(server).to receive(:write_body_and_close) From b009848ff84de5c3e1eae8f76c456c5cf516d5c7 Mon Sep 17 00:00:00 2001 From: Anton Zhuravsky Date: Fri, 5 Apr 2024 11:04:36 +0100 Subject: [PATCH 2/2] Simplifying #hijacked? check --- lib/protocol/http1/connection.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/protocol/http1/connection.rb b/lib/protocol/http1/connection.rb index 366e8a4..49e4cf1 100644 --- a/lib/protocol/http1/connection.rb +++ b/lib/protocol/http1/connection.rb @@ -53,7 +53,6 @@ def initialize(stream, persistent = true) @stream = stream @persistent = persistent - @hijacked = false @count = 0 end @@ -110,14 +109,13 @@ def write_upgrade_header(upgrade) # IO has been handed over and is not usable anymore. # @return [Boolean] hijack status def hijacked? - @hijacked + @stream.nil? end # Effectively close the connection and return the underlying IO. # @return [IO] the underlying non-blocking IO. def hijack! @persistent = false - @hijacked = true stream = @stream @stream.flush