org.apache.tomcat.util.http.ServerCookie Java Examples

The following examples show how to use org.apache.tomcat.util.http.ServerCookie. 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: Response.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
public StringBuffer generateCookieString(final Cookie cookie) {
    final StringBuffer sb = new StringBuffer();
    //web application code can receive a IllegalArgumentException
    //from the appendCookieValue invocation
    if (SecurityUtil.isPackageProtectionEnabled()) {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run(){
                ServerCookie.appendCookieValue
                    (sb, cookie.getVersion(), cookie.getName(),
                     cookie.getValue(), cookie.getPath(),
                     cookie.getDomain(), cookie.getComment(),
                     cookie.getMaxAge(), cookie.getSecure(),
                     cookie.isHttpOnly());
                return null;
            }
        });
    } else {
        ServerCookie.appendCookieValue
            (sb, cookie.getVersion(), cookie.getName(), cookie.getValue(),
                 cookie.getPath(), cookie.getDomain(), cookie.getComment(),
                 cookie.getMaxAge(), cookie.getSecure(),
                 cookie.isHttpOnly());
    }
    return sb;
}
 
Example #2
Source File: Response.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public StringBuffer generateCookieString(final Cookie cookie) {
    final StringBuffer sb = new StringBuffer();
    //web application code can receive a IllegalArgumentException
    //from the appendCookieValue invocation
    if (SecurityUtil.isPackageProtectionEnabled()) {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run(){
                ServerCookie.appendCookieValue
                    (sb, cookie.getVersion(), cookie.getName(),
                     cookie.getValue(), cookie.getPath(),
                     cookie.getDomain(), cookie.getComment(),
                     cookie.getMaxAge(), cookie.getSecure(),
                     cookie.isHttpOnly());
                return null;
            }
        });
    } else {
        ServerCookie.appendCookieValue
            (sb, cookie.getVersion(), cookie.getName(), cookie.getValue(),
                 cookie.getPath(), cookie.getDomain(), cookie.getComment(),
                 cookie.getMaxAge(), cookie.getSecure(),
                 cookie.isHttpOnly());
    }
    return sb;
}
 
Example #3
Source File: CoyoteAdapter.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Parse session id in Cookie.
 *
 * @param request The Servlet request object
 */
protected void parseSessionCookiesId(Request request) {

    // If session tracking via cookies has been disabled for the current
    // context, don't go looking for a session ID in a cookie as a cookie
    // from a parent context with a session ID may be present which would
    // overwrite the valid session ID encoded in the URL
    Context context = request.getMappingData().context;
    if (context != null && !context.getServletContext()
            .getEffectiveSessionTrackingModes().contains(
                    SessionTrackingMode.COOKIE)) {
        return;
    }

    // Parse session id from cookies
    ServerCookies serverCookies = request.getServerCookies();
    int count = serverCookies.getCookieCount();
    if (count <= 0) {
        return;
    }

    String sessionCookieName = SessionConfig.getSessionCookieName(context);

    for (int i = 0; i < count; i++) {
        ServerCookie scookie = serverCookies.getCookie(i);
        if (scookie.getName().equals(sessionCookieName)) {
            // Override anything requested in the URL
            if (!request.isRequestedSessionIdFromCookie()) {
                // Accept only the first session id cookie
                convertMB(scookie.getValue());
                request.setRequestedSessionId
                    (scookie.getValue().toString());
                request.setRequestedSessionCookie(true);
                request.setRequestedSessionURL(false);
                if (log.isDebugEnabled()) {
                    log.debug(" Requested cookie session id is " +
                        request.getRequestedSessionId());
                }
            } else {
                if (!request.isRequestedSessionIdValid()) {
                    // Replace the session id until one is valid
                    convertMB(scookie.getValue());
                    request.setRequestedSessionId
                        (scookie.getValue().toString());
                }
            }
        }
    }

}
 
Example #4
Source File: Request.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Converts the parsed cookies (parsing the Cookie headers first if they
 * have not been parsed) into Cookie objects.
 */
