Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow Rack::File and Rack::Static to serve custom HTTP Headers

  • Loading branch information...
commit beddb2a46626515690d9d2ea02b30904e8a346af 1 parent ab67e70
@thomasklemm thomasklemm authored
View
13 lib/rack/file.rb
@@ -21,9 +21,14 @@ class File
alias :to_path :path
- def initialize(root, cache_control = nil)
+ def initialize(root, headers={})
@root = root
- @cache_control = cache_control
+ # Allow a cache_control string for backwards compatibility
+ if headers.instance_of? String
+ @headers = { 'Cache-Control' => headers }
+ else
+ @headers = headers
+ end
end
def call(env)
@@ -78,7 +83,9 @@ def serving(env)
},
env["REQUEST_METHOD"] == "HEAD" ? [] : self
]
- response[1]['Cache-Control'] = @cache_control if @cache_control
+
+ # Set custom headers
+ @headers.each { |field, content| response[1][field] = content } if @headers
# NOTE:
# We check via File::size? whether this file provides size info
View
16 lib/rack/static.rb
@@ -32,6 +32,16 @@ module Rack
#
# use Rack::Static, :root => 'public', :cache_control => 'public'
#
+ # Set custom HTTP Headers for all served files:
+ #
+ # use Rack::Static, :root => 'public', :headers =>
+ # {'Cache-Control' => 'public, max-age=31536000',
+ # 'Access-Control-Allow-Origin' => '*'}
+ #
+ # Note: If both :headers => {'Cache-Control' => 'public, max-age=42'}
+ # and :cache_control => 'public, max-age=38' are being provided
+ # the :headers setting takes precedence
+ #
class Static
@@ -40,8 +50,10 @@ def initialize(app, options={})
@urls = options[:urls] || ["/favicon.ico"]
@index = options[:index]
root = options[:root] || Dir.pwd
- cache_control = options[:cache_control]
- @file_server = Rack::File.new(root, cache_control)
+ headers = options[:headers] || {}
+ # Allow for legacy :cache_control option while prioritizing headers hash settings
+ headers['Cache-Control'] ||= options[:cache_control] if options[:cache_control]
+ @file_server = Rack::File.new(root, headers)
end
def overwrite_file_path(path)
View
21 test/spec_file.rb
@@ -145,7 +145,7 @@ def file(*args)
res["Content-Range"].should.equal "bytes */193"
end
- should "support cache control options" do
+ should "support legacy cache control options provided as string" do
env = Rack::MockRequest.env_for("/cgi/test")
status, heads, _ = file(DOCROOT, 'public, max-age=38').call(env)
@@ -153,6 +153,25 @@ def file(*args)
heads['Cache-Control'].should.equal 'public, max-age=38'
end
+ should "support custom http headers" do
+ env = Rack::MockRequest.env_for("/cgi/test")
+ status, heads, _ = file(DOCROOT, 'Cache-Control' => 'public, max-age=38',
+ 'Access-Control-Allow-Origin' => '*').call(env)
+
+ status.should.equal 200
+ heads['Cache-Control'].should.equal 'public, max-age=38'
+ heads['Access-Control-Allow-Origin'].should.equal '*'
+ end
+
+ should "support not add custom http headers if none are supplied" do
+ env = Rack::MockRequest.env_for("/cgi/test")
+ status, heads, _ = file(DOCROOT).call(env)
+
+ status.should.equal 200
+ heads['Cache-Control'].should.equal nil
+ heads['Access-Control-Allow-Origin'].should.equal nil
+ end
+
should "only support GET and HEAD requests" do
req = Rack::MockRequest.new(file(DOCROOT))
View
21 test/spec_static.rb
@@ -12,7 +12,7 @@ def call(env)
def static(app, *args)
Rack::Lint.new Rack::Static.new(app, *args)
end
-
+
root = File.expand_path(File.dirname(__FILE__))
OPTIONS = {:urls => ["/cgi"], :root => root}
@@ -78,4 +78,23 @@ def static(app, *args)
res.headers['Cache-Control'].should == 'public'
end
+ it "supports serving custom http headers" do
+ opts = OPTIONS.merge(:headers => {'Cache-Control' => 'public, max-age=42',
+ 'Access-Control-Allow-Origin' => '*'})
+ request = Rack::MockRequest.new(static(DummyApp.new, opts))
+ res = request.get("/cgi/test")
+ res.should.be.ok
+ res.headers['Cache-Control'].should == 'public, max-age=42'
+ res.headers['Access-Control-Allow-Origin'].should == '*'
+ end
+
+ it "allows headers hash to take priority over fixed cache-control" do
+ opts = OPTIONS.merge(:cache_control => 'public, max-age=38',
+ :headers => {'Cache-Control' => 'public, max-age=42'})
+ request = Rack::MockRequest.new(static(DummyApp.new, opts))
+ res = request.get("/cgi/test")
+ res.should.be.ok
+ res.headers['Cache-Control'].should == 'public, max-age=42'
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.