Пример #1
0
void TestBrowserCookieJar::setDiskStorageEnabled(bool enabled)
{
    m_storageEnabled = enabled;

    if (enabled && allCookies().isEmpty())
        loadFromDisk();

    // When disabling, save current cookies.
    if (!enabled && !allCookies().isEmpty())
        scheduleSaveToDisk();
}
Пример #2
0
QList<QNetworkCookie> CookieJar::cookies(const QUrl& url) const
{
	if(url.isEmpty())
		return allCookies();
	else
		return cookiesForUrl(url);
}
Пример #3
0
void CookieJar::StoreCookies()
{
    if (cookieDiskFile_.isEmpty())
        return;
    if (allCookies().empty())
        return;

    QFile cookiesFile(cookieDiskFile_);
    if (!cookiesFile.open(QIODevice::WriteOnly))
        return;

    QDataStream cookieData(&cookiesFile);
    foreach (QNetworkCookie cookie, allCookies())
        cookieData << cookie.toRawForm();
    cookiesFile.close();
}
Пример #4
0
void CookieJar::save()
{
    expireCookies();

    QByteArray cookieData;
    QDataStream stream(&cookieData, QIODevice::WriteOnly);
    stream << cookieFileVersion;
    QList<QNetworkCookie> cookies = allCookies();

    QMutableListIterator<QNetworkCookie> it(cookies);
    while (it.hasNext()) {
        if (it.next().isSessionCookie())
            it.remove();
    }

    stream << qint32(cookies.count());
    foreach (const QNetworkCookie& cookie, cookies)
        stream << cookie.toRawForm();

    QFile cookieFile(m_cookieFileName + ".tmp");
    if (!cookieFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        return;

    cookieFile.write(cookieData);
    cookieFile.close();
    // ### use atomic rename
    QFile::remove(m_cookieFileName);
    cookieFile.rename(m_cookieFileName);
}
Пример #5
0
CookieJar* CookieJar::clone(QObject *parent)
{
	CookieJar *cookieJar(new CookieJar(m_isPrivate, parent));
	cookieJar->setAllCookies(allCookies());

	return cookieJar;
}
void CleanableCookieJar::clean_session_cookies(){
  QList<QNetworkCookie> cookies(allCookies());

  for(QList<QNetworkCookie>::iterator i = cookies.begin(); i != cookies.end(); i++)
    while(i->isSessionCookie())
      i = cookies.erase(i);

  setAllCookies(cookies);
}
Пример #7
0
void SalorCookieJar::extractRawCookies()
{
    qDebug() << "SalorCookieJar::extractRawCookies()";
    QList<QNetworkCookie> cookies = allCookies();
    m_rawCookies.clear();

    foreach (const QNetworkCookie &cookie, cookies) {
      m_rawCookies.append(cookie.toRawForm());
    }
Пример #8
0
void TestBrowserCookieJar::extractRawCookies()
{
    QList<QNetworkCookie> cookies = allCookies();
    m_rawCookies.clear();

    foreach (const QNetworkCookie &cookie, cookies) {
        if (!cookie.isSessionCookie())
            m_rawCookies.append(cookie.toRawForm());
    }
}
Пример #9
0
void SimpleFileCookieJar::clean()
{
    auto cookies = allCookies();

    QDateTime now = QDateTime::currentDateTime();
    foreach(QNetworkCookie cookie, cookies)
        if(cookie.expirationDate() < now)
            cookies.removeAll(cookie);

    setAllCookies(cookies);
}
Пример #10
0
QList<QNetworkCookie> CookieJar::getCookies(const QString &domain) const
{
    if (!domain.isEmpty())
    {
        const QList<QNetworkCookie> cookies = allCookies();
        QList<QNetworkCookie> domainCookies;

        for (int i = 0; i < cookies.count(); ++i)
        {
            if (cookies.at(i).domain() == domain || (cookies.at(i).domain().startsWith(QLatin1Char('.')) && domain.endsWith(cookies.at(i).domain())))
            {
                domainCookies.append(cookies.at(i));
            }
        }

        return domainCookies;
    }

    return allCookies();
}
Пример #11
0
void SimpleFileCookieJar::save()
{
    clean();

    QFile f(cookieFile);
    f.open(QIODevice::WriteOnly);

    foreach(QNetworkCookie cookie, allCookies())
    {
        f.write(cookie.toRawForm());
        f.write("\n");
    }
QVariant NetworkCookieJar::ToVariant() const
{
	QVariantList axRet;
	QList<QNetworkCookie> axC=allCookies();
	forint(i,axC)
	{
		const QNetworkCookie& xC=axC[i];
		//qDebug() << "WCookie:" << xC;
		axRet.append(xC.toRawForm());
	};
	return axRet;
}
Пример #13
0
void CookieJar::purgeOldCookies()
{
  auto cookies = allCookies();
  if (cookies.isEmpty())
    return;
  auto now = QDateTime::currentDateTime();
  for (int i = cookies.count() - 1; i >= 0; --i) {
    if (!cookies.at(i).isSessionCookie() && cookies.at(i).expirationDate() < now)
      cookies.removeAt(i);
  }
  setAllCookies(cookies);
}
void NetworkCookieJar::DoEndSession()
{
	QDateTime xNow=QDateTime::currentDateTime().toTimeSpec(Qt::UTC);
	QList<QNetworkCookie> axC=allCookies();
	forint(i,axC)
	{
		const QNetworkCookie& xC=axC[i];
		if(xC.isSessionCookie()) {axC.removeAt(i);i--;continue;};
		if(xNow>xC.expirationDate()) {axC.removeAt(i);i--;continue;};
	};
	setAllCookies(axC);
}
Пример #15
0
void PersistentCookieJar::saveState()
{
  QSettings settings(MainApplication::ORGANISATION_NAME, MainApplication::APPLICATION_NAME);
  QByteArray cookieData;
  foreach (QNetworkCookie cookie, allCookies())
  {
    if (!cookie.isSessionCookie())
    {
      cookieData.append(cookie.toRawForm());
      cookieData.append("\n");
    }
  }
  settings.setValue("Application/Cookies", cookieData);
}
Пример #16
0
void CookieJar::saveCookies(const QString& filename)
{
  purgeOldCookies();
  QFile cookieFile(filename);
  if (!cookieFile.open(QIODevice::WriteOnly)) {
    qWarning("Couldn't save cookie file.");
    return;
  }
  auto cookies = allCookies();
  for (const auto &c : cookies) {
    cookieFile.write(c.toRawForm());
    cookieFile.write("\n");
  }
}
Пример #17
0
void CookieJar::purgeExpiredCookies()
{
	QList<QNetworkCookie> cookiesList = allCookies();

	if(cookiesList.isEmpty()) return;

	int cookieCount = cookiesList.count();
	QDateTime now = QDateTime::currentDateTimeUtc();
	for(int i = cookieCount - 1;i >= 0; --i) {
		if(!cookiesList.at(i).isSessionCookie() && cookiesList.at(i).expirationDate() < now) cookiesList.removeAt(i);
	}

	if(cookieCount != cookiesList.count()) setAllCookies(cookiesList);
}
void ACNetworkCookieJar::save()
{
    QMutexLocker lock(&mutex);
    Q_UNUSED(lock);
    QList<QNetworkCookie> list = allCookies();
    QByteArray data;
    foreach (QNetworkCookie cookie, list) {
        if (!cookie.isSessionCookie() && cookie.domain() == ".acfun.tv"){
            data.append(cookie.toRawForm());
            data.append("\n");
        }
    }
    Utility::Instance()->setValue("Cookies", data);
}
Пример #19
0
void CookieJar::expireCookies()
{
    const QList<QNetworkCookie>& currentCookies = allCookies();
    QList<QNetworkCookie> newCookies;
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
    newCookies.reserve(currentCookies.count());
#endif
    QDateTime now = QDateTime::currentDateTime();
    foreach (const QNetworkCookie& cookie, currentCookies) {
        if (!cookie.isSessionCookie() && cookie.expirationDate() < now)
            continue;
        newCookies += cookie;
    }
    setAllCookies(newCookies);
}
void TBNetworkCookieJar::save()
{
    QMutexLocker lock(&mutex);
    Q_UNUSED(lock);
    QList<QNetworkCookie> list = allCookies();
    QByteArray data;
    foreach (QNetworkCookie cookie, list) {
        if (!cookie.isSessionCookie()){
            QString domain = cookie.domain();
            if (domain.endsWith("tieba.baidu.com")||domain.endsWith("wappass.baidu.com")||domain == ".baidu.com"){
                data.append(cookie.toRawForm());
                data.append("\n");
            }
        }
    }
    Utility::Instance()->setValue("cookies", data);
}
Пример #21
0
/**
 * This function comes from the official qt demo at
 *      http://doc.qt.io/qt-5/qtwebkitexamples-webkitwidgets-browser-cookiejar-cpp.html
 */
void CookieJar::purgeOldCookies(){

    QList<QNetworkCookie> cookies = allCookies();
    if (cookies.isEmpty())
        return;
    int oldCount = cookies.count();
    QDateTime now = QDateTime::currentDateTime();
    for (int i = cookies.count() - 1; i >= 0; --i) {
        if (!cookies.at(i).isSessionCookie() && cookies.at(i).expirationDate() < now)
            cookies.removeAt(i);
    }
    if (oldCount == cookies.count())
        return;
    setAllCookies(cookies);
    //This last line from the example was omitted as it is not relevant to the function of this project?
    //emit cookiesChanged();
}
Пример #22
0
void CookieJar::save()
{
    qDebug() << Q_FUNC_INFO;
    qDebug() << COOKIE_PATH;
    QFile f(COOKIE);
    if(!f.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        return;
    }

    QList<QNetworkCookie> l = allCookies();
    foreach(QNetworkCookie c, l)
    {
        if(!c.name().isEmpty() && !c.value().isEmpty())
        {
            f.write(c.toRawForm()+"\n");
        }
    }
}
Пример #23
0
void SharedCookieJarQt::deleteCookiesForHostname(const String& hostname)
{
    if (!m_database.isOpen())
        return;

    QList<QNetworkCookie> cookies = allCookies();
    QList<QNetworkCookie>::Iterator it = cookies.begin();
    QList<QNetworkCookie>::Iterator end = cookies.end();
    QSqlQuery sqlQuery(m_database);
    sqlQuery.prepare(QLatin1String("DELETE FROM cookies WHERE cookieId=:cookieIdvalue"));
    while (it != end) {
        if (it->domain() == QString(hostname)) {
            sqlQuery.bindValue(QLatin1String(":cookieIdvalue"), it->domain().append(QLatin1String(it->name())));
            sqlQuery.exec();
            it = cookies.erase(it);
        } else
            it++;
    }
    setAllCookies(cookies);
}
void
PersistentCookieJar::save()
{   
    QList<QNetworkCookie> list = allCookies();
    QByteArray data;

    qDebug() << "Cookie count: " << list.count();

    foreach (QNetworkCookie cookie, list)
    {
        // don't save session cookies
        if (!cookie.isSessionCookie())
        {
            data.append(cookie.toRawForm());
            data.append("\n");
        }
    }

    unicorn::AppSettings settings;
    settings.setValue("Cookies", data);
}
Пример #25
0
void NetworkCookieJar::serialize()
{
	QMutexLocker m(&_memberMutex);

	// write the cookies to the specified file
	Json::Value cookieRoot;
	
	QList<QNetworkCookie> cookies = allCookies();
	QListIterator<QNetworkCookie> iter(cookies);
	while (iter.hasNext())
	{
		const QNetworkCookie& cookie = iter.next();
		if (cookie.isSessionCookie() || cookie.expirationDate() < QDateTime::currentDateTime())
			continue;
		QByteArray bytes = cookie.toRawForm();
		cookieRoot.append(QString(bytes).toUtf8().constData());		
	}
	Json::StyledWriter jsonWriter;
	QString outStr = QString::fromUtf8(jsonWriter.write(cookieRoot).c_str());
	write_file_utf8(outStr, _cookieJarFilePath);
}
Пример #26
0
bool CookieJar::setCookie(const QString& cookieName, const QString& value, int maxAge, const QString& path, const QString& domain, bool isSecure)
{
	QNetworkCookie cookie;
	QUrl url = QString(isSecure ? "https://" : "http://" + QString(domain.startsWith('.') ? "www" : "") + domain + (path.isEmpty() ? "/" : path));
#ifdef U_CJ_DEBUG
	qDebug() << Q_FUNC_INFO << allCookies().count() << url;
#endif
	cookie.setName(cookieName.toUtf8());
	cookie.setValue(value.toUtf8());
	cookie.setDomain(domain);
	cookie.setPath(path);
	cookie.setSecure(isSecure);

	if(maxAge != 0) {
		QDateTime expireDate = QDateTime::currentDateTimeUtc().addSecs(maxAge);
		cookie.setExpirationDate(expireDate);
	}
#ifdef U_CJ_DEBUG
	qDebug() << Q_FUNC_INFO << cookie;
#endif
	return setCookiesFromUrl(QList<QNetworkCookie>() << cookie, url);
}
Пример #27
0
void CookieJar::deleteCookieForUrl(const QUrl& url, const QString& cookieName)
{
#ifdef U_CJ_DEBUG
	qDebug() << Q_FUNC_INFO;
#endif
	QList<QNetworkCookie> cookieList;

	if(url.isEmpty() || !url.isValid()) {
		if(cookieName.isEmpty()) {
			setAllCookies(QList<QNetworkCookie>());
		} else {
			cookieList = allCookies();

			QList<QNetworkCookie>::Iterator it = cookieList.begin();
			while(it != cookieList.end()) {
				if(it->name() == cookieName) {
					deleteCookie(*it);
					it = cookieList.erase(it);
				} else
					it++;
			}
		}
	} else {
		cookieList = cookiesForUrl(url);

		QList<QNetworkCookie>::Iterator it = cookieList.begin();
		while(it != cookieList.end()) {
			if(it->name() == cookieName || cookieName.isEmpty()) {
				deleteCookie(*it);
				it = cookieList.erase(it);

				if(!cookieName.isEmpty())
					break;
			} else
				it++;
		}
	}
}
Пример #28
0
void CookieJar::saveCookies()
{
    QString fName = m_appPath;
    fName += "cookies";
    QSettings cnFile(fName, QSettings::IniFormat);
    cnFile.clear();

    cnFile.setValue("login", m_sVkLogin);

    QList<QNetworkCookie> cookies = allCookies();
    cnFile.beginWriteArray("cookies");
    for(int i=0; i<cookies.size(); i++) {
        QNetworkCookie cook = cookies.at(i);

        cnFile.setArrayIndex(i);

        cnFile.setValue("name", cook.name());
        cnFile.setValue("value", cook.value());
        cnFile.setValue("domain", cook.domain());
        cnFile.setValue("path", cook.path());
    }
    cnFile.endArray();
}
Пример #29
0
void CookieJar::save()
{
    QSaveFile file(SessionsManager::getWritableDataPath(QLatin1String("cookies.dat")));

    if (!file.open(QIODevice::WriteOnly))
    {
        return;
    }

    const QList<QNetworkCookie> cookies = allCookies();
    QDataStream stream(&file);
    stream << quint32(cookies.count());

    for (int i = 0; i < cookies.count(); ++i)
    {
        if (!cookies.at(i).isSessionCookie())
        {
            stream << cookies.at(i).toRawForm();
        }
    }

    file.commit();
}
Пример #30
0
bool GagCookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
{
    bool addedCookies = false;

    // lets save cookies for 90 days only
    QDateTime soon = QDateTime::currentDateTime();
    soon = soon.addDays(90);

    foreach(QNetworkCookie cookie, cookieList) {
        QList<QNetworkCookie> lst;
        if (!cookie.isSessionCookie() && cookie.expirationDate() > soon) {
            cookie.setExpirationDate(soon);
        }
        lst += cookie;
        if (QNetworkCookieJar::setCookiesFromUrl(lst, url)) {
            addedCookies = true;
        } else {
            // finally force it in if wanted
            QList<QNetworkCookie> cookies = allCookies();
            cookies += cookie;
            setAllCookies(cookies);
            addedCookies = true;
        }
    }