remm 2005/08/01 02:40:14 Modified: jk/java/org/apache/coyote/ajp LocalStrings.properties AjpMessage.java webapps/docs changelog.xml Log: - Internationalization and code cleanups. - No functional change. Revision Changes Path 1.4 +7 -1 jakarta-tomcat-connectors/jk/java/org/apache/coyote/ajp/LocalStrings.properties Index: LocalStrings.properties =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/jk/java/org/apache/coyote/ajp/LocalStrings.properties,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- LocalStrings.properties 29 Jul 2005 10:23:55 -0000 1.3 +++ LocalStrings.properties 1 Aug 2005 09:40:14 -0000 1.4 @@ -28,3 +28,9 @@ ajpprocessor.request.process=Error processing request ajpprocessor.certs.fail=Certificate convertion failed ajpprocessor.socket.info=Exception getting socket information + +ajpmessage.null=Cannot append null value +ajpmessage.overflow=Overflow error for buffer adding {0} bytes at position {1} +ajpmessage.read=Requested {0} bytes exceeds message available data +ajpmessage.invalid=Invalid message recieved with signature {0} + 1.4 +102 -63 jakarta-tomcat-connectors/jk/java/org/apache/coyote/ajp/AjpMessage.java Index: AjpMessage.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/jk/java/org/apache/coyote/ajp/AjpMessage.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- AjpMessage.java 29 Jul 2005 10:23:55 -0000 1.3 +++ AjpMessage.java 1 Aug 2005 09:40:14 -0000 1.4 @@ -21,6 +21,7 @@ import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.buf.CharChunk; import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.res.StringManager; /** * A single packet for communication between the web server and the @@ -36,29 +37,45 @@ * @author Costin Manolache */ public class AjpMessage { - - private static org.apache.commons.logging.Log log= - org.apache.commons.logging.LogFactory.getLog( AjpMessage.class ); - + + protected static org.apache.commons.logging.Log log = + org.apache.commons.logging.LogFactory.getLog(AjpMessage.class); + + /** + * The string manager for this package. + */ + protected static StringManager sm = + StringManager.getManager(Constants.Package); + + + // ----------------------------------------------------- Instance Variables + + /** * Fixed size buffer. */ - private byte buf[] = new byte[8*1024]; - + protected byte buf[] = new byte[8 * 1024]; + + /** * The current read or write position in the buffer. */ - private int pos; - + protected int pos; + + /** * This actually means different things depending on whether the * packet is read or write. For read, it's the length of the * payload (excluding the header). For write, it's the length of * the packet as a whole (counting the header). Oh, well. */ - private int len; + protected int len; + + // --------------------------------------------------------- Public Methods + + /** * Prepare this packet for accumulating a message from the container to * the web server. Set the write position to just after the header @@ -68,7 +85,8 @@ len = 4; pos = 4; } - + + /** * For a packet to be sent to the web server, finish the process of * accumulating data and write the length of the data payload into @@ -84,15 +102,16 @@ buf[3] = (byte) (dLen & 0xFF); } + public byte[] getBuffer() { return buf; } + public int getLen() { return len; } - // ============ Data Writing Methods =================== /** * Add an int. @@ -104,10 +123,12 @@ buf[pos++] = (byte) (val & 0xFF); } + public void appendByte(int val) { buf[pos++] = (byte) val; } + public void appendLongInt(int val) { buf[pos++] = (byte) ((val >>> 24) & 0xFF); buf[pos++] = (byte) ((val >>> 16) & 0xFF); @@ -115,6 +136,7 @@ buf[pos++] = (byte) (val & 0xFF); } + /** * Write a String out at the current write position. Strings are * encoded with the length in two bytes first, then the string, and @@ -142,26 +164,25 @@ } } + public void appendByteChunk(ByteChunk bc) throws IOException { if (bc == null) { - log.error("appendByteChunk() null"); + log.error(sm.getString("ajpmessage.null"), + new NullPointerException()); appendInt(0); appendByte(0); return; } - - byte[] bytes = bc.getBytes(); - int start = bc.getStart(); - appendInt(bc.getLength()); - cpBytes(bytes, start, bc.getLength()); - appendByte(0); + appendBytes(bc.getBytes(), bc.getStart(), bc.getLength()); } + public void appendCharChunk(CharChunk cc) throws IOException { - if(cc == null) { - log.error("appendByteChunk() null"); + if (cc == null) { + log.error(sm.getString("ajpmessage.null"), + new NullPointerException()); appendInt(0); appendByte(0); return; @@ -187,10 +208,12 @@ appendByte(0); } + public void appendString(String str) throws IOException { - if(str == null) { - log.error("appendByteChunk() null"); + if (str == null) { + log.error(sm.getString("ajpmessage.null"), + new NullPointerException()); appendInt(0); appendByte(0); return; @@ -215,6 +238,7 @@ appendByte(0); } + /** * Copy a chunk of bytes into the packet, starting at the current * write position. The chunk of bytes is encoded with the length @@ -226,27 +250,22 @@ * @param off The offset into the array at which to start copying * @param numBytes The number of bytes to copy. */ - public void appendBytes(byte b[], int off, int numBytes) { - appendInt(numBytes); - cpBytes(b, off, numBytes); - appendByte(0); - } - - private void cpBytes(byte b[], int off, int numBytes) { - if (pos + numBytes >= buf.length) { - log.error("Buffer overflow: buffer.len=" + buf.length + " pos=" + - pos + " data=" + numBytes); - dump("Overflow/coBytes"); - log.error("Overflow ", new Throwable()); + public void appendBytes(byte[] b, int off, int numBytes) { + if (pos + numBytes + 3 >= buf.length) { + log.error(sm.getString("ajpmessage.overflow", "" + numBytes, "" + pos), + new ArrayIndexOutOfBoundsException()); + if (log.isDebugEnabled()) { + dump("Overflow/coBytes"); + } return; } + appendInt(numBytes); System.arraycopy(b, off, buf, pos, numBytes); pos += numBytes; - // buf[pos + numBytes] = 0; // Terminating \0 + appendByte(0); } - - // ============ Data Reading Methods =================== + /** * Read an integer from packet, and advance the read position past * it. Integers are encoded as two unsigned bytes with the @@ -254,29 +273,33 @@ * little-endian order within each byte. */ public int getInt() { - int b1 = buf[pos++] & 0xFF; // No swap, Java order + int b1 = buf[pos++] & 0xFF; // No swap, Java order int b2 = buf[pos++] & 0xFF; return (b1<<8) + b2; } + public int peekInt() { - int b1 = buf[pos] & 0xFF; // No swap, Java order + int b1 = buf[pos] & 0xFF; // No swap, Java order int b2 = buf[pos+1] & 0xFF; return (b1<<8) + b2; } + public byte getByte() { byte res = buf[pos++]; return res; } + public byte peekByte() { byte res = buf[pos]; return res; } + public void getBytes(MessageBytes mb) { int length = getInt(); if ((length == 0xFFFF) || (length == -1)) { @@ -288,6 +311,7 @@ pos++; // Skip the terminating \0 } + /** * Copy a chunk of bytes from the packet into an array and advance * the read position past the chunk. See appendBytes() for details @@ -295,24 +319,24 @@ * * @return The number of bytes copied. */ - public int getBytes(byte dest[]) { + public int getBytes(byte[] dest) { int length = getInt(); - if (length > buf.length) { - // XXX Should be if(pos + length > buff.legth)? - log.error("getBytes() buffer overflow " + length + " " + buf.length); + if (pos + length > buf.length) { + log.error(sm.getString("ajpmessage.read", "" + length)); + return 0; } if ((length == 0xFFFF) || (length == -1)) { - log.info("Null string " + length); return 0; } - System.arraycopy(buf, pos, dest, 0, length); + System.arraycopy(buf, pos, dest, 0, length); pos += length; - pos++; // Skip terminating \0 XXX I believe this is wrong but harmless + pos++; // Skip terminating \0 return length; } + /** * Read a 32 bits integer from packet, and advance the read position past * it. Integers are encoded as four unsigned bytes with the @@ -320,7 +344,7 @@ * little-endian order within each byte. */ public int getLongInt() { - int b1 = buf[pos++] & 0xFF; // No swap, Java order + int b1 = buf[pos++] & 0xFF; // No swap, Java order b1 <<= 8; b1 |= (buf[pos++] & 0xFF); b1 <<= 8; @@ -330,65 +354,80 @@ return b1; } + public int getHeaderLength() { return 4; } + public int processHeader() { pos = 0; int mark = getInt(); len = getInt(); if ((mark != 0x1234) && (mark != 0x4142)) { - // XXX Logging - log.error("BAD packet signature " + mark); - dump("In: "); + log.error(sm.getString("ajpmessage.invalid", "" + mark)); + if (log.isDebugEnabled()) { + dump("In: "); + } return -1; } - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("Received " + len + " " + buf[0]); + } return len; } - /* -------------------- Utilities -------------------- */ public void dump(String msg) { - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug(msg + ": " + buf + " " + pos +"/" + (len + 4)); + } int max = pos; if (len + 4 > pos) max = len+4; if (max > 1000) max = 1000; - if (log.isDebugEnabled()) - for(int j = 0; j < max; j += 16) + if (log.isDebugEnabled()) { + for (int j = 0; j < max; j += 16) { log.debug(hexLine(buf, j, len)); + } + } } - private static String hexLine(byte buf[], int start, int len) { + + // ------------------------------------------------------ Protected Methods + + + protected static String hexLine(byte buf[], int start, int len) { StringBuffer sb = new StringBuffer(); for (int i = start; i < start + 16 ; i++) { - if (i < len + 4) + if (i < len + 4) { sb.append(hex(buf[i]) + " "); - else + } else { sb.append(" "); + } } sb.append(" | "); for (int i = start; i < start + 16 && i < len + 4; i++) { - if (!Character.isISOControl((char)buf[i])) - sb.append(new Character((char)buf[i])); - else + if (!Character.isISOControl((char) buf[i])) { + sb.append(new Character((char) buf[i])); + } else { sb.append("."); + } } return sb.toString(); } - private static String hex(int x) { + + protected static String hex(int x) { String h = Integer.toHexString(x); - if (h.length() == 1) + if (h.length() == 1) { h = "0" + h; + } return h.substring(h.length() - 2); } + } 1.348 +3 -0 jakarta-tomcat-catalina/webapps/docs/changelog.xml Index: changelog.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/webapps/docs/changelog.xml,v retrieving revision 1.347 retrieving revision 1.348 diff -u -r1.347 -r1.348 --- changelog.xml 30 Jul 2005 21:22:25 -0000 1.347 +++ changelog.xml 1 Aug 2005 09:40:14 -0000 1.348 @@ -85,6 +85,9 @@ <bug>35942</bug>: Fix NPE retriving cipher suite attribute when no certificate was submitted (for example with no SSL). (remm) </fix> + <fix> + Internationalization and code cleanups for APR AJP implementation. (remm) + </fix> </changelog> </subsection>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]