Java Code Examples for org.apache.tomcat.util.buf.ByteChunk#getEnd()

The following examples show how to use org.apache.tomcat.util.buf.ByteChunk#getEnd() . 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: Cookies.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Unescapes any double quotes in the given cookie value.
 *
 * @param bc The cookie value to modify
 */
private static void unescapeDoubleQuotes(ByteChunk bc) {

    if (bc == null || bc.getLength() == 0 || bc.indexOf('"', 0) == -1) {
        return;
    }

    int src = bc.getStart();
    int end = bc.getEnd();
    int dest = src;
    byte[] buffer = bc.getBuffer();

    while (src < end) {
        if (buffer[src] == '\\' && src < end && buffer[src+1]  == '"') {
            src++;
        }
        buffer[dest] = buffer[src];
        dest ++;
        src ++;
    }
    bc.setEnd(dest);
}
 
Example 2
Source File: Cookies.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Unescapes any double quotes in the given cookie value.
 *
 * @param bc The cookie value to modify
 */
private static void unescapeDoubleQuotes(ByteChunk bc) {

    if (bc == null || bc.getLength() == 0 || bc.indexOf('"', 0) == -1) {
        return;
    }

    int src = bc.getStart();
    int end = bc.getEnd();
    int dest = src;
    byte[] buffer = bc.getBuffer();

    while (src < end) {
        if (buffer[src] == '\\' && src < end && buffer[src+1]  == '"') {
            src++;
        }
        buffer[dest] = buffer[src];
        dest ++;
        src ++;
    }
    bc.setEnd(dest);
}
 
Example 3
Source File: Tomcat90OutputBuffer.java    From armeria with Apache License 2.0 6 votes vote down vote up
public int doWrite(ByteChunk chunk) {
    final int start = chunk.getStart();
    final int end = chunk.getEnd();
    final int length = end - start;
    if (length == 0) {
        return 0;
    }

    // NB: We make a copy because Tomcat reuses the underlying byte array of 'chunk'.
    final byte[] content = Arrays.copyOfRange(chunk.getBuffer(), start, end);

    data.add(HttpData.wrap(content));

    bytesWritten += length;
    return length;
}
 
Example 4
Source File: AbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Specialized utility method: find a sequence of lower case bytes inside
 * a ByteChunk.
 */
protected int findBytes(ByteChunk bc, byte[] b) {

    byte first = b[0];
    byte[] buff = bc.getBuffer();
    int start = bc.getStart();
    int end = bc.getEnd();

    // Look for first char
    int srcEnd = b.length;

    for (int i = start; i <= (end - srcEnd); i++) {
        if (Ascii.toLower(buff[i]) != first) {
            continue;
        }
        // found first char, now look for a match
        int myPos = i+1;
        for (int srcPos = 1; srcPos < srcEnd;) {
            if (Ascii.toLower(buff[myPos++]) != b[srcPos++]) {
                break;
            }
            if (srcPos == srcEnd) {
                return i - start; // found it
            }
        }
    }
    return -1;
}
 
Example 5
Source File: AbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Specialized utility method: find a sequence of lower case bytes inside
 * a ByteChunk.
 */
protected int findBytes(ByteChunk bc, byte[] b) {

    byte first = b[0];
    byte[] buff = bc.getBuffer();
    int start = bc.getStart();
    int end = bc.getEnd();

    // Look for first char
    int srcEnd = b.length;

    for (int i = start; i <= (end - srcEnd); i++) {
        if (Ascii.toLower(buff[i]) != first) {
            continue;
        }
        // found first char, now look for a match
        int myPos = i+1;
        for (int srcPos = 1; srcPos < srcEnd;) {
            if (Ascii.toLower(buff[myPos++]) != b[srcPos++]) {
                break;
            }
            if (srcPos == srcEnd) {
                return i - start; // found it
            }
        }
    }
    return -1;
}
 
Example 6
Source File: Host.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public MessageBytesReader(MessageBytes mb) {
    ByteChunk bc = mb.getByteChunk();
    bytes = bc.getBytes();
    pos = bc.getOffset();
    end = bc.getEnd();
}
 
