Class: Rack::Mount::Prefix

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mount/prefix.rb

Overview

:nodoc:

Constant Summary collapse

EMPTY_STRING =
''.freeze
PATH_INFO =
'PATH_INFO'.freeze
SCRIPT_NAME =
'SCRIPT_NAME'.freeze
SLASH =
'/'.freeze
KEY =
'rack.mount.prefix'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, prefix = nil) ⇒ Prefix

Returns a new instance of Prefix.



12
13
14
15
# File 'lib/rack/mount/prefix.rb', line 12

def initialize(app, prefix = nil)
  @app, @prefix = app, prefix.freeze
  freeze
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/mount/prefix.rb', line 17

def call(env)
  if prefix = env[KEY] || @prefix
    old_path_info = env[PATH_INFO].dup
    old_script_name = env[SCRIPT_NAME].dup

    begin
      env[PATH_INFO] = env[PATH_INFO].sub(prefix, EMPTY_STRING)
      env[PATH_INFO] = EMPTY_STRING if env[PATH_INFO] == SLASH
      env[SCRIPT_NAME] = Utils.normalize_path(env[SCRIPT_NAME].to_s + prefix)
      @app.call(env)
    ensure
      env[PATH_INFO] = old_path_info
      env[SCRIPT_NAME] = old_script_name
    end
  else
    @app.call(env)
  end
end