public void init(ClientSocket stream) { _stream = stream; _is = stream.getInputStream(); _chunkLength = 0; _isDead = false; }
/** Handle the request. */ @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; OutputStream out = res.getOutputStream(); String sessionId = null; ClientSocket stream = _loadBalancer.openSticky(sessionId, null); boolean isValid = false; if (stream == null) { if (log.isLoggable(Level.FINE)) log.fine(this + " cannot open a stream from " + _loadBalancer); res.sendError(503); return; } try { // fcgiSocket.setExpire(Alarm.getCurrentTime() + _keepaliveTimeout); boolean isKeepalive = true; long startRequestTime = Alarm.getCurrentTime(); if (handleRequest(req, res, stream, out, isKeepalive)) { stream.free(startRequestTime); stream = null; } } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } finally { if (stream != null) stream.close(); } }
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; }