Skip to content

Commit d5f0b31

Browse files
authored
Tidy up body code. (#181)
* Remove `Async::HTTP::Body::Delayed` with no replacement. * Remove `Async::HTTP::Body::Slowloris` with no replacement. * Fix handling of stream `close_write`.
1 parent b1c6cf6 commit d5f0b31

File tree

18 files changed

+90
-286
lines changed

18 files changed

+90
-286
lines changed

examples/upload/client.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,27 @@
99

1010
require 'async'
1111
require 'protocol/http/body/file'
12-
require 'async/http/body/delayed'
1312
require 'async/http/client'
1413
require 'async/http/endpoint'
1514

15+
class Delayed < ::Protocol::HTTP::Body::Wrapper
16+
def initialize(body, delay = 0.01)
17+
super(body)
18+
19+
@delay = delay
20+
end
21+
22+
def ready?
23+
false
24+
end
25+
26+
def read
27+
sleep(@delay)
28+
29+
return super
30+
end
31+
end
32+
1633
Async do
1734
endpoint = Async::HTTP::Endpoint.parse("http://localhost:9222")
1835
client = Async::HTTP::Client.new(endpoint, protocol: Async::HTTP::Protocol::HTTP2)
@@ -21,7 +38,7 @@
2138
['accept', 'text/plain'],
2239
]
2340

24-
body = Async::HTTP::Body::Delayed.new(Protocol::HTTP::Body::File.open(File.join(__dir__, "data.txt"), block_size: 32))
41+
body = Delayed.new(Protocol::HTTP::Body::File.open(File.join(__dir__, "data.txt"), block_size: 32))
2542

2643
response = client.post(endpoint.path, headers, body)
2744

fixtures/async/http/a_protocol.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module HTTP
162162
request_received.wait
163163
headers.add('etag', 'abcd')
164164

165-
body.close
165+
body.close_write
166166
end
167167

168168
response = client.post("/", headers, body)
@@ -187,7 +187,7 @@ module HTTP
187187
response_received.wait
188188
headers.add('etag', 'abcd')
189189

190-
body.close
190+
body.close_write
191191
end
192192

193193
::Protocol::HTTP::Response[200, headers, body]
@@ -395,9 +395,9 @@ module HTTP
395395
let(:app) do
396396
::Protocol::HTTP::Middleware.for do |request|
397397
Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
398-
stream.write content
399-
stream.write content
400-
stream.close
398+
stream.write(content)
399+
stream.write(content)
400+
stream.close_write
401401
end
402402
end
403403
end

fixtures/async/http/body/a_writable_body.rb

Lines changed: 0 additions & 105 deletions
This file was deleted.

gems.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# gem "protocol-http2", path: "../protocol-http2"
2121
# gem "protocol-hpack", path: "../protocol-hpack"
2222

23+
gem "protocol-http", git: "https://github.com/socketry/protocol-http.git"
24+
2325
group :maintenance, optional: true do
2426
gem "bake-modernize"
2527
gem "bake-gem"

lib/async/http/body/delayed.rb

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/async/http/body/hijack.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def stream?
3636
end
3737

3838
def call(stream)
39-
return @block.call(stream)
39+
@block.call(stream)
4040
end
4141

4242
attr :input

lib/async/http/body/pipe.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def initialize(input, output = Writable.new, task: Task.current)
1717

1818
head, tail = ::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)
1919

20-
@head = ::IO::Stream::Buffered.new(head)
20+
@head = ::IO::Stream(head)
2121
@tail = tail
2222

2323
@reader = nil
@@ -52,8 +52,10 @@ def reader(task)
5252
end
5353

5454
@head.close_write
55+
rescue => error
56+
raise
5557
ensure
56-
@input.close($!)
58+
@input.close(error)
5759

5860
close_head if @writer&.finished?
5961
end
@@ -68,8 +70,10 @@ def writer(task)
6870
while chunk = @head.read_partial
6971
@output.write(chunk)
7072
end
73+
rescue => error
74+
raise
7175
ensure
72-
@output.close($!)
76+
@output.close_write(error)
7377

7478
close_head if @reader&.finished?
7579
end

lib/async/http/body/slowloris.rb

Lines changed: 0 additions & 55 deletions
This file was deleted.

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,31 @@ def each(task: Task.current)
6868
stream = write_upgrade_body(protocol)
6969

7070
# At this point, the request body is hijacked, so we don't want to call #finish below.
71-
request = nil unless request.body
71+
request = nil
7272
response = nil
7373

7474
# We must return here as no further request processing can be done:
7575
return body.call(stream)
76+
elsif response.status == 101
77+
# This code path is to support legacy behavior where the response status is set to 101, but the protocol is not upgraded. This may not be a valid use case, but it is supported for compatibility. We expect the response headers to contain the `upgrade` header.
78+
write_response(@version, response.status, response.headers)
79+
80+
stream = write_tunnel_body(request.version)
81+
82+
# Same as above:
83+
request = nil
84+
response = nil
85+
86+
# We must return here as no further request processing can be done:
87+
return body&.call(stream)
7688
else
7789
write_response(@version, response.status, response.headers)
7890

7991
if request.connect? and response.success?
8092
stream = write_tunnel_body(request.version)
8193

8294
# Same as above:
83-
request = nil unless request.body
95+
request = nil
8496
response = nil
8597

8698
# We must return here as no further request processing can be done:

lib/async/http/protocol/http2/input.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
# Released under the MIT License.
44
# Copyright, 2020-2023, by Samuel Williams.
55

6-
require_relative '../../body/writable'
6+
require 'protocol/http/body/writable'
77

88
module Async
99
module HTTP
1010
module Protocol
1111
module HTTP2
1212
# A writable body which requests window updates when data is read from it.
13-
class Input < Body::Writable
13+
class Input < ::Protocol::HTTP::Body::Writable
1414
def initialize(stream, length)
1515
super(length)
1616

0 commit comments

Comments
 (0)