Ejemplo n.º 1
0
  /**
   * Process an incoming HTTP request on the Socket that has been assigned to this Processor. Any
   * exceptions that occur during processing must be swallowed and dealt with.
   *
   * @param socket The socket on which we are connected to the client
   */
  private void process(Socket socket) {

    boolean ok = true;
    boolean finishResponse = true;
    SocketInputStream input = null;
    OutputStream output = null;

    // Construct and initialize the objects we will need
    try {
      input = new SocketInputStream(socket.getInputStream(), connector.getBufferSize());
    } catch (Exception e) {
      log("process.create", e);
      ok = false;
    }

    keepAlive = true;

    while (!stopped && ok && keepAlive) {

      finishResponse = true;

      try {
        request.setStream(input);
        request.setResponse(response);
        output = socket.getOutputStream();
        response.setStream(output);
        response.setRequest(request);
        ((HttpServletResponse) response.getResponse()).setHeader("Server", SERVER_INFO);
      } catch (Exception e) {
        log("process.create", e);
        ok = false;
      }

      // Parse the incoming request
      try {
        if (ok) {
          parseConnection(socket);
          parseRequest(input, output);
          if (!request.getRequest().getProtocol().startsWith("HTTP/0")) parseHeaders(input);
          if (http11) {
            // Sending a request acknowledge back to the client if
            // requested.
            ackRequest(output);
            // If the protocol is HTTP/1.1, chunking is allowed.
            if (connector.isChunkingAllowed()) response.setAllowChunking(true);
          }
        }
      } catch (EOFException e) {
        // It's very likely to be a socket disconnect on either the
        // client or the server
        ok = false;
        finishResponse = false;
      } catch (ServletException e) {
        ok = false;
        try {
          ((HttpServletResponse) response.getResponse())
              .sendError(HttpServletResponse.SC_BAD_REQUEST);
        } catch (Exception f) {;
        }
      } catch (InterruptedIOException e) {
        if (debug > 1) {
          try {
            log("process.parse", e);
            ((HttpServletResponse) response.getResponse())
                .sendError(HttpServletResponse.SC_BAD_REQUEST);
          } catch (Exception f) {;
          }
        }
        ok = false;
      } catch (Exception e) {
        try {
          log("process.parse", e);
          ((HttpServletResponse) response.getResponse())
              .sendError(HttpServletResponse.SC_BAD_REQUEST);
        } catch (Exception f) {;
        }
        ok = false;
      }

      // Ask our Container to process this request
      try {
        ((HttpServletResponse) response).setHeader("Date", FastHttpDateFormat.getCurrentDate());
        if (ok) {
          connector.getContainer().invoke(request, response);
        }
      } catch (ServletException e) {
        log("process.invoke", e);
        try {
          ((HttpServletResponse) response.getResponse())
              .sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (Exception f) {;
        }
        ok = false;
      } catch (InterruptedIOException e) {
        ok = false;
      } catch (Throwable e) {
        log("process.invoke", e);
        try {
          ((HttpServletResponse) response.getResponse())
              .sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } catch (Exception f) {;
        }
        ok = false;
      }

      // Finish up the handling of the request
      if (finishResponse) {
        try {
          response.finishResponse();
        } catch (IOException e) {
          ok = false;
        } catch (Throwable e) {
          log("process.invoke", e);
          ok = false;
        }
        try {
          request.finishRequest();
        } catch (IOException e) {
          ok = false;
        } catch (Throwable e) {
          log("process.invoke", e);
          ok = false;
        }
        try {
          if (output != null) output.flush();
        } catch (IOException e) {
          ok = false;
        }
      }

      // We have to check if the connection closure has been requested
      // by the application or the response stream (in case of HTTP/1.0
      // and keep-alive).
      if ("close".equals(response.getHeader("Connection"))) {
        keepAlive = false;
      }

      // End of request processing
      status = Constants.PROCESSOR_IDLE;

      // Recycling the request and the response objects
      request.recycle();
      response.recycle();
    }

    try {
      shutdownInput(input);
      socket.close();
    } catch (IOException e) {;
    } catch (Throwable e) {
      log("process.invoke", e);
    }
    socket = null;
  }