Example 7
Source File: CoyoteAdapter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Extract the path parameters from the request. This assumes parameters are
 * of the form /path;name=value;name2=value2/ etc. Currently only really
 * interested in the session ID that will be in this form. Other parameters
 * can safely be ignored.
 *
 * @param req
 * @param request
 */
protected void parsePathParameters(org.apache.coyote.Request req,
        Request request) {

    // Process in bytes (this is default format so this is normally a NO-OP
    req.decodedURI().toBytes();

    ByteChunk uriBC = req.decodedURI().getByteChunk();
    int semicolon = uriBC.indexOf(';', 0);

    // What encoding to use? Some platforms, eg z/os, use a default
    // encoding that doesn't give the expected result so be explicit
    String enc = connector.getURIEncoding();
    if (enc == null) {
        enc = "ISO-8859-1";
    }
    Charset charset = null;
    try {
        charset = B2CConverter.getCharset(enc);
    } catch (UnsupportedEncodingException e1) {
        log.warn(sm.getString("coyoteAdapter.parsePathParam",
                enc));
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("coyoteAdapter.debug", "uriBC",
                uriBC.toString()));
        log.debug(sm.getString("coyoteAdapter.debug", "semicolon",
                String.valueOf(semicolon)));
        log.debug(sm.getString("coyoteAdapter.debug", "enc", enc));
    }

    while (semicolon > -1) {
        // Parse path param, and extract it from the decoded request URI
        int start = uriBC.getStart();
        int end = uriBC.getEnd();

        int pathParamStart = semicolon + 1;
        int pathParamEnd = ByteChunk.findBytes(uriBC.getBuffer(),
                start + pathParamStart, end,
                new byte[] {';', '/'});

        String pv = null;

        if (pathParamEnd >= 0) {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            pathParamEnd - pathParamStart, charset);
            }
            // Extract path param from decoded request URI
            byte[] buf = uriBC.getBuffer();
            for (int i = 0; i < end - start - pathParamEnd; i++) {
                buf[start + semicolon + i]
                    = buf[start + i + pathParamEnd];
            }
            uriBC.setBytes(buf, start,
                    end - start - pathParamEnd + semicolon);
        } else {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            (end - start) - pathParamStart, charset);
            }
            uriBC.setEnd(start + semicolon);
        }

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamStart",
                    String.valueOf(pathParamStart)));
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamEnd",
                    String.valueOf(pathParamEnd)));
            log.debug(sm.getString("coyoteAdapter.debug", "pv", pv));
        }

        if (pv != null) {
            int equals = pv.indexOf('=');
            if (equals > -1) {
                String name = pv.substring(0, equals);
                String value = pv.substring(equals + 1);
                request.addPathParameter(name, value);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("coyoteAdapter.debug", "equals",
                            String.valueOf(equals)));
                    log.debug(sm.getString("coyoteAdapter.debug", "name",
                            name));
                    log.debug(sm.getString("coyoteAdapter.debug", "value",
                            value));
                }
            }
        }

        semicolon = uriBC.indexOf(';', semicolon);
    }
}
 
Example 8
Source File: Base64.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Decodes Base64 data into octets
 *
 * @param base64DataBC Byte array containing Base64 data
 * @param decodedDataCC The decoded data chars
 */
