summary refs log tree commit
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2017-08-10 09:32:46 -0700
committerGitHub <noreply@github.com>2017-08-10 09:32:46 -0700
commitcb00d24c473ae476bdec3fc122babb33bb9ab4fc (patch)
tree47214eed8c9ef3d8a2f80b67cc7d64b6925c7c16
parent911c4fe15e3e57d44ac891c0cbabbf44bdf71201 (diff)
parentf2361997623e5141e6baa907d79f1212b36fbb8b (diff)
downloadrack-cb00d24c473ae476bdec3fc122babb33bb9ab4fc.tar.gz
Merge pull request #1187 from yoshuki/multiple-accel-mapping
Add multi mapping support via header
-rw-r--r--lib/rack/sendfile.rb8
-rw-r--r--test/spec_sendfile.rb35
2 files changed, 41 insertions, 2 deletions
diff --git a/lib/rack/sendfile.rb b/lib/rack/sendfile.rb
index bdb7cf2f..34c1a84e 100644
--- a/lib/rack/sendfile.rb
+++ b/lib/rack/sendfile.rb
@@ -150,8 +150,12 @@ module Rack
       if mapping = @mappings.find { |internal,_| internal =~ path }
         path.sub(*mapping)
       elsif mapping = env['HTTP_X_ACCEL_MAPPING']
-        internal, external = mapping.split('=', 2).map(&:strip)
-        path.sub(/^#{internal}/i, external)
+        mapping.split(',').map(&:strip).each do |m|
+          internal, external = m.split('=', 2).map(&:strip)
+          new_path = path.sub(/^#{internal}/i, external)
+          return new_path unless path == new_path
+        end
+        path
       end
     end
   end
diff --git a/test/spec_sendfile.rb b/test/spec_sendfile.rb
index 18689857..1eb9413c 100644
--- a/test/spec_sendfile.rb
+++ b/test/spec_sendfile.rb
@@ -122,4 +122,39 @@ describe Rack::Sendfile do
       FileUtils.remove_entry_secure dir2
     end
   end
+
+  it "sets X-Accel-Redirect response header and discards body when initialized with multiple mappings via header" do
+    begin
+      dir1 = Dir.mktmpdir
+      dir2 = Dir.mktmpdir
+
+      first_body = open_file(File.join(dir1, 'rack_sendfile'))
+      first_body.puts 'hello world'
+
+      second_body = open_file(File.join(dir2, 'rack_sendfile'))
+      second_body.puts 'goodbye world'
+
+      headers = {
+        'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
+        'HTTP_X_ACCEL_MAPPING' => "#{dir1}/=/foo/bar/, #{dir2}/=/wibble/"
+      }
+
+      request(headers, first_body) do |response|
+        response.must_be :ok?
+        response.body.must_be :empty?
+        response.headers['Content-Length'].must_equal '0'
+        response.headers['X-Accel-Redirect'].must_equal '/foo/bar/rack_sendfile'
+      end
+
+      request(headers, second_body) do |response|
+        response.must_be :ok?
+        response.body.must_be :empty?
+        response.headers['Content-Length'].must_equal '0'
+        response.headers['X-Accel-Redirect'].must_equal '/wibble/rack_sendfile'
+      end
+    ensure
+      FileUtils.remove_entry_secure dir1
+      FileUtils.remove_entry_secure dir2
+    end
+  end
 end