Пример #1
0
    public void init(final ISocket fcgiSocket) throws IOException {
      _fcgiSocket = fcgiSocket;

      _is = fcgiSocket.getInputStream();
      _chunkLength = 0;
      _isDead = false;
    }
Пример #2
0
  public void service(final RequestAdapter request, final ResponseAdapter response)
      throws IOException {

    final OutputStream out = response.getOutputStream();

    final ISocket fcgiSocket = connectionFactory.getConnection();
    fcgiSocket.setSoTimeout((int) READ_TIMEOUT);

    try {
      synchronized (fcgiSocket) {
        handleRequest(request, response, fcgiSocket, out, keepAlive);
      }
    } finally {
      if (fcgiSocket != null) {
        connectionFactory.releaseConnection(fcgiSocket);
      }
      fcgiSocket.close();
    }
  }
Пример #3
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;
  }