public static void decode( ByteChunk base64DataBC, CharChunk decodedDataCC)
{
    int start = base64DataBC.getStart();
    int end = base64DataBC.getEnd();
    byte[] base64Data = base64DataBC.getBuffer();
    
    decodedDataCC.recycle();
    
    // handle the edge case, so we don't have to worry about it later
    if(end - start == 0) { return; }

    int      numberQuadruple    = (end - start)/FOURBYTE;
    byte     b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;

    // Throw away anything not in base64Data

    int encodedIndex = 0;
    int dataIndex = start;
    char[] decodedData = null;
    
    {
        // this sizes the output array properly - rlw
        int lastData = end - start;
        // ignore the '=' padding
        while (base64Data[start+lastData-1] == PAD)
        {
            if (--lastData == 0)
            {
                return;
            }
        }
        decodedDataCC.allocate(lastData - numberQuadruple, -1);
        decodedDataCC.setEnd(lastData - numberQuadruple);
        decodedData = decodedDataCC.getBuffer();
    }

    for (int i = 0; i < numberQuadruple; i++)
    {
        dataIndex = start + i * 4;
        marker0   = base64Data[dataIndex + 2];
        marker1   = base64Data[dataIndex + 3];

        b1 = base64Alphabet[base64Data[dataIndex]];
        b2 = base64Alphabet[base64Data[dataIndex +1]];

        if (marker0 != PAD && marker1 != PAD)
        {
            //No PAD e.g 3cQl
            b3 = base64Alphabet[ marker0 ];
            b4 = base64Alphabet[ marker1 ];

            decodedData[encodedIndex]   = (char) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
            decodedData[encodedIndex + 2] = (char) (( b3<<6 | b4 ) & 0xff);
        }
        else if (marker0 == PAD)
        {
            //Two PAD e.g. 3c[Pad][Pad]
            decodedData[encodedIndex]   = (char) ((  b1 <<2 | b2>>4 ) & 0xff);
        }
        else if (marker1 == PAD)
        {
            //One PAD e.g. 3cQ[Pad]
            b3 = base64Alphabet[ marker0 ];

            decodedData[encodedIndex]   = (char) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
        }
        encodedIndex += 3;
    }
}
 
Example 9
Source File: Base64.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Decodes Base64 data into octets
 *
 * @param base64DataBC Byte array containing Base64 data
 * @param decodedDataBC The decoded data bytes
 */
public static void decode( ByteChunk base64DataBC, ByteChunk decodedDataBC)
{
    int start = base64DataBC.getStart();
    int end = base64DataBC.getEnd();
    byte[] base64Data = base64DataBC.getBuffer();
    
    decodedDataBC.recycle();
    
    // handle the edge case, so we don't have to worry about it later
    if(end - start == 0) { return; }

    int      numberQuadruple    = (end - start)/FOURBYTE;
    byte     b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;

    // Throw away anything not in base64Data

    int encodedIndex = 0;
    int dataIndex = start;
    byte[] decodedData = null;
    
    {
        // this sizes the output array properly - rlw
        int lastData = end - start;
        // ignore the '=' padding
        while (base64Data[start+lastData-1] == PAD)
        {
            if (--lastData == 0)
            {
                return;
            }
        }
        decodedDataBC.allocate(lastData - numberQuadruple, -1);
        decodedDataBC.setEnd(lastData - numberQuadruple);
        decodedData = decodedDataBC.getBuffer();
    }

    for (int i = 0; i < numberQuadruple; i++)
    {
        dataIndex = start + i * 4;
        marker0   = base64Data[dataIndex + 2];
        marker1   = base64Data[dataIndex + 3];

        b1 = base64Alphabet[base64Data[dataIndex]];
        b2 = base64Alphabet[base64Data[dataIndex +1]];

        if (marker0 != PAD && marker1 != PAD)
        {
            //No PAD e.g 3cQl
            b3 = base64Alphabet[ marker0 ];
            b4 = base64Alphabet[ marker1 ];

            decodedData[encodedIndex]   = (byte) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
            decodedData[encodedIndex + 2] = (byte) (( b3<<6 | b4 ) & 0xff);
        }
        else if (marker0 == PAD)
        {
            //Two PAD e.g. 3c[Pad][Pad]
            decodedData[encodedIndex]   = (byte) ((  b1 <<2 | b2>>4 ) & 0xff);
        }
        else if (marker1 == PAD)
        {
            //One PAD e.g. 3cQ[Pad]
            b3 = base64Alphabet[ marker0 ];

            decodedData[encodedIndex]   = (byte) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
        }
        encodedIndex += 3;
    }
}
 
