From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 1/2] webrick: favor .write over << method
Date: Tue, 26 Dec 2017 04:39:35 +0000 [thread overview]
Message-ID: <20171226043936.7694-1-e@80x24.org> (raw)
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
next reply other threads:[~2017-12-26 4:39 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-26 4:39 Eric Wong [this message]
2017-12-26 4:39 ` [PATCH 2/2] webrick/httpresponse: IO.copy_stream for regular files Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171226043936.7694-1-e@80x24.org \
--to=e@80x24.org \
--cc=spew@80x24.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).