Java Code Examples for org.apache.tomcat.util.buf.MessageBytes#getCharChunk()

The following examples show how to use org.apache.tomcat.util.buf.MessageBytes#getCharChunk() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoyoteAdapter.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * 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);

}
 
Example 2
Source File: CoyoteAdapter.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);

}
 
Example 3
Source File: AjpMessage.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
    }
}
 
Example 4
Source File: CoyoteAdapter.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);

}
 
Example 5
Source File: AjpMessage.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
    }
}
 
Example 6
Source File: AbstractOutputBuffer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
    }

}
 
Example 7
Source File: Mapper.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);

}
 
Example 8
Source File: AbstractOutputBuffer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
    }

}
 
Example 9
Source File: Mapper.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * 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);

}
 
Example 10
Source File: CoyoteAdapter.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * 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;

}
 
Example 11
Source File: CoyoteAdapter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example 12
Source File: CoyoteAdapter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * 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;

}
 
Example 13
Source File: Mapper.java    From Tomcat8-Source-Read with MIT License 3 votes vote down vote up
/**
 * Map the specified URI relative to the context,
 * mutating the given mapping data.
 *
 * @param context The actual context
 * @param uri URI
 * @param mappingData This structure will contain the result of the mapping
 *                    operation
 * @throws IOException if the buffers are too small to hold the results of
 *                     the mapping.
 */
public void map(Context context, MessageBytes uri,
        MappingData mappingData) throws IOException {

    ContextVersion contextVersion =
            contextObjectToContextVersionMap.get(context);
    uri.toChars();
    CharChunk uricc = uri.getCharChunk();
    uricc.setLimit(-1);
    internalMapWrapper(contextVersion, uricc, mappingData);
}