Skip to content

Commit 9b0bf86

Browse files
yardAnton Zhuravsky
and
Anton Zhuravsky
authored
Avoid writing to the body after full hijack (#151)
* Handling partially and fully hijacked connections gracefully * A more precise test of hijacked body * Bumping protocol-http1 and updating hijack check * Closing the body after hijack --------- Co-authored-by: Anton Zhuravsky <[email protected]>
1 parent 9ab4032 commit 9b0bf86

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

async-http.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
2323
spec.add_dependency "async-io", ">= 1.28"
2424
spec.add_dependency "async-pool", ">= 0.2"
2525
spec.add_dependency "protocol-http", "~> 0.26.0"
26-
spec.add_dependency "protocol-http1", "~> 0.18.0"
26+
spec.add_dependency "protocol-http1", "~> 0.19.0"
2727
spec.add_dependency "protocol-http2", "~> 0.16.0"
2828
spec.add_dependency "traces", ">= 0.10.0"
2929
end

lib/async/http/protocol/http1/server.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def each(task: Task.current)
5050
response = yield(request, self)
5151
body = response&.body
5252

53-
if @stream.nil? and body.nil?
54-
# Full hijack.
53+
if hijacked?
54+
body&.close
5555
return
5656
end
5757

test/async/http/protocol/http11.rb

+29-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def around
6565
"Hello World!"
6666
)
6767
peer.close
68-
68+
6969
nil
7070
end
7171
end
@@ -82,5 +82,33 @@ def around
8282
expect(response.reason).to be == "It worked!"
8383
end
8484
end
85+
86+
with 'full hijack with empty response' do
87+
let(:body) {Async::HTTP::Body::Buffered.new([], 0)}
88+
89+
let(:app) do
90+
::Protocol::HTTP::Middleware.for do |request|
91+
peer = request.hijack!
92+
93+
peer.write(
94+
"#{request.version} 200 It worked!\r\n" +
95+
"connection: close\r\n" +
96+
"\r\n" +
97+
"Hello World!"
98+
)
99+
peer.close
100+
101+
::Protocol::HTTP::Response[-1, {}, body]
102+
end
103+
end
104+
105+
it "works properly" do
106+
expect(body).to receive(:close)
107+
108+
response = client.get("/")
109+
110+
expect(response.read).to be == "Hello World!"
111+
end
112+
end
85113
end
86114
end

0 commit comments

Comments
 (0)