Copy proxy_file in chunks

pull/553/head
Omar Roth 6 years ago
parent 3ac8de0a64
commit 06bf0c2622
No known key found for this signature in database
GPG Key ID: B8254FB7EC3D37F2

@ -631,19 +631,31 @@ def cache_annotation(db, id, annotations)
end end
def proxy_file(response, env) def proxy_file(response, env)
if response.headers["Content-Length"]? && response.headers["Content-Length"] == "0" if !response.body_io?
return return
end end
if response.headers.includes_word?("Content-Encoding", "gzip") begin
Gzip::Writer.open(env.response) do |deflate| if response.headers.includes_word?("Content-Encoding", "gzip")
IO.copy(response.body_io, deflate) Gzip::Writer.open(env.response) do |deflate|
end copy_in_chunks(response.body_io, deflate)
elsif response.headers.includes_word?("Content-Encoding", "deflate") end
Flate::Writer.open(env.response) do |deflate| elsif response.headers.includes_word?("Content-Encoding", "deflate")
IO.copy(response.body_io, deflate) Flate::Writer.open(env.response) do |deflate|
copy_in_chunks(response.body_io, deflate)
end
else
copy_in_chunks(response.body_io, env.response)
end end
else rescue ex
IO.copy(response.body_io, env.response) end
end
# https://stackoverflow.com/a/44802810 <3
def copy_in_chunks(input, output, chunk_size = 4096)
size = 1
while size > 0
size = IO.copy(input, output, chunk_size)
Fiber.yield
end end
end end

Loading…
Cancel
Save