001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jk.common;
019:
020: import java.io.IOException;
021:
022: import org.apache.jk.core.Msg;
023: import org.apache.tomcat.util.buf.ByteChunk;
024: import org.apache.tomcat.util.buf.MessageBytes;
025:
026: /**
027: * A single packet for communication between the web server and the
028: * container. Designed to be reused many times with no creation of
029: * garbage. Understands the format of data types for these packets.
030: * Can be used (somewhat confusingly) for both incoming and outgoing
031: * packets.
032: *
033: * See Ajp14/Ajp13Packet.java.
034: *
035: * @author Henri Gomez [hgomez@apache.org]
036: * @author Dan Milstein [danmil@shore.net]
037: * @author Keith Wannamaker [Keith@Wannamaker.org]
038: * @author Kevin Seguin
039: * @author Costin Manolache
040: */
041: public class MsgAjp extends Msg {
042: private static org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory
043: .getLog(MsgAjp.class);
044:
045: // that's the original buffer size in ajp13 - otherwise we'll get interoperability problems.
046: private byte buf[];
047: // The current read or write position in the buffer
048: private int pos;
049: /**
050: * This actually means different things depending on whether the
051: * packet is read or write. For read, it's the length of the
052: * payload (excluding the header). For write, it's the length of
053: * the packet as a whole (counting the header). Oh, well.
054: */
055: private int len;
056:
057: /**
058: * The maximum packet size
059: */
060: private int bufsize;
061:
062: /**
063: * Constructor that takes a buffer size
064: */
065: public MsgAjp(int bsize) {
066: if (bsize < 8 * 1024) {
067: bsize = 8 * 1024;
068: }
069: bufsize = bsize;
070: buf = new byte[bsize];
071:
072: }
073:
074: /**
075: * No arg constructor.
076: * @deprecated Use the buffer size constructor.
077: */
078: public MsgAjp() {
079: this (8 * 1024);
080: }
081:
082: /**
083: * Prepare this packet for accumulating a message from the container to
084: * the web server. Set the write position to just after the header
085: * (but leave the length unwritten, because it is as yet unknown).
086: */
087: public void reset() {
088: len = 4;
089: pos = 4;
090: }
091:
092: /**
093: * For a packet to be sent to the web server, finish the process of
094: * accumulating data and write the length of the data payload into
095: * the header.
096: */
097: public void end() {
098: len = pos;
099: int dLen = len - 4;
100:
101: buf[0] = (byte) 0x41;
102: buf[1] = (byte) 0x42;
103: buf[2] = (byte) ((dLen >>> 8) & 0xFF);
104: buf[3] = (byte) (dLen & 0xFF);
105: }
106:
107: public byte[] getBuffer() {
108: return buf;
109: }
110:
111: public int getLen() {
112: return len;
113: }
114:
115: // ============ Data Writing Methods ===================
116:
117: /**
118: * Add an int.
119: *
120: * @param val The integer to write.
121: */
122: public void appendInt(int val) {
123: buf[pos++] = (byte) ((val >>> 8) & 0xFF);
124: buf[pos++] = (byte) (val & 0xFF);
125: }
126:
127: public void appendByte(int val) {
128: buf[pos++] = (byte) val;
129: }
130:
131: public void appendLongInt(int val) {
132: buf[pos++] = (byte) ((val >>> 24) & 0xFF);
133: buf[pos++] = (byte) ((val >>> 16) & 0xFF);
134: buf[pos++] = (byte) ((val >>> 8) & 0xFF);
135: buf[pos++] = (byte) (val & 0xFF);
136: }
137:
138: /**
139: * Write a String out at the current write position. Strings are
140: * encoded with the length in two bytes first, then the string, and
141: * then a terminating \0 (which is <B>not</B> included in the
142: * encoded length). The terminator is for the convenience of the C
143: * code, where it saves a round of copying. A null string is
144: * encoded as a string with length 0.
145: */
146: public void appendBytes(MessageBytes mb) throws IOException {
147: if (mb == null || mb.isNull()) {
148: appendInt(0);
149: appendByte(0);
150: return;
151: }
152:
153: // XXX Convert !!
154: ByteChunk bc = mb.getByteChunk();
155: appendByteChunk(bc);
156: }
157:
158: public void appendByteChunk(ByteChunk bc) throws IOException {
159: if (bc == null) {
160: log.error("appendByteChunk() null");
161: appendInt(0);
162: appendByte(0);
163: return;
164: }
165:
166: byte[] bytes = bc.getBytes();
167: int start = bc.getStart();
168: int length = bc.getLength();
169: appendInt(length);
170: cpBytes(bytes, start, length);
171: appendByte(0);
172: }
173:
174: /**
175: * Copy a chunk of bytes into the packet, starting at the current
176: * write position. The chunk of bytes is encoded with the length
177: * in two bytes first, then the data itself, and finally a
178: * terminating \0 (which is <B>not</B> included in the encoded
179: * length).
180: *
181: * @param b The array from which to copy bytes.
182: * @param off The offset into the array at which to start copying
183: * @param numBytes The number of bytes to copy.
184: */
185: public void appendBytes(byte b[], int off, int numBytes) {
186: appendInt(numBytes);
187: cpBytes(b, off, numBytes);
188: appendByte(0);
189: }
190:
191: private void cpBytes(byte b[], int off, int numBytes) {
192: if (pos + numBytes >= buf.length) {
193: log.error("Buffer overflow: buffer.len=" + buf.length
194: + " pos=" + pos + " data=" + numBytes);
195: dump("Overflow/coBytes");
196: log.error("Overflow ", new Throwable());
197: return;
198: }
199: System.arraycopy(b, off, buf, pos, numBytes);
200: pos += numBytes;
201: // buf[pos + numBytes] = 0; // Terminating \0
202: }
203:
204: // ============ Data Reading Methods ===================
205:
206: /**
207: * Read an integer from packet, and advance the read position past
208: * it. Integers are encoded as two unsigned bytes with the
209: * high-order byte first, and, as far as I can tell, in
210: * little-endian order within each byte.
211: */
212: public int getInt() {
213: int b1 = buf[pos++] & 0xFF; // No swap, Java order
214: int b2 = buf[pos++] & 0xFF;
215:
216: return (b1 << 8) + b2;
217: }
218:
219: public int peekInt() {
220: int b1 = buf[pos] & 0xFF; // No swap, Java order
221: int b2 = buf[pos + 1] & 0xFF;
222:
223: return (b1 << 8) + b2;
224: }
225:
226: public byte getByte() {
227: byte res = buf[pos++];
228: return res;
229: }
230:
231: public byte peekByte() {
232: byte res = buf[pos];
233: return res;
234: }
235:
236: public void getBytes(MessageBytes mb) {
237: int length = getInt();
238: if ((length == 0xFFFF) || (length == -1)) {
239: mb.recycle();
240: return;
241: }
242: mb.setBytes(buf, pos, length);
243: mb.getCharChunk().recycle();
244: pos += length;
245: pos++; // Skip the terminating \0
246: }
247:
248: /**
249: * Copy a chunk of bytes from the packet into an array and advance
250: * the read position past the chunk. See appendBytes() for details
251: * on the encoding.
252: *
253: * @return The number of bytes copied.
254: */
255: public int getBytes(byte dest[]) {
256: int length = getInt();
257: if (length > buf.length) {
258: // XXX Should be if(pos + length > buff.legth)?
259: log.error("getBytes() buffer overflow " + length + " "
260: + buf.length);
261: }
262:
263: if ((length == 0xFFFF) || (length == -1)) {
264: log.info("Null string " + length);
265: return 0;
266: }
267:
268: System.arraycopy(buf, pos, dest, 0, length);
269: pos += length;
270: pos++; // Skip terminating \0 XXX I believe this is wrong but harmless
271: return length;
272: }
273:
274: /**
275: * Read a 32 bits integer from packet, and advance the read position past
276: * it. Integers are encoded as four unsigned bytes with the
277: * high-order byte first, and, as far as I can tell, in
278: * little-endian order within each byte.
279: */
280: public int getLongInt() {
281: int b1 = buf[pos++] & 0xFF; // No swap, Java order
282: b1 <<= 8;
283: b1 |= (buf[pos++] & 0xFF);
284: b1 <<= 8;
285: b1 |= (buf[pos++] & 0xFF);
286: b1 <<= 8;
287: b1 |= (buf[pos++] & 0xFF);
288: return b1;
289: }
290:
291: public int getHeaderLength() {
292: return 4;
293: }
294:
295: public int processHeader() {
296: pos = 0;
297: int mark = getInt();
298: len = getInt();
299:
300: if (mark != 0x1234 && mark != 0x4142) {
301: // XXX Logging
302: log.error("BAD packet signature " + mark);
303: dump("In: ");
304: return -1;
305: }
306:
307: if (log.isDebugEnabled())
308: log.debug("Received " + len + " " + buf[0]);
309: return len;
310: }
311:
312: public void dump(String msg) {
313: if (log.isDebugEnabled())
314: log.debug(msg + ": " + buf + " " + pos + "/" + (len + 4));
315: int max = pos;
316: if (len + 4 > pos)
317: max = len + 4;
318: if (max > 1000)
319: max = 1000;
320: if (log.isDebugEnabled())
321: for (int j = 0; j < max; j += 16)
322: log.debug(hexLine(buf, j, len));
323:
324: }
325:
326: /* -------------------- Utilities -------------------- */
327: // XXX Move to util package
328: public static String hexLine(byte buf[], int start, int len) {
329: StringBuffer sb = new StringBuffer();
330: for (int i = start; i < start + 16; i++) {
331: if (i < len + 4)
332: sb.append(hex(buf[i]) + " ");
333: else
334: sb.append(" ");
335: }
336: sb.append(" | ");
337: for (int i = start; i < start + 16 && i < len + 4; i++) {
338: if (!Character.isISOControl((char) buf[i]))
339: sb.append(new Character((char) buf[i]));
340: else
341: sb.append(".");
342: }
343: return sb.toString();
344: }
345:
346: private static String hex(int x) {
347: // if( x < 0) x=256 + x;
348: String h = Integer.toHexString(x);
349: if (h.length() == 1)
350: h = "0" + h;
351: return h.substring(h.length() - 2);
352: }
353:
354: }
|