Example 10
Source File: CoyoteAdapter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Extract the path parameters from the request. This assumes parameters are
 * of the form /path;name=value;name2=value2/ etc. Currently only really
 * interested in the session ID that will be in this form. Other parameters
 * can safely be ignored.
 *
 * @param req
 * @param request
 */
protected void parsePathParameters(org.apache.coyote.Request req,
        Request request) {

    // Process in bytes (this is default format so this is normally a NO-OP
    req.decodedURI().toBytes();

    ByteChunk uriBC = req.decodedURI().getByteChunk();
    int semicolon = uriBC.indexOf(';', 0);

    // What encoding to use? Some platforms, eg z/os, use a default
    // encoding that doesn't give the expected result so be explicit
    String enc = connector.getURIEncoding();
    if (enc == null) {
        enc = "ISO-8859-1";
    }
    Charset charset = null;
    try {
        charset = B2CConverter.getCharset(enc);
    } catch (UnsupportedEncodingException e1) {
        log.warn(sm.getString("coyoteAdapter.parsePathParam",
                enc));
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("coyoteAdapter.debug", "uriBC",
                uriBC.toString()));
        log.debug(sm.getString("coyoteAdapter.debug", "semicolon",
                String.valueOf(semicolon)));
        log.debug(sm.getString("coyoteAdapter.debug", "enc", enc));
    }

    while (semicolon > -1) {
        // Parse path param, and extract it from the decoded request URI
        int start = uriBC.getStart();
        int end = uriBC.getEnd();

        int pathParamStart = semicolon + 1;
        int pathParamEnd = ByteChunk.findBytes(uriBC.getBuffer(),
                start + pathParamStart, end,
                new byte[] {';', '/'});

        String pv = null;

        if (pathParamEnd >= 0) {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            pathParamEnd - pathParamStart, charset);
            }
            // Extract path param from decoded request URI
            byte[] buf = uriBC.getBuffer();
            for (int i = 0; i < end - start - pathParamEnd; i++) {
                buf[start + semicolon + i]
                    = buf[start + i + pathParamEnd];
            }
            uriBC.setBytes(buf, start,
                    end - start - pathParamEnd + semicolon);
        } else {
            if (charset != null) {
                pv = new String(uriBC.getBuffer(), start + pathParamStart,
                            (end - start) - pathParamStart, charset);
            }
            uriBC.setEnd(start + semicolon);
        }

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamStart",
                    String.valueOf(pathParamStart)));
            log.debug(sm.getString("coyoteAdapter.debug", "pathParamEnd",
                    String.valueOf(pathParamEnd)));
            log.debug(sm.getString("coyoteAdapter.debug", "pv", pv));
        }

        if (pv != null) {
            int equals = pv.indexOf('=');
            if (equals > -1) {
                String name = pv.substring(0, equals);
                String value = pv.substring(equals + 1);
                request.addPathParameter(name, value);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("coyoteAdapter.debug", "equals",
                            String.valueOf(equals)));
                    log.debug(sm.getString("coyoteAdapter.debug", "name",
                            name));
                    log.debug(sm.getString("coyoteAdapter.debug", "value",
                            value));
                }
            }
        }

        semicolon = uriBC.indexOf(';', semicolon);
    }
}
 
Example 11
Source File: Base64.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Decodes Base64 data into octets
 *
 * @param base64DataBC Byte array containing Base64 data
 * @param decodedDataCC The decoded data chars
 */
