about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-11-02 01:00:00 +0000
committerEric Wong <normalperson@yhbt.net>2013-11-02 01:19:45 +0000
commit13a09dec6c029e01e8d959b0bf0feb94d72ae32d (patch)
tree05222cbf7927ce114320beaa0bf5a7a9bb3e676d /lib
parent1ffa99f4f8f653fc016affecbe41d91a0b85f90d (diff)
downloadyahns-13a09dec6c029e01e8d959b0bf0feb94d72ae32d.tar.gz
When running a static file server, we must account for filesystem
activity outside of our control where files may grow/shrink as
they're being served.

For truncated files, we must abort any persistent connections
downloading a truncated file to avoid confusing clients because
the Content-Length header was already set to the big value.

We also must ensure (we already did so before this commit,
this just adds a test for it) we do not send additional data
when a file grows on us after we've started sending the response.
Diffstat (limited to 'lib')
-rw-r--r--lib/yahns/wbuf_common.rb10
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/yahns/wbuf_common.rb b/lib/yahns/wbuf_common.rb
index cfafaa2..b3a4502 100644
--- a/lib/yahns/wbuf_common.rb
+++ b/lib/yahns/wbuf_common.rb
@@ -14,8 +14,16 @@ module Yahns::WbufCommon # :nodoc:
       @sf_offset += rv # keep going otherwise
     when :wait_writable, :wait_readable
       return rv
+    when nil
+      # response got truncated, drop the connection
+      # this may happens when using Rack::File or similar, we can't
+      # keep the connection alive because we already sent our Content-Length
+      # header the client would be confused.
+      @wbuf_persist = false
+      return wbuf_close(client)
     else
-      raise "BUG: #{rv.nil? ? "EOF" : rv.inspect} on tmpio=#{@tmpio.inspect} " \
+      raise "BUG: rv=#{rv.inspect} " \
+            "on tmpio=#{@tmpio.inspect} " \
             "sf_offset=#@sf_offset sf_count=#@sf_count"
     end while true
   end