/**
   * This method is the simplified version of the similar method in
   * org.apache.catalina.connector.http.HttpProcessor. However, this method only parses some "easy"
   * headers, such as "cookie", "content-length", and "content-type", and ignore other headers.
   *
   * @param input The input stream connected to our socket
   * @exception IOException if an input/output error occurs
   * @exception ServletException if a parsing error occurs
   */
  private void parseHeaders(SocketInputStream input) throws IOException, ServletException {
    while (true) {
      HttpHeader header = new HttpHeader();
      ;

      // Read the next header
      input.readHeader(header);
      if (header.nameEnd == 0) {
        if (header.valueEnd == 0) {
          return;
        } else {
          throw new ServletException(sm.getString("httpProcessor.parseHeaders.colon"));
        }
      }

      String name = new String(header.name, 0, header.nameEnd);
      String value = new String(header.value, 0, header.valueEnd);
      request.addHeader(name, value);
      // do something for some headers, ignore others.
      if (name.equals("cookie")) {
        Cookie cookies[] = RequestUtil.parseCookieHeader(value);
        for (int i = 0; i < cookies.length; i++) {
          if (cookies[i].getName().equals("jsessionid")) {
            // Override anything requested in the URL
            if (!request.isRequestedSessionIdFromCookie()) {
              // Accept only the first session id cookie
              request.setRequestedSessionId(cookies[i].getValue());
              request.setRequestedSessionCookie(true);
              request.setRequestedSessionURL(false);
            }
          }
          request.addCookie(cookies[i]);
        }
      } else if (name.equals("content-length")) {
        int n = -1;
        try {
          n = Integer.parseInt(value);
        } catch (Exception e) {
          throw new ServletException(sm.getString("httpProcessor.parseHeaders.contentLength"));
        }
        request.setContentLength(n);
      } else if (name.equals("content-type")) {
        request.setContentType(value);
      }
    } // end while
  }
  /**
   * Parse the incoming HTTP request headers, and set the appropriate request headers.
   *
   * @param input The input stream connected to our socket
   * @exception IOException if an input/output error occurs
   * @exception ServletException if a parsing error occurs
   */
  private void parseHeaders(SocketInputStream input) throws IOException, ServletException {

    while (true) {

      HttpHeader header = request.allocateHeader();

      // Read the next header
      input.readHeader(header);
      if (header.nameEnd == 0) {
        if (header.valueEnd == 0) {
          return;
        } else {
          throw new ServletException(sm.getString("httpProcessor.parseHeaders.colon"));
        }
      }

      String value = new String(header.value, 0, header.valueEnd);
      if (debug >= 1) log(" Header " + new String(header.name, 0, header.nameEnd) + " = " + value);

      // Set the corresponding request headers
      if (header.equals(DefaultHeaders.AUTHORIZATION_NAME)) {
        request.setAuthorization(value);
      } else if (header.equals(DefaultHeaders.ACCEPT_LANGUAGE_NAME)) {
        parseAcceptLanguage(value);
      } else if (header.equals(DefaultHeaders.COOKIE_NAME)) {
        Cookie cookies[] = RequestUtil.parseCookieHeader(value);
        for (int i = 0; i < cookies.length; i++) {
          if (cookies[i].getName().equals(Globals.SESSION_COOKIE_NAME)) {
            // Override anything requested in the URL
            if (!request.isRequestedSessionIdFromCookie()) {
              // Accept only the first session id cookie
              request.setRequestedSessionId(cookies[i].getValue());
              request.setRequestedSessionCookie(true);
              request.setRequestedSessionURL(false);
              if (debug >= 1)
                log(
                    " Requested cookie session id is "
                        + ((HttpServletRequest) request.getRequest()).getRequestedSessionId());
            }
          }
          if (debug >= 1)
            log(" Adding cookie " + cookies[i].getName() + "=" + cookies[i].getValue());
          request.addCookie(cookies[i]);
        }
      } else if (header.equals(DefaultHeaders.CONTENT_LENGTH_NAME)) {
        int n = -1;
        try {
          n = Integer.parseInt(value);
        } catch (Exception e) {
          throw new ServletException(sm.getString("httpProcessor.parseHeaders.contentLength"));
        }
        request.setContentLength(n);
      } else if (header.equals(DefaultHeaders.CONTENT_TYPE_NAME)) {
        request.setContentType(value);
      } else if (header.equals(DefaultHeaders.HOST_NAME)) {
        int n = value.indexOf(':');
        if (n < 0) {
          if (connector.getScheme().equals("http")) {
            request.setServerPort(80);
          } else if (connector.getScheme().equals("https")) {
            request.setServerPort(443);
          }
          if (proxyName != null) request.setServerName(proxyName);
          else request.setServerName(value);
        } else {
          if (proxyName != null) request.setServerName(proxyName);
          else request.setServerName(value.substring(0, n).trim());
          if (proxyPort != 0) request.setServerPort(proxyPort);
          else {
            int port = 80;
            try {
              port = Integer.parseInt(value.substring(n + 1).trim());
            } catch (Exception e) {
              throw new ServletException(sm.getString("httpProcessor.parseHeaders.portNumber"));
            }
            request.setServerPort(port);
          }
        }
      } else if (header.equals(DefaultHeaders.CONNECTION_NAME)) {
        if (header.valueEquals(DefaultHeaders.CONNECTION_CLOSE_VALUE)) {
          keepAlive = false;
          response.setHeader("Connection", "close");
        }
        // request.setConnection(header);
        /*
          if ("keep-alive".equalsIgnoreCase(value)) {
          keepAlive = true;
          }
        */
      } else if (header.equals(DefaultHeaders.EXPECT_NAME)) {
        if (header.valueEquals(DefaultHeaders.EXPECT_100_VALUE)) sendAck = true;
        else
          throw new ServletException(sm.getString("httpProcessor.parseHeaders.unknownExpectation"));
      } else if (header.equals(DefaultHeaders.TRANSFER_ENCODING_NAME)) {
        // request.setTransferEncoding(header);
      }

      request.nextHeader();
    }
  }