1 16 17 package org.apache.jk.common; 18 19 import java.io.IOException ; 20 21 import org.apache.jk.core.Msg; 22 import org.apache.tomcat.util.buf.ByteChunk; 23 import org.apache.tomcat.util.buf.MessageBytes; 24 25 40 public class MsgAjp extends Msg { 41 private static org.apache.commons.logging.Log log= 42 org.apache.commons.logging.LogFactory.getLog( MsgAjp.class ); 43 44 private byte buf[]=new byte[8*1024]; 46 private int pos; 48 54 private int len; 55 56 57 58 59 64 public void reset() { 65 len = 4; 66 pos = 4; 67 } 68 69 74 public void end() { 75 len=pos; 76 int dLen=len-4; 77 78 buf[0] = (byte)0x41; 79 buf[1] = (byte)0x42; 80 buf[2]= (byte)((dLen>>>8 ) & 0xFF ); 81 buf[3] = (byte)(dLen & 0xFF); 82 } 83 84 public byte[] getBuffer() { 85 return buf; 86 } 87 88 public int getLen() { 89 return len; 90 } 91 92 94 99 public void appendInt( int val ) { 100 buf[pos++] = (byte) ((val >>> 8) & 0xFF); 101 buf[pos++] = (byte) (val & 0xFF); 102 } 103 104 public void appendByte( int val ) { 105 buf[pos++] = (byte)val; 106 } 107 108 public void appendLongInt( int val ) { 109 buf[pos++] = (byte) ((val >>> 24) & 0xFF); 110 buf[pos++] = (byte) ((val >>> 16) & 0xFF); 111 buf[pos++] = (byte) ((val >>> 8) & 0xFF); 112 buf[pos++] = (byte) (val & 0xFF); 113 } 114 115 123 public void appendBytes(MessageBytes mb) throws IOException { 124 if(mb==null || mb.isNull() ) { 125 appendInt( 0); 126 appendByte(0); 127 return; 128 } 129 130 ByteChunk bc= mb.getByteChunk(); 132 appendByteChunk(bc); 133 } 134 135 public void appendByteChunk(ByteChunk bc) throws IOException { 136 if(bc==null) { 137 log.error("appendByteChunk() null"); 138 appendInt( 0); 139 appendByte(0); 140 return; 141 } 142 143 byte[] bytes = bc.getBytes(); 144 int start=bc.getStart(); 145 appendInt( bc.getLength() ); 146 cpBytes(bytes, start, bc.getLength()); 147 appendByte(0); 148 } 149 150 161 public void appendBytes( byte b[], int off, int numBytes ) { 162 appendInt( numBytes ); 163 cpBytes( b, off, numBytes ); 164 appendByte(0); 165 } 166 167 private void cpBytes( byte b[], int off, int numBytes ) { 168 if( pos + numBytes >= buf.length ) { 169 log.error("Buffer overflow: buffer.len=" + buf.length + " pos=" + 170 pos + " data=" + numBytes ); 171 dump("Overflow/coBytes"); 172 log.error( "Overflow ", new Throwable ()); 173 return; 174 } 175 System.arraycopy( b, off, buf, pos, numBytes); 176 pos += numBytes; 177 } 179 180 181 182 184 190 public int getInt() { 191 int b1 = buf[pos++] & 0xFF; int b2 = buf[pos++] & 0xFF; 193 194 return (b1<<8) + b2; 195 } 196 197 public int peekInt() { 198 int b1 = buf[pos] & 0xFF; int b2 = buf[pos+1] & 0xFF; 200 201 return (b1<<8) + b2; 202 } 203 204 public byte getByte() { 205 byte res = buf[pos++]; 206 return res; 207 } 208 209 public byte peekByte() { 210 byte res = buf[pos]; 211 return res; 212 } 213 214 public void getBytes(MessageBytes mb) { 215 int length = getInt(); 216 if( (length == 0xFFFF) || (length == -1) ) { 217 mb.recycle(); 218 return; 219 } 220 mb.setBytes( buf, pos, length ); 221 pos += length; 222 pos++; } 224 225 232 public int getBytes(byte dest[]) { 233 int length = getInt(); 234 if( length > buf.length ) { 235 log.error("getBytes() buffer overflow " + length + " " + buf.length ); 237 } 238 239 if( (length == 0xFFFF) || (length == -1) ) { 240 log.info("Null string " + length); 241 return 0; 242 } 243 244 System.arraycopy( buf, pos, dest, 0, length ); 245 pos += length; 246 pos++; return length; 248 } 249 250 256 public int getLongInt() { 257 int b1 = buf[pos++] & 0xFF; b1 <<= 8; 259 b1 |= (buf[pos++] & 0xFF); 260 b1 <<= 8; 261 b1 |= (buf[pos++] & 0xFF); 262 b1 <<=8; 263 b1 |= (buf[pos++] & 0xFF); 264 return b1; 265 } 266 267 public int getHeaderLength() { 268 return 4; 269 } 270 271 public int processHeader() { 272 pos = 0; 273 int mark = getInt(); 274 len = getInt(); 275 276 if( mark != 0x1234 && mark != 0x4142 ) { 277 log.error("BAD packet signature " + mark); 279 dump( "In: " ); 280 return -1; 281 } 282 283 if( log.isDebugEnabled() ) 284 log.debug( "Received " + len + " " + buf[0] ); 285 return len; 286 } 287 288 public void dump(String msg) { 289 if( log.isDebugEnabled() ) 290 log.debug( msg + ": " + buf + " " + pos +"/" + (len + 4)); 291 int max=pos; 292 if( len + 4 > pos ) 293 max=len+4; 294 if( max >1000 ) max=1000; 295 if( log.isDebugEnabled() ) 296 for( int j=0; j < max; j+=16 ) 297 log.debug( hexLine( buf, j, len )); 298 299 } 300 301 302 304 public static String hexLine( byte buf[], int start, int len ) { 305 StringBuffer sb=new StringBuffer (); 306 for( int i=start; i< start+16 ; i++ ) { 307 if( i < len + 4) 308 sb.append( hex( buf[i] ) + " "); 309 else 310 sb.append( " " ); 311 } 312 sb.append(" | "); 313 for( int i=start; i < start+16 && i < len + 4; i++ ) { 314 if( ! Character.isISOControl( (char)buf[i] )) 315 sb.append( new Character ((char)buf[i]) ); 316 else 317 sb.append( "." ); 318 } 319 return sb.toString(); 320 } 321 322 private static String hex( int x ) { 323 String h=Integer.toHexString( x ); 325 if( h.length() == 1 ) h = "0" + h; 326 return h.substring( h.length() - 2 ); 327 } 328 329 } 330 | Popular Tags |