Skip to content

Commit

Permalink
Try to get headers working on Ruby 1.8.7
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasklemm committed Aug 31, 2012
1 parent 308e57e commit a446874
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ stage
Gemfile.lock
.rbx
doc
.rbenv-version
37 changes: 22 additions & 15 deletions lib/rack/static.rb
Expand Up @@ -114,31 +114,38 @@ def call(env)

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)
end
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
Expand Down
46 changes: 37 additions & 9 deletions test/spec_static.rb
Expand Up @@ -23,6 +23,16 @@ def static(app, *args)
@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
Expand Down Expand Up @@ -91,40 +101,58 @@ def static(app, *args)
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')
Expand Down

0 comments on commit a446874

Please sign in to comment.