Sha256: a9c4892beb4b1800bde5659e7afe997a2856518686a98a6f839fcb17fcb3e9a7

Contents?: true

Size: 1.72 KB

Versions: 231

Compression:

Stored size: 1.72 KB

Contents

require 'uri'

module Rack
  # Rack::ForwardRequest gets caught by Rack::Recursive and redirects
  # the current request to the app at +url+.
  #
  #   raise ForwardRequest.new("/not-found")
  #

  class ForwardRequest < Exception
    attr_reader :url, :env

    def initialize(url, env={})
      @url = URI(url)
      @env = env

      @env["PATH_INFO"] =       @url.path
      @env["QUERY_STRING"] =    @url.query  if @url.query
      @env["HTTP_HOST"] =       @url.host   if @url.host
      @env["HTTP_PORT"] =       @url.port   if @url.port
      @env["rack.url_scheme"] = @url.scheme if @url.scheme

      super "forwarding to #{url}"
    end
  end

  # Rack::Recursive allows applications called down the chain to
  # include data from other applications (by using
  # <tt>rack['rack.recursive.include'][...]</tt> or raise a
  # ForwardRequest to redirect internally.

  class Recursive
    def initialize(app)
      @app = app
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      @script_name = env["SCRIPT_NAME"]
      @app.call(env.merge('rack.recursive.include' => method(:include)))
    rescue ForwardRequest => req
      call(env.merge(req.env))
    end

    def include(env, path)
      unless path.index(@script_name) == 0 && (path[@script_name.size] == ?/ ||
                                               path[@script_name.size].nil?)
        raise ArgumentError, "can only include below #{@script_name}, not #{path}"
      end

      env = env.merge("PATH_INFO" => path, "SCRIPT_NAME" => @script_name,
                      "REQUEST_METHOD" => "GET",
                      "CONTENT_LENGTH" => "0", "CONTENT_TYPE" => "",
                      "rack.input" => StringIO.new(""))
      @app.call(env)
    end
  end
end

Version data entries

231 entries across 199 versions & 35 rubygems

Version Path
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/recursive.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/recursive.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/recursive.rb
classiccms-0.7.5 vendor/bundle/gems/rack-1.4.1/lib/rack/recursive.rb
classiccms-0.7.4 vendor/bundle/gems/rack-1.4.1/lib/rack/recursive.rb
classiccms-0.7.3 vendor/bundle/gems/rack-1.4.1/lib/rack/recursive.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/recursive.rb
rack-1.4.7 lib/rack/recursive.rb
rack-1.5.5 lib/rack/recursive.rb
rack-1.4.6 lib/rack/recursive.rb
rack-1.5.4 lib/rack/recursive.rb
rack-1.5.3 lib/rack/recursive.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/rack-1.4.5/lib/rack/recursive.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/recursive.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/recursive.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/recursive.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/recursive.rb
nanumfont-rails-0.1 vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/recursive.rb
rack-1.6.0.beta lib/rack/recursive.rb
scout_realtime-1.0.5 lib/vendor/rack-1.5.2/lib/rack/recursive.rb