Sha256: 25e658dde16b74c18754a00f51438b25939d2681521fb60404f0d03a0f5d1d98

Contents?: true

Size: 1.67 KB

Versions: 93

Compression:

Stored size: 1.67 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)
      @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

93 entries across 92 versions & 26 rubygems

Version Path
rack-1.1.1.pre lib/rack/recursive.rb
eac-rack-1.1.1 lib/rack/recursive.rb
nbudin-castronaut-0.7.5 vendor/rack/lib/rack/recursive.rb
merb-core-1.1.3 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/recursive.rb
merb-core-1.1.2 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/recursive.rb
merb-core-1.1.1 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/recursive.rb
merb-core-1.1.0 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/recursive.rb
merb-core-1.1.0.rc1 spec10/public/webrat/test_app/gems/gems/rack-0.4.0/lib/rack/recursive.rb
akamai_bookmarklet-0.1.2 vendor/gems/ruby/1.8/gems/rack-1.1.0/lib/rack/recursive.rb
middleman-0.13.1 lib/middleman/vendor/gems/ruby/1.8/gems/rack-1.1.0/lib/rack/recursive.rb
middleman-0.13.1 lib/middleman/vendor/gems/ruby/1.9.1/gems/rack-1.1.0/lib/rack/recursive.rb
akamai_bookmarklet-0.1.1 vendor/gems/ruby/1.8/gems/rack-1.1.0/lib/rack/recursive.rb
akamai_bookmarklet-0.1.0 vendor/gems/ruby/1.8/gems/rack-1.1.0/lib/rack/recursive.rb
rack-1.1.0 lib/rack/recursive.rb
middleman-0.12.2 lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/recursive.rb
middleman-0.12.1 lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/recursive.rb
middleman-0.12.0.pre3 lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/recursive.rb
middleman-0.12.0.pre2 lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/recursive.rb
colouringcode-passenger-0.1 vendor/rack-1.0.0-git/lib/rack/recursive.rb
middleman-0.12.0.pre lib/middleman/vendor/gems/gems/rack-1.0.1/lib/rack/recursive.rb