yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH] extras/try_gzip_static: resolve symlinks
Date: Wed, 22 Jun 2016 22:10:56 +0000	[thread overview]
Message-ID: <20160622221056.29276-1-e@80x24.org> (raw)

Static gzip files may not exist for symlinks, but they
could resolve to a file for which a pre-gzipped file
exists.
---
 extras/try_gzip_static.rb           |  7 ++++++-
 test/test_extras_try_gzip_static.rb | 25 +++++++++++++++----------
 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 @@ def head_no_gz(res, env, path, st)
   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 @@ def test_gzip_static
 
     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 @@ def test_gzip_static
         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

                 reply	other threads:[~2016-06-22 22:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/yahns/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160622221056.29276-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/yahns.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).