Revision 1f8588f6

b/daemons/ganeti-rapi
36 36
from ganeti import ssconf
37 37
from ganeti import utils
38 38
from ganeti import luxi
39
from ganeti import serializer
39 40
from ganeti.rapi import connector
40 41

  
41 42
import ganeti.http.auth
......
52 53
    self.handler_access = None
53 54

  
54 55

  
56
class JsonErrorRequestExecutor(http.server.HttpServerRequestExecutor):
57
  """Custom Request Executor class that formats HTTP errors in JSON.
58

  
59
  """
60
  error_content_type = "application/json"
61

  
62
  def _FormatErrorMessage(self, values):
63
    """Formats the body of an error message.
64

  
65
    @type values: dict
66
    @param values: dictionary with keys code, message and explain.
67
    @rtype: string
68
    @return: the body of the message
69

  
70
    """
71
    return serializer.DumpJson(values, indent=True)
72

  
73

  
55 74
class RemoteApiHttpServer(http.auth.HttpServerRequestAuthentication,
56 75
                          http.server.HttpServer):
57 76
  """REST Request Handler Class.
......
233 252
  try:
234 253
    mainloop = daemon.Mainloop()
235 254
    server = RemoteApiHttpServer(mainloop, "", options.port,
236
                                 ssl_params=ssl_params, ssl_verify_peer=False)
255
                                 ssl_params=ssl_params, ssl_verify_peer=False,
256
                                 request_executor_class=
257
                                 JsonErrorRequestExecutor)
237 258
    server.Start()
238 259
    try:
239 260
      mainloop.Run()
b/lib/http/server.py
207 207
    return http.HttpClientToServerStartLine(method, path, version)
208 208

  
209 209

  
210
class _HttpServerRequestExecutor(object):
210
class HttpServerRequestExecutor(object):
211 211
  """Implements server side of HTTP.
212 212

  
213 213
  This class implements the server side of HTTP. It's based on code of
......
405 405
    headers[http.HTTP_CONTENT_TYPE] = self.error_content_type
406 406
    self.response_msg.headers = headers
407 407

  
408
    self.response_msg.body = self.error_message_format % values
408
    self.response_msg.body = self._FormatErrorMessage(values)
409 409

  
410
  def _FormatErrorMessage(self, values):
411
    """Formats the body of an error message.
412

  
413
    @type values: dict
414
    @param values: dictionary with keys code, message and explain.
415
    @rtype: string
416
    @return: the body of the message
417

  
418
    """
419
    return self.error_message_format % values
410 420

  
411 421
class HttpServer(http.HttpBase):
412 422
  """Generic HTTP server class
......
417 427
  MAX_CHILDREN = 20
418 428

  
419 429
  def __init__(self, mainloop, local_address, port,
420
               ssl_params=None, ssl_verify_peer=False):
430
               ssl_params=None, ssl_verify_peer=False,
431
               request_executor_class=None):
421 432
    """Initializes the HTTP server
422 433

  
423 434
    @type mainloop: ganeti.daemon.Mainloop
......
431 442
    @type ssl_verify_peer: bool
432 443
    @param ssl_verify_peer: Whether to require client certificate
433 444
        and compare it with our certificate
445
    @type request_executor_class: class
446
    @param request_executor_class: an class derived from the
447
        HttpServerRequestExecutor class
434 448

  
435 449
    """
436 450
    http.HttpBase.__init__(self)
437 451

  
452
    if request_executor_class is None:
453
      self.request_executor = HttpServerRequestExecutor
454
    else:
455
      self.request_executor = request_executor_class
456

  
438 457
    self.mainloop = mainloop
439 458
    self.local_address = local_address
440 459
    self.port = port
......
505 524
    if pid == 0:
506 525
      # Child process
507 526
      try:
508
        _HttpServerRequestExecutor(self, connection, client_addr)
527
        self.request_executor(self, connection, client_addr)
509 528
      except Exception:
510 529
        logging.exception("Error while handling request from %s:%s",
511 530
                          client_addr[0], client_addr[1])

Also available in: Unified diff