Class: Puppet::HTTP::Redirector Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Handle HTTP redirects

Instance Method Summary collapse

Constructor Details

#initialize(redirect_limit) ⇒ Redirector

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new redirect handler

Parameters:

  • redirect_limit (Integer)

    maximum number of redirects allowed



12
13
14
# File 'lib/puppet/http/redirector.rb', line 12

def initialize(redirect_limit)
  @redirect_limit = redirect_limit
end

Instance Method Details

#redirect?(request, response) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine of the HTTP response code indicates a redirect

Parameters:

Returns:

  • (Boolean)

    true if the response code is 301, 302, or 307.



24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/http/redirector.rb', line 24

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) ⇒ Net::HTTP

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Implement the HTTP request redirection

Parameters:

  • request (Net::HTTP)

    request that has been redirected

  • response (Puppet::HTTP::Response)
  • redirects (Integer)

    the current number of redirects

Returns:

  • (Net::HTTP)

    A new request based on the original request, but with the redirected location

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/http/redirector.rb', line 45

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

  location = parse_location(response)
  url = request.uri.merge(location)

  new_request = request.class.new(url)
  new_request.body = request.body
  request.each do |header, value|
    unless Puppet[:location_trusted]
      # skip adding potentially sensitive header to other hosts
      next if header.casecmp('Authorization').zero? && request.uri.host.casecmp(location.host) != 0
      next if header.casecmp('Cookie').zero? && request.uri.host.casecmp(location.host) != 0
    end
    # Allow Net::HTTP to set its own Accept-Encoding header to avoid errors with HTTP compression.
    # See https://github.com/puppetlabs/puppet/issues/9143
    next if header.casecmp('Accept-Encoding').zero?

    new_request[header] = value
  end

  # mimic private Net::HTTP#addr_port
  new_request['Host'] = if (location.scheme == 'https' && location.port == 443) ||
                           (location.scheme == 'http' && location.port == 80)
                          location.host
                        else
                          "#{location.host}:#{location.port}"
                        end

  new_request
end