Revision ab221ddf daemons/ganeti-rapi

b/daemons/ganeti-rapi
52 52
    self.handler = None
53 53
    self.handler_fn = None
54 54
    self.handler_access = None
55
    self.body_data = None
55 56

  
56 57

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

  
60 61
  """
61
  error_content_type = "application/json"
62
  error_content_type = http.HttpJsonConverter.CONTENT_TYPE
62 63

  
63 64
  def _FormatErrorMessage(self, values):
64 65
    """Formats the body of an error message.
......
116 117
      if ctx.handler_access is None:
117 118
        raise AssertionError("Permissions definition missing")
118 119

  
120
      # This is only made available in HandleRequest
121
      ctx.body_data = None
122

  
119 123
      req.private = ctx
120 124

  
121 125
    return req.private
......
162 166
    """
163 167
    ctx = self._GetRequestContext(req)
164 168

  
169
    # Deserialize request parameters
170
    if req.request_body:
171
      # RFC2616, 7.2.1: Any HTTP/1.1 message containing an entity-body SHOULD
172
      # include a Content-Type header field defining the media type of that
173
      # body. [...] If the media type remains unknown, the recipient SHOULD
174
      # treat it as type "application/octet-stream".
175
      req_content_type = req.request_headers.get(http.HTTP_CONTENT_TYPE,
176
                                                 http.HTTP_APP_OCTET_STREAM)
177
      if (req_content_type.lower() !=
178
          http.HttpJsonConverter.CONTENT_TYPE.lower()):
179
        raise http.HttpUnsupportedMediaType()
180

  
181
      try:
182
        ctx.body_data = serializer.LoadJson(req.request_body)
183
      except Exception:
184
        raise http.HttpBadRequest(message="Unable to parse JSON data")
185
    else:
186
      ctx.body_data = None
187

  
165 188
    try:
166 189
      result = ctx.handler_fn()
167 190
    except luxi.TimeoutError:
......
173 196
      logging.exception("Error while handling the %s request", method)
174 197
      raise
175 198

  
176
    return result
199
    req.resp_headers[http.HTTP_CONTENT_TYPE] = \
200
      http.HttpJsonConverter.CONTENT_TYPE
201

  
202
    return serializer.DumpJson(result)
177 203

  
178 204

  
179 205
def CheckRapi(options, args):

Also available in: Unified diff