Railsで学ぶフレームワーク作り - Dispatcher編 その1

フレームワーク作りの参考にRailsのコードを読んでみる事にしました。
Rails使ってたのは1.0のころなので、もうすっかり忘れてますが。

actionpack/lib/action_controller/dispatcher.rb がいかにもって感じなので、そこから読んでみることに。

確かRailsもRack対応なんですよね、ってことで読んでみるとcallメソッドがありますね。callがRackの入り口ですね。Rackのrequest, response作ってdispatchすると。

    def call(env)
      @request = RackRequest.new(env)
      @response = RackResponse.new(@request)
      dispatch
    end

dispatch部分は、一応マルチスレッド対応のコードがあるようだけど、
ここはパスして、dispatch_unlockedに。

    def dispatch
      if ActionController::Base.allow_concurrency
        dispatch_unlocked
      else
        @@guard.synchronize do
          dispatch_unlocked
        end
      end
    end

dispatch前後にcallbackを用意してますね。何に使うのかはまだわからないですが、これはまたあとで。handle_requestで、request処理をと。

    def dispatch_unlocked
      begin
        run_callbacks :before_dispatch
        handle_request
      rescue Exception => exception
        failsafe_rescue exception
      ensure
        run_callbacks :after_dispatch, :enumerator => :reverse_each
      end
    end

handle_requestを見ると、Routing::RoutesがDispatcherになってるみたいですね。
Routes系っぽく、Controllerを見つけて、見つけたControllerに処理委譲してみたいですね。このRequestがResponseが

    protected
      def handle_request
        @controller = Routing::Routes.recognize(@request)
        @controller.process(@request, @response).out(@output)
      end 

なんだか、HTTP::Engineでそのまま処理できそうな気がしてきますよねー。
では次は、いかにもDispatcherなRouting::Routesみていきます。
これがHTTP::Routerが参考にしてるDispatcherってことでしょうね、多分。