/* */ #include "CookieParser.h" #include "Util.h" void CookieParser::setField(Cookie& cookie, const string& name, const string& value) const { if(name.size() == string("secure").size() && strcasecmp(name.c_str(), "secure") == 0) { cookie.secure = true; } else if(name.size() == string("domain").size() && strcasecmp(name.c_str(), "domain") == 0) { cookie.domain = value; } else if(name.size() == string("path").size() && strcasecmp(name.c_str(), "path") == 0) { cookie.path = value; } else if(name.size() == string("expires").size() && strcasecmp(name.c_str(), "expires") == 0) { cookie.expires = Util::httpGMT(value); cookie.onetime = false; } else { cookie.name = name; cookie.value = value; } } Cookie CookieParser::parse(const string& cookieStr) const { return parse(cookieStr, "", ""); } Cookie CookieParser::parse(const string& cookieStr, const string& defaultDomain, const string& defaultPath) const { Cookie cookie; cookie.domain = defaultDomain; cookie.path = defaultPath; Strings terms; Util::slice(terms, Util::trim(cookieStr), ';', true); for(Strings::iterator itr = terms.begin(); itr != terms.end(); itr++) { pair nv; Util::split(nv, *itr, '='); setField(cookie, nv.first, nv.second); } return cookie; } Cookies CookieParser::parse(istream& s) const { Cookies cookies; string line; while(getline(s, line)) { if(Util::trim(line) == "" || Util::startsWith(line, "#")) { continue; } Cookie cookie = parse(line); if(cookie.good()) { cookies.push_back(cookie); } } return cookies; }