private void addHeader(ClientSocket stream, WriteStream ws, CharBuffer key, String value) throws IOException { int keyLen = key.getLength(); int valLen = value.length(); int len = keyLen + valLen; if (keyLen < 0x80) len += 1; else len += 4; if (valLen < 0x80) len += 1; else len += 4; writeHeader(ws, FCGI_PARAMS, len); if (keyLen < 0x80) ws.write(keyLen); else { ws.write(0x80 | (keyLen >> 24)); ws.write(keyLen >> 16); ws.write(keyLen >> 8); ws.write(keyLen); } if (valLen < 0x80) ws.write(valLen); else { ws.write(0x80 | (valLen >> 24)); ws.write(valLen >> 16); ws.write(valLen >> 8); ws.write(valLen); } ws.print(key.getBuffer(), 0, keyLen); ws.print(value); }
/** Writes to the underlying log. */ protected void write(byte[] buffer, int offset, int length) throws IOException { /* String s = new String(buffer, offset, length); if (s.startsWith("127")) { System.out.println("WRITE: " + s); Thread.dumpStack(); } */ synchronized (_logLock) { if (_isRollingOver && getTempStreamMax() < _tempStreamSize) { try { _logLock.wait(); } catch (Exception e) { } } if (!_isRollingOver) { if (_os == null) openLog(); if (_os != null) _os.write(buffer, offset, length); } else { if (_tempStream == null) { _tempStream = createTempStream(); _tempStreamSize = 0; } _tempStreamSize += length; _tempStream.write(buffer, offset, length, false); } } }
/** Prints the date to a stream. */ public void printDate(WriteStream os) throws IOException { os.print(DAY_NAMES[(int) (_dayOfEpoch % 7 + 11) % 7]); os.write(','); os.write(' '); os.print((_dayOfMonth + 1) / 10); os.print((_dayOfMonth + 1) % 10); os.write(' '); os.print(MONTH_NAMES[(int) _month]); os.write(' '); os.print(_year); os.write(' '); os.print((_timeOfDay / 36000000) % 10); os.print((_timeOfDay / 3600000) % 10); os.write(':'); os.print((_timeOfDay / 600000) % 6); os.print((_timeOfDay / 60000) % 10); os.write(':'); os.print((_timeOfDay / 10000) % 6); os.print((_timeOfDay / 1000) % 10); if (_zoneName == null) { os.print(" GMT"); return; } long offset = _zoneOffset; if (offset < 0) { os.write(' '); os.write('-'); offset = -offset; } else { os.write(' '); os.write('+'); } os.print((offset / 36000000) % 10); os.print((offset / 3600000) % 10); os.print((offset / 600000) % 6); os.print((offset / 60000) % 10); os.write(' '); os.write('('); os.print(_zoneName); os.write(')'); }
private void writeHeader(WriteStream ws, int type, int length) throws IOException { int id = 1; int pad = 0; ws.write(FCGI_VERSION); ws.write(type); ws.write(id >> 8); ws.write(id); ws.write(length >> 8); ws.write(length); ws.write(pad); ws.write(0); }
private Class generateProxy() { try { JavaClassLoader jLoader = new JavaClassLoader(_cl.getClassLoader()); JavaClass jClass = new JavaClass(jLoader); jClass.setAccessFlags(Modifier.PUBLIC); ConstantPool cp = jClass.getConstantPool(); jClass.setWrite(true); jClass.setMajor(49); jClass.setMinor(0); String superClassName = _cl.getName().replace('.', '/'); String thisClassName = superClassName + "$BeanProxy"; jClass.setSuperClass(superClassName); jClass.setThisClass(thisClassName); jClass.addInterface("java/io/Serializable"); jClass.addInterface("com/caucho/config/inject/HandleAware"); generateConstructors(jClass, superClassName); generateWriteReplace(jClass); generateSetHandle(jClass); ByteArrayOutputStream bos = new ByteArrayOutputStream(); WriteStream out = Vfs.openWrite(bos); jClass.write(out); out.close(); byte[] buffer = bos.toByteArray(); if (false) { String userName = System.getProperty("user.name"); out = Vfs.lookup("file:/tmp/" + userName + "/qa/temp.class").openWrite(); out.write(buffer, 0, buffer.length); out.close(); } String cleanName = thisClassName.replace('/', '.'); _proxyClass = (Class<X>) new ProxyClassLoader().loadClass(cleanName, buffer); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } return _proxyClass; }
@Override public void close(int code, String msg) { if (_isWriteClosed.getAndSet(true)) return; try { WriteStream out = getWriteStream(); out.write(0x88); out.write(0x00); out.flush(); } catch (IOException e) { log.log(Level.WARNING, e.toString(), e); } finally { disconnect(); } }
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; }