kcookiejar.cpp

来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,549 行 · 第 1/3 页

CPP
1,549
字号
       }       domains.remove(it);       it = domains.begin(); // Continue from begin of remaining list    }    if (advice == KCookieDunno)        advice = m_globalAdvice;    return advice;}//// This function gets the advice for all cookies originating from// _domain.//KCookieAdvice KCookieJar::getDomainAdvice(const QString &_domain){    KHttpCookieList *cookieList = m_cookieDomains[_domain];    KCookieAdvice advice;    if (cookieList)    {        advice = cookieList->getAdvice();    }    else    {        advice = KCookieDunno;    }    return advice;}//// This function sets the advice for all cookies originating from// _domain.//void KCookieJar::setDomainAdvice(const QString &_domain, KCookieAdvice _advice){    QString domain(_domain);    KHttpCookieList *cookieList = m_cookieDomains[domain];    if (cookieList)    {        if (cookieList->getAdvice() != _advice)        {           m_configChanged = true;           // domain is already known           cookieList->setAdvice( _advice);        }        if ((cookieList->isEmpty()) &&            (_advice == KCookieDunno))        {            // This deletes cookieList!            m_cookieDomains.remove(domain);            m_domainList.remove(domain);        }    }    else    {        // domain is not yet known        if (_advice != KCookieDunno)        {            // We should create a domain entry            m_configChanged = true;            // Make a new cookie list            cookieList = new KHttpCookieList();            cookieList->setAutoDelete(true);            cookieList->setAdvice( _advice);            m_cookieDomains.insert( domain, cookieList);            // Update the list of domains            m_domainList.append( domain);        }    }}//// This function sets the advice for all cookies originating from// the same domain as _cookie//void KCookieJar::setDomainAdvice(KHttpCookiePtr cookiePtr, KCookieAdvice _advice){    QString domain;    stripDomain(cookiePtr->host(), domain); // We file the cookie under this domain.    setDomainAdvice(domain, _advice);}//// This function sets the global advice for cookies//void KCookieJar::setGlobalAdvice(KCookieAdvice _advice){    if (m_globalAdvice != _advice)       m_configChanged = true;    m_globalAdvice = _advice;}//// Get a list of all domains known to the cookie jar.//const QStringList& KCookieJar::getDomainList(){    return m_domainList;}//// Get a list of all cookies in the cookie jar originating from _domain.//const KHttpCookieList *KCookieJar::getCookieList(const QString & _domain,                                                 const QString & _fqdn ){    QString domain;    if (_domain.isEmpty())        stripDomain( _fqdn, domain );    else        domain = _domain;    return m_cookieDomains[domain];}//// 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::eatCookiesForDomain(const QString &domain){   KHttpCookieList *cookieList = m_cookieDomains[domain];   if (!cookieList || cookieList->isEmpty()) return;   cookieList->clear();   if (cookieList->getAdvice() == KCookieDunno)   {       // This deletes cookieList!       m_cookieDomains.remove(domain);       m_domainList.remove(domain);   }   m_cookiesChanged = true;}void KCookieJar::eatSessionCookies( long windowId ){    if (!windowId)        return;    QStringList::Iterator it=m_domainList.begin();    for ( ; it != m_domainList.end(); ++it )        eatSessionCookies( *it, windowId, false );}void KCookieJar::eatAllCookies(){    for ( QStringList::Iterator it=m_domainList.begin();          it != m_domainList.end();)    {        QString domain = *it++;        // This might remove domain from domainList!        eatCookiesForDomain(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();}typedef char *charPtr;static const char *parseField(charPtr &buffer, bool keepQuotes=false){    char *result;    if (!keepQuotes && (*buffer == '\"'))    {        // Find terminating "        buffer++;        result = buffer;        while((*buffer != '\"') && (*buffer))            buffer++;    }    else    {        // Find first white space        result = buffer;        while((*buffer != ' ') && (*buffer != '\t') && (*buffer != '\n') && (*buffer))            buffer++;    }    if (!*buffer)        return result; //    *buffer++ = '\0';    // Skip white-space    while((*buffer == ' ') || (*buffer == '\t') || (*buffer == '\n'))        buffer++;    return result;}//// Reloads all cookies from the file '_filename'.// On succes 'true' is returned.// On failure 'false' is returned.bool KCookieJar::loadCookies(const QString &_filename){    FILE *fStream = fopen( QFile::encodeName(_filename), "r");    if (fStream == 0)    {        return false;    }    time_t curTime = time(0);    char *buffer = new char[READ_BUFFER_SIZE];    bool err = false;    err = (fgets(buffer, READ_BUFFER_SIZE, fStream) == 0);    int version = 1;    if (!err)    {        if (strcmp(buffer, "# KDE Cookie File\n") == 0)        {          // version 1        }        else if (sscanf(buffer, "# KDE Cookie File v%d\n", &version) != 1)        {          err = true;        }    }    if (!err)    {        while(fgets(buffer, READ_BUFFER_SIZE, fStream) != 0)        {            char *line = buffer;            // Skip lines which begin with '#' or '['            if ((line[0] == '#') || (line[0] == '['))                continue;            const char *host( parseField(line) );            const char *domain( parseField(line) );            const char *path( parseField(line) );            const char *expStr( parseField(line) );            if (!expStr) continue;            int expDate  = (time_t) strtoul(expStr, 0, 10);            const char *verStr( parseField(line) );            if (!verStr) continue;            int protVer  = (time_t) strtoul(verStr, 0, 10);            const char *name( parseField(line) );            bool keepQuotes = false;            bool secure = false;            bool httpOnly = false;            bool explicitPath = false;            const char *value = 0;            if ((version == 2) || (protVer >= 200))            {                if (protVer >= 200)                    protVer -= 200;                int i = atoi( parseField(line) );                secure = i & 1;                httpOnly = i & 2;                explicitPath = i & 4;                if (i & 8)                   name = "";                line[strlen(line)-1] = '\0'; // Strip LF.                value = line;            }            else            {                if (protVer >= 100)                {                    protVer -= 100;                    keepQuotes = true;                }                value = parseField(line, keepQuotes);                secure = atoi( parseField(line) );            }            // Parse error            if (!value) continue;            // Expired or parse error            if ((expDate == 0) || (expDate < curTime))                continue;            KHttpCookie *cookie = new KHttpCookie(QString::fromLatin1(host),                                                  QString::fromLatin1(domain),                                                   QString::fromLatin1(path),                                                   QString::fromLatin1(name),                                                  QString::fromLatin1(value),                                                   expDate, protVer,                                                  secure, httpOnly, explicitPath);            addCookie(cookie);        }    }    delete [] buffer;    m_cookiesChanged = false;    fclose( fStream);    return err;}//// Save the cookie configuration//void KCookieJar::saveConfig(KConfig *_config){    if (!m_configChanged)        return;    _config->setGroup("Cookie Dialog");    _config->writeEntry("PreferredPolicy", m_preferredPolicy);    _config->writeEntry("ShowCookieDetails", m_showCookieDetails );    _config->setGroup("Cookie Policy");    _config->writeEntry("CookieGlobalAdvice", adviceToStr( m_globalAdvice));    QStringList domainSettings;    for ( QStringList::Iterator it=m_domainList.begin();          it != m_domainList.end();          it++ )    {         const QString &domain = *it;         KCookieAdvice advice = getDomainAdvice( domain);         if (advice != KCookieDunno)         {             QString value(domain);             value += ':';             value += adviceToStr(advice);             domainSettings.append(value);         }    }    _config->writeEntry("CookieDomainAdvice", domainSettings);    _config->sync();    m_configChanged = false;}//// Load the cookie configuration//void KCookieJar::loadConfig(KConfig *_config, bool reparse ){    if ( reparse )        _config->reparseConfiguration();    _config->setGroup("Cookie Dialog");    m_showCookieDetails = _config->readBoolEntry( "ShowCookieDetails" );    m_preferredPolicy = _config->readNumEntry( "PreferredPolicy", 0 );    _config->setGroup("Cookie Policy");    QStringList domainSettings = _config->readListEntry("CookieDomainAdvice");    m_rejectCrossDomainCookies = _config->readBoolEntry( "RejectCrossDomainCookies", true );    m_autoAcceptSessionCookies = _config->readBoolEntry( "AcceptSessionCookies", true );    m_ignoreCookieExpirationDate = _config->readBoolEntry( "IgnoreExpirationDate", false );    QString value = _config->readEntry("CookieGlobalAdvice", L1("Ask"));    m_globalAdvice = strToAdvice(value);    // Reset current domain settings first.    for ( QStringList::Iterator it=m_domainList.begin(); it != m_domainList.end(); )    {         // Make sure to update iterator before calling setDomainAdvice()         // setDomainAdvice() might delete the domain from domainList.         QString domain = *it++;         setDomainAdvice(domain, KCookieDunno);    }    // Now apply the domain settings read from config file...    for ( QStringList::Iterator it=domainSettings.begin();          it != domainSettings.end(); )    {        const QString &value = *it++;        int sepPos = value.findRev(':');        if (sepPos <= 0)          continue;        QString domain(value.left(sepPos));        KCookieAdvice advice = strToAdvice( value.mid(sepPos + 1) );        setDomainAdvice(domain, advice);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?