Commit 377ae13e authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

http.server: Move error message formatting to handler class



Like before this patch moves more functionality from the actual server
class into a separate handler class. At the same time the function is
changed to return both content-type and body instead of relying on a
class attribute.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent cca5b3fc
......@@ -265,10 +265,6 @@ class HttpServerRequestExecutor(object):
# Most web servers default to HTTP 0.9, i.e. don't send a status line.
default_request_version = http.HTTP_0_9
# Error message settings
error_message_format = DEFAULT_ERROR_MESSAGE
error_content_type = DEFAULT_ERROR_CONTENT_TYPE
responses = BaseHTTPServer.BaseHTTPRequestHandler.responses
# Timeouts in seconds for socket layer
......@@ -420,26 +416,18 @@ class HttpServerRequestExecutor(object):
"explain": longmsg,
}
self.response_msg.start_line.code = err.code
(content_type, body) = self.handler.FormatErrorMessage(values)
headers = {
http.HTTP_CONTENT_TYPE: content_type,
}
headers = {}
if err.headers:
headers.update(err.headers)
headers[http.HTTP_CONTENT_TYPE] = self.error_content_type
self.response_msg.headers = headers
self.response_msg.body = self._FormatErrorMessage(values)
def _FormatErrorMessage(self, values):
"""Formats the body of an error message.
@type values: dict
@param values: dictionary with keys code, message and explain.
@rtype: string
@return: the body of the message
"""
return self.error_message_format % values
self.response_msg.start_line.code = err.code
self.response_msg.headers = headers
self.response_msg.body = body
class HttpServer(http.HttpBase, asyncore.dispatcher):
......@@ -592,3 +580,15 @@ class HttpServerHandler(object):
"""
raise NotImplementedError()
@staticmethod
def FormatErrorMessage(values):
"""Formats the body of an error message.
@type values: dict
@param values: dictionary with keys C{code}, C{message} and C{explain}.
@rtype: tuple; (string, string)
@return: Content-type and response body
"""
return (DEFAULT_ERROR_CONTENT_TYPE, DEFAULT_ERROR_MESSAGE % values)
......@@ -64,24 +64,6 @@ class RemoteApiRequestContext(object):
self.body_data = None
class JsonErrorRequestExecutor(http.server.HttpServerRequestExecutor):
"""Custom Request Executor class that formats HTTP errors in JSON.
"""
error_content_type = http.HTTP_APP_JSON
def _FormatErrorMessage(self, values):
"""Formats the body of an error message.
@type values: dict
@param values: dictionary with keys code, message and explain.
@rtype: string
@return: the body of the message
"""
return serializer.DumpJson(values)
class RemoteApiHandler(http.auth.HttpServerRequestAuthentication,
http.server.HttpServerHandler):
"""REST Request Handler Class.
......@@ -127,6 +109,18 @@ class RemoteApiHandler(http.auth.HttpServerRequestAuthentication,
return True
@staticmethod
def FormatErrorMessage(values):
"""Formats the body of an error message.
@type values: dict
@param values: dictionary with keys C{code}, C{message} and C{explain}.
@rtype: tuple; (string, string)
@return: Content-type and response body
"""
return (http.HTTP_APP_JSON, serializer.DumpJson(values))
def _GetRequestContext(self, req):
"""Returns the context for a request.
......@@ -319,8 +313,7 @@ def PrepRapi(options, _):
server = \
http.server.HttpServer(mainloop, options.bind_address, options.port,
handler, ssl_params=options.ssl_params, ssl_verify_peer=False,
request_executor_class=JsonErrorRequestExecutor)
handler, ssl_params=options.ssl_params, ssl_verify_peer=False)
server.Start()
return (mainloop, server)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment