void Cookies::clear(Cookie & cookie) { cookie.setValue("deleted"); cookie.setExpires(QDateTime::fromTime_t(0)); m_cookies[cookie.getName()] = cookie; }
void CookieJar::addCookieHeader( const std::string &domain_in, const std::string &path_in, const std::string &request) { // printf("addCookieHeader: %s \nFrom: %s\n",request.c_str(), domain_in.c_str()); std::string path(path_in); std::string domain(domain_in); bool secure(false); string lowCaseRequest(request); transform(lowCaseRequest.begin(), lowCaseRequest.end(), lowCaseRequest.begin(), ::tolower); ParameterSet paramSet(request); ParameterSet lowCaseParamSet(lowCaseRequest); if ( lowCaseParamSet.hasParameter(PATH_STR) ) { lowCaseParamSet.parameter(PATH_STR, path); } else { // sort out basename if (path.length() and path[path.length()-1] != '/') { path = nds::File::dirname(path.c_str()); } } if ( lowCaseParamSet.hasParameter(DOMAIN_STR) ) { lowCaseParamSet.parameter(DOMAIN_STR, domain); // reject domains that do not start with a dot /*if (domain[0] != '.') { printf("Reject cookie for %s\n", domain.c_str()); return; }*/ } if ( lowCaseParamSet.hasParameter(SECURE_STR) ) { secure = true; } int expires = -1; if (lowCaseParamSet.hasParameter(EXPIRES_STR)) { std::string expval; const KeyValueMap & keyValueMap(paramSet.keyValueMap()); for (KeyValueMap::const_iterator it(keyValueMap.begin()); it != keyValueMap.end(); ++it) { string name = it->first; string lowCaseName(name); transform(lowCaseName.begin(), lowCaseName.end(), lowCaseName.begin(), ::tolower); if (lowCaseName == EXPIRES_STR) { expires = DateUtils::parseDate(it->second.c_str()); break; } } } const KeyValueMap & keyValueMap(paramSet.keyValueMap()); for (KeyValueMap::const_iterator it(keyValueMap.begin()); it != keyValueMap.end(); ++it) { string name = it->first; string lowCaseName(name); transform(lowCaseName.begin(), lowCaseName.end(), lowCaseName.begin(), ::tolower); if (lowCaseName == PATH_STR or lowCaseName == SECURE_STR or lowCaseName == HTTP_ONLY or lowCaseName == EXPIRES_STR or lowCaseName == DOMAIN_STR) { continue; } Cookie * existingCookie = hasCookieForDomain(domain, name); const string &value(it->second); if (existingCookie != 0) { // replace with new value existingCookie->setValue(value); existingCookie->setExpires(expires); existingCookie->setSaved(false); } else { Cookie * cookie(new Cookie(name, value, domain, path, expires, secure)); m_cookies.push_back(cookie); } } }