Class: Excon::Middleware::RedirectFollower

Inherits:
Base
  • Object
show all
Defined in:
lib/excon/middlewares/redirect_follower.rb

Instance Method Summary collapse

Methods inherited from Base

#error_call, #initialize, #request_call

Constructor Details

This class inherits a constructor from Excon::Middleware::Base

Instance Method Details

#response_call(datum) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/excon/middlewares/redirect_follower.rb', line 4

def response_call(datum)
  if datum.has_key?(:response)
    case datum[:response][:status]
    when 301, 302, 303, 307, 308
      uri_parser = datum[:uri_parser] || Excon.defaults[:uri_parser]
      _, location = datum[:response][:headers].detect do |key, value|
        key.casecmp('Location') == 0
      end
      uri = uri_parser.parse(location)

      # delete old/redirect response
      response = datum.delete(:response)

      params = datum.dup
      params.delete(:stack)
      params.delete(:connection)
      params[:method] = :get if [301, 302, 303].include? response[:status]
      params[:headers] = datum[:headers].dup
      params[:headers].delete('Authorization')
      params[:headers].delete('Proxy-Connection')
      params[:headers].delete('Proxy-Authorization')
      params[:headers].delete('Host')
      params.merge!(
        :scheme     => uri.scheme || datum[:scheme],
        :host       => uri.host   || datum[:host],
        :hostname   => uri.hostname || datum[:hostname],
        :port       => uri.port   || datum[:port],
        :path       => uri.path,
        :query      => uri.query
      )

      params.merge!(:user => Utils.unescape_uri(uri.user)) if uri.user
      params.merge!(:password => Utils.unescape_uri(uri.password)) if uri.password

      response = Excon::Connection.new(params).request
      datum.merge!({:response => response.data})
    else
      @stack.response_call(datum)
    end
  else
    @stack.response_call(datum)
  end
end