Module: Puppet::Network::HTTP::Handler

Includes:
Issues
Included in:
RackREST, WEBrickREST
Defined in:
lib/puppet/network/http/handler.rb

Constant Summary collapse

DISALLOWED_KEYS =

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

["node", "ip"]

Constants included from Issues

Issues::ENVIRONMENT_NOT_FOUND, Issues::FAILED_AUTHORIZATION, Issues::HANDLER_NOT_FOUND, Issues::MISSING_HEADER_FIELD, Issues::NO_INDIRECTION_REMOTE_REQUESTS, Issues::RESOURCE_NOT_FOUND, Issues::RUNTIME_ERROR, Issues::UNSUPPORTED_FORMAT, Issues::UNSUPPORTED_MEDIA_TYPE, Issues::UNSUPPORTED_METHOD

Instance Method Summary collapse

Instance Method Details

#format_to_mime(format) ⇒ Object

Deprecated.

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.



43
44
45
# File 'lib/puppet/network/http/handler.rb', line 43

def format_to_mime(format)
  format.is_a?(Puppet::Network::Format) ? format.mime : format
end

#headers(request) ⇒ Object

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

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/puppet/network/http/handler.rb', line 35

def headers(request)
  raise NotImplementedError
end

#process(request, response) ⇒ Object

handle an HTTP request



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/network/http/handler.rb', line 48

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}") % { request_method: request_method, request_path: request_path }, [:http, request_method, request_path]) do
    if route = @routes.find { |r| r.matches?(new_request) }
      route.process(new_request, new_response)
    else
      raise Puppet::Network::HTTP::Error::HTTPNotFoundError.new(_("No route for %{request} %{path}") % { request: new_request.method, path: 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 StandardError => e
  http_e = Puppet::Network::HTTP::Error::HTTPServerError.new(e)
  log_msg = [http_e.message, *e.backtrace].join("\n")
  Puppet.err(log_msg)
  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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppet/network/http/handler.rb', line 17

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: %{regexes}") % { 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) ⇒ Object

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



97
98
99
100
101
102
103
104
# File 'lib/puppet/network/http/handler.rb', line 97

def resolve_node(result)
  begin
    return Resolv.getname(result[:ip])
  rescue => detail
    Puppet.err _("Could not resolve %{ip}: %{detail}") % { ip: result[:ip], detail: detail }
  end
  result[:ip]
end

#set_content_type(response, format) ⇒ Object

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

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/puppet/network/http/handler.rb', line 91

def set_content_type(response, format)
  raise NotImplementedError
end

#set_response(response, body, status = 200) ⇒ Object

Set the response up, with the body and status.

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/puppet/network/http/handler.rb', line 86

def set_response(response, body, status = 200)
  raise NotImplementedError
end