From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: * X-Spam-ASN: AS62744 199.249.223.0/24 X-Spam-Status: No, score=1.2 required=3.0 tests=BAYES_00,RCVD_IN_MSPIKE_BL, RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,RDNS_NONE,SPF_FAIL,SPF_HELO_FAIL, TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (unknown [199.249.223.67]) by dcvr.yhbt.net (Postfix) with ESMTP id EF86F1F406 for ; Tue, 26 Dec 2017 04:39:39 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH 1/2] webrick: favor .write over << method Date: Tue, 26 Dec 2017 04:39:35 +0000 Message-Id: <20171226043936.7694-1-e@80x24.org> List-Id: This will make the next change to use IO.copy_stream easier-to-read. When we can drop Ruby 2.4 support in a few years, this will allow us to use writev(2) with multiple arguments for headers and chunked responses. * lib/webrick/cgi.rb (write): new wrapper method lib/webrick/httpresponse.rb (_write_data): remove (send_header): use socket.write (send_body_io): ditto (send_body_string): ditto (send_body_proc): ditto (ChunkedWrapper#write): ditto (_send_file): ditto --- lib/webrick/cgi.rb | 4 ++++ lib/webrick/httpresponse.rb | 24 ++++++++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/webrick/cgi.rb b/lib/webrick/cgi.rb index 94f385f1dd..33f1542731 100644 --- a/lib/webrick/cgi.rb +++ b/lib/webrick/cgi.rb @@ -265,6 +265,10 @@ def <<(data) @out_port << data end + def write(data) + @out_port.write(data) + end + def cert return nil unless defined?(OpenSSL) if pem = @env["SSL_SERVER_CERT"] diff --git a/lib/webrick/httpresponse.rb b/lib/webrick/httpresponse.rb index d76310f935..d9e46bbf73 100644 --- a/lib/webrick/httpresponse.rb +++ b/lib/webrick/httpresponse.rb @@ -293,7 +293,7 @@ def send_header(socket) # :nodoc: data << "Set-Cookie: " << cookie.to_s << CRLF } data << CRLF - _write_data(socket, data) + socket.write(data) end end @@ -401,14 +401,14 @@ def send_body_io(socket) @body.readpartial(@buffer_size, buf) size = buf.bytesize data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}" - _write_data(socket, data) + socket.write(data) data.clear @sent_size += size rescue EOFError break end while true buf.clear - _write_data(socket, "0#{CRLF}#{CRLF}") + socket.write("0#{CRLF}#{CRLF}") else size = @header['content-length'].to_i _send_file(socket, @body, 0, size) @@ -429,13 +429,13 @@ def send_body_string(socket) size = buf.bytesize data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}" buf.clear - _write_data(socket, data) + socket.write(data) @sent_size += size end - _write_data(socket, "0#{CRLF}#{CRLF}") + socket.write("0#{CRLF}#{CRLF}") else if @body && @body.bytesize > 0 - _write_data(socket, @body) + socket.write(@body) @sent_size = @body.bytesize end end @@ -446,7 +446,7 @@ def send_body_proc(socket) # do nothing elsif chunked? @body.call(ChunkedWrapper.new(socket, self)) - _write_data(socket, "0#{CRLF}#{CRLF}") + socket.write("0#{CRLF}#{CRLF}") else size = @header['content-length'].to_i @body.call(socket) @@ -466,7 +466,7 @@ def write(buf) @resp.instance_eval { size = buf.bytesize data = "#{size.to_s(16)}#{CRLF}#{buf}#{CRLF}" - _write_data(socket, data) + socket.write(data) data.clear @sent_size += size } @@ -483,22 +483,18 @@ def _send_file(output, input, offset, size) if size == 0 while buf = input.read(@buffer_size) - _write_data(output, buf) + output.write(buf) end else while size > 0 sz = @buffer_size < size ? @buffer_size : size buf = input.read(sz) - _write_data(output, buf) + output.write(buf) size -= buf.bytesize end end end - def _write_data(socket, data) - socket << data - end - # :startdoc: end -- EW