public static void decode( ByteChunk base64DataBC, CharChunk decodedDataCC)
{
    int start = base64DataBC.getStart();
    int end = base64DataBC.getEnd();
    byte[] base64Data = base64DataBC.getBuffer();
    
    decodedDataCC.recycle();
    
    // handle the edge case, so we don't have to worry about it later
    if(end - start == 0) { return; }

    int      numberQuadruple    = (end - start)/FOURBYTE;
    byte     b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;

    // Throw away anything not in base64Data

    int encodedIndex = 0;
    int dataIndex = start;
    char[] decodedData = null;
    
    {
        // this sizes the output array properly - rlw
        int lastData = end - start;
        // ignore the '=' padding
        while (base64Data[start+lastData-1] == PAD)
        {
            if (--lastData == 0)
            {
                return;
            }
        }
        decodedDataCC.allocate(lastData - numberQuadruple, -1);
        decodedDataCC.setEnd(lastData - numberQuadruple);
        decodedData = decodedDataCC.getBuffer();
    }

    for (int i = 0; i < numberQuadruple; i++)
    {
        dataIndex = start + i * 4;
        marker0   = base64Data[dataIndex + 2];
        marker1   = base64Data[dataIndex + 3];

        b1 = base64Alphabet[base64Data[dataIndex]];
        b2 = base64Alphabet[base64Data[dataIndex +1]];

        if (marker0 != PAD && marker1 != PAD)
        {
            //No PAD e.g 3cQl
            b3 = base64Alphabet[ marker0 ];
            b4 = base64Alphabet[ marker1 ];

            decodedData[encodedIndex]   = (char) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
            decodedData[encodedIndex + 2] = (char) (( b3<<6 | b4 ) & 0xff);
        }
        else if (marker0 == PAD)
        {
            //Two PAD e.g. 3c[Pad][Pad]
            decodedData[encodedIndex]   = (char) ((  b1 <<2 | b2>>4 ) & 0xff);
        }
        else if (marker1 == PAD)
        {
            //One PAD e.g. 3cQ[Pad]
            b3 = base64Alphabet[ marker0 ];

            decodedData[encodedIndex]   = (char) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
        }
        encodedIndex += 3;
    }
}
 
Example 12
Source File: Base64.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Decodes Base64 data into octets
 *
 * @param base64DataBC Byte array containing Base64 data
 * @param decodedDataBC The decoded data bytes
 */
public static void decode( ByteChunk base64DataBC, ByteChunk decodedDataBC)
{
    int start = base64DataBC.getStart();
    int end = base64DataBC.getEnd();
    byte[] base64Data = base64DataBC.getBuffer();
    
    decodedDataBC.recycle();
    
    // handle the edge case, so we don't have to worry about it later
    if(end - start == 0) { return; }

    int      numberQuadruple    = (end - start)/FOURBYTE;
    byte     b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;

    // Throw away anything not in base64Data

    int encodedIndex = 0;
    int dataIndex = start;
    byte[] decodedData = null;
    
    {
        // this sizes the output array properly - rlw
        int lastData = end - start;
        // ignore the '=' padding
        while (base64Data[start+lastData-1] == PAD)
        {
            if (--lastData == 0)
            {
                return;
            }
        }
        decodedDataBC.allocate(lastData - numberQuadruple, -1);
        decodedDataBC.setEnd(lastData - numberQuadruple);
        decodedData = decodedDataBC.getBuffer();
    }

    for (int i = 0; i < numberQuadruple; i++)
    {
        dataIndex = start + i * 4;
        marker0   = base64Data[dataIndex + 2];
        marker1   = base64Data[dataIndex + 3];

        b1 = base64Alphabet[base64Data[dataIndex]];
        b2 = base64Alphabet[base64Data[dataIndex +1]];

        if (marker0 != PAD && marker1 != PAD)
        {
            //No PAD e.g 3cQl
            b3 = base64Alphabet[ marker0 ];
            b4 = base64Alphabet[ marker1 ];

            decodedData[encodedIndex]   = (byte) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
            decodedData[encodedIndex + 2] = (byte) (( b3<<6 | b4 ) & 0xff);
        }
        else if (marker0 == PAD)
        {
            //Two PAD e.g. 3c[Pad][Pad]
            decodedData[encodedIndex]   = (byte) ((  b1 <<2 | b2>>4 ) & 0xff);
        }
        else if (marker1 == PAD)
        {
            //One PAD e.g. 3cQ[Pad]
            b3 = base64Alphabet[ marker0 ];

            decodedData[encodedIndex]   = (byte) ((  b1 <<2 | b2>>4 ) & 0xff);
            decodedData[encodedIndex + 1] =
                (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff);
        }
        encodedIndex += 3;
    }
}