Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/rack/sendfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ def map_accel_path(env, path)
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
Expand Down
35 changes: 35 additions & 0 deletions test/spec_sendfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,39 @@ def open_file(path)
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