diff --git a/lib/http/server.py b/lib/http/server.py index 8e9a638da9e765d5ddbded08b439238a75b8a5c9..25e79280693791be57f200ecde68fdbb7cc17338 100644 --- a/lib/http/server.py +++ b/lib/http/server.py @@ -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) diff --git a/lib/server/rapi.py b/lib/server/rapi.py index b19571198b7eb989853209d048b4d16e523228c4..2bc0dc4f87c8ebc84dfb1d680acba3a683b00399 100644 --- a/lib/server/rapi.py +++ b/lib/server/rapi.py @@ -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)