Class: Rack::Client::FollowRedirects

Inherits:
Object
  • Object
show all
Includes:
DualBand
Defined in:
lib/rack/client/middleware/follow_redirects.rb

Instance Method Summary collapse

Methods included from DualBand

#call

Constructor Details

#initialize(app) ⇒ FollowRedirects

Returns a new instance of FollowRedirects.



6
7
8
# File 'lib/rack/client/middleware/follow_redirects.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#async_call(env, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rack/client/middleware/follow_redirects.rb', line 10

def async_call(env, &block)
  @app.call(env) do |tuple|
    response = Response.new(*tuple)

    if response.redirect?
      follow_redirect(response, env, &block)
    else
      yield response.finish
    end
  end
end

#follow_redirect(response, env, &block) ⇒ Object



27
28
29
# File 'lib/rack/client/middleware/follow_redirects.rb', line 27

def follow_redirect(response, env, &block)
  call(next_env(response, env), &block)
end

#next_env(response, env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rack/client/middleware/follow_redirects.rb', line 31

def next_env(response, env)
  env = env.dup

  original    = URI.parse(env['REQUEST_URI'])
  redirection = URI.parse(response['Location'])

  uri = original.merge(redirection)

  env.update 'REQUEST_METHOD' => 'GET'
  env.update 'PATH_INFO'      => uri.path.empty? ? '/' : uri.path
  env.update 'REQUEST_URI'    => uri.to_s
  env.update 'SERVER_NAME'    => uri.host
  env.update 'SERVER_PORT'    => uri.port
  env.update 'SCRIPT_NAME'    => ''

  env.update 'rack.url_scheme'  => uri.scheme

  env.update 'HTTPS'  => env["rack.url_scheme"] == "https" ? "on" : "off"

  env
end

#sync_call(env, &block) ⇒ Object



22
23
24
25
# File 'lib/rack/client/middleware/follow_redirects.rb', line 22

def sync_call(env, &block)
  response = Response.new(*@app.call(env))
  response.redirect? ? follow_redirect(response, env, &block) : response
end