// twin parsing method, different enough that it's better to split it into
// a different method
internal Cookie GetServer() {
Cookie cookie = m_savedCookie;
m_savedCookie = null;
// only first ocurence of an attribute value must be counted
bool domainSet = false;
bool pathSet = false;
bool portSet = false; //special case as it may have no value in header
do {
bool first = cookie==null || cookie.Name==null || cookie.Name.Length==0;
CookieToken token = m_tokenizer.Next(first, false);
if (first && (token==CookieToken.NameValuePair || token==CookieToken.Attribute)) {
if (cookie==null) {
cookie = new Cookie();
}
if (cookie.InternalSetName(m_tokenizer.Name) == false) {
//will be rejected
cookie.InternalSetName(string.Empty);
}
cookie.Value = m_tokenizer.Value;
}
else {
switch (token) {
case CookieToken.NameValuePair:
switch (m_tokenizer.Token) {
case CookieToken.Domain:
if (!domainSet) {
domainSet = true;
cookie.Domain = CheckQuoted(m_tokenizer.Value);
cookie.IsQuotedDomain = m_tokenizer.Quoted;
}
break;
case CookieToken.Path:
if (!pathSet) {
pathSet = true;
cookie.Path = m_tokenizer.Value;
}
break;
case CookieToken.Port:
if (!portSet) {
portSet = true;
try {
cookie.Port = m_tokenizer.Value;
}
catch (CookieException) {
//this cookie will be rejected
cookie.InternalSetName(string.Empty);
}
}
break;
case CookieToken.Version:
// this is a new cookie, this token is for the next cookie.
m_savedCookie = new Cookie();
int parsed;
if (int.TryParse(m_tokenizer.Value, out parsed)) {
m_savedCookie.Version = parsed;
}
return cookie;
case CookieToken.Unknown:
// this is a new cookie, the token is for the next cookie.
m_savedCookie = new Cookie();
if (m_savedCookie.InternalSetName(m_tokenizer.Name) == false) {
//will be rejected
m_savedCookie.InternalSetName(string.Empty);
}
m_savedCookie.Value = m_tokenizer.Value;
return cookie;
}
break;
case CookieToken.Attribute:
switch (m_tokenizer.Token) {
case CookieToken.Port:
if (!portSet) {
portSet = true;
cookie.Port = string.Empty;
}
break;
}
break;
}
}
} while (!m_tokenizer.Eof && !m_tokenizer.EndOfCookie);
return cookie;
}