Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for connection upgrade and bi-directional streaming. #101

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions lib/webrick/httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ class InvalidHeader < StandardError

attr_reader :sent_size

##
# Set the response body proc as an streaming/upgrade response.

attr_accessor :upgrade

def upgrade!(protocol)
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
@upgrade = protocol
@keep_alive = false
@chunked = false
end

##
# Creates a new HTTP response object. WEBrick::Config::HTTP is the
# default configuration.
Expand Down Expand Up @@ -242,6 +253,14 @@ def setup_header() # :nodoc:
@header['server'] ||= @config[:ServerSoftware]
@header['date'] ||= Time.now.httpdate

if @upgrade
@header['connection'] = 'upgrade'
@header['upgrade'] = @upgrade
@keep_alive = false

return
end

# HTTP/0.9 features
if @request_http_version < "1.0"
@http_version = HTTPVersion.new("0.9")
Expand All @@ -268,11 +287,10 @@ def setup_header() # :nodoc:
elsif %r{^multipart/byteranges} =~ @header['content-type']
@header.delete('content-length')
elsif @header['content-length'].nil?
if @body.respond_to? :readpartial
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
elsif @body.respond_to? :call
make_body_tempfile
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
else
if @body.respond_to?(:bytesize)
@header['content-length'] = (@body ? @body.bytesize : 0).to_s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(@body ? @body.bytesize : 0).to_s looks weird if you already know that @body responds to bytesize. With the change in this PR, probably this should be @body.bytesize.to_s

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, good call, I just copied the previous code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, fixed.

else
@header['connection'] = 'close'
end
end

Expand Down Expand Up @@ -517,14 +535,14 @@ def send_body_proc(socket)
@body.call(ChunkedWrapper.new(socket, self))
socket.write("0#{CRLF}#{CRLF}")
else
size = @header['content-length'].to_i
if @bodytempfile
@bodytempfile.rewind
IO.copy_stream(@bodytempfile, socket)
else
@body.call(socket)
end
@sent_size = size

@sent_size = @header['content-length'].to_i
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down
33 changes: 33 additions & 0 deletions test/webrick/test_httpresponse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,39 @@ def test_send_body_proc_chunked
assert_equal 0, logger.messages.length
end

def test_send_body_proc_upgrade
@res.body = Proc.new { |out| out.write('hello'); out.close }
@res.upgrade!("text")

IO.pipe do |r, w|
@res.send_response(w)
w.close
assert_match /Connection: upgrade\r\nUpgrade: text\r\n\r\nhello/, r.read
end
assert_empty logger.messages
end

def test_send_body_proc_stream
@res.body = Proc.new do |socket|
chunk = socket.read
socket.write(chunk)
socket.close
end

UNIXSocket.pair do |s1, s2|
thread = Thread.new do
@res.send_response(s1)
end

s2.write("hello")
s2.close_write
chunk = s2.read
assert_match /Connection: close\r\n\r\nhello/, chunk
s2.close
end
assert_empty logger.messages
end

def test_set_error
status = 400
message = 'missing attribute'
Expand Down
3 changes: 1 addition & 2 deletions test/webrick/test_httpserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ def test_response_io_without_chunked_set
:ServerName => "localhost"
}
log_tester = lambda {|log, access_log|
assert_equal(1, log.length)
assert_match(/WARN Could not determine content-length of response body./, log[0])
assert_empty log
}
TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log|
server.mount_proc("/", lambda { |req, res|
Expand Down