mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
webrick: favor .write over << method
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: (send_header): use socket.write (send_body_io): ditto (send_body_string): ditto (send_body_proc): ditto (_write_data): ditto (ChunkedWrapper#write): ditto (_send_file): ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62953 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ebf5348528
commit
c44978b99f
2 changed files with 15 additions and 10 deletions
|
@ -265,6 +265,10 @@ module WEBrick
|
|||
@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"]
|
||||
|
|
|
@ -293,7 +293,7 @@ module WEBrick
|
|||
data << "Set-Cookie: " << cookie.to_s << CRLF
|
||||
}
|
||||
data << CRLF
|
||||
_write_data(socket, data)
|
||||
socket.write(data)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -401,14 +401,14 @@ module WEBrick
|
|||
@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 @@ module WEBrick
|
|||
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 @@ module WEBrick
|
|||
# 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 @@ module WEBrick
|
|||
@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,18 +483,19 @@ module WEBrick
|
|||
|
||||
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
|
||||
|
||||
# preserved for compatibility with some 3rd-party handlers
|
||||
def _write_data(socket, data)
|
||||
socket << data
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue