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 106 def find_route_or_raise(request) 107 route = @routes.find { |r| r.matches?(request) } 108 if route 109 return route 110 else 111 raise Puppet::Network::HTTP::Error::HTTPNotFoundError.new( 112 _("No route for %{request} %{path}") % { request: request.method, path: request.path }, 113 HANDLER_NOT_FOUND) 114 end 115 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 42 def format_to_mime(format) 43 format.is_a?(Puppet::Network::Format) ? format.mime : format 44 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 34 def headers(request) 35 raise NotImplementedError 36 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 48 def make_generic_request(request) 49 request_path = path(request) 50 51 Puppet::Network::HTTP::Request.new( 52 headers(request), 53 params(request), 54 http_method(request), 55 request_path, # path 56 request_path, # routing_path 57 client_cert(request), 58 body(request) 59 ) 60 end
process(external_request, response)
click to toggle source
handle an HTTP
request
# File lib/puppet/network/http/handler.rb 76 def process(external_request, response) 77 # The response_wrapper stores the response and modifies it as a side effect. 78 # The caller will use the original response 79 response_wrapper = Puppet::Network::HTTP::Response.new(self, response) 80 request = make_generic_request(external_request) 81 82 set_puppet_version_header(response) 83 84 respond_to_errors(response_wrapper) do 85 with_request_profiling(request) do 86 find_route_or_raise(request).process(request, response_wrapper) 87 end 88 end 89 end
register(routes)
click to toggle source
# File lib/puppet/network/http/handler.rb 16 def register(routes) 17 # There's got to be a simpler way to do this, right? 18 dupes = {} 19 routes.each { |r| dupes[r.path_matcher] = (dupes[r.path_matcher] || 0) + 1 } 20 dupes = dupes.collect { |pm, count| pm if count > 1 }.compact 21 if dupes.count > 0 22 raise ArgumentError, _("Given multiple routes with identical path regexes: %{regexes}") % { regexes: dupes.map{ |rgx| rgx.inspect }.join(', ') } 23 end 24 25 @routes = routes 26 Puppet.debug("Routes Registered:") 27 @routes.each do |route| 28 Puppet.debug(route.inspect) 29 end 30 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 133 def resolve_node(result) 134 begin 135 return Resolv.getname(result[:ip]) 136 rescue => detail 137 Puppet.err _("Could not resolve %{ip}: %{detail}") % { ip: result[:ip], detail: detail } 138 end 139 result[:ip] 140 end
respond_to_errors(response) { || ... }
click to toggle source
# File lib/puppet/network/http/handler.rb 91 def respond_to_errors(response) 92 yield 93 rescue Puppet::Network::HTTP::Error::HTTPError => e 94 Puppet.info(e.message) 95 respond_with_http_error(response, e) 96 rescue StandardError => e 97 http_e = Puppet::Network::HTTP::Error::HTTPServerError.new(e) 98 Puppet.err([http_e.message, *e.backtrace].join("\n")) 99 respond_with_http_error(response, http_e) 100 end
respond_with_http_error(response, exception)
click to toggle source
# File lib/puppet/network/http/handler.rb 102 def respond_with_http_error(response, exception) 103 response.respond_with(exception.status, "application/json", exception.to_json) 104 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 127 def set_content_type(response, format) 128 raise NotImplementedError 129 end
set_puppet_version_header(response)
click to toggle source
# File lib/puppet/network/http/handler.rb 117 def set_puppet_version_header(response) 118 response[Puppet::Network::HTTP::HEADER_PUPPET_VERSION] = Puppet.version 119 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 122 def set_response(response, body, status = 200) 123 raise NotImplementedError 124 end
with_request_profiling(request) { || ... }
click to toggle source
# File lib/puppet/network/http/handler.rb 62 def with_request_profiling(request) 63 profiler = configure_profiler(request.headers, request.params) 64 65 Puppet::Util::Profiler.profile( 66 _("Processed request %{request_method} %{request_path}") % { request_method: request.method, request_path: request.path }, 67 [:http, request.method, request.path] 68 ) do 69 yield 70 end 71 ensure 72 remove_profiler(profiler) if profiler 73 end
Private Instance Methods
allowed_parameter?(name)
click to toggle source
# File lib/puppet/network/http/handler.rb 178 def allowed_parameter?(name) 179 not (name.nil? || name.empty? || DISALLOWED_KEYS.include?(name)) 180 end
body(request)
click to toggle source
# File lib/puppet/network/http/handler.rb 158 def body(request) 159 raise NotImplementedError 160 end
client_cert(request)
click to toggle source
# File lib/puppet/network/http/handler.rb 166 def client_cert(request) 167 raise NotImplementedError 168 end
configure_profiler(request_headers, request_params)
click to toggle source
# File lib/puppet/network/http/handler.rb 205 def configure_profiler(request_headers, request_params) 206 if (request_headers.has_key?(Puppet::Network::HTTP::HEADER_ENABLE_PROFILING.downcase) or Puppet[:profile]) 207 Puppet::Util::Profiler.add_profiler(Puppet::Util::Profiler::Aggregate.new(Puppet.method(:info), request_params.object_id)) 208 end 209 end
decode_params(params)
click to toggle source
# File lib/puppet/network/http/handler.rb 170 def decode_params(params) 171 params.select { |key, _| allowed_parameter?(key) }.inject({}) do |result, ary| 172 param, value = ary 173 result[param.to_sym] = parse_parameter_value(param, value) 174 result 175 end 176 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 146 def http_method(request) 147 raise NotImplementedError 148 end
params(request)
click to toggle source
# File lib/puppet/network/http/handler.rb 162 def params(request) 163 raise NotImplementedError 164 end
parse_parameter_value(param, value)
click to toggle source
# File lib/puppet/network/http/handler.rb 182 def parse_parameter_value(param, value) 183 if value.is_a?(Array) 184 value.collect { |v| parse_primitive_parameter_value(v) } 185 else 186 parse_primitive_parameter_value(value) 187 end 188 end
parse_primitive_parameter_value(value)
click to toggle source
# File lib/puppet/network/http/handler.rb 190 def parse_primitive_parameter_value(value) 191 case value 192 when "true" 193 true 194 when "false" 195 false 196 when /^\d+$/ 197 Integer(value) 198 when /^\d+\.\d+$/ 199 value.to_f 200 else 201 value 202 end 203 end
path(request)
click to toggle source
# File lib/puppet/network/http/handler.rb 150 def path(request) 151 raise NotImplementedError 152 end
remove_profiler(profiler)
click to toggle source
# File lib/puppet/network/http/handler.rb 211 def remove_profiler(profiler) 212 profiler.shutdown 213 Puppet::Util::Profiler.remove_profiler(profiler) 214 end
request_key(request)
click to toggle source
# File lib/puppet/network/http/handler.rb 154 def request_key(request) 155 raise NotImplementedError 156 end