Skip to content

Commit

Permalink
Tidy up body code. (#181)
Browse files Browse the repository at this point in the history
* Remove `Async::HTTP::Body::Delayed` with no replacement.

* Remove `Async::HTTP::Body::Slowloris` with no replacement.

* Fix handling of stream `close_write`.
  • Loading branch information
ioquatix committed Sep 9, 2024
1 parent b1c6cf6 commit d5f0b31
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 286 deletions.
21 changes: 19 additions & 2 deletions examples/upload/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@

require 'async'
require 'protocol/http/body/file'
require 'async/http/body/delayed'
require 'async/http/client'
require 'async/http/endpoint'

class Delayed < ::Protocol::HTTP::Body::Wrapper
def initialize(body, delay = 0.01)
super(body)

@delay = delay
end

def ready?
false
end

def read
sleep(@delay)

return super
end
end

Async do
endpoint = Async::HTTP::Endpoint.parse("http://localhost:9222")
client = Async::HTTP::Client.new(endpoint, protocol: Async::HTTP::Protocol::HTTP2)
Expand All @@ -21,7 +38,7 @@
['accept', 'text/plain'],
]

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

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

Expand Down
10 changes: 5 additions & 5 deletions fixtures/async/http/a_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ module HTTP
request_received.wait
headers.add('etag', 'abcd')

body.close
body.close_write
end

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

body.close
body.close_write
end

::Protocol::HTTP::Response[200, headers, body]
Expand Down Expand Up @@ -395,9 +395,9 @@ module HTTP
let(:app) do
::Protocol::HTTP::Middleware.for do |request|
Async::HTTP::Body::Hijack.response(request, 200, {}) do |stream|
stream.write content
stream.write content
stream.close
stream.write(content)
stream.write(content)
stream.close_write
end
end
end
Expand Down
105 changes: 0 additions & 105 deletions fixtures/async/http/body/a_writable_body.rb

This file was deleted.

2 changes: 2 additions & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# gem "protocol-http2", path: "../protocol-http2"
# gem "protocol-hpack", path: "../protocol-hpack"

gem "protocol-http", git: "https://github.com/socketry/protocol-http.git"

group :maintenance, optional: true do
gem "bake-modernize"
gem "bake-gem"
Expand Down
32 changes: 0 additions & 32 deletions lib/async/http/body/delayed.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/async/http/body/hijack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def stream?
end

def call(stream)
return @block.call(stream)
@block.call(stream)
end

attr :input
Expand Down
10 changes: 7 additions & 3 deletions lib/async/http/body/pipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(input, output = Writable.new, task: Task.current)

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

@head = ::IO::Stream::Buffered.new(head)
@head = ::IO::Stream(head)
@tail = tail

@reader = nil
Expand Down Expand Up @@ -52,8 +52,10 @@ def reader(task)
end

@head.close_write
rescue => error
raise
ensure
@input.close($!)
@input.close(error)

close_head if @writer&.finished?
end
Expand All @@ -68,8 +70,10 @@ def writer(task)
while chunk = @head.read_partial
@output.write(chunk)
end
rescue => error
raise
ensure
@output.close($!)
@output.close_write(error)

close_head if @reader&.finished?
end
Expand Down
55 changes: 0 additions & 55 deletions lib/async/http/body/slowloris.rb

This file was deleted.

16 changes: 14 additions & 2 deletions lib/async/http/protocol/http1/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,31 @@ def each(task: Task.current)
stream = write_upgrade_body(protocol)

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

# We must return here as no further request processing can be done:
return body.call(stream)
elsif response.status == 101
# 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.
write_response(@version, response.status, response.headers)

stream = write_tunnel_body(request.version)

# Same as above:
request = nil
response = nil

# We must return here as no further request processing can be done:
return body&.call(stream)
else
write_response(@version, response.status, response.headers)

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

# Same as above:
request = nil unless request.body
request = nil
response = nil

# We must return here as no further request processing can be done:
Expand Down
4 changes: 2 additions & 2 deletions lib/async/http/protocol/http2/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.

require_relative '../../body/writable'
require 'protocol/http/body/writable'

module Async
module HTTP
module Protocol
module HTTP2
# A writable body which requests window updates when data is read from it.
class Input < Body::Writable
class Input < ::Protocol::HTTP::Body::Writable
def initialize(stream, length)
super(length)

Expand Down
Loading

0 comments on commit d5f0b31

Please sign in to comment.