Skip to content

Commit

Permalink
(maint) Refactor Handler#process
Browse files Browse the repository at this point in the history
I originally found this method hard to read. I've tried to have this
function at a single level of abstraction.

The only apparent functional difference is removing the call to cleanup.
This is no longer used by anything.
  • Loading branch information
pcarlisle committed Apr 18, 2018
1 parent 144758c commit ff17d86
Showing 1 changed file with 59 additions and 27 deletions.
86 changes: 59 additions & 27 deletions lib/puppet/network/http/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,78 @@ def format_to_mime(format)
format.is_a?(Puppet::Network::Format) ? format.mime : format
end

# handle an HTTP request
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)
# Create a generic puppet request from the implementation-specific request
# created by the web server
def make_generic_request(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))
Puppet::Network::HTTP::Request.new(
headers(request),
params(request),
http_method(request),
request_path, # path
request_path, # routing_path
client_cert(request),
body(request)
)
end

def with_request_profiling(request)
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
yield
end
ensure
remove_profiler(profiler) if profiler
end

response[Puppet::Network::HTTP::HEADER_PUPPET_VERSION] = Puppet.version
# handle an HTTP request
def process(external_request, response)
# The response_wrapper stores the response and modifies it as a side effect.
# The caller will use the original response
response_wrapper = Puppet::Network::HTTP::Response.new(self, response)
request = make_generic_request(external_request)

profiler = configure_profiler(request_headers, request_params)
set_puppet_version_header(response)

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)
respond_to_errors(response_wrapper) do
with_request_profiling(request) do
find_route_or_raise(request).process(request, response_wrapper)
end
end
end

def respond_to_errors(response)
yield
rescue Puppet::Network::HTTP::Error::HTTPError => e
Puppet.info(e.message)
new_response.respond_with(e.status, "application/json", e.to_json)
respond_with_http_error(response, e)
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)
Puppet.err([http_e.message, *e.backtrace].join("\n"))
respond_with_http_error(response, http_e)
end

def respond_with_http_error(response, exception)
response.respond_with(exception.status, "application/json", exception.to_json)
end

def find_route_or_raise(request)
if route = @routes.find { |r| r.matches?(request) }
return route
else
raise Puppet::Network::HTTP::Error::HTTPNotFoundError.new(
_("No route for %{request} %{path}") % { request: request.method, path: request.path },
HANDLER_NOT_FOUND)
end
cleanup(request)
end

def set_puppet_version_header(response)
response[Puppet::Network::HTTP::HEADER_PUPPET_VERSION] = Puppet.version
end

# Set the response up, with the body and status.
Expand Down Expand Up @@ -131,10 +167,6 @@ def client_cert(request)
raise NotImplementedError
end

def cleanup(request)
# By default, there is nothing to cleanup.
end

def decode_params(params)
params.select { |key, _| allowed_parameter?(key) }.inject({}) do |result, ary|
param, value = ary
Expand Down

0 comments on commit ff17d86

Please sign in to comment.