Package org.glassfish.grizzly.http

Examples of org.glassfish.grizzly.http.Cookie


            //There is no Cookie header in request so generate a new JSESSIONID  or
            //failover has occured in which case you can generate a new JSESSIONID
            sessionId = createSessionId();
        }
        StringBuilder sb = new StringBuilder();
        final Cookie cookie = new Cookie(SESSION_COOKIE_NAME, sessionId);
        cookie.setMaxAge(MAX_AGE);
        cookie.setPath(ASADMIN_PATH);
        cookie.setVersion(1);
        CookieSerializerUtils.serializeServerCookie(sb, true, false, cookie);
        return sb.toString();

    }
View Full Code Here


            //There is no Cookie header in request so generate a new JSESSIONID  or
            //failover has occured in which case you can generate a new JSESSIONID
            sessionId = createSessionId();
        }
        StringBuilder sb = new StringBuilder();
        final Cookie cookie = new Cookie(SESSION_COOKIE_NAME, sessionId);
        cookie.setMaxAge(MAX_AGE);
        cookie.setPath(ASADMIN_PATH);
        cookie.setVersion(1);
        CookieSerializerUtils.serializeServerCookie(sb, true, false, cookie);
        return sb.toString();

    }
View Full Code Here

            //There is no Cookie header in request so generate a new JSESSIONID  or
            //failover has occured in which case you can generate a new JSESSIONID
            sessionId = createSessionId();
        }
        StringBuilder sb = new StringBuilder();
        final Cookie cookie = new Cookie(SESSION_COOKIE_NAME, sessionId);
        cookie.setMaxAge(MAX_AGE);
        cookie.setPath(ASADMIN_PATH);
        cookie.setVersion(1);
        CookieSerializerUtils.serializeServerCookie(sb, true, false, cookie);
        return sb.toString();

    }
