about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-06-22 22:08:05 +0000
committerEric Wong <e@80x24.org>2016-06-22 22:08:05 +0000
commit5347a8ab447bd094fa9e0923663fc734a78222ee (patch)
tree8f63f59903e275a4f4ab9581687334bf0550ef67
parentcc12407ec90b0c94c87643439ff3727f94c2bf42 (diff)
downloadyahns-5347a8ab447bd094fa9e0923663fc734a78222ee.tar.gz
Static gzip files may not exist for symlinks, but they
could resolve to a file for which a pre-gzipped file
exists.
-rw-r--r--extras/try_gzip_static.rb7
-rw-r--r--test/test_extras_try_gzip_static.rb25
2 files changed, 21 insertions, 11 deletions
diff --git a/extras/try_gzip_static.rb b/extras/try_gzip_static.rb
index 31c1aa1..ed561cb 100644
--- a/extras/try_gzip_static.rb
+++ b/extras/try_gzip_static.rb
@@ -92,7 +92,12 @@ class TryGzipStatic
   def stat_path(env)
     path = fspath(env) or return r(403)
     begin
-      st = File.stat(path)
+      st = File.lstat(path)
+      if st.symlink?
+        path = File.readlink(path)
+        path[0] == '/'.freeze or path = "#@root/#{path}"
+        st = File.stat(path)
+      end
       return r(404) unless st.file?
       return r(403) unless st.readable?
       [ path, st ]
diff --git a/test/test_extras_try_gzip_static.rb b/test/test_extras_try_gzip_static.rb
index 4d20b5a..60129df 100644
--- a/test/test_extras_try_gzip_static.rb
+++ b/test/test_extras_try_gzip_static.rb
@@ -48,6 +48,8 @@ class TestExtrasTryGzipStatic < Testcase
 
     begin # setup
       gpl = "#{tmpdir}/COPYING"
+      File.symlink gpl, "#{tmpdir}/COPYING.abssymlink"
+      File.symlink "COPYING", "#{tmpdir}/COPYING.relsymlink"
       gplgz = "#{tmpdir}/COPYING.gz"
       FileUtils.cp("COPYING", gpl)
       _, status = Process.waitpid2(fork do
@@ -88,16 +90,19 @@ class TestExtrasTryGzipStatic < Testcase
         when "HEAD" then assert_nil body
         end
 
-        req = "#{m} /COPYING HTTP/1.0\r\nAccept-Encoding: gzip"
-        body = check.call(req) do |head|
-          assert_match %r{^Content-Encoding: gzip\b}, head
-          assert_match %r{^Content-Type: text/plain\b}, head
-          assert_match %r{^Content-Length: #{gz_st.size}\b}, head
-        end
-        case m
-        when "GET"
-          assert_equal GPL_TEXT, Zlib::GzipReader.new(StringIO.new(body)).read
-        when "HEAD" then assert_nil body
+        %w(COPYING COPYING.abssymlink COPYING.relsymlink).each do |path|
+          req = "#{m} /#{path} HTTP/1.0\r\nAccept-Encoding: gzip"
+          body = check.call(req) do |head|
+            assert_match %r{^Content-Encoding: gzip\b}, head
+            assert_match %r{^Content-Type: text/plain\b}, head
+            assert_match %r{^Content-Length: #{gz_st.size}\b}, head
+          end
+          case m
+          when "GET"
+            body =StringIO.new(body)
+            assert_equal GPL_TEXT, Zlib::GzipReader.new(body).read
+          when "HEAD" then assert_nil body
+          end
         end
       end
     end