Skip to content

Commit

Permalink
Revert back to close_read implementation/state transition.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 19, 2024
1 parent 50b1201 commit cc05882
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/protocol/http1/body/chunked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def close(error = nil)
@connection = nil

unless @finished
connection.close
connection.close_read
end
end

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/http1/body/fixed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def close(error = nil)
@connection = nil

unless @remaining == 0
connection.close
connection.close_read
end
end

Expand Down
10 changes: 7 additions & 3 deletions lib/protocol/http1/body/remainder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/protocol/http1/body/chunked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -96,7 +96,7 @@

body.close

expect(connection).to be(:closed?)
expect(connection).to be(:half_closed_remote?)
end
end

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/protocol/http1/body/fixed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/protocol/http1/body/remainder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
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
body.read
body.close(EOFError)
expect(buffer).to be(:closed?)

expect(connection).to be(:closed?)
expect(connection).to be(:half_closed_remote?)
end
end

Expand Down

0 comments on commit cc05882

Please sign in to comment.