Exemplo n.º 1
0
    def _SendResponse(sock, req_msg, msg, timeout):
        """Sends the response to the client.

    """
        try:
            _HttpServerToClientMessageWriter(sock, req_msg, msg, timeout)
        except http.HttpSocketTimeout:
            raise http.HttpError("Timeout while sending response")
        except socket.error as err:
            raise http.HttpError("Error sending response: %s" % err)
Exemplo n.º 2
0
  def _ReadRequest(sock, timeout):
    """Reads a request sent by client.

    """
    msg = http.HttpMessage()

    try:
      reader = _HttpClientToServerMessageReader(sock, msg, timeout)
    except http.HttpSocketTimeout:
      raise http.HttpError("Timeout while reading request")
    except socket.error, err:
      raise http.HttpError("Error reading request: %s" % err)
Exemplo n.º 3
0
def _HandleServerRequestInner(handler, req_msg, reader):
    """Calls the handler function for the current request.

  """
    handler_context = _HttpServerRequest(req_msg.start_line.method,
                                         req_msg.start_line.path,
                                         req_msg.headers, req_msg.body,
                                         reader.sock)

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

    try:
        try:
            # Authentication, etc.
            handler.PreHandleRequest(handler_context)

            # Call actual request handler
            result = handler.HandleRequest(handler_context)
        except (http.HttpException, errors.RapiTestResult, KeyboardInterrupt,
                SystemExit):
            raise
        except Exception as err:
            logging.exception("Caught exception")
            raise http.HttpInternalServerError(message=str(err))
        except:
            logging.exception("Unknown exception")
            raise http.HttpInternalServerError(message="Unknown error")

        if not isinstance(result, (str, bytes)):
            raise http.HttpError("Handler function didn't return string type")

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