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 11, 2021
1 parent b81a14f commit 30ecef5
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/puma/minissl.rb
Expand Up @@ -114,31 +114,34 @@ 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?
n = 0
byte_size = data.bytesize
enc_wr = ''.dup
enc = nil

data_size = data.bytesize
need = data_size
while n < byte_size
n += @engine.write(n == 0 ? data : data.byteslice(n..-1))

while true
wrote = @engine.write data
enc_wr << enc while (enc = @engine.extract)

enc_wr = ''.dup
while (enc = @engine.extract)
enc_wr << enc
end
@socket.write enc_wr unless enc_wr.empty?

need -= wrote

return data_size if need == 0

data = data.byteslice(wrote..-1)
enc_wr.clear
end
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 30ecef5

Please sign in to comment.