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, indent=True)
-
-
-class RemoteApiHttpServer(http.auth.HttpServerRequestAuthentication,
- http.server.HttpServer):
+class RemoteApiHandler(http.auth.HttpServerRequestAuthentication,
+ http.server.HttpServerHandler):
"""REST Request Handler Class.
"""
AUTH_REALM = "Ganeti Remote API"
- def __init__(self, *args, **kwargs):
+ def __init__(self, _client_cls=None):
# pylint: disable=W0233
# it seems pylint doesn't see the second parent class there
- http.server.HttpServer.__init__(self, *args, **kwargs)
+ http.server.HttpServerHandler.__init__(self)
http.auth.HttpServerRequestAuthentication.__init__(self)
+ self._client_cls = _client_cls
self._resmap = connector.Mapper()
self._users = None
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.
self._resmap.getController(req.request_path)
ctx = RemoteApiRequestContext()
- ctx.handler = HandlerClass(items, args, req)
+ ctx.handler = HandlerClass(items, args, req, _client_cls=self._client_cls)
method = req.request_method.upper()
try:
"""Prep remote API function, executed with the PID file held.
"""
-
mainloop = daemon.Mainloop()
- server = RemoteApiHttpServer(mainloop, options.bind_address, options.port,
- ssl_params=options.ssl_params,
- ssl_verify_peer=False,
- request_executor_class=JsonErrorRequestExecutor)
+ handler = RemoteApiHandler()
# Setup file watcher (it'll be driven by asyncore)
SetupFileWatcher(constants.RAPI_USERS_FILE,
- compat.partial(server.LoadUsers, constants.RAPI_USERS_FILE))
+ compat.partial(handler.LoadUsers, constants.RAPI_USERS_FILE))
- server.LoadUsers(constants.RAPI_USERS_FILE)
+ handler.LoadUsers(constants.RAPI_USERS_FILE)
- # pylint: disable=E1101
- # it seems pylint doesn't see the second parent class there
+ server = \
+ http.server.HttpServer(mainloop, options.bind_address, options.port,
+ handler, ssl_params=options.ssl_params, ssl_verify_peer=False)
server.Start()
return (mainloop, server)