Skip to content

Commit 54fa9b7

Browse files
committed
Ensure "upgrade" response use 101 status code.
1 parent 105c1fc commit 54fa9b7

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,40 @@ def each(task: Task.current)
5959
if response
6060
trailer = response.headers.trailer!
6161

62-
write_response(@version, response.status, response.headers)
63-
6462
# Some operations in this method are long running, that is, it's expected that `body.call(stream)` could literally run indefinitely. In order to facilitate garbage collection, we want to nullify as many local variables before calling the streaming body. This ensures that the garbage collection can clean up as much state as possible during the long running operation, so we don't retain objects that are no longer needed.
65-
63+
6664
if body and protocol = response.protocol
65+
# We force a 101 response if the protocol is upgraded - HTTP/2 CONNECT will return 200 for success, but this won't be understood by HTTP/1 clients:
66+
write_response(@version, 101, response.headers)
67+
6768
stream = write_upgrade_body(protocol)
6869

6970
# At this point, the request body is hijacked, so we don't want to call #finish below.
70-
request = response = nil
71-
72-
body.call(stream)
73-
elsif request.connect? and response.success?
74-
stream = write_tunnel_body(request.version)
75-
76-
# Same as above:
77-
request = response = nil
71+
request = nil unless request.body
72+
response = nil
7873

7974
body.call(stream)
8075
else
81-
head = request.head?
82-
version = request.version
83-
84-
# Same as above:
85-
request = nil unless request.body
86-
response = nil
76+
write_response(@version, response.status, response.headers)
8777

88-
write_body(version, body, head, trailer)
78+
if request.connect? and response.success?
79+
stream = write_tunnel_body(request.version)
80+
81+
# Same as above:
82+
request = nil unless request.body
83+
response = nil
84+
85+
body.call(stream)
86+
else
87+
head = request.head?
88+
version = request.version
89+
90+
# Same as above:
91+
request = nil unless request.body
92+
response = nil
93+
94+
write_body(version, body, head, trailer)
95+
end
8996
end
9097

9198
# We are done with the body, you shouldn't need to call close on it:

0 commit comments

Comments
 (0)