class Puppet::HTTP::Session

The session is the mechanism by which services may be connected to and accessed.

@api public

Constants

CAP_JSON
CAP_LOCALES

capabilities for a site

SUPPORTED_JSON_DEFAULT

puppet version where JSON was enabled by default

SUPPORTED_LOCALES_MOUNT_AGENT_VERSION

puppet version where locales mount was added

Public Class Methods

new(client, resolvers) click to toggle source

Create a new HTTP session. The session is the mechanism by which services may be connected to and accessed. Sessions should be created using `Puppet::HTTP::Client#create_session`.

@param [Puppet::HTTP::Client] client the container for this session @param [Array<Puppet::HTTP::Resolver>] resolvers array of resolver strategies

to implement.

@api private

   # File lib/puppet/http/session.rb
25 def initialize(client, resolvers)
26   @client = client
27   @resolvers = resolvers
28   @resolved_services = {}
29   @server_versions = {}
30 end

Public Instance Methods

process_response(response) click to toggle source

Collect per-site server versions. This will allow us to modify future requests based on the version of puppetserver we are talking to.

@param [Puppet::HTTP::Response] response the request response containing headers

@api private

   # File lib/puppet/http/session.rb
87 def process_response(response)
88   version = response[Puppet::HTTP::HEADER_PUPPET_VERSION]
89   if version
90     site = Puppet::HTTP::Site.from_uri(response.url)
91     @server_versions[site] = version
92   end
93 end
route_to(name, url: nil, ssl_context: nil) click to toggle source

If an explicit server and port are specified on the command line or configuration file, this method always returns a Service with that host and port. Otherwise, we walk the list of resolvers in priority order:

- DNS SRV
- Server List
- Puppet server/port settings

If a given resolver fails to connect, it tries the next available resolver until a successful connection is found and returned. The successful service is cached and returned if `route_to` is called again.

@param [Symbol] name the service to resolve @param [URI] url optional explicit url to use, if it is already known @param [Puppet::SSL::SSLContext] ssl_context ssl context to be

used for connections

@return [Puppet::HTTP::Service] the resolved service

@api public

   # File lib/puppet/http/session.rb
50 def route_to(name, url: nil, ssl_context: nil)
51   raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)
52 
53   # short circuit if explicit URL host & port given
54   if url && url.host != nil && !url.host.empty?
55     service = Puppet::HTTP::Service.create_service(@client, self, name, url.host, url.port)
56     service.connect(ssl_context: ssl_context)
57     return service
58   end
59 
60   cached = @resolved_services[name]
61   return cached if cached
62 
63   canceled = false
64   canceled_handler  = lambda { |cancel| canceled = cancel }
65 
66   @resolvers.each do |resolver|
67     Puppet.debug("Resolving service '#{name}' using #{resolver.class}")
68     service = resolver.resolve(self, name, ssl_context: ssl_context, canceled_handler: canceled_handler)
69     if service
70       @resolved_services[name] = service
71       Puppet.debug("Resolved service '#{name}' to #{service.url}")
72       return service
73     elsif canceled
74       break
75     end
76   end
77 
78   raise Puppet::HTTP::RouteError, "No more routes to #{name}"
79 end
supports?(name, capability) click to toggle source

Determine if a session supports a capability. Depending on the server version we are talking to, we know certain features are available or not. These specifications are defined here so we can modify our requests appropriately.

@param [Symbol] name name of the service to check @param [String] capability the capability, ie `locales` or `json`

@return [Boolean]

@api public

    # File lib/puppet/http/session.rb
105 def supports?(name, capability)
106   raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)
107 
108   service = @resolved_services[name]
109   return false unless service
110 
111   site = Puppet::HTTP::Site.from_uri(service.url)
112   server_version = @server_versions[site]
113 
114   case capability
115   when CAP_LOCALES
116     !server_version.nil? && Gem::Version.new(server_version) >= SUPPORTED_LOCALES_MOUNT_AGENT_VERSION
117   when CAP_JSON
118     server_version.nil? || Gem::Version.new(server_version) >= SUPPORTED_JSON_DEFAULT
119   else
120     false
121   end
122 end