Skip to content

Commit

Permalink
minissl.rb - allow chaining with '<<', improve/simplify write
Browse files Browse the repository at this point in the history
  • Loading branch information
MSP-Greg committed Apr 13, 2021
1 parent 0090de9 commit a262e29
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions lib/puma/minissl.rb
Expand Up @@ -21,6 +21,7 @@ class Socket
def initialize(socket, engine)
@socket = socket
@engine = engine
@buffer = Puma::IOBuffer.new
@peercert = nil
end

Expand Down Expand Up @@ -114,31 +115,40 @@ def read_nonblock(size, *_)
end
end

# When returning a large response body (2MB), Ubuntu works fine with
# `syswrite`, but macOS & Windows have OpenSSL errors on the client.
#
def write(data)
return 0 if data.empty?

data_size = data.bytesize
need = data_size

while true
wrote = @engine.write data

enc_wr = ''.dup
while (enc = @engine.extract)
enc_wr << enc
write_size = 128 * 1024
ttl = 0
running = 0
byte_size = data.bytesize
enc_wr = @buffer
enc = nil

while ttl < byte_size
inc = @engine.write(ttl.zero? ? data : data.byteslice(ttl..-1))
running += inc
ttl += inc
enc_wr.write(enc) while (enc = @engine.extract)

if running > write_size
@socket.write enc_wr.read
running = 0
end
@socket.write enc_wr unless enc_wr.empty?

need -= wrote

return data_size if need == 0

data = data.byteslice(wrote..-1)
end
@socket.write(enc_wr.read) unless enc_wr.empty?
enc.clear unless enc.nil?
byte_size
end

alias_method :syswrite, :write
alias_method :<<, :write

def <<(data)
write data
self
end

# This is a temporary fix to deal with websockets code using
# write_nonblock.
Expand Down

0 comments on commit a262e29

Please sign in to comment.