예제 #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;
  }
예제 #2
0
  private boolean handleRequest(
      HttpServletRequest req,
      HttpServletResponse res,
      ClientSocket stream,
      OutputStream out,
      boolean keepalive)
      throws ServletException, IOException {
    ReadStream rs = stream.getInputStream();
    WriteStream ws = stream.getOutputStream();

    writeHeader(ws, FCGI_BEGIN_REQUEST, 8);

    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(stream, ws, req);

    InputStream in = req.getInputStream();
    TempBuffer tempBuf = TempBuffer.allocate();
    byte[] buf = tempBuf.getBuffer();
    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);
    }

    TempBuffer.free(tempBuf);
    tempBuf = null;

    /*
    if (hasStdin)
      writeHeader(fcgiSocket, ws, FCGI_STDIN, 0);
    */
    writeHeader(ws, FCGI_STDIN, 0);

    ws.flush();

    FastCGIInputStream is = new FastCGIInputStream(stream);

    int ch = parseHeaders(res, is);

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

    TempBuffer tb = TempBuffer.allocate();
    byte[] buffer = tb.getBuffer();

    while ((sublen = is.read(buffer, 0, buffer.length)) > 0) {
      out.write(buffer, 0, sublen);
    }

    TempBuffer.free(tb);

    return !is.isDead() && keepalive;
  }