1 16 17 package org.apache.ajp; 18 19 import java.io.UnsupportedEncodingException ; 20 21 import org.apache.tomcat.util.buf.MessageBytes; 22 import org.apache.tomcat.util.http.MimeHeaders; 23 24 25 40 public class Ajp13Packet { 41 42 private static org.apache.commons.logging.Log log= 43 org.apache.commons.logging.LogFactory.getLog( Ajp13Packet.class ); 44 45 public static final String DEFAULT_CHAR_ENCODING = "8859_1"; 46 public static final int AJP13_WS_HEADER = 0x1234; 47 public static final int AJP13_SW_HEADER = 0x4142; 49 52 String encoding = DEFAULT_CHAR_ENCODING; 53 54 57 byte buff[]; 58 59 62 int pos; 63 64 70 int len; 71 72 76 public Ajp13Packet( int size ) { 77 buff = new byte[size]; 78 } 79 80 84 public Ajp13Packet( byte b[] ) { 85 buff = b; 86 } 87 88 93 public void setEncoding(String encoding) { 94 this.encoding = encoding; 95 } 96 97 102 public String getEncoding() { 103 return encoding; 104 } 105 106 110 public byte[] getBuff() { 111 return buff; 112 } 113 114 121 public int getLen() { 122 return len; 123 } 124 125 129 public int getByteOff() { 130 return pos; 131 } 132 133 137 public void setByteOff(int c) { 138 pos=c; 139 } 140 141 149 public int checkIn() { 150 pos = 0; 151 int mark = getInt(); 152 len = getInt(); 153 154 if( mark != AJP13_WS_HEADER ) { 155 if (log.isDebugEnabled()) 156 log.debug("BAD packet " + mark); 157 dump( "In: " ); 158 return -1; 159 } 160 return len; 161 } 162 163 168 public void reset() { 169 len = 4; 170 pos = 4; 171 buff[0] = (byte)(AJP13_SW_HEADER >> 8); 172 buff[1] = (byte)(AJP13_SW_HEADER & 0xFF); 173 } 174 175 180 public void end() { 181 len = pos; 182 setInt( 2, len-4 ); 183 } 184 185 187 195 private void setInt( int bPos, int val ) { 196 buff[bPos] = (byte) ((val >>> 8) & 0xFF); 197 buff[bPos+1] = (byte) (val & 0xFF); 198 } 199 200 public void appendInt( int val ) { 201 setInt( pos, val ); 202 pos += 2; 203 } 204 205 public void appendByte( byte val ) { 206 buff[pos++] = val; 207 } 208 209 public void appendBool( boolean val) { 210 buff[pos++] = (byte) (val ? 1 : 0); 211 } 212 213 221 public void appendString(String str) throws UnsupportedEncodingException { 222 if(str == null) { 227 setInt( pos, 0); 228 buff[pos + 2] = 0; 229 pos += 3; 230 return; 231 } 232 233 byte[] bytes = str.getBytes(encoding); 238 appendBytes(bytes, 0, bytes.length); 239 240 262 } 263 264 275 public void appendBytes( byte b[], int off, int numBytes ) { 276 appendInt( numBytes ); 277 if( pos + numBytes >= buff.length ) { 278 if (log.isDebugEnabled()) 279 log.debug("Buffer overflow " + buff.length + " " + pos + " " + numBytes ); 280 } 281 System.arraycopy( b, off, buff, pos, numBytes); 282 buff[pos + numBytes] = 0; pos += numBytes + 1; 284 } 285 286 294 private void setLongInt( int bPos, int val ) { 295 buff[bPos] = (byte) ((val >>> 24) & 0xFF); 296 buff[bPos+1] = (byte) ((val >>> 16) & 0xFF); 297 buff[bPos+2] = (byte) ((val >>> 8) & 0xFF); 298 buff[bPos+3] = (byte) (val & 0xFF); 299 } 300 301 public void appendLongInt( int val ) { 302 setLongInt( pos, val ); 303 pos += 4; 304 } 305 306 315 public void appendXBytes(byte[] b, int off, int numBytes) { 316 if( pos + numBytes > buff.length ) { 317 if (log.isDebugEnabled()) 318 log.debug("appendXBytes - Buffer overflow " + buff.length + " " + pos + " " + numBytes ); 319 } 320 System.arraycopy( b, off, buff, pos, numBytes); 321 pos += numBytes; 322 } 323 324 325 327 333 public int getInt() { 334 int result = peekInt(); 335 pos += 2; 336 return result; 337 } 338 339 343 public int peekInt() { 344 int b1 = buff[pos] & 0xFF; int b2 = buff[pos + 1] & 0xFF; 346 347 return (b1<<8) + b2; 348 } 349 350 public byte getByte() { 351 byte res = buff[pos]; 352 pos++; 353 return res; 354 } 355 356 public byte peekByte() { 357 return buff[pos]; 358 } 359 360 public boolean getBool() { 361 return (getByte() == (byte) 1); 362 } 363 364 public void getMessageBytes(MessageBytes mb) { 365 int length = getInt(); 366 if( (length == 0xFFFF) || (length == -1) ) { 367 mb.setString( null ); 368 return; 369 } 370 mb.setBytes( buff, pos, length ); 371 pos += length; 372 pos++; } 374 375 public MessageBytes addHeader(MimeHeaders headers) { 376 int length = getInt(); 377 if( (length == 0xFFFF) || (length == -1) ) { 378 return null; 379 } 380 MessageBytes vMB=headers.addValue( buff, pos, length ); 381 pos += length; 382 pos++; 384 return vMB; 385 } 386 387 391 public String getString() throws java.io.UnsupportedEncodingException { 392 int length = getInt(); 393 if( (length == 0xFFFF) || (length == -1) ) { 394 if (log.isDebugEnabled()) 395 log.debug("null string " + length); 396 return null; 397 } 398 String s = new String (buff, pos, length, encoding); 399 400 pos += length; 401 pos++; return s; 403 } 404 405 412 public int getBytes(byte dest[]) { 413 int length = getInt(); 414 if( length > buff.length ) { 415 if (log.isDebugEnabled()) 417 log.debug("XXX Assert failed, buff too small "); 418 } 419 420 if( (length == 0xFFFF) || (length == -1) ) { 421 if (log.isDebugEnabled()) 422 log.debug("null string " + length); 423 return 0; 424 } 425 426 System.arraycopy( buff, pos, dest, 0, length ); 427 pos += length; 428 pos++; return length; 430 } 431 432 438 public int getLongInt() { 439 int result = peekLongInt(); 440 pos += 4; 441 return result; 442 } 443 444 451 public int getXBytes(byte[] dest, int length) { 452 if( length > buff.length ) { 453 if (log.isDebugEnabled()) 455 log.debug("XXX Assert failed, buff too small "); 456 } 457 458 System.arraycopy( buff, pos, dest, 0, length ); 459 pos += length; 460 return length; 461 } 462 463 467 public int peekLongInt() { 468 int b1 = buff[pos] & 0xFF; b1 <<= 8; 470 b1 |= (buff[pos + 1] & 0xFF); 471 b1 <<= 8; 472 b1 |= (buff[pos + 2] & 0xFF); 473 b1 <<=8; 474 b1 |= (buff[pos + 3] & 0xFF); 475 return b1; 476 } 477 478 private String hex( int x ) { 480 String h=Integer.toHexString( x ); 482 if( h.length() == 1 ) h = "0" + h; 483 return h.substring( h.length() - 2 ); 484 } 485 486 private void hexLine( int start ) { 487 int pkgEnd = len + 4; 488 if( pkgEnd > buff.length ) 489 pkgEnd = buff.length; 490 for( int i=start; i< start+16 ; i++ ) { 491 if( i < pkgEnd) { 492 if (log.isDebugEnabled()) 493 log.debug( hex( buff[i] ) + " "); 494 } else { 495 if (log.isDebugEnabled()) 496 log.debug( " " ); 497 } 498 } 499 if (log.isDebugEnabled()) 500 log.debug(" | "); 501 for( int i=start; i < start+16 && i < pkgEnd; i++ ) { 502 if( Character.isLetterOrDigit( (char)buff[i] )) { 503 if (log.isDebugEnabled()) 504 log.debug( new Character ((char)buff[i]) ); 505 } else { 506 if (log.isDebugEnabled()) 507 log.debug( "." ); 508 } 509 } 510 } 511 512 public void dump(String msg) { 513 if (log.isDebugEnabled()) 514 log.debug( msg + ": " + buff + " " + pos +"/" + (len + 4)); 515 516 for( int j=0; j < len + 4; j+=16 ) 517 hexLine( j ); 518 519 } 520 } 521 | Popular Tags |