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
24 def initialize(client, resolvers)
25   @client = client
26   @resolvers = resolvers
27   @resolved_services = {}
28   @server_versions = {}
29 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
86 def process_response(response)
87   version = response[Puppet::HTTP::HEADER_PUPPET_VERSION]
88   if version
89     site = Puppet::HTTP::Site.from_uri(response.url)
90     @server_versions[site] = version
91   end
92 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
49 def route_to(name, url: nil, ssl_context: nil)
50   raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)
51 
52   # short circuit if explicit URL host & port given
53   if url && url.host != nil && !url.host.empty?
54     service = Puppet::HTTP::Service.create_service(@client, self, name, url.host, url.port)
55     service.connect(ssl_context: ssl_context)
56     return service
57   end
58 
59   cached = @resolved_services[name]
60   return cached if cached
61 
62   canceled = false
63   canceled_handler  = lambda { |cancel| canceled = cancel }
64 
65   @resolvers.each do |resolver|
66     Puppet.debug("Resolving service '#{name}' using #{resolver.class}")
67     service = resolver.resolve(self, name, ssl_context: ssl_context, canceled_handler: canceled_handler)
68     if service
69       @resolved_services[name] = service
70       Puppet.debug("Resolved service '#{name}' to #{service.url}")
71       return service
72     elsif canceled
73       break
74     end
75   end
76 
77   raise Puppet::HTTP::RouteError, "No more routes to #{name}"
78 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
104 def supports?(name, capability)
105   raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)
106 
107   service = @resolved_services[name]
108   return false unless service
109 
110   site = Puppet::HTTP::Site.from_uri(service.url)
111   server_version = @server_versions[site]
112 
113   case capability
114   when CAP_LOCALES
115     !server_version.nil? && Gem::Version.new(server_version) >= SUPPORTED_LOCALES_MOUNT_AGENT_VERSION
116   when CAP_JSON
117     server_version.nil? || Gem::Version.new(server_version) >= SUPPORTED_JSON_DEFAULT
118   else
119     false
120   end
121 end