1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote.ajp;
import java.nio.ByteBuffer;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.HexUtils;
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 container. Designed to be reused many times with no
* creation of garbage. Understands the format of data types for these packets. Can be used (somewhat confusingly) for
* both incoming and outgoing packets.
*
* @author Henri Gomez
* @author Dan Milstein
* @author Keith Wannamaker
* @author Kevin Seguin
* @author Costin Manolache
*/
public class AjpMessage {
private static final Log log = LogFactory.getLog(AjpMessage.class);
/**
* The string manager for this package.
*/
protected static final StringManager sm = StringManager.getManager(AjpMessage.class);
// ------------------------------------------------------------ Constructor
public AjpMessage(int packetSize) {
buf = new byte[packetSize];
}
// ----------------------------------------------------- Instance Variables
/**
* Fixed size buffer.
*/
protected final byte[] buf;
/**
* The current read or write position in the buffer.
*/
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.
*/
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 (but leave the length unwritten, because it is as yet unknown).
*/
public void reset() {
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 the header.
*/
public void end() {
len = pos;
int dLen = len - 4;
buf[0] = (byte) 0x41;
buf[1] = (byte) 0x42;
buf[2] = (byte) ((dLen >>> 8) & 0xFF);
buf[3] = (byte) (dLen & 0xFF);
}
/**
* Return the underlying byte buffer.
*
* @return The buffer
*/
public byte[] getBuffer() {
return buf;
}
/**
* Return the current message length.
*
* @return 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).
*/
public int getLen() {
return len;
}
/**
* Add a short integer (2 bytes) to the message.
*
* @param val The integer to append
*/
public void appendInt(int val) {
buf[pos++] = (byte) ((val >>> 8) & 0xFF);
buf[pos++] = (byte) (val & 0xFF);
}
/**
* Append a byte (1 byte) to the message.
*
* @param val The byte value to append
*/
public void appendByte(int val) {
buf[pos++] = (byte) val;
}
/**
* Write a MessageBytes out at the current write position. A null MessageBytes is encoded as a string with length 0.
*
* @param mb The data to write
*/
public void appendBytes(MessageBytes mb) {
if (mb == null) {
log.error(sm.getString("ajpmessage.null"), new NullPointerException());
appendInt(0);
appendByte(0);
return;
}
if (mb.getType() != MessageBytes.T_BYTES) {
mb.toBytes();
ByteChunk bc = mb.getByteChunk();
// Need to filter out CTLs excluding TAB. ISO-8859-1 and UTF-8
// values will be OK. Strings using other encodings may be
// corrupted.
byte[] buffer = bc.getBuffer();
for (int i = bc.getStart(); i < bc.getLength(); i++) {
// byte values are signed i.e. -128 to 127
// The values are used unsigned. 0 to 31 are CTLs so they are
// filtered (apart from TAB which is 9). 127 is a control (DEL).
// The values 128 to 255 are all OK. Converting those to signed
// gives -128 to -1.
if ((buffer[i] > -1 && buffer[i] <= 31 && buffer[i] != 9) || buffer[i] == 127) {
buffer[i] = ' ';
}
}
}
appendByteChunk(mb.getByteChunk());
}
/**
* Write a ByteChunk out at the current write position. A null ByteChunk is encoded as a string with length 0.
*
* @param bc The data to write
*/
public void appendByteChunk(ByteChunk bc) {
if (bc == null) {
log.error(sm.getString("ajpmessage.null"), new NullPointerException());
appendInt(0);
appendByte(0);
return;
}
appendBytes(bc.getBytes(), bc.getStart(), bc.getLength());
}
/**
* Copy a chunk of bytes into the packet, starting at the current write position. The chunk of bytes is encoded with
* the length in two bytes first, then the data itself, and finally a terminating \0 (which is <B>not</B> included
* in the encoded length).
*
* @param b The array from which to copy bytes.
* @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) {
if (checkOverflow(numBytes)) {
return;
}
appendInt(numBytes);
System.arraycopy(b, off, buf, pos, numBytes);
pos += numBytes;
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 in two bytes first, then the data itself, and finally a terminating \0 (which is <B>not</B> included
* in the encoded length).
*
* @param b The ByteBuffer from which to copy bytes.
*/
public void appendBytes(ByteBuffer b) {
int numBytes = b.remaining();
if (checkOverflow(numBytes)) {
return;
}
appendInt(numBytes);
b.get(buf, pos, numBytes);
pos += numBytes;
appendByte(0);
}
private boolean checkOverflow(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 true;
}
return false;
}
/**
* Read an integer from packet, and advance the read position past it. Integers are encoded as two unsigned bytes
* with the high-order byte first, and, as far as I can tell, in little-endian order within each byte.
*
* @return The integer value read from the message
*/
public int getInt() {
int b1 = buf[pos++] & 0xFF;
int b2 = buf[pos++] & 0xFF;
validatePos(pos);
return (b1 << 8) + b2;
}
public int peekInt() {
validatePos(pos + 2);
int b1 = buf[pos] & 0xFF;
int b2 = buf[pos + 1] & 0xFF;
return (b1 << 8) + b2;
}
public byte getByte() {
byte res = buf[pos++];
validatePos(pos);
return res;
}
public void getBytes(MessageBytes mb) {
doGetBytes(mb, true);
}
public void getBodyBytes(MessageBytes mb) {
doGetBytes(mb, false);
}
private void doGetBytes(MessageBytes mb, boolean terminated) {
int length = getInt();
if (length == 0xFFFF || length == -1) {
mb.recycle();
return;
}
if (terminated) {
validatePos(pos + length + 1);
} else {
validatePos(pos + length);
}
mb.setBytes(buf, pos, length);
mb.getCharChunk().recycle(); // not valid anymore
pos += length;
if (terminated) {
pos++; // Skip the terminating \0
}
}
/**
* Read a 32 bits integer from packet, and advance the read position past it. Integers are encoded as four unsigned
* bytes with the high-order byte first, and, as far as I can tell, in little-endian order within each byte.
*
* @return The long value read from the message
*/
public int getLongInt() {
int b1 = buf[pos++] & 0xFF; // No swap, Java order
b1 <<= 8;
b1 |= (buf[pos++] & 0xFF);
b1 <<= 8;
b1 |= (buf[pos++] & 0xFF);
b1 <<= 8;
b1 |= (buf[pos++] & 0xFF);
validatePos(pos);
return b1;
}
public int processHeader(boolean toContainer) {
pos = 0;
int mark = getInt();
len = getInt();
// Verify message signature
if ((toContainer && mark != 0x1234) || (!toContainer && mark != 0x4142)) {
log.error(sm.getString("ajpmessage.invalid", "" + mark));
if (log.isTraceEnabled()) {
dump("In");
}
return -1;
}
if (log.isTraceEnabled()) {
log.trace("Received " + len + " " + buf[0]);
}
return len;
}
private void dump(String prefix) {
if (log.isTraceEnabled()) {
log.trace(prefix + ": " + HexUtils.toHexString(buf) + " " + pos + "/" + (len + 4));
}
int max = pos;
if (len + 4 > pos) {
max = len + 4;
}
if (max > 1000) {
max = 1000;
}
if (log.isTraceEnabled()) {
for (int j = 0; j < max; j += 16) {
log.trace(hexLine(buf, j, len));
}
}
}
private void validatePos(int posToTest) {
if (posToTest > len + 4) {
// Trying to read data beyond the end of the AJP message
throw new ArrayIndexOutOfBoundsException(sm.getString("ajpMessage.invalidPos", Integer.valueOf(posToTest)));
}
}
// ------------------------------------------------------ Protected Methods
protected static String hexLine(byte[] buf, int start, int len) {
StringBuilder sb = new StringBuilder();
for (int i = start; i < start + 16; i++) {
if (i < len + 4) {
sb.append(hex(buf[i])).append(' ');
} else {
sb.append(" ");
}
}
sb.append(" | ");
for (int i = start; i < start + 16 && i < len + 4; i++) {
if (!Character.isISOControl((char) buf[i])) {
sb.append(Character.valueOf((char) buf[i]));
} else {
sb.append('.');
}
}
return sb.toString();
}
protected static String hex(int x) {
String h = Integer.toHexString(x);
if (h.length() == 1) {
h = "0" + h;
}
return h.substring(h.length() - 2);
}
}
|