/* Verify HTTP/1.1 request
   * @exception HttpException problem with the request.
   * @exception IOException problem with the connection.
   */
  private void verifyHTTP_1_1() throws HttpException, IOException {
    // Check Host Field exists
    String host = _request.getField(HttpFields.__Host);
    if (host == null) throw new HttpException(HttpResponse.__400_Bad_Request);

    // check and enable requests transfer encodings.
    String transfer_coding = _request.getField(HttpFields.__TransferEncoding);

    if (transfer_coding != null && transfer_coding.length() > 0) {
      // Handling of codings other than chunking is now
      // the responsibility of handlers, filters or servlets.
      // Thanks to the compression filter, we now don't know if
      // what we can handle here.
      if (transfer_coding.equalsIgnoreCase(HttpFields.__Chunked)
          || StringUtil.endsWithIgnoreCase(transfer_coding, HttpFields.__Chunked))
        _inputStream.setChunking();
      else if (StringUtil.asciiToLowerCase(transfer_coding).indexOf(HttpFields.__Chunked) >= 0)
        throw new HttpException(HttpResponse.__400_Bad_Request);
    }

    // Check input content length can be determined
    int content_length = _request.getIntField(HttpFields.__ContentLength);
    String content_type = _request.getField(HttpFields.__ContentType);
    if (!_inputStream.isChunking()) {
      // If we have a content length, use it
      if (content_length >= 0) _inputStream.setContentLength(content_length);
      // else if we have no content
      else if (content_type == null || content_type.length() == 0) _inputStream.setContentLength(0);
      // else we need a content length
      else {
        // TODO - can't do this check as IE stuff up on
        // a redirect.
        // throw new HttpException(HttpResponse.__411_Length_Required);
        _inputStream.setContentLength(0);
      }
    }

    // Handle Continue Expectations
    String expect = _request.getField(HttpFields.__Expect);
    if (expect != null && expect.length() > 0) {
      if (StringUtil.asciiToLowerCase(expect).equals(HttpFields.__ExpectContinue)) {
        _inputStream.setExpectContinues(_outputStream.getOutputStream());
      } else throw new HttpException(HttpResponse.__417_Expectation_Failed);
    } else if (__2068_Continues
        && _inputStream.available() <= 0
        && (HttpRequest.__PUT.equals(_request.getMethod())
            || HttpRequest.__POST.equals(_request.getMethod()))) {
      // Send continue for RFC 2068 exception
      OutputStream real_out = _outputStream.getOutputStream();
      real_out.write(HttpResponse.__Continue);
      real_out.flush();
    }

    // Persistent unless requested otherwise
    _persistent = !_close;
  }
Beispiel #2
0
  /* ------------------------------------------------------------ */
  public void service(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    String pathInContext =
        StringUtil.nonNull(req.getServletPath()) + StringUtil.nonNull(req.getPathInfo());

    if (log.isDebugEnabled()) log.debug("CGI: req.getContextPath() : " + req.getContextPath());
    if (log.isDebugEnabled()) log.debug("CGI: req.getServletPath() : " + req.getServletPath());
    if (log.isDebugEnabled()) log.debug("CGI: req.getPathInfo()    : " + req.getPathInfo());
    if (log.isDebugEnabled()) log.debug("CGI: _docRoot             : " + _docRoot);

    // pathInContext may actually comprises scriptName/pathInfo...We will
    // walk backwards up it until we find the script - the rest must
    // be the pathInfo;

    String both = pathInContext;
    String first = both;
    String last = "";

    File exe = new File(_docRoot, first);

    while ((first.endsWith("/") || !exe.exists()) && !first.isEmpty()) {
      int index = first.lastIndexOf('/');

      first = first.substring(0, index);
      last = both.substring(index, both.length());
      exe = new File(_docRoot, first);
    }

    if (first.length() == 0
        || !exe.exists()
        || !exe.getCanonicalPath().equals(exe.getAbsolutePath())
        || exe.isDirectory()) res.sendError(404);
    else {
      if (log.isDebugEnabled()) log.debug("CGI: script is " + exe);
      if (log.isDebugEnabled()) log.debug("CGI: pathInfo is " + last);

      exec(exe, last, req, res);
    }
  }
Beispiel #3
0
 /** Set a name/value pair, null values will be treated as an empty String */
 public void set(String name, String value) {
   envMap.put(name, name + "=" + StringUtil.nonNull(value));
 }