Search in sources :

Example 11 with CharChunk

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

the class CoyoteAdapter method checkNormalize.

/**
 * Check that the URI is normalized following character decoding.
 * <p>
 * This method checks for "\", 0, "//", "/./" and "/../". This method will
 * return false if sequences that are supposed to be normalized are still
 * present in the URI.
 *
 * @param uriMB URI to be checked (should be chars)
 */
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 12 with CharChunk

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

the class CoyoteAdapter method convertURI.

/**
 * Character conversion of the URI.
 */
protected void convertURI(MessageBytes uri, Request request) throws Exception {
    ByteChunk bc = uri.getByteChunk();
    int length = bc.getLength();
    CharChunk cc = uri.getCharChunk();
    cc.allocate(length, -1);
    String enc = connector.getURIEncoding();
    if (enc != null) {
        B2CConverter conv = request.getURIConverter();
        try {
            if (conv == null) {
                conv = new B2CConverter(enc, true);
                request.setURIConverter(conv);
            } else {
                conv.recycle();
            }
        } catch (IOException e) {
            log.error("Invalid URI encoding; using HTTP default");
            connector.setURIEncoding(null);
        }
        if (conv != null) {
            try {
                conv.convert(bc, cc, true);
                uri.setChars(cc.getBuffer(), cc.getStart(), cc.getLength());
                return;
            } catch (IOException ioe) {
                // Should never happen as B2CConverter should replace
                // problematic characters
                request.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
    }
    // Default encoding: fast conversion for ISO-8859-1
    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);
    }
    uri.setChars(cbuf, 0, length);
}
Also used : B2CConverter(org.apache.tomcat.util.buf.B2CConverter) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) IOException(java.io.IOException) CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 13 with CharChunk

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

the class AbstractOutputBuffer method write.

/**
 * This method will write the contents of the specified message bytes
 * buffer to the output stream, without filtering. This method is meant to
 * be used to write the response header.
 *
 * @param mb data to be written
 */
protected void write(MessageBytes mb) {
    if (mb.getType() == MessageBytes.T_BYTES) {
        ByteChunk bc = mb.getByteChunk();
        write(bc);
    } else if (mb.getType() == MessageBytes.T_CHARS) {
        CharChunk cc = mb.getCharChunk();
        write(cc);
    } else {
        write(mb.toString());
    }
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 14 with CharChunk

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

the class Mapper method map.

/**
 * Map the specified URI relative to the context,
 * mutating the given mapping data.
 *
 * @param uri URI
 * @param mappingData This structure will contain the result of the mapping
 *                    operation
 */
public void map(MessageBytes uri, MappingData mappingData) throws Exception {
    uri.toChars();
    CharChunk uricc = uri.getCharChunk();
    uricc.setLimit(-1);
    internalMapWrapper(context, uricc, mappingData);
}
Also used : CharChunk(org.apache.tomcat.util.buf.CharChunk)

Example 15 with CharChunk

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

the class CoyoteAdapter method convertMB.

/**
 * Character conversion of the a US-ASCII MessageBytes.
 *
 * @param mb The MessageBytes instance containing the bytes that should be converted to chars
 */
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)

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