Class: Puppet::HTTP::Redirector

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/http/redirector.rb

Instance Method Summary collapse

Constructor Details

#initialize(redirect_limit) ⇒ Redirector


2
3
4
# File 'lib/puppet/http/redirector.rb', line 2

def initialize(redirect_limit)
  @redirect_limit = redirect_limit
end

Instance Method Details

#redirect?(request, response) ⇒ Boolean


6
7
8
9
10
11
12
13
14
15
# File 'lib/puppet/http/redirector.rb', line 6

def redirect?(request, response)
  # Net::HTTPRedirection is not used because historically puppet
  # has only handled these, and we're not a browser
  case response.code
  when 301, 302, 307
    true
  else
    false
  end
end

#redirect_to(request, response, redirects) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/http/redirector.rb', line 17

def redirect_to(request, response, redirects)
  raise Puppet::HTTP::TooManyRedirects.new(request.uri) if redirects >= @redirect_limit

  location = parse_location(response)
  if location.relative?
    url = request.uri.dup
    url.path = location.path
  else
    url = location.dup
  end
  url.query = request.uri.query

  new_request = request.class.new(url)
  new_request.body = request.body
  request.each do |header, value|
    new_request[header] = value
  end

  new_request
end