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

The following examples show how to use org.apache.tomcat.util.buf.ByteChunk#setEnd() . 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: SavedRequestInputFilter.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * @deprecated Unused. Will be removed in Tomcat 9. Use
 *             {@link #doRead(ApplicationBufferHandler)}
 */
@Deprecated
@Override
public int doRead(ByteChunk chunk) throws IOException {
    if(input.getOffset()>= input.getEnd())
        return -1;

    int writeLength = 0;

    if (chunk.getLimit() > 0 && chunk.getLimit() < input.getLength()) {
        writeLength = chunk.getLimit();
    } else {
        writeLength = input.getLength();
    }

    input.substract(chunk.getBuffer(), 0, writeLength);
    chunk.setOffset(0);
    chunk.setEnd(writeLength);

    return writeLength;
}
 
Example 2
Source File: SavedRequestInputFilter.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes.
 */
@Override
public int doRead(ByteChunk chunk, org.apache.coyote.Request request)
        throws IOException {
    int writeLength = 0;
    
    if (chunk.getLimit() > 0 && chunk.getLimit() < input.getLength()) {
        writeLength = chunk.getLimit();
    } else {
        writeLength = input.getLength();
    }
    
    if(input.getOffset()>= input.getEnd())
        return -1;
    
    input.substract(chunk.getBuffer(), 0, writeLength);
    chunk.setOffset(0);
    chunk.setEnd(writeLength);
    
    return writeLength;
}
 
Example 3
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 4
Source File: SavedRequestInputFilter.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Read bytes.
 */
@Override
public int doRead(ByteChunk chunk, org.apache.coyote.Request request)
        throws IOException {
    int writeLength = 0;
    
    if (chunk.getLimit() > 0 && chunk.getLimit() < input.getLength()) {
        writeLength = chunk.getLimit();
    } else {
        writeLength = input.getLength();
    }
    
    if(input.getOffset()>= input.getEnd())
        return -1;
    
    input.substract(chunk.getBuffer(), 0, writeLength);
    chunk.setOffset(0);
    chunk.setEnd(writeLength);
    
    return writeLength;
}
 
Example 5
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 6
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 7
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 8
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 9
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;
    }
}