Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created May 27, 2011 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/995116 to your computer and use it in GitHub Desktop.
Save romanbsd/995116 to your computer and use it in GitHub Desktop.
Patch Rack::File to look for pre-gzipped files (mimic nginx's gzip_static behavior)
# Patch Rack::File to support pre-gzipped files
module Rack
class File
def _call_with_static_gzip(env)
@path_info = Utils.unescape(env["PATH_INFO"])
return forbidden if @path_info.include? ".."
@path = F.join(@root, @path_info) + '.gz'
if F.file?(@path) && F.readable?(@path)
serving
else
_call_without_static_gzip(env)
end
end
alias_method_chain :_call, :static_gzip
def serving_with_static_gzip
res = serving_without_static_gzip
if @path.end_with?('.gz')
headers = res[1]
headers['Content-Encoding'] = 'gzip'
headers['Content-Type'] = Mime.mime_type(F.extname(@path.sub(/.gz$/, '')), headers['Content-Type'])
end
res
end
alias_method_chain :serving, :static_gzip
end
end
module ActionDispatch
class Static
def file_exist?(path)
full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
[full_path, full_path + '.gz'].any? do |_full_path|
File.file?(_full_path) && File.readable?(_full_path)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment