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

The following examples show how to use org.apache.tomcat.util.buf.ByteChunk#indexOf() . 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: LegacyCookieProcessor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Unescapes any double quotes in the given cookie value.
 *
 * @param bc The cookie value to modify
 */
private static final void unescapeDoubleQuotes(ByteChunk bc) {

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

    // Take a copy of the buffer so the original cookie header is not
    // modified by this unescaping.
    byte[] original = bc.getBuffer();
    int len = bc.getLength();

    byte[] copy = new byte[len];
    System.arraycopy(original, bc.getStart(), copy, 0, len);

    int src = 0;
    int dest = 0;

    while (src < len) {
        if (copy[src] == '\\' && src < len && copy[src+1]  == '"') {
            src++;
        }
        copy[dest] = copy[src];
        dest ++;
        src ++;
    }
    bc.setBytes(copy, 0, dest);
}
 
Example 4
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 5
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);
    }
}