Skip to content
This repository
Browse code

Add internal middleware stack to Dispatcher

  config.middleware.use Rack::Cache
  • Loading branch information...
commit 06ed8e451198b2296d8b2752741e259b4f995081 1 parent 3c07a88
Joshua Peek authored December 01, 2008
1  actionpack/lib/action_controller.rb
@@ -57,6 +57,7 @@ def self.load_all!
57 57
   autoload :Integration, 'action_controller/integration'
58 58
   autoload :IntegrationTest, 'action_controller/integration'
59 59
   autoload :Layout, 'action_controller/layout'
  60
+  autoload :MiddlewareStack, 'action_controller/middleware_stack'
60 61
   autoload :MimeResponds, 'action_controller/mime_responds'
61 62
   autoload :PolymorphicRoutes, 'action_controller/polymorphic_routes'
62 63
   autoload :RackRequest, 'action_controller/rack_process'
8  actionpack/lib/action_controller/dispatcher.rb
@@ -85,6 +85,9 @@ def failsafe_logger
85 85
         end
86 86
     end
87 87
 
  88
+    cattr_accessor :middleware
  89
+    self.middleware = MiddlewareStack.new
  90
+
88 91
     cattr_accessor :error_file_path
89 92
     self.error_file_path = Rails.public_path if defined?(Rails.public_path)
90 93
 
@@ -93,6 +96,7 @@ def failsafe_logger
93 96
 
94 97
     def initialize(output = $stdout, request = nil, response = nil)
95 98
       @output, @request, @response = output, request, response
  99
+      @app = @@middleware.build(lambda { |env| self._call(env) })
96 100
     end
97 101
 
98 102
     def dispatch_unlocked
@@ -127,6 +131,10 @@ def dispatch_cgi(cgi, session_options)
127 131
     end
128 132
 
129 133
     def call(env)
  134
+      @app.call(env)
  135
+    end
  136
+
  137
+    def _call(env)
130 138
       @request = RackRequest.new(env)
131 139
       @response = RackResponse.new(@request)
132 140
       dispatch
42  actionpack/lib/action_controller/middleware_stack.rb
... ...
@@ -0,0 +1,42 @@
  1
+module ActionController
  2
+  class MiddlewareStack < Array
  3
+    class Middleware
  4
+      attr_reader :klass, :args, :block
  5
+
  6
+      def initialize(klass, *args, &block)
  7
+        @klass = klass.is_a?(Class) ? klass : klass.to_s.constantize
  8
+        @args = args
  9
+        @block = block
  10
+      end
  11
+
  12
+      def ==(middleware)
  13
+        case middleware
  14
+        when Middleware
  15
+          klass == middleware.klass
  16
+        when Class
  17
+          klass == middleware
  18
+        else
  19
+          klass == middleware.to_s.constantize
  20
+        end
  21
+      end
  22
+
  23
+      def inspect
  24
+        str = @klass.to_s
  25
+        @args.each { |arg| str += ", #{arg.inspect}" }
  26
+        str
  27
+      end
  28
+
  29
+      def build(app)
  30
+        klass.new(app, *args, &block)
  31
+      end
  32
+    end
  33
+
  34
+    def use(*args, &block)
  35
+      push(Middleware.new(*args, &block))
  36
+    end
  37
+
  38
+    def build(app)
  39
+      reverse.inject(app) { |a, e| e.build(a) }
  40
+    end
  41
+  end
  42
+end
5  railties/lib/initializer.rb
@@ -881,6 +881,11 @@ def to_prepare(&callback)
881 881
       end
882 882
     end
883 883
 
  884
+    def middleware
  885
+      require 'action_controller'
  886
+      ActionController::Dispatcher.middleware
  887
+    end
  888
+
884 889
     def builtin_directories
885 890
       # Include builtins only in the development environment.
886 891
       (environment == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
7  railties/lib/tasks/middleware.rake
... ...
@@ -0,0 +1,7 @@
  1
+desc 'Prints out your Rack middleware stack'
  2
+task :middleware => :environment do
  3
+  ActionController::Dispatcher.middleware.each do |middleware|
  4
+    puts "use #{middleware.inspect}"
  5
+  end
  6
+  puts "run ActionController::Dispatcher.new"
  7
+end

1 note on commit 06ed8e4

Stefano Cobianchi

It’s funny that so many people are saying that you’re doing the “embrace & extend” thing with Rack, yet nobody noticed this commit (event though it was featured prominently in a recent “This week on Edge Rails” post" ).

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