protected void convertCookies() {
    if (cookiesConverted) {
        return;
    }

    cookiesConverted = true;

    if (getContext() == null) {
        return;
    }

    parseCookies();

    ServerCookies serverCookies = coyoteRequest.getCookies();
    CookieProcessor cookieProcessor = getContext().getCookieProcessor();

    int count = serverCookies.getCookieCount();
    if (count <= 0) {
        return;
    }

    cookies = new Cookie[count];

    int idx=0;
    for (int i = 0; i < count; i++) {
        ServerCookie scookie = serverCookies.getCookie(i);
        try {
            // We must unescape the '\\' escape character
            Cookie cookie = new Cookie(scookie.getName().toString(),null);
            int version = scookie.getVersion();
            cookie.setVersion(version);
            scookie.getValue().getByteChunk().setCharset(cookieProcessor.getCharset());
            cookie.setValue(unescape(scookie.getValue().toString()));
            cookie.setPath(unescape(scookie.getPath().toString()));
            String domain = scookie.getDomain().toString();
            if (domain!=null) {
                cookie.setDomain(unescape(domain));//avoid NPE
            }
            String comment = scookie.getComment().toString();
            cookie.setComment(version==1?unescape(comment):null);
            cookies[idx++] = cookie;
        } catch(IllegalArgumentException e) {
            // Ignore bad cookie
        }
    }
    if( idx < count ) {
        Cookie [] ncookies = new Cookie[idx];
        System.arraycopy(cookies, 0, ncookies, 0, idx);
        cookies = ncookies;
    }
}
 
Example #5
Source File: Cookie.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private static void parseCookieRfc6265(ByteBuffer bb, ServerCookies serverCookies) {

        boolean moreToProcess = true;

        while (moreToProcess) {
            skipLWS(bb);

            ByteBuffer name = readToken(bb);
            ByteBuffer value = null;

            skipLWS(bb);

            SkipResult skipResult = skipByte(bb, EQUALS_BYTE);
            if (skipResult == SkipResult.FOUND) {
                skipLWS(bb);
                value = readCookieValueRfc6265(bb);
                if (value == null) {
                    logInvalidHeader(bb);
                    // Invalid cookie value. Skip to the next semi-colon
                    skipUntilSemiColon(bb);
                    continue;
                }
                skipLWS(bb);
            }

            skipResult = skipByte(bb, SEMICOLON_BYTE);
            if (skipResult == SkipResult.FOUND) {
                // NO-OP
            } else if (skipResult == SkipResult.NOT_FOUND) {
                logInvalidHeader(bb);
                // Invalid cookie. Ignore it and skip to the next semi-colon
                skipUntilSemiColon(bb);
                continue;
            } else {
                // SkipResult.EOF
                moreToProcess = false;
            }

            if (name.hasRemaining()) {
                ServerCookie sc = serverCookies.addCookie();
                sc.getName().setBytes(name.array(), name.position(), name.remaining());
                if (value == null) {
                    sc.getValue().setBytes(EMPTY_BYTES, 0, EMPTY_BYTES.length);
                } else {
                    sc.getValue().setBytes(value.array(), value.position(), value.remaining());
                }
            }
        }
    }
 
Example #6
Source File: CoyoteAdapter.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Parse session id in URL.
 */
protected void parseSessionCookiesId(org.apache.coyote.Request req, Request request) {

    // If session tracking via cookies has been disabled for the current
    // context, don't go looking for a session ID in a cookie as a cookie
    // from a parent context with a session ID may be present which would
    // overwrite the valid session ID encoded in the URL
    Context context = (Context) request.getMappingData().context;
    if (context != null && !context.getServletContext()
            .getEffectiveSessionTrackingModes().contains(
                    SessionTrackingMode.COOKIE)) {
        return;
    }

    // Parse session id from cookies
    Cookies serverCookies = req.getCookies();
    int count = serverCookies.getCookieCount();
    if (count <= 0) {
        return;
    }

    String sessionCookieName = SessionConfig.getSessionCookieName(context);

    for (int i = 0; i < count; i++) {
        ServerCookie scookie = serverCookies.getCookie(i);
        if (scookie.getName().equals(sessionCookieName)) {
            // Override anything requested in the URL
            if (!request.isRequestedSessionIdFromCookie()) {
                // Accept only the first session id cookie
                convertMB(scookie.getValue());
                request.setRequestedSessionId
                    (scookie.getValue().toString());
                request.setRequestedSessionCookie(true);
                request.setRequestedSessionURL(false);
                if (log.isDebugEnabled()) {
                    log.debug(" Requested cookie session id is " +
                        request.getRequestedSessionId());
                }
            } else {
                if (!request.isRequestedSessionIdValid()) {
                    // Replace the session id until one is valid
                    convertMB(scookie.getValue());
                    request.setRequestedSessionId
                        (scookie.getValue().toString());
                }
            }
        }
    }

}
 
