CookieTokenizerpublic class CookieTokenizer extends Object Parse a Cookie: header into individual tokens according to RFC 2109. |
Fields Summary |
---|
private static final int | MAX_COOKIE_TOKENSUpper bound on the number of cookie tokens to accept. The limit is
based on the 4 different attributes (4.3.4) and the 20 cookie minimum
(6.3) given in RFC 2109 multiplied by 2 to accomodate the 2 tokens in
each name=value pair ("JSESSIONID=1234" is 2 tokens). | private String[] | tokensArray of cookie tokens. Even indices contain name tokens while odd
indices contain value tokens (or null). | private int | numTokensNumber of cookie tokens currently in the tokens[] array. |
Methods Summary |
---|
public int | getNumTokens()Return the number of cookie tokens parsed from the Cookie: header.
return numTokens;
| private int | parseNameValue(java.lang.String cookies, int beginIndex)Parse a name=value pair from the Cookie: header.
int length = cookies.length();
int index = beginIndex;
while (index < length) {
switch (cookies.charAt(index)) {
case ';":
case ',":
// Found end of name token without value
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
if (tokens[numTokens].length() > 0) {
numTokens++;
tokens[numTokens] = null;
numTokens++;
}
return index + 1;
case '=":
// Found end of name token with value
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
numTokens++;
return parseValue(cookies, index + 1);
case '"":
// Skip past quoted span
do index++; while (cookies.charAt(index) != '"");
break;
}
index++;
}
if (index > beginIndex) {
// Found end of name token without value
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
if (tokens[numTokens].length() > 0) {
numTokens++;
tokens[numTokens] = null;
numTokens++;
}
}
return index;
| private int | parseValue(java.lang.String cookies, int beginIndex)Parse the value token from a name=value pair.
int length = cookies.length();
int index = beginIndex;
while (index < length) {
switch (cookies.charAt(index)) {
case ';":
case ',":
// Found end of value token
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
numTokens++;
return index + 1;
case '"":
// Skip past quoted span
do index++; while (cookies.charAt(index) != '"");
break;
}
index++;
}
// Found end of value token
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
numTokens++;
return index;
| public java.lang.String | tokenAt(int index)Returns a given cookie token from the Cookie: header.
return tokens[index];
| public int | tokenize(java.lang.String cookies)Parse the name=value tokens from a Cookie: header.
numTokens = 0;
if (cookies != null) {
try {
// Advance through cookies, parsing name=value pairs
int length = cookies.length();
int index = 0;
while (index < length)
index = parseNameValue(cookies, index);
}
catch (ArrayIndexOutOfBoundsException e) {
// Filled up the tokens[] array
}
catch (IndexOutOfBoundsException e) {
// Walked off the end of the cookies header
}
}
return numTokens;
|
|