Commit 68fa9caf authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

ganeti.http: Add support for private data in HTTP requests

Reviewed-by: amishchenko
parent be500c29
......@@ -86,6 +86,10 @@ class _HttpServerRequest(object):
# Response attributes
self.resp_headers = {}
# Private data for request handler (useful in combination with
# authentication)
self.private = None
class _HttpServerToClientMessageWriter(http.HttpMessageWriter):
"""Writes an HTTP response to client.
......@@ -308,26 +312,30 @@ class _HttpServerRequestExecutor(object):
handler_context = _HttpServerRequest(self.request_msg)
try:
# Authentication, etc.
self.server.PreHandleRequest(handler_context)
# Call actual request handler
result = self.server.HandleRequest(handler_context)
except (http.HttpException, KeyboardInterrupt, SystemExit):
raise
except Exception, err:
logging.exception("Caught exception")
raise http.HttpInternalServerError(message=str(err))
except:
logging.exception("Unknown exception")
raise http.HttpInternalServerError(message="Unknown error")
# TODO: Content-type
encoder = http.HttpJsonConverter()
self.response_msg.start_line.code = http.HTTP_OK
self.response_msg.body = encoder.Encode(result)
self.response_msg.headers = handler_context.resp_headers
self.response_msg.headers[http.HTTP_CONTENT_TYPE] = encoder.CONTENT_TYPE
try:
# Authentication, etc.
self.server.PreHandleRequest(handler_context)
# Call actual request handler
result = self.server.HandleRequest(handler_context)
except (http.HttpException, KeyboardInterrupt, SystemExit):
raise
except Exception, err:
logging.exception("Caught exception")
raise http.HttpInternalServerError(message=str(err))
except:
logging.exception("Unknown exception")
raise http.HttpInternalServerError(message="Unknown error")
# TODO: Content-type
encoder = http.HttpJsonConverter()
self.response_msg.start_line.code = http.HTTP_OK
self.response_msg.body = encoder.Encode(result)
self.response_msg.headers = handler_context.resp_headers
self.response_msg.headers[http.HTTP_CONTENT_TYPE] = encoder.CONTENT_TYPE
finally:
# No reason to keep this any longer, even for exceptions
handler_context.private = None
def _SendResponse(self):
"""Sends the response to the client.
......
......@@ -78,6 +78,7 @@ class TestMisc(unittest.TestCase):
self.assert_(hasattr(server_request, "request_headers"))
self.assert_(hasattr(server_request, "request_body"))
self.assert_(isinstance(server_request.resp_headers, dict))
self.assert_(hasattr(server_request, "private"))
def testServerSizeLimits(self):
"""Test HTTP server size limits"""
......
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