diff options
author | Thomas Klemm <github@tklemm.eu> | 2012-08-31 22:46:12 +0200 |
---|---|---|
committer | James Tucker <raggi@google.com> | 2013-01-04 09:31:11 -0500 |
commit | 907fb768d77e61e2aa9433d04f2a4ea483aca4ca (patch) | |
tree | f6dce2009f775fc6fda9b284021b0aee048dc53f | |
parent | fb5785294241a07deab95dea565267098fe186d2 (diff) | |
download | rack-907fb768d77e61e2aa9433d04f2a4ea483aca4ca.tar.gz |
Try to get headers working on Ruby 1.8.7
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | lib/rack/static.rb | 37 | ||||
-rw-r--r-- | test/spec_static.rb | 46 |
3 files changed, 60 insertions, 24 deletions
@@ -8,3 +8,4 @@ stage Gemfile.lock .rbx doc +.rbenv-version diff --git a/lib/rack/static.rb b/lib/rack/static.rb index d42e0364..286e5456 100644 --- a/lib/rack/static.rb +++ b/lib/rack/static.rb @@ -114,8 +114,7 @@ module Rack if can_serve(path) env["PATH_INFO"] = (path =~ /\/$/ ? path + @index : @urls[path]) if overwrite_file_path(path) - @path = env["PATH_INFO"] - set_headers + set_headers(env["PATH_INFO"]) @file_server.call(env) else @app.call(env) @@ -123,22 +122,30 @@ module Rack end # Convert header rules to headers - def set_headers + def set_headers(path) @header_rules.each do |rule, headers| - case rule - when :global # Global + if rule == :global # Global set_header(headers) - when :fonts # Fonts Shortcut - set_header(headers) if @path.match(%r{\.(?:ttf|otf|eot|woff|svg)\z}) - when String # Folder - path = ::Rack::Utils.unescape(@path) - set_header(headers) if - (path.start_with?(rule) || path.start_with?('/' + rule)) - when Array # Extension/Extensions + elsif rule == :fonts # Fonts Shortcut + if path.match(%r{\.(?:ttf|otf|eot|woff|svg)\z}) + set_header(headers) + end + elsif rule.instance_of?(String) # Folder + path = ::Rack::Utils.unescape(path) + if path.start_with?(rule) + set_header(headers) + elsif path.start_with?('/' + rule) + set_header(headers) + end + elsif rule.instance_of?(Array) # Extension/Extensions extensions = rule.join('|') - set_header(headers) if @path.match(%r{\.(#{extensions})\z}) - when Regexp # Flexible Regexp - set_header(headers) if @path.match(rule) + if path.match(%r{\.(#{extensions})\z}) + set_header(headers) + end + elsif rule.instance_of?(Regexp) # Flexible Regexp + if path.match(rule) + set_header(headers) + end else end end diff --git a/test/spec_static.rb b/test/spec_static.rb index 07572309..51df5dff 100644 --- a/test/spec_static.rb +++ b/test/spec_static.rb @@ -23,6 +23,16 @@ describe Rack::Static do @static_request = Rack::MockRequest.new(static(DummyApp.new, STATIC_OPTIONS)) @hash_request = Rack::MockRequest.new(static(DummyApp.new, HASH_OPTIONS)) + @header_rules = { + :header_rules => { + :global => {'Cache-Control' => 'public, max-age=100'}, + :fonts => {'Cache-Control' => 'public, max-age=200'}, + %w(png jpg) => {'Cache-Control' => 'public, max-age=300'}, + 'cgi/assets/folder/' => {'Cache-Control' => 'public, max-age=400'}, + '/cgi/assets/javascripts' => {'Cache-Control' => 'public, max-age=500'}, + /\.(css|erb)\z/ => {'Cache-Control' => 'public, max-age=600'}, + }} + it "serves files" do res = @request.get("/cgi/test") res.should.be.ok @@ -91,40 +101,58 @@ describe Rack::Static do res.headers['Cache-Control'].should == 'public, max-age=42' end - it "support HTTP header rules" do - opts = OPTIONS.merge(:header_rules => { - :global => {'Cache-Control' => 'public, max-age=100'}, - :fonts => {'Cache-Control' => 'public, max-age=200'}, - %w(png jpg) => {'Cache-Control' => 'public, max-age=300'}, - 'cgi/assets/folder/' => {'Cache-Control' => 'public, max-age=400'}, - '/cgi/assets/javascripts' => {'Cache-Control' => 'public, max-age=500'}, - /\.(css|erb)\z/ => {'Cache-Control' => 'public, max-age=600'}, - }) + it "support HTTP header rule :global" do + opts = OPTIONS.merge(@header_rules) request = Rack::MockRequest.new(static(DummyApp.new, opts)) # Global Headers via :global shortcut res = request.get('/cgi/assets/index.html') res.should.be.ok res.headers['Cache-Control'].should == 'public, max-age=100' + end + + it "support HTTP header rule :fonts" do + opts = OPTIONS.merge(@header_rules) + request = Rack::MockRequest.new(static(DummyApp.new, opts)) # Headers for web fonts via :fonts shortcut res = request.get('/cgi/assets/fonts/font.eot') res.should.be.ok res.headers['Cache-Control'].should == 'public, max-age=200' + end + + it "support HTTP header rules for file extensions via Array" do + opts = OPTIONS.merge(@header_rules) + request = Rack::MockRequest.new(static(DummyApp.new, opts)) # Headers for file extensions via array res = request.get('/cgi/assets/images/image.png') res.should.be.ok res.headers['Cache-Control'].should == 'public, max-age=300' + end + + it "support HTTP header rules for folders via String starting without '/'" do + opts = OPTIONS.merge(@header_rules) + request = Rack::MockRequest.new(static(DummyApp.new, opts)) # Headers for files in folder via string res = request.get('/cgi/assets/folder/test.js') res.should.be.ok res.headers['Cache-Control'].should == 'public, max-age=400' + end + + it "support HTTP header rules for folders via String starting with '/'" do + opts = OPTIONS.merge(@header_rules) + request = Rack::MockRequest.new(static(DummyApp.new, opts)) res = request.get('/cgi/assets/javascripts/app.js') res.should.be.ok res.headers['Cache-Control'].should == 'public, max-age=500' + end + + it "support flexible HTTP header rules via Regexp" do + opts = OPTIONS.merge(@header_rules) + request = Rack::MockRequest.new(static(DummyApp.new, opts)) # Flexible Headers via Regexp res = request.get('/cgi/assets/stylesheets/app.css') |