module Puppet::Network::HTTP::Handler

Constants

DISALLOWED_KEYS

These shouldn't be allowed to be set by clients in the query string, for security reasons.

Public Instance Methods

find_route_or_raise(request) click to toggle source
    # File lib/puppet/network/http/handler.rb
107 def find_route_or_raise(request)
108   route = @routes.find { |r| r.matches?(request) }
109   if route
110     return route
111   else
112     raise Puppet::Network::HTTP::Error::HTTPNotFoundError.new(
113             _("No route for %{request} %{path}") % { request: request.method, path: request.path },
114             HANDLER_NOT_FOUND)
115   end
116 end
format_to_mime(format) click to toggle source

The mime type is always passed to the `set_content_type` method, so it is no longer necessary to retrieve the Format's mime type.

@deprecated

   # File lib/puppet/network/http/handler.rb
43 def format_to_mime(format)
44   format.is_a?(Puppet::Network::Format) ? format.mime : format
45 end
headers(request) click to toggle source

Retrieve all headers from the http request, as a hash with the header names (lower-cased) as the keys

   # File lib/puppet/network/http/handler.rb
35 def headers(request)
36   raise NotImplementedError
37 end
make_generic_request(request) click to toggle source

Create a generic puppet request from the implementation-specific request created by the web server

   # File lib/puppet/network/http/handler.rb
49 def make_generic_request(request)
50   request_path = path(request)
51 
52   Puppet::Network::HTTP::Request.new(
53     headers(request),
54     params(request),
55     http_method(request),
56     request_path, # path
57     request_path, # routing_path
58     client_cert(request),
59     body(request)
60   )
61 end
process(external_request, response) click to toggle source

handle an HTTP request

   # File lib/puppet/network/http/handler.rb
77 def process(external_request, response)
78   # The response_wrapper stores the response and modifies it as a side effect.
79   # The caller will use the original response
80   response_wrapper = Puppet::Network::HTTP::Response.new(self, response)
81   request = make_generic_request(external_request)
82 
83   set_puppet_version_header(response)
84 
85   respond_to_errors(response_wrapper) do
86     with_request_profiling(request) do
87       find_route_or_raise(request).process(request, response_wrapper)
88     end
89   end
90 end
register(routes) click to toggle source
   # File lib/puppet/network/http/handler.rb
17 def register(routes)
18   # There's got to be a simpler way to do this, right?
19   dupes = {}
20   routes.each { |r| dupes[r.path_matcher] = (dupes[r.path_matcher] || 0) + 1 }
21   dupes = dupes.collect { |pm, count| pm if count > 1 }.compact
22   if dupes.count > 0
23     raise ArgumentError, _("Given multiple routes with identical path regexes: %{regexes}") % { regexes: dupes.map{ |rgx| rgx.inspect }.join(', ') }
24   end
25 
26   @routes = routes
27   Puppet.debug("Routes Registered:")
28   @routes.each do |route|
29     Puppet.debug(route.inspect)
30   end
31 end
resolve_node(result) click to toggle source

resolve node name from peer's ip address this is used when the request is unauthenticated

    # File lib/puppet/network/http/handler.rb
134 def resolve_node(result)
135   begin
136     return Resolv.getname(result[:ip])
137   rescue => detail
138     Puppet.err _("Could not resolve %{ip}: %{detail}") % { ip: result[:ip], detail: detail }
139   end
140   result[:ip]
141 end
respond_to_errors(response) { || ... } click to toggle source
    # File lib/puppet/network/http/handler.rb
 92 def respond_to_errors(response)
 93   yield
 94 rescue Puppet::Network::HTTP::Error::HTTPError => e
 95   Puppet.info(e.message)
 96   respond_with_http_error(response, e)
 97 rescue StandardError => e
 98   http_e = Puppet::Network::HTTP::Error::HTTPServerError.new(e)
 99   Puppet.err([http_e.message, *e.backtrace].join("\n"))
100   respond_with_http_error(response, http_e)
101 end
respond_with_http_error(response, exception) click to toggle source
    # File lib/puppet/network/http/handler.rb
