diff --git a/lib/protocol/http1/body/chunked.rb b/lib/protocol/http1/body/chunked.rb index bc1fb20..56ebb63 100644 --- a/lib/protocol/http1/body/chunked.rb +++ b/lib/protocol/http1/body/chunked.rb @@ -40,7 +40,7 @@ def close(error = nil) @connection = nil unless @finished - connection.close + connection.close_read end end @@ -86,7 +86,7 @@ def read return chunk else # The connection has been closed before we have read the requested length: - @connection.close + @connection.close_read @connection = nil end end diff --git a/lib/protocol/http1/body/fixed.rb b/lib/protocol/http1/body/fixed.rb index 2acc0d6..9dd8fe3 100644 --- a/lib/protocol/http1/body/fixed.rb +++ b/lib/protocol/http1/body/fixed.rb @@ -28,7 +28,7 @@ def close(error = nil) @connection = nil unless @remaining == 0 - connection.close + connection.close_read end end diff --git a/lib/protocol/http1/body/remainder.rb b/lib/protocol/http1/body/remainder.rb index 2262b89..2997017 100644 --- a/lib/protocol/http1/body/remainder.rb +++ b/lib/protocol/http1/body/remainder.rb @@ -26,7 +26,7 @@ def discard @connection = nil # Ensure no further requests can be read from the connection, as we are discarding the body which may not be fully read: - connection.close + connection.close_read end end @@ -39,8 +39,12 @@ def close(error = nil) def read @connection&.readpartial(BLOCK_SIZE) rescue EOFError - @connection.receive_end_stream! - @connection = nil + if connection = @connection + @connection = nil + connection.receive_end_stream! + end + + return nil end def inspect diff --git a/lib/protocol/http1/connection.rb b/lib/protocol/http1/connection.rb index 7dd3308..e4f19f2 100644 --- a/lib/protocol/http1/connection.rb +++ b/lib/protocol/http1/connection.rb @@ -186,6 +186,12 @@ def hijack! return stream end + def close_read + @persistent = false + @stream&.close_read + self.receive_end_stream! + end + # Close the connection and underlying stream. def close @persistent = false diff --git a/test/protocol/http1/body/chunked.rb b/test/protocol/http1/body/chunked.rb index 903ca11..471b3c8 100644 --- a/test/protocol/http1/body/chunked.rb +++ b/test/protocol/http1/body/chunked.rb @@ -28,21 +28,21 @@ with "#close" do it "invokes close_read on the stream if closing without reading all chunks" do - expect(buffer).to receive(:close) + expect(buffer).to receive(:close_read) body.close expect(body).to be(:empty?) - expect(connection).to be(:closed?) + expect(connection).to be(:half_closed_remote?) end it "invokes close_read on the stream if closing with an error" do - expect(buffer).to receive(:close) + expect(buffer).to receive(:close_read) body.close(EOFError) expect(body).to be(:empty?) - expect(connection).to be(:closed?) + expect(connection).to be(:half_closed_remote?) end end @@ -96,7 +96,7 @@ body.close - expect(connection).to be(:closed?) + expect(connection).to be(:half_closed_remote?) end end @@ -106,7 +106,7 @@ it "raises error" do expect{body.read}.to raise_exception(EOFError) - expect(connection).to be(:closed?) + expect(connection).to be(:half_closed_remote?) end end end diff --git a/test/protocol/http1/body/fixed.rb b/test/protocol/http1/body/fixed.rb index 2970430..27d4679 100644 --- a/test/protocol/http1/body/fixed.rb +++ b/test/protocol/http1/body/fixed.rb @@ -29,7 +29,7 @@ body.close(EOFError) expect(buffer).to be(:closed?) - expect(connection).to be(:closed?) + expect(connection).to be(:half_closed_remote?) end it "doesn't close the stream when EOF was reached" do diff --git a/test/protocol/http1/body/remainder.rb b/test/protocol/http1/body/remainder.rb index 59100dd..b101000 100644 --- a/test/protocol/http1/body/remainder.rb +++ b/test/protocol/http1/body/remainder.rb @@ -29,7 +29,7 @@ body.close(EOFError) expect(buffer).to be(:closed?) - expect(connection).to be(:closed?) + expect(connection).to be(:half_closed_remote?) end it "closes the stream when EOF was reached" do @@ -37,7 +37,7 @@ body.close(EOFError) expect(buffer).to be(:closed?) - expect(connection).to be(:closed?) + expect(connection).to be(:half_closed_remote?) end end