Skip to content

Commit

Permalink
When passed a filename, download a report in chunks
Browse files Browse the repository at this point in the history
Rather than pulling an entire 6-8 GB report into a string in memory to
immediately write it out to a file, use the built-in feature of Net::HTTP to
retrieve the result in chunks and write them out as they are read from the
remote host. This prevents receiving `NoMemoryError` issues when downloading
huge reports.
  • Loading branch information
toofishes committed Jun 1, 2018
1 parent 74e5564 commit 9ff95f2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/nexpose/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,15 @@ def download(url, file_name = nil)
http.cert_store = @trust_store
end
headers = { 'Cookie' => "nexposeCCSessionID=#{@session_id}" }
resp = http.get(uri.to_s, headers)

if file_name
::File.open(file_name, 'wb') { |file| file.write(resp.body) }
http.request_get(uri.to_s, headers) do |resp|
::File.open(file_name, 'wb') do |file|
resp.read_body { |chunk| file.write(chunk) }
end
end
else
resp = http.get(uri.to_s, headers)
resp.body
end
end
Expand Down

0 comments on commit 9ff95f2

Please sign in to comment.