rack/rack — pull request #422 by thomasklemm (2012-08-31 19:34:00+00:00)
Title: Allow Rack::Static to assign HTTP Header based on Rules

Hey,

this PR allows Rack::Static to assign different headers to served files based on certain rules.

```ruby
# Set custom HTTP Headers for based on rules:
#
# use Rack::Static, :root => 'public',
# :header_rules => {
# rule => { header_field => content },
# another_rule => { header_field => content }
# }
#
# These rules for generating HTTP Headers are shipped along:
# 1) Global
# :global => Matches every file
#
# Ex.:
# :header_rules => {
# :global => {'Cache-Control' => 'public, max-age=123'}
# }
#
# 2) Folders
# '/folder' => Matches all files in a certain folder
# '/folder/subfolder' => ...
# Note: Provide the folder as a string,
# with or without the starting slash
#
# 3) File Extensions
# ['css', 'js'] => Will match all files ending in .css or .js
# %w(css js) => ...
# Note: Provide the file extensions in an array,
# use any ruby syntax you like to set that array up
#
# 4) Regular Expressions / Regexp
# %r{\.(?:css|js)\z} => will match all files ending in .css or .js
# /\.(?:eot|ttf|otf|woff|svg)\z/ => will match all files ending
# in the most common web font formats
#
# 5) Shortcuts
# There is currently only one shortcut defined.
# :fonts => will match all files ending in eot, ttf, otf, woff, svg
# using the Regexp stated right above
#
# Example use:
#
# use Rack::Static, :root => 'public',
# :header_rules => {
# # Cache all static files in http caches as well as on the client
# :global => { 'Cache-Control' => 'public, max-age=31536000' },
# # Provide Web Fonts with Access-Control-Headers
# :fonts => { 'Access-Control-Allow-Origin' => '*' }
# }
#
# Note: The rules will be applied in the order they are provided,
# thus more special rules further down below can override
# general global HTTP header settings
```

Note: I removed the :headers option in favor of :header_rules => {:global => {}}.
It has been added through a PR of mine a few weeks ago that did not make it into a release yet.

Thanks for reviewing this. I think there might be some demand for this, I find it especially useful to add certain access control headers that firefox requires web fonts to be served with when served via a CDN.

Thomas