Search in sources :

Example 1 with CharChunk

use of org.apache.tomcat.util.buf.CharChunk in project tomcat by apache.

the class CoyoteAdapter method checkNormalize.

/**
     * Check that the URI is normalized following character decoding. This
     * method checks for "\", 0, "//", "/./" and "/../".
     *
     * @param uriMB URI to be checked (should be chars)
     *
     * @return <code>false</code> if sequences that are supposed to be
     *         normalized are still present in the URI, otherwise
     *         <code>true</code>
     */
public static boolean checkNormalize(MessageBytes uriMB) {
    CharChunk uriCC = uriMB.getCharChunk();
    char[] c = uriCC.getChars();
    int start = uriCC.getStart();
    int end = uriCC.getEnd();
    int pos = 0;
    // Check for '\' and 0
    for (pos = start; pos < end; pos++) {
        if (c[pos] == '\\') {
            return false;
        }
        if (c[pos] == 0) {
            return false;
        }
    }
    // Check for "//"
    for (pos = start; pos < (end - 1); pos++) {
        if (c[pos] == '/') {
            if (c[pos + 1] == '/') {
                return false;
            }
        }
    }
    // Check for ending with "/." or "/.."
    if (((end - start) >= 2) && (c[end - 1] == '.')) {
        if ((c[end - 2] == '/') || ((c[end - 2] == '.') && (c[end - 3] == '/'))) {
            return false;
        }
    }
    // Check for "/./"
    if (uriCC.indexOf("/./", 0, 3, 0) >= 0) {
        return false;
    }
    // Check for "/../"
    if (uriCC.indexOf("/../", 0, 4, 0) >= 0) {
        return false;
    }
    return true;
}
Also used : CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 2 with CharChunk

use of org.apache.tomcat.util.buf.CharChunk in project tomcat70 by apache.

the class AjpMessage method appendBytes.

/**
 * Write a MessageBytes out at the current write position.
 * A null MessageBytes is encoded as a string with length 0.
 */
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) {
        ByteChunk bc = mb.getByteChunk();
        appendByteChunk(bc);
    } else if (mb.getType() == MessageBytes.T_CHARS) {
        CharChunk cc = mb.getCharChunk();
        appendCharChunk(cc);
    } else {
        appendString(mb.toString());
    }
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 3 with CharChunk

use of org.apache.tomcat.util.buf.CharChunk in project tomcat70 by apache.

the class CoyoteAdapter method convertMB.

/**
 * Character conversion of the a US-ASCII MessageBytes.
 */
protected void convertMB(MessageBytes mb) {
    // This is of course only meaningful for bytes
    if (mb.getType() != MessageBytes.T_BYTES) {
        return;
    }
    ByteChunk bc = mb.getByteChunk();
    CharChunk cc = mb.getCharChunk();
    int length = bc.getLength();
    cc.allocate(length, -1);
    // Default encoding: fast conversion
    byte[] bbuf = bc.getBuffer();
    char[] cbuf = cc.getBuffer();
    int start = bc.getStart();
    for (int i = 0; i < length; i++) {
        cbuf[i] = (char) (bbuf[i + start] & 0xff);
    }
    mb.setChars(cbuf, 0, length);
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 4 with CharChunk

use of org.apache.tomcat.util.buf.CharChunk in project tomcat70 by apache.

the class InputBuffer method recycle.

// --------------------------------------------------------- Public Methods
/**
 * Recycle the output buffer.
 */
public void recycle() {
    state = INITIAL_STATE;
    // If usage of mark made the buffer too big, reallocate it
    if (cb.getChars().length > size) {
        cb = new CharChunk(size);
        cb.setLimit(size);
        cb.setOptimizedWrite(false);
        cb.setCharInputChannel(this);
        cb.setCharOutputChannel(this);
    } else {
        cb.recycle();
    }
    markPos = -1;
    bb.recycle();
    closed = false;
    if (conv != null) {
        conv.recycle();
    }
    gotEnc = false;
    enc = null;
}
Also used : CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 5 with CharChunk

use of org.apache.tomcat.util.buf.CharChunk in project tomcat70 by apache.

the class TesterBase64Performance method testDecode.

@SuppressWarnings("deprecation")
@Test
public void testDecode() throws Exception {
    List<ByteChunk> inputs = new ArrayList<ByteChunk>(SIZE);
    for (int i = 0; i < SIZE; i++) {
        String decodedString = "abc" + Integer.valueOf(i) + ":abc" + Integer.valueOf(i);
        byte[] decodedBytes = decodedString.getBytes(B2CConverter.ISO_8859_1);
        String encodedString = DatatypeConverter.printBase64Binary(decodedBytes);
        byte[] encodedBytes = encodedString.getBytes(B2CConverter.ISO_8859_1);
        ByteChunk bc = new ByteChunk(encodedBytes.length);
        bc.append(encodedBytes, 0, encodedBytes.length);
        inputs.add(bc);
    }
    long startTomcat = System.currentTimeMillis();
    for (ByteChunk bc : inputs) {
        CharChunk cc = new CharChunk(bc.getLength());
        Base64.decode(bc, cc);
    }
    long stopTomcat = System.currentTimeMillis();
    System.out.println("Tomcat: " + (stopTomcat - startTomcat) + " ms");
    long startCodec = System.currentTimeMillis();
    for (ByteChunk bc : inputs) {
        org.apache.tomcat.util.codec.binary.Base64.decodeBase64(bc.getBuffer(), bc.getOffset(), bc.getLength());
    }
    long stopCodec = System.currentTimeMillis();
    System.out.println("Codec: " + (stopCodec - startCodec) + " ms");
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) CharChunk(org.apache.tomcat.util.buf.CharChunk) Test(org.junit.Test)

Aggregations

CharChunk (org.apache.tomcat.util.buf.CharChunk)17 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)7 IOException (java.io.IOException)5 MessageBytes (org.apache.tomcat.util.buf.MessageBytes)5 Wrapper (org.apache.catalina.Wrapper)3 ServletException (jakarta.servlet.ServletException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2 Charset (java.nio.charset.Charset)2 NamingException (javax.naming.NamingException)2 B2CConverter (org.apache.tomcat.util.buf.B2CConverter)2 Cookie (jakarta.servlet.http.Cookie)1 HttpServletMapping (jakarta.servlet.http.HttpServletMapping)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 ServletException (javax.servlet.ServletException)1 Context (org.apache.catalina.Context)1 LifecycleException (org.apache.catalina.LifecycleException)1 Pipeline (org.apache.catalina.Pipeline)1