Skip to content
This repository
Browse code

Make static faster as we don't have to serve multiple paths anymore.

  • Loading branch information...
commit d5ad92ced1786b742c3ecce3cb60d851c7200bc9 1 parent afc8288
José Valim authored April 15, 2011
43  actionpack/lib/action_dispatch/middleware/static.rb
@@ -2,25 +2,23 @@
2 2
 
3 3
 module ActionDispatch
4 4
   class FileHandler
5  
-    def initialize(at, root)
6  
-      @at, @root     = at.chomp('/'), root.chomp('/')
7  
-      @compiled_at   = @at.blank? ? nil : /^#{Regexp.escape(at)}/
  5
+    def initialize(root)
  6
+      @root          = root.chomp('/')
8 7
       @compiled_root = /^#{Regexp.escape(root)}/
9 8
       @file_server   = ::Rack::File.new(@root)
10 9
     end
11 10
 
12 11
     def match?(path)
13 12
       path = path.dup
14  
-      if !@compiled_at || path.sub!(@compiled_at, '')
15  
-        full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
16  
-        paths = "#{full_path}#{ext}"
17 13
 
18  
-        matches = Dir[paths]
19  
-        match = matches.detect { |m| File.file?(m) }
20  
-        if match
21  
-          match.sub!(@compiled_root, '')
22  
-          match
23  
-        end
  14
+      full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
  15
+      paths = "#{full_path}#{ext}"
  16
+
  17
+      matches = Dir[paths]
  18
+      match = matches.detect { |m| File.file?(m) }
  19
+      if match
  20
+        match.sub!(@compiled_root, '')
  21
+        match
24 22
       end
25 23
     end
26 24
 
@@ -39,9 +37,9 @@ def ext
39 37
   class Static
40 38
     FILE_METHODS = %w(GET HEAD).freeze
41 39
 
42  
-    def initialize(app, roots)
  40
+    def initialize(app, path)
43 41
       @app = app
44  
-      @file_handlers = create_file_handlers(roots)
  42
+      @file_handler = FileHandler.new(path)
45 43
     end
46 44
 
47 45
     def call(env)
@@ -49,24 +47,13 @@ def call(env)
49 47
       method = env['REQUEST_METHOD']
50 48
 
51 49
       if FILE_METHODS.include?(method)
52  
-        @file_handlers.each do |file_handler|
53  
-          if match = file_handler.match?(path)
54  
-            env["PATH_INFO"] = match
55  
-            return file_handler.call(env)
56  
-          end
  50
+        if match = @file_handler.match?(path)
  51
+          env["PATH_INFO"] = match
  52
+          return @file_handler.call(env)
57 53
         end
58 54
       end
59 55
 
60 56
       @app.call(env)
61 57
     end
62  
-
63  
-    private
64  
-      def create_file_handlers(roots)
65  
-        roots = { '' => roots } unless roots.is_a?(Hash)
66  
-
67  
-        roots.map do |at, root|
68  
-          FileHandler.new(at, root) if File.exist?(root)
69  
-        end.compact
70  
-      end
71 58
   end
72 59
 end
33  actionpack/test/dispatch/static_test.rb
@@ -47,35 +47,4 @@ def setup
47 47
   end
48 48
 
49 49
   include StaticTests
50  
-end
51  
-
52  
-class MultipleDirectorisStaticTest < ActiveSupport::TestCase
53  
-  DummyApp = lambda { |env|
54  
-    [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
55  
-  }
56  
-  App = ActionDispatch::Static.new(DummyApp,
57  
-            { "/"     => "#{FIXTURE_LOAD_PATH}/public",
58  
-              "/blog" => "#{FIXTURE_LOAD_PATH}/blog_public",
59  
-              "/foo"  => "#{FIXTURE_LOAD_PATH}/non_existing_dir"
60  
-            })
61  
-
62  
-  def setup
63  
-    @app = App
64  
-  end
65  
-
66  
-  include StaticTests
67  
-
68  
-  test "serves files from other mounted directories" do
69  
-    assert_html "/blog/index.html", get("/blog/index.html")
70  
-    assert_html "/blog/index.html", get("/blog/index")
71  
-    assert_html "/blog/index.html", get("/blog/")
72  
-
73  
-    assert_html "/blog/blog.html", get("/blog/blog/")
74  
-    assert_html "/blog/blog.html", get("/blog/blog.html")
75  
-    assert_html "/blog/blog.html", get("/blog/blog")
76  
-
77  
-    assert_html "/blog/subdir/index.html", get("/blog/subdir/index.html")
78  
-    assert_html "/blog/subdir/index.html", get("/blog/subdir/")
79  
-    assert_html "/blog/subdir/index.html", get("/blog/subdir")
80  
-  end
81  
-end
  50
+end
23  actionpack/test/template/asset_tag_helper_test.rb
@@ -477,15 +477,6 @@ def test_string_asset_id
477 477
     assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
478 478
   end
479 479
 
480  
-  def test_env_asset_path
481  
-    @controller.config.asset_path = "/assets%s"
482  
-    def @controller.env; @_env ||= {} end
483  
-    @controller.env["action_dispatch.asset_path"] = "/omg%s"
484  
-
485  
-    expected_path = "/assets/omg/images/rails.png"
486  
-    assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
487  
-  end
488  
-
489 480
   def test_proc_asset_id
490 481
     @controller.config.asset_path = Proc.new do |asset_path|
491 482
       "/assets.v12345#{asset_path}"
@@ -495,20 +486,6 @@ def test_proc_asset_id
495 486
     assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
496 487
   end
497 488
 
498  
-  def test_env_proc_asset_path
499  
-    @controller.config.asset_path = Proc.new do |asset_path|
500  
-      "/assets.v12345#{asset_path}"
501  
-    end
502  
-
503  
-    def @controller.env; @_env ||= {} end
504  
-    @controller.env["action_dispatch.asset_path"] = Proc.new do |asset_path|
505  
-      "/omg#{asset_path}"
506  
-    end
507  
-
508  
-    expected_path = "/assets.v12345/omg/images/rails.png"
509  
-    assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
510  
-  end
511  
-
512 489
   def test_image_tag_interpreting_email_cid_correctly
513 490
     # An inline image has no need for an alt tag to be automatically generated from the cid:
514 491
     assert_equal '<img src="cid:thi%25%25sis@acontentid" />', image_tag("cid:thi%25%25sis@acontentid")
2  railties/lib/rails/application.rb
@@ -161,7 +161,7 @@ def default_middleware_stack
161 161
         end
162 162
 
163 163
         if config.serve_static_assets
164  
-          middleware.use ::ActionDispatch::Static, "/" => paths["public"].first
  164
+          middleware.use ::ActionDispatch::Static, paths["public"].first
165 165
         end
166 166
 
167 167
         middleware.use ::Rack::Lock unless config.allow_concurrency

0 notes on commit d5ad92c

Please sign in to comment.
Something went wrong with that request. Please try again.