Class CookieParser
cookie = "Cookie:" cookie-version 1*((";" | ",") cookie-value) cookie-value = NAME "=" VALUE [";" path] [";" domain] cookie-version = "$Version" "=" value NAME = attr VALUE = value path = "$Path" "=" value domain = "$Domain" "=" valueThe cookie header may consist of several cookies. Each cookie can be extracted from the header by examining the it syntax of the cookie header. The syntax of the cookie header is defined in RFC 2109.
Each cookie has a $Version
attribute followed by multiple
cookies. Each contains a name and a value, followed by an optional
$Path
and $Domain
attribute. This will parse
a given cookie header and return each cookie extracted as a
Cookie
object.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
This is used to represent anIterator
that will iterate over the available cookies within the provided source text.private class
This is a token object that is used to store the offset and length of a region of chars in theCookieParser.buf
array. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate CookieParser.Token
Used to store the$Domain
values.private boolean
Determines when theParser
has finished.private CookieParser.Token
Used to store the name of theCookie
.private boolean
Used so theParser
does not parse twice.private CookieParser.Token
Used to store the$Path
values.private CookieParser.Token
Used to store the value of theCookie
.private int
Version of theCookie
being parsed. -
Constructor Summary
ConstructorsConstructorDescriptionCreate aCookieParser
that contains no cookies.CookieParser
(String header) This is primarily a convineance constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
cookie()
This is used to parse aCookie
from the buffer that contains theCookie
values.private void
data()
This initializes the value token and extracts the value of thisCookie
.private void
domain()
Initializes the domain token and extracts the$Domain
of thisCookie
.private Cookie
Creates theCookie
from the token objects.private Cookie
Creates theCookie
from the token objects.protected void
init()
Resets the cookie and the buffer variables for thisCookieParser
.iterator()
This is used to acquire the cookie values from the provided the provided source text.private void
name()
This initializes the name token and extracts the name of thisCookie
.protected void
parse()
This will extract the nextCookie
from the buffer.private void
path()
This initializes the path token and extracts the$Path
of thisCookie
.void
reset()
This is used so that the collection ofCookies
can be reiterated.protected boolean
This is used to skip an arbitraryString
within thechar
buf.private boolean
terminal
(char ch) This is used to determine if a given iso8859-1 character is a terminal character.private void
value()
Used to extract everything found after theNAME '='
within aCookie
.private void
version()
This extracts the$Version
of thisCookie
.Methods inherited from class org.simpleframework.common.parse.Parser
digit, ensureCapacity, parse, space, toLower
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
finished
private boolean finishedDetermines when theParser
has finished. -
parsed
private boolean parsedUsed so theParser
does not parse twice. -
version
private int versionVersion of theCookie
being parsed. -
name
Used to store the name of theCookie
. -
value
Used to store the value of theCookie
. -
path
Used to store the$Path
values. -
domain
Used to store the$Domain
values.
-
-
Constructor Details
-
CookieParser
public CookieParser()Create aCookieParser
that contains no cookies. the instance will returnfalse
for thehasNext
method. cookies may be parsed using this instance by using theparse
method. -
CookieParser
This is primarily a convineance constructor. This will parse theString
given to extract the cookies. This could be achived by calling the default no-arg constructor and then using the instance to invoke theparse
method on thatString
.- Parameters:
header
- aString
containing a cookie value
-
-
Method Details
-
init
protected void init()Resets the cookie and the buffer variables for thisCookieParser
. It is used to set the state of the parser to start parsing a new cookie. -
parse
protected void parse()This will extract the nextCookie
from the buffer. If all the characters in the buffer have already been examined then this method will simply do nothing. Otherwise this will parse the remainder of the buffer and (if it follows RFC 2109) produce aCookie
. -
skip
This is used to skip an arbitraryString
within thechar
buf. It checks the length of theString
first to ensure that it will not go out of bounds. A comparison is then made with the buffers contents and theString
if the reigon in the buffer matched theString
then the offset within the buffer is increased by theString
's length so that it has effectively skipped it.This
skip
method will ignore all of the whitespace text. This will also skip trailing spaces within the the input text and all spaces within the source text. For example if the input was the string "s omete xt" and the source was "some text to skip" then the result of a skip ignoring spaces would be "to skip" in the source string, as the trailing spaces are also eaten by this. -
iterator
This is used to acquire the cookie values from the provided the provided source text. This allows the cookie parser to be used within a for each loop to parse out the values of a cookie one by one so that they may be used or stored. -
reset
public void reset()This is used so that the collection ofCookies
can be reiterated. This allows the collection to be reused. Thereset
method will invoke the super classesinit
method. This will reinitialize thisParser
so the cookie will be reparsed. -
getCookie
Creates theCookie
from the token objects. It is assumed that theCookie
String
has been parsed when this is called. This should only be used after theparse
method has been called.If there is no
$Domain
or$Path
within theCookie
String
then thegetDomain
andgetPath
are null.- Returns:
- the
Cookie
that was just parsed
-
getCookie
Creates theCookie
from the token objects. It is assumed that theCookie
String
has been parsed when this is called. This should only be used after theparse
method has been called.If there is no
$Domain
or$Path
within theCookie
String
then thegetDomain
andgetPath
are null.- Parameters:
name
- the name that theCookie
containsvalue
- the value that theCookie
contains- Returns:
- the
Cookie
that was just parsed
-
cookie
private void cookie()This is used to parse aCookie
from the buffer that contains theCookie
values. This will first try to remove any trailing value after the version/prevCookie
once this is removed it will extract the name/value pair from theCookie
. The name and value of theCookie
will be saved by the name and value tokens. -
name
private void name()This initializes the name token and extracts the name of thisCookie
. The offset and length of the name will be saved in the name token. This will read allchar
's upto but excluding the first '='char
encountered from theoff
within the buffer. -
value
private void value()Used to extract everything found after theNAME '='
within aCookie
. This extracts theCookie
value the$Path
and$Domain
attributes if they exist (i.e.$Path
and$Domain
are optional in a cookie see RFC 2109).The path method reads the terminal found before it as does the
domain
method that is ";$Path" is read as the first part of the path method. This is because if there is no path the parser should not read data it does not know belongs to a specific part of theCookie
. -
data
private void data()This initializes the value token and extracts the value of thisCookie
. The offset and length of the value will be saved in the value token. This will read allchar
's upto but excluding the first terminal char encountered from the off within the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character that is '\'). -
path
private void path()This initializes the path token and extracts the$Path
of thisCookie
. The offset and length of the path will be saved in the path token. This will read allchar
's up to but excluding the first terminalchar
encountered from theoff
within the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character, that is '\').This reads the terminal before the
$Path
so that if there is no$Path
for theCookie
then the character before it will not be read needlessly. -
domain
private void domain()Initializes the domain token and extracts the$Domain
of thisCookie
. The offset and length of the domain will be saved in the path token. This will read all characters up to but excluding the first terminalchar
encountered from the off within the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character, that is '\').This reads the terminal before the
$Domain
so that if there is no$Domain
for theCookie
then the character before it will not be read needlessly. -
version
private void version()This extracts the$Version
of thisCookie
. The version is parsed and converted into a decimal int from the digit characters that make up a version.This will read all digit
char
's up to but excluding the first non digitchar
that it encounters from the offset within the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character i.e. '\'). -
terminal
private boolean terminal(char ch) This is used to determine if a given iso8859-1 character is a terminal character. That is either the ';' or ',' characters. Although the RFC 2109 says the terminal can be either a comma, it is not used by any browsers.- Parameters:
ch
- the character that is to be compared- Returns:
- true if this is a semicolon character
-