View Full Code Here

        int nameStart;
        int nameEnd;
        int valueStart;
        int valueEnd;

        Cookie cookie = null;
        LazyCookieState lazyCookie = null;

        boolean isQuoted;

        while (pos < end) {
            isQuoted = false;

            // Skip whitespace and non-token characters (separators)
            while (pos < end
                    && (isSeparator(bytes[pos]) || isWhiteSpace(bytes[pos]))) {
                pos++;
            }

            if (pos >= end) {
                return;
            }

            // Get the cookie name. This must be a token
            nameStart = pos;
            pos = nameEnd = getTokenEndPosition(bytes, pos, end);

            // Skip whitespace
            while (pos < end && isWhiteSpace(bytes[pos])) {
                pos++;
            }

            // Check for an '=' -- This could also be a name-only
            // cookie at the end of the cookie header, so if we
            // are past the end of the header, but we have a name
            // skip to the name-only part.
            if (pos < end && bytes[pos] == '=') {

                // Skip whitespace
                do {
                    pos++;
                } while (pos < end && isWhiteSpace(bytes[pos]));

                if (pos >= end) {
                    return;
                }

                // Determine what type of value this is, quoted value,
                // token, name-only with an '=', or other (bad)
                switch (bytes[pos]) {
                    case '"':
                        // Quoted Value
                        isQuoted = true;
                        valueStart = pos + 1; // strip "
                        // getQuotedValue returns the position before
                        // at the last qoute. This must be dealt with
                        // when the bytes are copied into the cookie
                        valueEnd = getQuotedValueEndPosition(bytes,
                                valueStart, end);
                        // We need pos to advance
                        pos = valueEnd;
                        // Handles cases where the quoted value is
                        // unterminated and at the end of the header,
                        // e.g. [myname="value]
                        if (pos >= end) {
                            return;
                        }
                        break;
                    case ';':
                    case ',':
                        // Name-only cookie with an '=' after the name token
                        // This may not be RFC compliant
                        valueStart = valueEnd = -1;
                        // The position is OK (On a delimiter)
                        break;
                    default:
                        if (!isSeparator(bytes[pos], versionOneStrictCompliance)) {
                            // Token
                            // Token
                            valueStart = pos;
                            // getToken returns the position at the delimeter
                            // or other non-token character
                            valueEnd = getTokenEndPosition(bytes, valueStart, end,
                                    versionOneStrictCompliance);
                            // We need pos to advance
                            pos = valueEnd;
                        } else {
                            // INVALID COOKIE, advance to next delimiter
                            // The starting character of the cookie value was
                            // not valid.
                            LOGGER.fine("Invalid cookie. Value not a token or quoted value");
                            while (pos < end && bytes[pos] != ';'
                                    && bytes[pos] != ',') {
                                pos++;
                            }

                            pos++;
                            // Make sure no special avpairs can be attributed to
                            // the previous cookie by setting the current cookie
                            // to null
                            cookie = null;
                            lazyCookie = null;
                            continue;
                        }
                }
            } else {
                // Name only cookie
                valueStart = valueEnd = -1;
                pos = nameEnd;

            }

            // We should have an avpair or name-only cookie at this
            // point. Perform some basic checks to make sure we are
            // in a good state.

            // Skip whitespace
            while (pos < end && isWhiteSpace(bytes[pos])) {
                pos++;
            }

            // Make sure that after the cookie we have a separator. This
            // is only important if this is not the last cookie pair
            while (pos < end && bytes[pos] != ';' && bytes[pos] != ',') {
                pos++;
            }

            pos++;

            // All checks passed. Add the cookie, start with the
            // special avpairs first
            if (cookie != null) {
                // Domain is more common, so it goes first
                if (lazyCookie.getDomain().isNull() &&
                        equalsIgnoreCase("Domain", bytes, nameStart, nameEnd)) {
                    lazyCookie.getDomain().setBytes(bytes,
                            valueStart, valueEnd);
                    continue;
                }

                // Path
                if (lazyCookie.getPath().isNull() &&
                        equalsIgnoreCase("Path", bytes, nameStart, nameEnd)) {
                    lazyCookie.getPath().setBytes(bytes,
                            valueStart, valueEnd);
                    continue;
                }

                // Version
                if (CookieUtils.equals("Version", bytes, nameStart, nameEnd)) {
                    if (rfc6265Enabled) {
                        continue;
                    }
                    // Set version
                    if (bytes[valueStart] == '1'
                            && valueEnd == (valueStart + 1)) {
                        cookie.setVersion(1);
                    } else {
                        // unknown version (Versioning is not very strict)
                    }
                    continue;
                }

                // Comment
                if (lazyCookie.getComment().isNull() &&
                        CookieUtils.equals("Comment", bytes, nameStart, nameEnd)) {
                    lazyCookie.getComment().setBytes(bytes,
                            valueStart, valueEnd);
                    continue;
                }

                // Max-Age
                if (cookie.getMaxAge() == -1 &&
                        CookieUtils.equals("Max-Age", bytes, nameStart, nameEnd)) {
                    cookie.setMaxAge(Ascii.parseInt(bytes,
                            valueStart,
                            valueEnd - valueStart));
                    continue;
                }

                // Expires
                if ((cookie.getVersion() == 0 || !cookie.isVersionSet()) && cookie.getMaxAge() == -1 &&
                        equalsIgnoreCase("Expires", bytes, nameStart, nameEnd)) {
                    try {
                        valueEnd = getTokenEndPosition(bytes, valueEnd + 1, end, false);
                        pos = valueEnd + 1;

                        final String expiresDate = new String(bytes,
                                valueStart, valueEnd - valueStart,
                                Charsets.ASCII_CHARSET);

                        final Date date = OLD_COOKIE_FORMAT.get().parse(expiresDate);
                        cookie.setMaxAge(getMaxAgeDelta(date.getTime(), System.currentTimeMillis()) / 1000);
                    } catch (ParseException ignore) {
                    }

                    continue;
                }

                // Secure
                if (!cookie.isSecure() &&
                        equalsIgnoreCase("Secure", bytes, nameStart, nameEnd)) {
                    lazyCookie.setSecure(true);
                    continue;
                }

                // HttpOnly
                if (!cookie.isHttpOnly() &&
                        CookieUtils.equals("HttpOnly", bytes, nameStart, nameEnd)) {
                    cookie.setHttpOnly(true);
                    continue;
                }

                if (CookieUtils.equals("Discard", bytes, nameStart, nameEnd)) {
                    continue;
                }
            }

            // Normal Cookie
            cookie = cookies.getNextUnusedCookie();
            if (!rfc6265Enabled && !cookie.isVersionSet()) {
                cookie.setVersion(0);
            }
            lazyCookie = cookie.getLazyCookieState();

            lazyCookie.getName().setBytes(bytes, nameStart, nameEnd);

            if (valueStart != -1) { // Normal AVPair
                lazyCookie.getValue().setBytes(bytes, valueStart, valueEnd);
View Full Code Here

        int nameStart;
        int nameEnd;
        int valueStart;
        int valueEnd;

        Cookie cookie = null;
        LazyCookieState lazyCookie = null;

        boolean isQuoted;

        while (pos < end) {
            isQuoted = false;

            // Skip whitespace and non-token characters (separators)
            while (pos < end
                    && (isSeparator(buffer.get(pos)) || isWhiteSpace(buffer.get(pos)))) {
                pos++;
            }

            if (pos >= end) {
                return;
            }

            // Get the cookie name. This must be a token
            nameStart = pos;
            pos = nameEnd = getTokenEndPosition(buffer, pos, end);

            // Skip whitespace
            while (pos < end && isWhiteSpace(buffer.get(pos))) {
                pos++;
            }

            // Check for an '=' -- This could also be a name-only
            // cookie at the end of the cookie header, so if we
            // are past the end of the header, but we have a name
            // skip to the name-only part.
            if (pos < end && buffer.get(pos) == '=') {

                // Skip whitespace
                do {
                    pos++;
                } while (pos < end && isWhiteSpace(buffer.get(pos)));

                if (pos >= end) {
                    return;
                }

                // Determine what type of value this is, quoted value,
                // token, name-only with an '=', or other (bad)
                switch (buffer.get(pos)) {
                    case '"':
                        // Quoted Value
                        isQuoted = true;
                        valueStart = pos + 1; // strip "
                        // getQuotedValue returns the position before
                        // at the last qoute. This must be dealt with
                        // when the bytes are copied into the cookie
                        valueEnd = getQuotedValueEndPosition(buffer,
                                valueStart, end);
                        // We need pos to advance
                        pos = valueEnd;
                        // Handles cases where the quoted value is
                        // unterminated and at the end of the header,
                        // e.g. [myname="value]
                        if (pos >= end) {
                            return;
                        }
                        break;
                    case ';':
                    case ',':
                        // Name-only cookie with an '=' after the name token
                        // This may not be RFC compliant
                        valueStart = valueEnd = -1;
                        // The position is OK (On a delimiter)
                        break;
                    default:
                        if (!isSeparator(buffer.get(pos), versionOneStrictCompliance)) {
                        // Token
                            // Token
                            valueStart = pos;
                            // getToken returns the position at the delimeter
                            // or other non-token character
                            valueEnd = getTokenEndPosition(buffer, valueStart, end,
                                versionOneStrictCompliance);
                            // We need pos to advance
                            pos = valueEnd;
                        } else {
                            // INVALID COOKIE, advance to next delimiter
                            // The starting character of the cookie value was
                            // not valid.
                            LOGGER.fine("Invalid cookie. Value not a token or quoted value");
                            while (pos < end && buffer.get(pos) != ';'
                                    && buffer.get(pos) != ',') {
                                pos++;
                            }

                            pos++;
                            // Make sure no special avpairs can be attributed to
                            // the previous cookie by setting the current cookie
                            // to null
                            cookie = null;
                            lazyCookie = null;
                            continue;
                        }
                }
            } else {
                // Name only cookie
                valueStart = valueEnd = -1;
                pos = nameEnd;

            }

            // We should have an avpair or name-only cookie at this
            // point. Perform some basic checks to make sure we are
            // in a good state.

            // Skip whitespace
            while (pos < end && isWhiteSpace(buffer.get(pos))) {
                pos++;
            }

            // Make sure that after the cookie we have a separator. This
            // is only important if this is not the last cookie pair
            while (pos < end && buffer.get(pos) != ';' && buffer.get(pos) != ',') {
                pos++;
            }

            pos++;

            // All checks passed. Add the cookie, start with the
            // special avpairs first
            if (cookie != null) {
                // Domain is more common, so it goes first
                if (lazyCookie.getDomain().isNull() &&
                        equalsIgnoreCase("Domain", buffer, nameStart, nameEnd)) {
                    lazyCookie.getDomain().setBuffer(buffer,
                            valueStart, valueEnd);
                    continue;
                }

                // Path
                if (lazyCookie.getPath().isNull() &&
                        equalsIgnoreCase("Path", buffer, nameStart, nameEnd)) {
                    lazyCookie.getPath().setBuffer(buffer,
                            valueStart, valueEnd);
                    continue;
                }

                // Version
                if (CookieUtils.equals("Version", buffer, nameStart, nameEnd)) {
                    if (rfc6265Enabled) {
                        continue;
                    }
                    // Set version
                    if (buffer.get(valueStart) == '1'
                            && valueEnd == (valueStart + 1)) {
                        cookie.setVersion(1);
                    } else {
                        // unknown version (Versioning is not very strict)
                    }
                    continue;
                }

                // Comment
                if (lazyCookie.getComment().isNull() &&
                        CookieUtils.equals("Comment", buffer, nameStart, nameEnd)) {
                    lazyCookie.getComment().setBuffer(buffer,
                            valueStart, valueEnd);
                    continue;
                }

                // Max-Age
                if (cookie.getMaxAge() == -1 &&
                        CookieUtils.equals("Max-Age", buffer, nameStart, nameEnd)) {
                    cookie.setMaxAge(Ascii.parseInt(buffer,
                            valueStart,
                            valueEnd - valueStart));
                    continue;
                }

                // Expires
                if ((cookie.getVersion() == 0 || !cookie.isVersionSet()) && cookie.getMaxAge() == -1 &&
                        equalsIgnoreCase("Expires", buffer, nameStart, nameEnd)) {
                    try {
                        valueEnd = getTokenEndPosition(buffer, valueEnd + 1, end, false);
                        pos = valueEnd + 1;

                        final String expiresDate =
                                buffer.toStringContent(Charsets.ASCII_CHARSET,
                                                       valueStart,
                                                       valueEnd);
                        final Date date = OLD_COOKIE_FORMAT.get().parse(expiresDate);
                        cookie.setMaxAge(getMaxAgeDelta(date.getTime(), System.currentTimeMillis()) / 1000);
                    } catch (ParseException ignore) {
                    }

                    continue;
                }

                // Secure
                if (!cookie.isSecure() &&
                        equalsIgnoreCase("Secure", buffer, nameStart, nameEnd)) {
                    lazyCookie.setSecure(true);
                    continue;
                }

                // HttpOnly
                if (!cookie.isHttpOnly() &&
                        CookieUtils.equals("HttpOnly", buffer, nameStart, nameEnd)) {
                    cookie.setHttpOnly(true);
                    continue;
                }

                if (CookieUtils.equals("Discard", buffer, nameStart, nameEnd)) {
                    continue;
                }
            }

            // Normal Cookie
            cookie = cookies.getNextUnusedCookie();
            if (!rfc6265Enabled && !cookie.isVersionSet()) {
                cookie.setVersion(0);
            }
            lazyCookie = cookie.getLazyCookieState();

            lazyCookie.getName().setBuffer(buffer, nameStart, nameEnd);

            if (valueStart != -1) { // Normal AVPair
                lazyCookie.getValue().setBuffer(buffer, valueStart, valueEnd);
View Full Code Here

        int nameStart;
        int nameEnd;
        int valueStart;
        int valueEnd;

        Cookie cookie = null;

        boolean isQuoted;

        while (pos < end) {
            isQuoted = false;

            // Skip whitespace and non-token characters (separators)
            while (pos < end
                    && (isSeparator(cookiesStr.charAt(pos)) || isWhiteSpace(cookiesStr.charAt(pos)))) {
                pos++;
            }

            if (pos >= end) {
                return;
            }

            // Get the cookie name. This must be a token
            nameStart = pos;
            pos = nameEnd = getTokenEndPosition(cookiesStr, pos, end);

            // Skip whitespace
            while (pos < end && isWhiteSpace(cookiesStr.charAt(pos))) {
                pos++;
            }

            // Check for an '=' -- This could also be a name-only
            // cookie at the end of the cookie header, so if we
            // are past the end of the header, but we have a name
            // skip to the name-only part.
            if (pos < end && cookiesStr.charAt(pos) == '=') {

                // Skip whitespace
                do {
                    pos++;
                } while (pos < end && isWhiteSpace(cookiesStr.charAt(pos)));

                if (pos >= end) {
                    return;
                }

                // Determine what type of value this is, quoted value,
                // token, name-only with an '=', or other (bad)
                switch (cookiesStr.charAt(pos)) {
                    case '"':
                        // Quoted Value
                        isQuoted = true;
                        valueStart = pos + 1; // strip "
                        // getQuotedValue returns the position before
                        // at the last qoute. This must be dealt with
                        // when the bytes are copied into the cookie
                        valueEnd = getQuotedValueEndPosition(cookiesStr,
                                valueStart, end);
                        // We need pos to advance
                        pos = valueEnd;
                        // Handles cases where the quoted value is
                        // unterminated and at the end of the header,
                        // e.g. [myname="value]
                        if (pos >= end) {
                            return;
                        }
                        break;
                    case ';':
                    case ',':
                        // Name-only cookie with an '=' after the name token
                        // This may not be RFC compliant
                        valueStart = valueEnd = -1;
                        // The position is OK (On a delimiter)
                        break;
                    default:
                        if (!isSeparator(cookiesStr.charAt(pos), versionOneStrictCompliance)) {
                        // Token
                            // Token
                            valueStart = pos;
                            // getToken returns the position at the delimeter
                            // or other non-token character
                            valueEnd = getTokenEndPosition(cookiesStr, valueStart, end,
                                versionOneStrictCompliance);
                            // We need pos to advance
                            pos = valueEnd;
                        } else {
                            // INVALID COOKIE, advance to next delimiter
                            // The starting character of the cookie value was
                            // not valid.
                            LOGGER.fine("Invalid cookie. Value not a token or quoted value");
                            while (pos < end && cookiesStr.charAt(pos) != ';'
                                    && cookiesStr.charAt(pos) != ',') {
                                pos++;
                            }

                            pos++;
                            // Make sure no special avpairs can be attributed to
                            // the previous cookie by setting the current cookie
                            // to null
                            cookie = null;
                            continue;
                        }
                }
            } else {
                // Name only cookie
                valueStart = valueEnd = -1;
                pos = nameEnd;

            }

            // We should have an avpair or name-only cookie at this
            // point. Perform some basic checks to make sure we are
            // in a good state.

            // Skip whitespace
            while (pos < end && isWhiteSpace(cookiesStr.charAt(pos))) {
                pos++;
            }

            // Make sure that after the cookie we have a separator. This
            // is only important if this is not the last cookie pair
            while (pos < end && cookiesStr.charAt(pos) != ';' && cookiesStr.charAt(pos) != ',') {
                pos++;
            }

            pos++;

            // All checks passed. Add the cookie, start with the
            // special avpairs first
            if (cookie != null) {
                // Domain is more common, so it goes first
                if (cookie.getDomain() == null &&
                        equalsIgnoreCase("Domain", cookiesStr, nameStart, nameEnd)) {
                    cookie.setDomain(cookiesStr.substring(valueStart, valueEnd));
                    continue;
                }

                // Path
                if (cookie.getPath() == null &&
                        equalsIgnoreCase("Path", cookiesStr, nameStart, nameEnd)) {
                    cookie.setPath(cookiesStr.substring(valueStart, valueEnd));
                    continue;
                }

                // Version
                if (CookieUtils.equals("Version", cookiesStr, nameStart, nameEnd)) {
                    if (rfc6265Enabled) {
                        continue;
                    }
                    // Set version
                    if (cookiesStr.charAt(valueStart) == '1'
                            && valueEnd == (valueStart + 1)) {
                        cookie.setVersion(1);
                    } else {
                        if (!rfc6265Enabled) {
                            cookie.setVersion(0);
                        }
                    }
                    continue;
                }

                // Comment
                if (cookie.getComment() == null &&
                        CookieUtils.equals("Comment", cookiesStr, nameStart, nameEnd)) {
                    cookie.setComment(cookiesStr.substring(valueStart, valueEnd));
                    continue;
                }

                // Max-Age
                if (cookie.getMaxAge() == -1 &&
                        CookieUtils.equals("Max-Age", cookiesStr, nameStart, nameEnd)) {
                    cookie.setMaxAge(Integer.parseInt(
                            cookiesStr.substring(valueStart, valueEnd)));
                    continue;
                }

                // Expires
                if ((cookie.getVersion() == 0 || cookie.isVersionSet()) && cookie.getMaxAge() == -1 &&
                        equalsIgnoreCase("Expires", cookiesStr, nameStart, nameEnd)) {
                    try {
                        valueEnd = getTokenEndPosition(cookiesStr, valueEnd + 1, end, false);
                        pos = valueEnd + 1;
                        final String expiresDate =
                                cookiesStr.substring(valueStart, valueEnd);
                        final Date date = OLD_COOKIE_FORMAT.get().parse(expiresDate);
                        cookie.setMaxAge(getMaxAgeDelta(date.getTime(), System.currentTimeMillis()) / 1000);
                    } catch (ParseException ignore) {
                    }

                    continue;
                }

                // Secure
                if (!cookie.isSecure() &&
                        equalsIgnoreCase("Secure", cookiesStr, nameStart, nameEnd)) {
                    cookie.setSecure(true);
                    continue;
                }

                // HttpOnly
                if (!cookie.isHttpOnly() &&
                        CookieUtils.equals("HttpOnly", cookiesStr, nameStart, nameEnd)) {
                    cookie.setHttpOnly(true);
                    continue;
                }

                if (CookieUtils.equals("Discard", cookiesStr,  nameStart,  nameEnd)) {
                    continue;
                }
            }

            // Normal Cookie
            String name = cookiesStr.substring(nameStart, nameEnd);
            String value;

            if (valueStart != -1) { // Normal AVPair
                if (isQuoted) {
                    // We know this is a byte value so this is safe
                    value = unescapeDoubleQuotes(cookiesStr, valueStart,
                            valueEnd - valueStart);
                } else {
                    value = cookiesStr.substring(valueStart, valueEnd);
                }
            } else {
                // Name Only
                value = "";
            }

            cookie = cookies.getNextUnusedCookie();
            if (!rfc6265Enabled && !cookie.isVersionSet()) {
                cookie.setVersion(0);
            }
            cookie.setName(name);
            cookie.setValue(value);
        }
    }
View Full Code Here

        if (!rfc6265Support && version == 1) {
            buf.append("$Version=\"1\"; ");
        }

        for (int i = 0; i < cookies.length; i++) {
            final Cookie cookie = cookies[i];

            buf.append(cookie.getName());
            buf.append('=');
            // Servlet implementation does not check anything else

            maybeQuote2(version, buf, cookie.getValue(), true, rfc6265Support);

            // If version == 1 - add domain and path
            if (!rfc6265Support && version == 1) {
                // $Domain="domain"
                final String domain = cookie.getDomain();
                if (domain != null) {
                    buf.append("; $Domain=");
                    maybeQuote2(version, buf, domain, versionOneStrictCompliance, rfc6265Support);
                }

                // $Path="path"
                String path = cookie.getPath();
                if (path != null) {
                    buf.append("; $Path=");

                    UEncoder encoder = new UEncoder();
                    encoder.addSafeCharacter('/');
View Full Code Here

        if (version == 1) {
            put(buf, "$Version=\"1\"; ");
        }

        for (int i = 0; i < cookies.length; i++) {
            final Cookie cookie = cookies[i];

            put(buf, cookie.getName());
            put(buf, "=");
            // Servlet implementation does not check anything else

            maybeQuote2(version, buf, cookie.getValue(), true);

            // If version == 1 - add domain and path
            if (version == 1) {
                // $Domain="domain"
                final String domain = cookie.getDomain();
                if (domain != null) {
                    put(buf, "; $Domain=");
                    maybeQuote2(version, buf, domain, versionOneStrictCompliance);
                }

                // $Path="path"
                String path = cookie.getPath();
                if (path != null) {
                    put(buf, "; $Path=");

                    UEncoder encoder = new UEncoder();
                    encoder.addSafeCharacter('/');
View Full Code Here

        int nameEnd;
        int valueStart;
        int valueEnd;
        int version = 0;

        Cookie cookie = null;
        LazyCookieState lazyCookie = null;

        boolean isSpecial;
        boolean isQuoted;

        while (pos < end) {
            isSpecial = false;
            isQuoted = false;

            // Skip whitespace and non-token characters (separators)
            while (pos < end
                    && (isSeparator(buffer.get(pos)) || isWhiteSpace(buffer.get(pos)))) {
                pos++;
            }

            if (pos >= end) {
                return;
            }

            // Detect Special cookies
            if (buffer.get(pos) == '$') {
                isSpecial = true;
                pos++;
            }

            // Get the cookie name. This must be a token
            nameStart = pos;
            pos = nameEnd = getTokenEndPosition(buffer, pos, end);

            // Skip whitespace
            while (pos < end && isWhiteSpace(buffer.get(pos))) {
                pos++;
            }

            // Check for an '=' -- This could also be a name-only
            // cookie at the end of the cookie header, so if we
            // are past the end of the header, but we have a name
            // skip to the name-only part.
            if (pos < end && buffer.get(pos) == '=') {

                // Skip whitespace
                do {
                    pos++;
                } while (pos < end && isWhiteSpace(buffer.get(pos)));

                if (pos >= end) {
                    return;
                }

                // Determine what type of value this is, quoted value,
                // token, name-only with an '=', or other (bad)
                switch (buffer.get(pos)) {
                    case '"':
                        // Quoted Value
                        isQuoted = true;
                        valueStart = pos + 1; // strip "
                        // getQuotedValue returns the position before
                        // at the last qoute. This must be dealt with
                        // when the bytes are copied into the cookie
                        valueEnd = getQuotedValueEndPosition(buffer,
                                valueStart, end);
                        // We need pos to advance
                        pos = valueEnd;
                        // Handles cases where the quoted value is
                        // unterminated and at the end of the header,
                        // e.g. [myname="value]
                        if (pos >= end) {
                            return;
                        }
                        break;
                    case ';':
                    case ',':
                        // Name-only cookie with an '=' after the name token
                        // This may not be RFC compliant
                        valueStart = valueEnd = -1;
                        // The position is OK (On a delimiter)
                        break;
                    default:
                        if (!isSeparator(buffer.get(pos), versionOneStrictCompliance)) {
                        // Token
                            // Token
                            valueStart = pos;
                            // getToken returns the position at the delimeter
                            // or other non-token character
                            valueEnd = getTokenEndPosition(buffer, valueStart, end,
                                versionOneStrictCompliance);
                            // We need pos to advance
                            pos = valueEnd;
                        } else {
                            // INVALID COOKIE, advance to next delimiter
                            // The starting character of the cookie value was
                            // not valid.
                            LOGGER.fine("Invalid cookie. Value not a token or quoted value");
                            while (pos < end && buffer.get(pos) != ';'
                                    && buffer.get(pos) != ',') {
                                pos++;
                            }

                            pos++;
                            // Make sure no special avpairs can be attributed to
                            // the previous cookie by setting the current cookie
                            // to null
                            cookie = null;
                            lazyCookie = null;
                            continue;
                        }
                }
            } else {
                // Name only cookie
                valueStart = valueEnd = -1;
                pos = nameEnd;

            }

            // We should have an avpair or name-only cookie at this
            // point. Perform some basic checks to make sure we are
            // in a good state.

            // Skip whitespace
            while (pos < end && isWhiteSpace(buffer.get(pos))) {
                pos++;
            }

            // Make sure that after the cookie we have a separator. This
            // is only important if this is not the last cookie pair
            while (pos < end && buffer.get(pos) != ';' && buffer.get(pos) != ',') {
                pos++;
            }

            pos++;

            // All checks passed. Add the cookie, start with the
            // special avpairs first
            if (isSpecial) {
                isSpecial = false;
                // $Version must be the first avpair in the cookie header
                // (sc must be null)
                if (CookieUtils.equals("Version", buffer, nameStart, nameEnd)
                        && cookie == null) {
                    if (rfc6265Enabled) {
                        continue;
                    }
                    // Set version
                    if (buffer.get(valueStart) == '1'
                            && valueEnd == (valueStart + 1)) {
                        version = 1;
                    } else {
                        // unknown version (Versioning is not very strict)
                    }
                    continue;
                }

                // We need an active cookie for Path/Port/etc.
                if (cookie == null) {
                    continue;
                }

                // Domain is more common, so it goes first
                if (CookieUtils.equals("Domain", buffer, nameStart, nameEnd)) {
                    lazyCookie.getDomain().setBuffer(buffer,
                            valueStart, valueEnd);
                    continue;
                }

                if (CookieUtils.equals("Path", buffer, nameStart, nameEnd)) {
                    lazyCookie.getPath().setBuffer(buffer,
                            valueStart, valueEnd);
                    continue;
                }


//                if (CookieUtils.equals("Port", buffer, nameStart, nameEnd)) {
//                    // sc.getPort is not currently implemented.
//                    // sc.getPort().setBytes( bytes,
//                    //                        valueStart,
//                    //                        valueEnd-valueStart );
//                    continue;
//                }

                // Unknown cookie, complain
                LOGGER.fine("Unknown Special Cookie");

            } else { // Normal Cookie
                cookie = cookies.getNextUnusedCookie();
                lazyCookie = cookie.getLazyCookieState();
                if (!rfc6265Enabled && !cookie.isVersionSet()) {
                    cookie.setVersion(version);
                }
                lazyCookie.getName().setBuffer(buffer, nameStart, nameEnd);

                if (valueStart != -1) { // Normal AVPair
                    lazyCookie.getValue().setBuffer(buffer, valueStart, valueEnd);
View Full Code Here

        int nameEnd;
        int valueStart;
        int valueEnd;
        int version = 0;

        Cookie cookie = null;
        LazyCookieState lazyCookie = null;

        boolean isSpecial;
        boolean isQuoted;

        while (pos < end) {
            isSpecial = false;
            isQuoted = false;

            // Skip whitespace and non-token characters (separators)
            while (pos < end
                    && (isSeparator(bytes[pos]) || isWhiteSpace(bytes[pos]))) {
                pos++;
            }

            if (pos >= end) {
                return;
            }

            // Detect Special cookies
            if (bytes[pos] == '$') {
                isSpecial = true;
                pos++;
            }

            // Get the cookie name. This must be a token
            nameStart = pos;
            pos = nameEnd = getTokenEndPosition(bytes, pos, end);

            // Skip whitespace
            while (pos < end && isWhiteSpace(bytes[pos])) {
                pos++;
            }

            // Check for an '=' -- This could also be a name-only
            // cookie at the end of the cookie header, so if we
            // are past the end of the header, but we have a name
            // skip to the name-only part.
            if (pos < end && bytes[pos] == '=') {

                // Skip whitespace
                do {
                    pos++;
                } while (pos < end && isWhiteSpace(bytes[pos]));

                if (pos >= end) {
                    return;
                }

                // Determine what type of value this is, quoted value,
                // token, name-only with an '=', or other (bad)
                switch (bytes[pos]) {
                    case '"':
                        // Quoted Value
                        isQuoted = true;
                        valueStart = pos + 1; // strip "
                        // getQuotedValue returns the position before
                        // at the last qoute. This must be dealt with
                        // when the bytes are copied into the cookie
                        valueEnd = getQuotedValueEndPosition(bytes,
                                valueStart, end);
                        // We need pos to advance
                        pos = valueEnd;
                        // Handles cases where the quoted value is
                        // unterminated and at the end of the header,
                        // e.g. [myname="value]
                        if (pos >= end) {
                            return;
                        }
                        break;
                    case ';':
                    case ',':
                        // Name-only cookie with an '=' after the name token
                        // This may not be RFC compliant
                        valueStart = valueEnd = -1;
                        // The position is OK (On a delimiter)
                        break;
                    default:
                        if (!isSeparator(bytes[pos], versionOneStrictCompliance)) {
                            // Token
                            // Token
                            valueStart = pos;
                            // getToken returns the position at the delimeter
                            // or other non-token character
                            valueEnd = getTokenEndPosition(bytes, valueStart, end,
                                    versionOneStrictCompliance);
                            // We need pos to advance
                            pos = valueEnd;
                        } else {
                            // INVALID COOKIE, advance to next delimiter
                            // The starting character of the cookie value was
                            // not valid.
                            LOGGER.fine("Invalid cookie. Value not a token or quoted value");
                            while (pos < end && bytes[pos] != ';'
                                    && bytes[pos] != ',') {
                                pos++;
                            }

                            pos++;
                            // Make sure no special avpairs can be attributed to
                            // the previous cookie by setting the current cookie
                            // to null
                            cookie = null;
                            lazyCookie = null;
                            continue;
                        }
                }
            } else {
                // Name only cookie
                valueStart = valueEnd = -1;
                pos = nameEnd;

            }

            // We should have an avpair or name-only cookie at this
            // point. Perform some basic checks to make sure we are
            // in a good state.

            // Skip whitespace
            while (pos < end && isWhiteSpace(bytes[pos])) {
                pos++;
            }

            // Make sure that after the cookie we have a separator. This
            // is only important if this is not the last cookie pair
            while (pos < end && bytes[pos] != ';' && bytes[pos] != ',') {
                pos++;
            }

            pos++;

            // All checks passed. Add the cookie, start with the
            // special avpairs first
            if (isSpecial) {
                isSpecial = false;
                // $Version must be the first avpair in the cookie header
                // (sc must be null)
                if (CookieUtils.equals("Version", bytes, nameStart, nameEnd)
                        && cookie == null) {
                    if (rfc6265Enabled) {
                        continue;
                    }
                    // Set version
                    if (bytes[valueStart] == '1'
                            && valueEnd == (valueStart + 1)) {
                        version = 1;
                    } else {
                        // unknown version (Versioning is not very strict)
                    }
                    continue;
                }

                // We need an active cookie for Path/Port/etc.
                if (cookie == null) {
                    continue;
                }

                // Domain is more common, so it goes first
                if (CookieUtils.equals("Domain", bytes, nameStart, nameEnd)) {
                    lazyCookie.getDomain().setBytes(bytes,
                            valueStart, valueEnd);
                    continue;
                }

                if (CookieUtils.equals("Path", bytes, nameStart, nameEnd)) {
                    lazyCookie.getPath().setBytes(bytes,
                            valueStart, valueEnd);
                    continue;
                }

                // Unknown cookie, complain
                LOGGER.fine("Unknown Special Cookie");

            } else { // Normal Cookie
                cookie = cookies.getNextUnusedCookie();
                lazyCookie = cookie.getLazyCookieState();
                if (!rfc6265Enabled && !cookie.isVersionSet()) {
                    cookie.setVersion(version);
                }
                lazyCookie.getName().setBytes(bytes, nameStart, nameEnd);

                if (valueStart != -1) { // Normal AVPair
                    lazyCookie.getValue().setBytes(bytes, valueStart, valueEnd);
View Full Code Here

TOP

Related Classes of org.glassfish.grizzly.http.Cookie

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.