Example #7
Source File: Request.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Parse cookies.
 */
protected void parseCookies() {

    cookiesParsed = true;

    Cookies serverCookies = coyoteRequest.getCookies();
    int count = serverCookies.getCookieCount();
    if (count <= 0) {
        return;
    }

    cookies = new Cookie[count];

    int idx=0;
    for (int i = 0; i < count; i++) {
        ServerCookie scookie = serverCookies.getCookie(i);
        try {
            /*
            we must unescape the '\\' escape character
            */
            Cookie cookie = new Cookie(scookie.getName().toString(),null);
            int version = scookie.getVersion();
            cookie.setVersion(version);
            cookie.setValue(unescape(scookie.getValue().toString()));
            cookie.setPath(unescape(scookie.getPath().toString()));
            String domain = scookie.getDomain().toString();
            if (domain!=null)
             {
                cookie.setDomain(unescape(domain));//avoid NPE
            }
            String comment = scookie.getComment().toString();
            cookie.setComment(version==1?unescape(comment):null);
            cookies[idx++] = cookie;
        } catch(IllegalArgumentException e) {
            // Ignore bad cookie
        }
    }
    if( idx < count ) {
        Cookie [] ncookies = new Cookie[idx];
        System.arraycopy(cookies, 0, ncookies, 0, idx);
        cookies = ncookies;
    }

}
 
Example #8
Source File: CoyoteAdapter.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Parse session id in URL.
 */
protected void parseSessionCookiesId(org.apache.coyote.Request req, Request request) {

    // If session tracking via cookies has been disabled for the current
    // context, don't go looking for a session ID in a cookie as a cookie
    // from a parent context with a session ID may be present which would
    // overwrite the valid session ID encoded in the URL
    Context context = (Context) request.getMappingData().context;
    if (context != null && !context.getServletContext()
            .getEffectiveSessionTrackingModes().contains(
                    SessionTrackingMode.COOKIE)) {
        return;
    }

    // Parse session id from cookies
    Cookies serverCookies = req.getCookies();
    int count = serverCookies.getCookieCount();
    if (count <= 0) {
        return;
    }

    String sessionCookieName = SessionConfig.getSessionCookieName(context);

    for (int i = 0; i < count; i++) {
        ServerCookie scookie = serverCookies.getCookie(i);
        if (scookie.getName().equals(sessionCookieName)) {
            // Override anything requested in the URL
            if (!request.isRequestedSessionIdFromCookie()) {
                // Accept only the first session id cookie
                convertMB(scookie.getValue());
                request.setRequestedSessionId
                    (scookie.getValue().toString());
                request.setRequestedSessionCookie(true);
                request.setRequestedSessionURL(false);
                if (log.isDebugEnabled()) {
                    log.debug(" Requested cookie session id is " +
                        request.getRequestedSessionId());
                }
            } else {
                if (!request.isRequestedSessionIdValid()) {
                    // Replace the session id until one is valid
                    convertMB(scookie.getValue());
                    request.setRequestedSessionId
                        (scookie.getValue().toString());
                }
            }
        }
    }

}
 
Example #9
Source File: Request.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Parse cookies.
 */
protected void parseCookies() {

    cookiesParsed = true;

    Cookies serverCookies = coyoteRequest.getCookies();
    int count = serverCookies.getCookieCount();
    if (count <= 0) {
        return;
    }

    cookies = new Cookie[count];

    int idx=0;
    for (int i = 0; i < count; i++) {
        ServerCookie scookie = serverCookies.getCookie(i);
        try {
            /*
            we must unescape the '\\' escape character
            */
            Cookie cookie = new Cookie(scookie.getName().toString(),null);
            int version = scookie.getVersion();
            cookie.setVersion(version);
            cookie.setValue(unescape(scookie.getValue().toString()));
            cookie.setPath(unescape(scookie.getPath().toString()));
            String domain = scookie.getDomain().toString();
            if (domain!=null)
             {
                cookie.setDomain(unescape(domain));//avoid NPE
            }
            String comment = scookie.getComment().toString();
            cookie.setComment(version==1?unescape(comment):null);
            cookies[idx++] = cookie;
        } catch(IllegalArgumentException e) {
            // Ignore bad cookie
        }
    }
    if( idx < count ) {
        Cookie [] ncookies = new Cookie[idx];
        System.arraycopy(cookies, 0, ncookies, 0, idx);
        cookies = ncookies;
    }

}