Revision 377ae13e
b/lib/http/server.py | ||
---|---|---|
265 | 265 |
# Most web servers default to HTTP 0.9, i.e. don't send a status line. |
266 | 266 |
default_request_version = http.HTTP_0_9 |
267 | 267 |
|
268 |
# Error message settings |
|
269 |
error_message_format = DEFAULT_ERROR_MESSAGE |
|
270 |
error_content_type = DEFAULT_ERROR_CONTENT_TYPE |
|
271 |
|
|
272 | 268 |
responses = BaseHTTPServer.BaseHTTPRequestHandler.responses |
273 | 269 |
|
274 | 270 |
# Timeouts in seconds for socket layer |
... | ... | |
420 | 416 |
"explain": longmsg, |
421 | 417 |
} |
422 | 418 |
|
423 |
self.response_msg.start_line.code = err.code |
|
419 |
(content_type, body) = self.handler.FormatErrorMessage(values) |
|
420 |
|
|
421 |
headers = { |
|
422 |
http.HTTP_CONTENT_TYPE: content_type, |
|
423 |
} |
|
424 | 424 |
|
425 |
headers = {} |
|
426 | 425 |
if err.headers: |
427 | 426 |
headers.update(err.headers) |
428 |
headers[http.HTTP_CONTENT_TYPE] = self.error_content_type |
|
429 |
self.response_msg.headers = headers |
|
430 |
|
|
431 |
self.response_msg.body = self._FormatErrorMessage(values) |
|
432 |
|
|
433 |
def _FormatErrorMessage(self, values): |
|
434 |
"""Formats the body of an error message. |
|
435 |
|
|
436 |
@type values: dict |
|
437 |
@param values: dictionary with keys code, message and explain. |
|
438 |
@rtype: string |
|
439 |
@return: the body of the message |
|
440 | 427 |
|
441 |
""" |
|
442 |
return self.error_message_format % values |
|
428 |
self.response_msg.start_line.code = err.code |
|
429 |
self.response_msg.headers = headers |
|
430 |
self.response_msg.body = body |
|
443 | 431 |
|
444 | 432 |
|
445 | 433 |
class HttpServer(http.HttpBase, asyncore.dispatcher): |
... | ... | |
592 | 580 |
|
593 | 581 |
""" |
594 | 582 |
raise NotImplementedError() |
583 |
|
|
584 |
@staticmethod |
|
585 |
def FormatErrorMessage(values): |
|
586 |
"""Formats the body of an error message. |
|
587 |
|
|
588 |
@type values: dict |
|
589 |
@param values: dictionary with keys C{code}, C{message} and C{explain}. |
|
590 |
@rtype: tuple; (string, string) |
|
591 |
@return: Content-type and response body |
|
592 |
|
|
593 |
""" |
|
594 |
return (DEFAULT_ERROR_CONTENT_TYPE, DEFAULT_ERROR_MESSAGE % values) |
b/lib/server/rapi.py | ||
---|---|---|
64 | 64 |
self.body_data = None |
65 | 65 |
|
66 | 66 |
|
67 |
class JsonErrorRequestExecutor(http.server.HttpServerRequestExecutor): |
|
68 |
"""Custom Request Executor class that formats HTTP errors in JSON. |
|
69 |
|
|
70 |
""" |
|
71 |
error_content_type = http.HTTP_APP_JSON |
|
72 |
|
|
73 |
def _FormatErrorMessage(self, values): |
|
74 |
"""Formats the body of an error message. |
|
75 |
|
|
76 |
@type values: dict |
|
77 |
@param values: dictionary with keys code, message and explain. |
|
78 |
@rtype: string |
|
79 |
@return: the body of the message |
|
80 |
|
|
81 |
""" |
|
82 |
return serializer.DumpJson(values) |
|
83 |
|
|
84 |
|
|
85 | 67 |
class RemoteApiHandler(http.auth.HttpServerRequestAuthentication, |
86 | 68 |
http.server.HttpServerHandler): |
87 | 69 |
"""REST Request Handler Class. |
... | ... | |
127 | 109 |
|
128 | 110 |
return True |
129 | 111 |
|
112 |
@staticmethod |
|
113 |
def FormatErrorMessage(values): |
|
114 |
"""Formats the body of an error message. |
|
115 |
|
|
116 |
@type values: dict |
|
117 |
@param values: dictionary with keys C{code}, C{message} and C{explain}. |
|
118 |
@rtype: tuple; (string, string) |
|
119 |
@return: Content-type and response body |
|
120 |
|
|
121 |
""" |
|
122 |
return (http.HTTP_APP_JSON, serializer.DumpJson(values)) |
|
123 |
|
|
130 | 124 |
def _GetRequestContext(self, req): |
131 | 125 |
"""Returns the context for a request. |
132 | 126 |
|
... | ... | |
319 | 313 |
|
320 | 314 |
server = \ |
321 | 315 |
http.server.HttpServer(mainloop, options.bind_address, options.port, |
322 |
handler, ssl_params=options.ssl_params, ssl_verify_peer=False, |
|
323 |
request_executor_class=JsonErrorRequestExecutor) |
|
316 |
handler, ssl_params=options.ssl_params, ssl_verify_peer=False) |
|
324 | 317 |
server.Start() |
325 | 318 |
|
326 | 319 |
return (mainloop, server) |
Also available in: Unified diff