From 2bdcd0bddea7de08b8845918ed27185fa3125734 Mon Sep 17 00:00:00 2001 From: normal Date: Wed, 28 Mar 2018 08:05:52 +0000 Subject: webrick/httpresponse: IO.copy_stream for regular files Remove the redundant _send_file method since its functionality is unnecessary with IO.copy_stream. IO.copy_stream also allows the use of sendfile under some OSes to speed up copies to non-TLS sockets. Testing with "curl >/dev/null" and "ruby -run -e httpd" to read a 1G file over Linux loopback reveals a reduction from around ~0.770 to ~0.490 seconds on the client side. * lib/webrick/httpresponse.rb (send_body_io): use IO.copy_stream (_send_file): remove [Feature #14237] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62954 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/webrick/httpresponse.rb | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) (limited to 'lib/webrick') diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb index eddd3388ed..82c53e60c7 100644 --- a/lib/webrick/httpresponse.rb +++ b/lib/webrick/httpresponse.rb @@ -410,9 +410,9 @@ module WEBrick buf.clear socket.write("0#{CRLF}#{CRLF}") else - size = @header['content-length'].to_i - _send_file(socket, @body, 0, size) - @sent_size = size + size = @header['content-length'] + size = size.to_i if size + @sent_size = IO.copy_stream(@body, socket, size) end ensure @body.close @@ -474,27 +474,6 @@ module WEBrick alias :<< :write end - def _send_file(output, input, offset, size) - while offset > 0 - sz = @buffer_size < size ? @buffer_size : size - buf = input.read(sz) - offset -= buf.bytesize - end - - if size == 0 - while buf = input.read(@buffer_size) - output.write(buf) - end - else - while size > 0 - sz = @buffer_size < size ? @buffer_size : size - buf = input.read(sz) - output.write(buf) - size -= buf.bytesize - end - end - end - # preserved for compatibility with some 3rd-party handlers def _write_data(socket, data) socket << data -- cgit v1.2.3