Stream from the venti server instead of reading into memory.
authorPete <pete@debu.gs>
Sun, 21 Jun 2020 06:05:22 +0000 (23:05 -0700)
committerPete <pete@debu.gs>
Sun, 21 Jun 2020 06:05:22 +0000 (23:05 -0700)
config.ru
lib/webvac.rb

index 37b8bfccbc0060bc7ae0a273cfd4fb7836fd575e..3293b2db44e2ecbb0c9ed68b061887141fac5293 100644 (file)
--- a/config.ru
+++ b/config.ru
@@ -32,6 +32,9 @@ module WebVac
                                'Content-Disposition' => "filename=\"#{name}\"",
                                'Fortune' => Fortunes.sample,
                        }.merge!(t.metadata(score) || {}).tap { |h|
                                'Content-Disposition' => "filename=\"#{name}\"",
                                'Fortune' => Fortunes.sample,
                        }.merge!(t.metadata(score) || {}).tap { |h|
+                               # Now that we stream, this condition never happens.
+                               # Maybe it's a good idea to wrap the IO object and fill in
+                               # the extra data as needed.
                                if contents
                                        h['Content-Type'] ||= t.guess_mime(contents) rescue nil
                                        h['Content-Length'] ||= contents.bytesize.to_s
                                if contents
                                        h['Content-Type'] ||= t.guess_mime(contents) rescue nil
                                        h['Content-Length'] ||= contents.bytesize.to_s
@@ -66,14 +69,12 @@ module WebVac
                        s = tab.path2score p
                        return [404, {}, ["404 Not found\nNo such path: #{p}\n"]] unless s
                        ct = Time.parse(env['HTTP_IF_MODIFIED_SINCE']) rescue nil
                        s = tab.path2score p
                        return [404, {}, ["404 Not found\nNo such path: #{p}\n"]] unless s
                        ct = Time.parse(env['HTTP_IF_MODIFIED_SINCE']) rescue nil
+                       hs = headers(tab, p, s, nil)
                        if ct && ct.to_i > 0
                        if ct && ct.to_i > 0
-                               hs = headers(tab, p, s, nil)
                                mt = Time.parse(hs['Last-Modified']) rescue nil
                                return [304, hs, []] if mt && mt > ct
                        end
                                mt = Time.parse(hs['Last-Modified']) rescue nil
                                return [304, hs, []] if mt && mt > ct
                        end
-                       contents = vac.load! s
-                       hs = headers(tab, p, s, contents)
-                       [200, headers(tab, p, s, contents), [contents]]
+                       [200, hs, vac.load_io(s)]
                }
        end
 
                }
        end
 
index fff4eaa2ba4ecbc22346a31e60b1be53b0441056..de1f67e1268ed35644e7da543fc7e354082d7321 100644 (file)
@@ -89,14 +89,18 @@ module WebVac
                        io.read.chomp.sub(/^vac:/, '')
                end
 
                        io.read.chomp.sub(/^vac:/, '')
                end
 
-               def load! vac
+               def load_io vac
                        unless /^vac:[a-f0-9]{40}$/.match(vac)
                                raise ArgumentError, "#{vac.inspect} not a vac score?"
                        end
                        IO.popen(
                                {'venti' => config.venti_server},
                                ["#{config.plan9bin}/unvac", '-c', vac]
                        unless /^vac:[a-f0-9]{40}$/.match(vac)
                                raise ArgumentError, "#{vac.inspect} not a vac score?"
                        end
                        IO.popen(
                                {'venti' => config.venti_server},
                                ["#{config.plan9bin}/unvac", '-c', vac]
-                       ).tap { |io| Thread.new { Process.wait(io.pid) } }.read
+                       ).tap { |io| Thread.new { Process.wait(io.pid) } }                      
+               end
+
+               def load! vac
+                       load_io(vac).read
                end
        end
 
                end
        end