Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

reimplement as subclass of ActionDispatch::Static #3

Merged
merged 1 commit into from

2 participants

@jjb
  • there was a lot of copy-pasted code from ActionDispatch::Static
  • now the code is smaller and smaller is good
  • if other behavior or api changes with ActionDispatch::Static, this class will automatically get it
  • an actual clear use case for inheritance, yay!
  • as such, maybe easier for ActionDispatch::Static maintainers to grok and to consider for integration of features upstream

minor todo/problem: tighter lockdown of version dependencies for actionpack? (both min and max versions).

What do you think?

@jbaudanza
Owner

Looks good to me. I assume all the tests pass?

@jjb
@jjb

okay, i did a few checks with a local app, all looks good!

@jbaudanza jbaudanza merged commit f2d24a9 into jbaudanza:master
@jjb

cool!

@jjb jjb deleted the jjb:inherits-from-actiondispatch-static branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 29, 2013
  1. @jjb
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 70 deletions.
  1. +1 −1  lib/action_dispatch/gz_railtie.rb
  2. +20 −69 lib/action_dispatch/gz_static.rb
View
2  lib/action_dispatch/gz_railtie.rb
@@ -6,4 +6,4 @@ class GzRailtie < Rails::Railtie
app.config.static_cache_control
end
end
-end
+end
View
89 lib/action_dispatch/gz_static.rb
@@ -1,89 +1,40 @@
# encoding: utf-8
-require 'rack/utils'
require 'rack/mime'
require 'rack/file'
-require 'active_support/core_ext/uri'
require 'action_dispatch/gz_railtie' if defined?(Rails)
+require 'action_dispatch/middleware/static'
+require 'action_controller'
module ActionDispatch
- class GzStatic
- def initialize(app, root, cache_control=nil)
- @app = app
- @root = root.chomp('/')
- @compiled_root = /^#{Regexp.escape(@root)}/
-
- # I wish there was a better way to do this. Rack::File changed its
- # interface after 1.4.1 and I don't know of a way to sniff out the change
- # without comparing the gem version
- if Gem.loaded_specs['rack'].version > Gem::Version.new('1.4.1')
- cache_control = {'Cache-Control' => cache_control}
- end
-
- @file_server = ::Rack::File.new(@root, cache_control)
- end
-
- def match?(path)
- path = path.dup
-
- full_path = path.empty? ? @root : File.join(@root, escape_glob_chars(unescape_path(path)))
- paths = "#{full_path}#{ext}"
-
- matches = Dir[paths]
- matches.detect { |m| File.file?(m) }
- end
-
+ class GzStatic < Static
def call(env)
- case env['REQUEST_METHOD']
- when 'GET', 'HEAD'
- path = env['PATH_INFO'].chomp('/')
- if match = match?(path)
+ return super unless %w[GET HEAD].include? env['REQUEST_METHOD']
+ path = env['PATH_INFO'].chomp('/')
+ return super unless @file_handler.match?(path)
- compressed_match = "#{match}.gz"
- compressed_exists = File.file?(compressed_match)
-
- wants_compressed = !!(env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/)
+ compressed_path = "#{path}.gz"
- if wants_compressed && compressed_exists
- mime = Rack::Mime.mime_type(::File.extname(match), 'text/plain')
- match = compressed_match
- end
+ compressed_exists = @file_handler.match?(compressed_path)
+ wants_compressed = env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
- match.sub!(@compiled_root, '')
- env["PATH_INFO"] = ::Rack::Utils.escape(match)
- status, headers, body = @file_server.call(env)
+ if wants_compressed && compressed_exists
+ env["PATH_INFO"] = compressed_path
+ end
- if compressed_exists
- headers['Vary'] = 'Accept-Encoding'
+ status, headers, body = super
- if wants_compressed
- headers['Content-Encoding'] = 'gzip'
- headers['Content-Type'] = mime if mime
- end
- end
+ if compressed_exists
+ headers['Vary'] = 'Accept-Encoding'
- return [status, headers, body]
+ if wants_compressed
+ headers['Content-Encoding'] = 'gzip'
+ mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
+ headers['Content-Type'] = mime if mime
end
end
- @app.call(env)
- end
-
- def ext
- @ext ||= begin
- #ext = ::ActionController::Base.default_static_extension
- ext = ".html"
- "{,#{ext},/index#{ext}}"
- end
- end
-
- def unescape_path(path)
- URI.parser.unescape(path)
- end
-
- def escape_glob_chars(path)
- path.force_encoding('binary') if path.respond_to? :force_encoding
- path.gsub(/[*?{}\[\]]/, "\\\\\\&")
+ [status, headers, body]
end
end
end
Something went wrong with that request. Please try again.