Revision c81f452f lib/http/server.py

b/lib/http/server.py
214 214
    return http.HttpClientToServerStartLine(method, path, version)
215 215

  
216 216

  
217
def HandleServerRequest(handler, req_msg):
218
  """Calls the handler function for the current request.
219

  
220
  """
221
  handler_context = _HttpServerRequest(req_msg.start_line.method,
222
                                       req_msg.start_line.path,
223
                                       req_msg.headers,
224
                                       req_msg.body)
225

  
226
  logging.debug("Handling request %r", handler_context)
227

  
228
  try:
229
    try:
230
      # Authentication, etc.
231
      handler.PreHandleRequest(handler_context)
232

  
233
      # Call actual request handler
234
      result = handler.HandleRequest(handler_context)
235
    except (http.HttpException, KeyboardInterrupt, SystemExit):
236
      raise
237
    except Exception, err:
238
      logging.exception("Caught exception")
239
      raise http.HttpInternalServerError(message=str(err))
240
    except:
241
      logging.exception("Unknown exception")
242
      raise http.HttpInternalServerError(message="Unknown error")
243

  
244
    if not isinstance(result, basestring):
245
      raise http.HttpError("Handler function didn't return string type")
246

  
247
    return (http.HTTP_OK, handler_context.resp_headers, result)
248
  finally:
249
    # No reason to keep this any longer, even for exceptions
250
    handler_context.private = None
251

  
252

  
217 253
class HttpServerRequestExecutor(object):
218 254
  """Implements server side of HTTP.
219 255

  
......
286 322
                http.HTTP_HOST not in self.request_msg.headers):
287 323
              raise http.HttpBadRequest(message="Missing Host header")
288 324

  
289
            self._HandleRequest()
325
            (self.response_msg.start_line.code, self.response_msg.headers,
326
             self.response_msg.body) = \
327
              HandleServerRequest(self.server, self.request_msg)
290 328

  
291 329
            # Only wait for client to close if we didn't have any exception.
292 330
            force_close = False
......
321 359

  
322 360
    return request_msg_reader
323 361

  
324
  def _HandleRequest(self):
325
    """Calls the handler function for the current request.
326

  
327
    """
328
    handler_context = _HttpServerRequest(self.request_msg.start_line.method,
329
                                         self.request_msg.start_line.path,
330
                                         self.request_msg.headers,
331
                                         self.request_msg.body)
332

  
333
    logging.debug("Handling request %r", handler_context)
334

  
335
    try:
336
      try:
337
        # Authentication, etc.
338
        self.server.PreHandleRequest(handler_context)
339

  
340
        # Call actual request handler
341
        result = self.server.HandleRequest(handler_context)
342
      except (http.HttpException, KeyboardInterrupt, SystemExit):
343
        raise
344
      except Exception, err:
345
        logging.exception("Caught exception")
346
        raise http.HttpInternalServerError(message=str(err))
347
      except:
348
        logging.exception("Unknown exception")
349
        raise http.HttpInternalServerError(message="Unknown error")
350

  
351
      if not isinstance(result, basestring):
352
        raise http.HttpError("Handler function didn't return string type")
353

  
354
      self.response_msg.start_line.code = http.HTTP_OK
355
      self.response_msg.headers = handler_context.resp_headers
356
      self.response_msg.body = result
357
    finally:
358
      # No reason to keep this any longer, even for exceptions
359
      handler_context.private = None
360

  
361 362
  def _SendResponse(self):
362 363
    """Sends the response to the client.
363 364

  

Also available in: Unified diff