Skip to content

Commit bf0c622

Browse files
committed
Ensure the connection stream is ended when closing the body.
1 parent cd57f8c commit bf0c622

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

lib/protocol/http1/body/chunked.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ def initialize(connection, headers)
2222
@count = 0
2323
end
2424

25+
attr :count
26+
27+
def length
28+
# We only know the length once we've read everything. This is because the length is not known until the final chunk is read.
29+
if @finished
30+
@length
31+
end
32+
end
33+
2534
def empty?
2635
@connection.nil?
2736
end
@@ -34,6 +43,8 @@ def discard
3443
unless @finished
3544
connection.close_read
3645
end
46+
47+
connection.receive_end_stream!
3748
end
3849
end
3950

lib/protocol/http1/body/fixed.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def discard
3030
if @remaining != 0
3131
connection.close_read
3232
end
33+
34+
connection.receive_end_stream!
3335
end
3436
end
3537

lib/protocol/http1/body/remainder.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def discard
2727

2828
# Ensure no further requests can be read from the connection, as we are discarding the body which may not be fully read:
2929
connection.close_read
30+
31+
connection.receive_end_stream!
3032
end
3133
end
3234

test/protocol/http1/body/chunked.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
body.close
3434

3535
expect(body).to be(:empty?)
36+
expect(connection).to be(:half_closed_remote?)
3637
end
3738

3839
it "invokes close_read on the stream if closing with an error" do
@@ -41,6 +42,7 @@
4142
body.close(EOFError)
4243

4344
expect(body).to be(:empty?)
45+
expect(connection).to be(:half_closed_remote?)
4446
end
4547
end
4648

@@ -50,13 +52,19 @@
5052
expect(body.read).to be == nil
5153
expect(body.read).to be == nil
5254

53-
expect(connection).to have_attributes(state: be == :half_closed_remote)
55+
expect(connection).to be(:half_closed_remote?)
5456
end
5557

5658
it "updates number of bytes retrieved" do
57-
body.read
58-
body.read # realizes there are no more chunks
59+
expect(body).to have_attributes(length: be_nil, count: be == 0)
60+
61+
expect(body.read).to be == "Hello World"
62+
expect(body.read).to be_nil # there are no more chunks
63+
64+
expect(body).to have_attributes(length: be == 11, count: be == 1)
65+
5966
expect(body).to be(:empty?)
67+
expect(connection).to be(:half_closed_remote?)
6068
end
6169

6270
with "trailer" do
@@ -70,6 +78,8 @@
7078

7179
expect(body.read).to be == nil
7280
expect(headers["etag"]).to be == "abcd"
81+
82+
expect(connection).to be(:half_closed_remote?)
7383
end
7484
end
7585

@@ -83,6 +93,9 @@
8393
expect(headers["etag"]).to be_nil
8494

8595
expect{body.read}.to raise_exception(Protocol::HTTP1::BadHeader)
96+
97+
body.close
98+
expect(connection).to be(:half_closed_remote?)
8699
end
87100
end
88101

@@ -91,6 +104,8 @@
91104

92105
it "raises error" do
93106
expect{body.read}.to raise_exception(EOFError)
107+
108+
expect(connection).to be(:half_closed_remote?)
94109
end
95110
end
96111
end

test/protocol/http1/body/fixed.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,25 @@
2828
it "closes the stream" do
2929
body.close(EOFError)
3030
expect(buffer).to be(:closed?)
31+
32+
expect(connection).to be(:half_closed_remote?)
3133
end
3234

3335
it "doesn't close the stream when EOF was reached" do
3436
body.read
3537
body.close(EOFError)
3638
expect(buffer).not.to be(:closed?)
39+
40+
expect(connection).to be(:half_closed_remote?)
3741
end
3842
end
3943

4044
with "#read" do
4145
it "retrieves chunks of content" do
4246
expect(body.read).to be == "Hello World"
4347
expect(body.read).to be == nil
48+
49+
expect(connection).to be(:half_closed_remote?)
4450
end
4551

4652
it "updates number of bytes retrieved" do
@@ -54,11 +60,19 @@
5460
it "retrieves content up to provided length" do
5561
expect(body.read).to be == "Hello"
5662
expect(body.read).to be == nil
63+
64+
expect(connection).to be(:half_closed_remote?)
5765
end
5866

5967
it "updates number of bytes retrieved" do
68+
expect(body).to have_attributes(remaining: be == body.length)
69+
6070
body.read
71+
72+
expect(body).to have_attributes(remaining: be == 0)
6173
expect(body).to be(:empty?)
74+
75+
expect(connection).to be(:half_closed_remote?)
6276
end
6377
end
6478

@@ -89,6 +103,8 @@
89103
length: be == chunk.bytesize,
90104
remaining: be == 0
91105
)
106+
107+
expect(connection).to be(:half_closed_remote?)
92108
end
93109
end
94110

@@ -99,6 +115,8 @@
99115
expect do
100116
body.read
101117
end.to raise_exception(EOFError)
118+
119+
expect(connection).to be(:half_closed_remote?)
102120
end
103121
end
104122
end

test/protocol/http1/body/remainder.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828
it "closes the stream" do
2929
body.close(EOFError)
3030
expect(buffer).to be(:closed?)
31+
32+
expect(connection).to be(:half_closed_remote?)
3133
end
3234

3335
it "closes the stream when EOF was reached" do
3436
body.read
3537
body.close(EOFError)
3638
expect(buffer).to be(:closed?)
39+
40+
expect(connection).to be(:half_closed_remote?)
3741
end
3842
end
3943

@@ -45,6 +49,8 @@
4549
expect(body.read).to be == nil
4650

4751
expect(body).to be(:empty?)
52+
53+
expect(connection).to be(:half_closed_remote?)
4854
end
4955
end
5056

@@ -53,6 +59,8 @@
5359
stream = StringIO.new
5460
body.call(stream)
5561
expect(stream.string).to be == "Hello World"
62+
63+
expect(connection).to be(:half_closed_remote?)
5664
end
5765
end
5866

@@ -63,6 +71,8 @@
6371
expect(body.join).to be == "Hello World"
6472

6573
expect(body).to be(:empty?)
74+
75+
expect(connection).to be(:half_closed_remote?)
6676
end
6777
end
6878
end

0 commit comments

Comments
 (0)