class Puppet::HTTP::Redirector

Handle HTTP redirects

@api private

Public Class Methods

new(redirect_limit) click to toggle source

Create a new redirect handler

@param [Integer] redirect_limit maximum number of redirects allowed

@api private

   # File lib/puppet/http/redirector.rb
10 def initialize(redirect_limit)
11   @redirect_limit = redirect_limit
12 end

Public Instance Methods

redirect?(request, response) click to toggle source

Determine of the HTTP response code indicates a redirect

@param [Net::HTTP] request request that received the response @param [Puppet::HTTP::Response] response

@return [Boolean] true if the response code is 301, 302, or 307.

@api private

   # File lib/puppet/http/redirector.rb
22 def redirect?(request, response)
23   # Net::HTTPRedirection is not used because historically puppet
24   # has only handled these, and we're not a browser
25   case response.code
26   when 301, 302, 307
27     true
28   else
29     false
30   end
31 end
redirect_to(request, response, redirects) click to toggle source

Implement the HTTP request redirection

@param [Net::HTTP] request request that has been redirected @param [Puppet::HTTP::Response] response @param [Integer] redirects the current number of redirects

@return [Net::HTTP] A new request based on the original request, but with

the redirected location

@api private

   # File lib/puppet/http/redirector.rb
43 def redirect_to(request, response, redirects)
44   raise Puppet::HTTP::TooManyRedirects.new(request.uri) if redirects >= @redirect_limit
45 
46   location = parse_location(response)
47   url = request.uri.merge(location)
48 
49   new_request = request.class.new(url)
50   new_request.body = request.body
51   request.each do |header, value|
52     new_request[header] = value
53   end
54 
55   # mimic private Net::HTTP#addr_port
56   new_request['Host'] = if (location.scheme == 'https' && location.port == 443) ||
57                            (location.scheme == 'http' && location.port == 80)
58                           location.host
59                         else
60                           "#{location.host}:#{location.port}"
61                         end
62 
63   new_request
64 end

Private Instance Methods

parse_location(response) click to toggle source
   # File lib/puppet/http/redirector.rb
68 def parse_location(response)
69   location = response['location']
70   raise Puppet::HTTP::ProtocolError.new(_("Location response header is missing")) unless location
71 
72   URI.parse(location)
73 rescue URI::InvalidURIError => e
74   raise Puppet::HTTP::ProtocolError.new(_("Location URI is invalid: %{detail}") % { detail: e.message}, e)
75 end