// // Eat a cookie out of the jar. // cookiePtr should be one of the cookies returned by getCookieList() // void KCookieJar::eatCookie(KHttpCookiePtr cookiePtr) { QString domain = stripDomain(cookiePtr); // We file the cookie under this domain. KHttpCookieList *cookieList = m_cookieDomains[domain]; if (cookieList) { // This deletes cookiePtr! if (cookieList->removeRef( cookiePtr )) m_cookiesChanged = true; if ((cookieList->isEmpty()) && (cookieList->getAdvice() == KCookieDunno)) { // This deletes cookieList! m_cookieDomains.remove(domain); m_domainList.remove(domain); } } }
void KCookieJar::eatSessionCookies( const QString& fqdn, long windowId, bool isFQDN ) { KHttpCookieList* cookieList; if ( !isFQDN ) cookieList = m_cookieDomains[fqdn]; else { QString domain; stripDomain( fqdn, domain ); cookieList = m_cookieDomains[domain]; } if ( cookieList ) { KHttpCookiePtr cookie=cookieList->first(); for (; cookie != 0;) { if ((cookie->expireDate() != 0) && !m_ignoreCookieExpirationDate) { cookie = cookieList->next(); continue; } QValueList<long> &ids = cookie->windowIds(); if (!ids.remove(windowId) || !ids.isEmpty()) { cookie = cookieList->next(); continue; } KHttpCookiePtr old_cookie = cookie; cookie = cookieList->next(); cookieList->removeRef( old_cookie ); } } }
// // Saves all cookies to the file '_filename'. // On succes 'true' is returned. // On failure 'false' is returned. bool KCookieJar::saveCookies(const QString &_filename) { KSaveFile saveFile(_filename, 0600); if (saveFile.status() != 0) return false; FILE *fStream = saveFile.fstream(); time_t curTime = time(0); fprintf(fStream, "# KDE Cookie File v2\n#\n"); fprintf(fStream, "%-20s %-20s %-12s %-10s %-4s %-20s %-4s %s\n", "# Host", "Domain", "Path", "Exp.date", "Prot", "Name", "Sec", "Value"); for ( QStringList::Iterator it=m_domainList.begin(); it != m_domainList.end(); it++ ) { const QString &domain = *it; bool domainPrinted = false; KHttpCookieList *cookieList = m_cookieDomains[domain]; KHttpCookiePtr cookie=cookieList->last(); for (; cookie != 0;) { if (cookie->isExpired(curTime)) { // Delete expired cookies KHttpCookiePtr old_cookie = cookie; cookie = cookieList->prev(); cookieList->removeRef( old_cookie ); } else if (cookie->expireDate() != 0 && !m_ignoreCookieExpirationDate) { if (!domainPrinted) { domainPrinted = true; fprintf(fStream, "[%s]\n", domain.local8Bit().data()); } // Store persistent cookies QString path = L1("\""); path += cookie->path(); path += '"'; QString domain = L1("\""); domain += cookie->domain(); domain += '"'; fprintf(fStream, "%-20s %-20s %-12s %10lu %3d %-20s %-4i %s\n", cookie->host().latin1(), domain.latin1(), path.latin1(), (unsigned long) cookie->expireDate(), cookie->protocolVersion(), cookie->name().isEmpty() ? cookie->value().latin1() : cookie->name().latin1(), (cookie->isSecure() ? 1 : 0) + (cookie->isHttpOnly() ? 2 : 0) + (cookie->hasExplicitPath() ? 4 : 0) + (cookie->name().isEmpty() ? 8 : 0), cookie->value().latin1()); cookie = cookieList->prev(); } else { // Skip session-only cookies cookie = cookieList->prev(); } } } return saveFile.close(); }