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

format_to_mime(format) click to toggle source
# File lib/puppet/network/http/handler.rb, line 42
def format_to_mime(format)
  format.is_a?(Puppet::Network::Format) ? format.mime : format
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, line 38
def headers(request)
  raise NotImplementedError
end
process(request, response) click to toggle source

handle an HTTP request

# File lib/puppet/network/http/handler.rb, line 47
def process(request, response)
  new_response = Puppet::Network::HTTP::Response.new(self, response)

  request_headers = headers(request)
  request_params = params(request)
  request_method = http_method(request)
  request_path = path(request)

  new_request = Puppet::Network::HTTP::Request.new(request_headers, request_params, request_method, request_path, request_path, client_cert(request), body(request))

  response[Puppet::Network::HTTP::HEADER_PUPPET_VERSION] = Puppet.version

  profiler = configure_profiler(request_headers, request_params)

  Puppet::Util::Profiler.profile("Processed request #{request_method} #{request_path}", [:http, request_method, request_path]) do
    if route = @routes.find { |route| route.matches?(new_request) }
      route.process(new_request, new_response)
    else
      raise Puppet::Network::HTTP::Error::HTTPNotFoundError.new("No route for #{new_request.method} #{new_request.path}", HANDLER_NOT_FOUND)
    end
  end

rescue Puppet::Network::HTTP::Error::HTTPError => e
  Puppet.info(e.message)
  new_response.respond_with(e.status, "application/json", e.to_json)
rescue Exception => e
  http_e = Puppet::Network::HTTP::Error::HTTPServerError.new(e)
  Puppet.err(http_e.message)
  new_response.respond_with(http_e.status, "application/json", http_e.to_json)
ensure
  if profiler
    remove_profiler(profiler)
  end
  cleanup(request)
end
register(routes) click to toggle source
# File lib/puppet/network/http/handler.rb, line 20
def register(routes)
  # There's got to be a simpler way to do this, right?
  dupes = {}
  routes.each { |r| dupes[r.path_matcher] = (dupes[r.path_matcher] || 0) + 1 }
  dupes = dupes.collect { |pm, count| pm if count > 1 }.compact
  if dupes.count > 0
    raise ArgumentError, "Given multiple routes with identical path regexes: #{dupes.map{ |rgx| rgx.inspect }.join(', ')}"
  end

  @routes = routes
  Puppet.debug("Routes Registered:")
  @routes.each do |route|
    Puppet.debug(route.inspect)
  end
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, line 95
def resolve_node(result)
  begin
    return Resolv.getname(result[:ip])
  rescue => detail
    Puppet.err "Could not resolve #{result[:ip]}: #{detail}"
  end
  result[:ip]
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, line 89
def set_content_type(response, format)
  raise NotImplementedError
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, line 84
def set_response(response, body, status = 200)
  raise NotImplementedError
end