103 def respond_with_http_error(response, exception)
104   response.respond_with(exception.status, "application/json", exception.to_json)
105 end
set_content_type(response, format) click to toggle source

Set the specified format as the content type of the response.

    # File lib/puppet/network/http/handler.rb
128 def set_content_type(response, format)
129   raise NotImplementedError
130 end
set_puppet_version_header(response) click to toggle source
    # File lib/puppet/network/http/handler.rb
118 def set_puppet_version_header(response)
119   response[Puppet::Network::HTTP::HEADER_PUPPET_VERSION] = Puppet.version
120 end
set_response(response, body, status = 200) click to toggle source

Set the response up, with the body and status.

    # File lib/puppet/network/http/handler.rb
123 def set_response(response, body, status = 200)
124   raise NotImplementedError
125 end
with_request_profiling(request) { || ... } click to toggle source
   # File lib/puppet/network/http/handler.rb
63 def with_request_profiling(request)
64   profiler = configure_profiler(request.headers, request.params)
65 
66   Puppet::Util::Profiler.profile(
67     _("Processed request %{request_method} %{request_path}") % { request_method: request.method, request_path: request.path },
68     [:http, request.method, request.path]
69   ) do
70     yield
71   end
72 ensure
73   remove_profiler(profiler) if profiler
74 end

Private Instance Methods

allowed_parameter?(name) click to toggle source
    # File lib/puppet/network/http/handler.rb
179 def allowed_parameter?(name)
180   not (name.nil? || name.empty? || DISALLOWED_KEYS.include?(name))
181 end
body(request) click to toggle source
    # File lib/puppet/network/http/handler.rb
159 def body(request)
160   raise NotImplementedError
161 end
client_cert(request) click to toggle source
    # File lib/puppet/network/http/handler.rb
167 def client_cert(request)
168   raise NotImplementedError
169 end
configure_profiler(request_headers, request_params) click to toggle source
    # File lib/puppet/network/http/handler.rb
206 def configure_profiler(request_headers, request_params)
207   if (request_headers.has_key?(Puppet::Network::HTTP::HEADER_ENABLE_PROFILING.downcase) or Puppet[:profile])
208     Puppet::Util::Profiler.add_profiler(Puppet::Util::Profiler::Aggregate.new(Puppet.method(:info), request_params.object_id))
209   end
210 end
decode_params(params) click to toggle source
    # File lib/puppet/network/http/handler.rb
171 def decode_params(params)
172   params.select { |key, _| allowed_parameter?(key) }.inject({}) do |result, ary|
173     param, value = ary
174     result[param.to_sym] = parse_parameter_value(param, value)
175     result
176   end
177 end
http_method(request) click to toggle source

methods to be overridden by the including web server class

    # File lib/puppet/network/http/handler.rb
147 def http_method(request)
148   raise NotImplementedError
149 end
params(request) click to toggle source
    # File lib/puppet/network/http/handler.rb
163 def params(request)
164   raise NotImplementedError
165 end
parse_parameter_value(param, value) click to toggle source
    # File lib/puppet/network/http/handler.rb
183 def parse_parameter_value(param, value)
184   if value.is_a?(Array)
185     value.collect { |v| parse_primitive_parameter_value(v) }
186   else
187     parse_primitive_parameter_value(value)
188   end
189 end
parse_primitive_parameter_value(value) click to toggle source
    # File lib/puppet/network/http/handler.rb
191 def parse_primitive_parameter_value(value)
192   case value
193   when "true"
194     true
195   when "false"
196     false
197   when /^\d+$/
198     Integer(value)
199   when /^\d+\.\d+$/
200     value.to_f
201   else
202     value
203   end
204 end
path(request) click to toggle source
    # File lib/puppet/network/http/handler.rb
151 def path(request)
152   raise NotImplementedError
153 end
remove_profiler(profiler) click to toggle source
    # File lib/puppet/network/http/handler.rb
212 def remove_profiler(profiler)
213   profiler.shutdown
214   Puppet::Util::Profiler.remove_profiler(profiler)
215 end
request_key(request) click to toggle source
    # File lib/puppet/network/http/handler.rb
155 def request_key(request)
156   raise NotImplementedError
157 end