Beispiel #1
0
  private boolean handleRequest(
      final RequestAdapter req,
      final ResponseAdapter res,
      final ISocket fcgiSocket,
      final OutputStream out,
      final boolean keepalive)
      throws IOException {
    final OutputStream ws = fcgiSocket.getOutputStream();

    writeHeader(ws, FCGI_BEGIN_REQUEST, 8);

    final int role = FCGI_RESPONDER;

    ws.write(role >> 8);
    ws.write(role);
    ws.write(keepalive ? FCGI_KEEP_CONN : 0); // flags
    for (int i = 0; i < 5; i++) {
      ws.write(0);
    }

    setEnvironment(ws, req);

    final InputStream in = req.getInputStream();
    final byte[] buf = new byte[4096];
    final int len = buf.length;
    int sublen;

    writeHeader(ws, FCGI_PARAMS, 0);

    boolean hasStdin = false;
    while ((sublen = in.read(buf, 0, len)) > 0) {
      hasStdin = true;
      writeHeader(ws, FCGI_STDIN, sublen);
      ws.write(buf, 0, sublen);
    }

    if (hasStdin) {
      writeHeader(ws, FCGI_STDIN, 0);
    }

    final FastCGIInputStream is = new FastCGIInputStream(fcgiSocket);

    int ch = parseHeaders(res, is);

    if (ch >= 0) {
      out.write(ch);
    }

    while ((ch = is.read()) >= 0) {
      out.write(ch);
    }

    return !is.isDead() && keepalive;
  }
Beispiel #2
0
  private void setEnvironment(final OutputStream ws, final RequestAdapter req) throws IOException {
    addHeader(ws, "REQUEST_URI", req.getRequestURI());
    addHeader(ws, "REQUEST_METHOD", req.getMethod());
    addHeader(ws, "SERVER_SOFTWARE", FastCGIHandler.class.getName());
    addHeader(ws, "SERVER_NAME", req.getServerName());
    addHeader(ws, "SERVER_PORT", String.valueOf(req.getServerPort()));
    addHeader(ws, "REMOTE_ADDR", req.getRemoteAddr());
    addHeader(ws, "REMOTE_HOST", req.getRemoteAddr());
    if (req.getRemoteUser() != null) {
      addHeader(ws, "REMOTE_USER", req.getRemoteUser());
    } else {
      addHeader(ws, "REMOTE_USER", "");
    }
    if (req.getAuthType() != null) {
      addHeader(ws, "AUTH_TYPE", req.getAuthType());
    }
    addHeader(ws, "GATEWAY_INTERFACE", "CGI/1.1");
    addHeader(ws, "SERVER_PROTOCOL", req.getProtocol());

    if (req.getQueryString() != null) {
      addHeader(ws, "QUERY_STRING", req.getQueryString());
    } else {
      addHeader(ws, "QUERY_STRING", "");
    }

    final String scriptPath = req.getServletPath();
    getLog().debug("FCGI file: " + scriptPath);
    addHeader(ws, "PATH_INFO", req.getContextPath() + scriptPath);
    addHeader(ws, "PATH_TRANSLATED", req.getRealPath(scriptPath));
    addHeader(ws, "SCRIPT_FILENAME", req.getRealPath(scriptPath));
    final int contentLength = req.getContentLength();
    if (contentLength < 0) {
      addHeader(ws, "CONTENT_LENGTH", "0");
    } else {
      addHeader(ws, "CONTENT_LENGTH", String.valueOf(contentLength));
    }

    addHeader(ws, "DOCUMENT_ROOT", req.getRealPath("/"));

    final Enumeration<String> e = req.getHeaderNames();
    while (e.hasMoreElements()) {
      final String key = e.nextElement();
      final String value = req.getHeader(key);

      if (!isHeaderFiltered(key)) {
        if (key.equalsIgnoreCase("content-length")) {
          addHeader(ws, "CONTENT_LENGTH", value);
        } else if (key.equalsIgnoreCase("content-type")) {
          addHeader(ws, "CONTENT_TYPE", value);
        } else {
          addHeader(ws, convertHeader(key), value);
        }
      }
    }
  }