whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [whimsy] branch master updated: Cheaper to write directly to file; fail fast if too big
Date Sat, 03 Aug 2019 20:52:19 GMT
This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new e8621c5  Cheaper to write directly to file; fail fast if too big
e8621c5 is described below

commit e8621c54877ed3fd2fd67ab7db842a7957fb6b5c
Author: Sebb <sebb@apache.org>
AuthorDate: Sat Aug 3 21:51:20 2019 +0100

    Cheaper to write directly to file; fail fast if too big
---
 .../views/actions/check-signature.json.rb          | 25 ++++++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/www/secretary/workbench/views/actions/check-signature.json.rb b/www/secretary/workbench/views/actions/check-signature.json.rb
index f914df7..7534a4b 100644
--- a/www/secretary/workbench/views/actions/check-signature.json.rb
+++ b/www/secretary/workbench/views/actions/check-signature.json.rb
@@ -11,15 +11,28 @@ ENV['GNUPGHOME'] = GNUPGHOME if GNUPGHOME
 KEYSERVERS = %w{sks-keyservers.net keyserver.ubuntu.com}
 # N.B. ensure the keyserver URI is known below
 MAX_KEY_SIZE = 20700 # don't import if the ascii keyfile is larger than this
+
 message = Mailbox.find(@message)
 
 require 'net/http'
-def getURI(uri)
+
+# fetch the Key from the URI and store in the file
+def getURI(uri,file)
   uri = URI.parse(uri)
   Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |https|
-    request = Net::HTTP::Get.new(uri.request_uri)
-    response = https.request(request)
-    return response.body
+    https.request_get(uri.request_uri) do |res|
+      cl = res.content_length
+      Wunderbar.warn "Content-Length: #{cl}"
+      if cl > MAX_KEY_SIZE # fail early
+        raise Exception.new("Content-Length: #{cl} > #{MAX_KEY_SIZE}")
+      end
+      File.open(file,"w") do |f|
+        # Save the data directly; don't store in memory
+        res.read_body do |segment|
+            f.puts segment
+        end
+      end
+    end
   end
 end
 
@@ -83,9 +96,7 @@ begin
       Dir.mktmpdir do |dir|
         begin
           tmpfile = File.join(dir, keyid)
-          File.open(tmpfile,"w") do |f|
-            f.puts(getURI(uri))
-          end
+          getURI(uri, tmpfile)
           size = File.size(tmpfile)
           Wunderbar.warn "File: #{tmpfile} Size: #{size}"
           if size < MAX_KEY_SIZE # Don't import if it appears to be too big


Mime
View raw message