2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "cookiejar.h"
|
|
|
|
#include "qupzilla.h"
|
2012-01-21 23:19:38 +01:00
|
|
|
#include "mainapplication.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#include <QNetworkCookie>
|
2012-12-21 16:25:00 +01:00
|
|
|
#include <QWebSettings>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QDateTime>
|
2012-07-13 16:43:47 +02:00
|
|
|
#include <QDebug>
|
2013-01-09 19:12:42 +01:00
|
|
|
#include <QWebPage> // QTWEBKIT_VERSION_CHECK macro
|
2012-07-13 16:43:47 +02:00
|
|
|
|
2011-11-05 15:04:29 +01:00
|
|
|
//#define COOKIE_DEBUG
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-10-19 13:59:40 +02:00
|
|
|
bool blockThirdParty(QString string, QString domain)
|
2012-09-01 11:41:12 +02:00
|
|
|
{
|
2012-10-19 13:59:40 +02:00
|
|
|
if (string.isEmpty()) {
|
|
|
|
// Some cookies have empty domain() ... bug?
|
2012-09-01 11:41:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-19 13:59:40 +02:00
|
|
|
if (string.startsWith(QLatin1String("www."))) {
|
|
|
|
string = string.mid(3);
|
|
|
|
}
|
2012-09-01 11:41:12 +02:00
|
|
|
|
2012-10-19 13:59:40 +02:00
|
|
|
if (domain.startsWith(QLatin1String("www."))) {
|
|
|
|
domain = domain.mid(4);
|
2012-09-01 11:41:12 +02:00
|
|
|
}
|
|
|
|
|
2012-10-19 13:59:40 +02:00
|
|
|
return !domain.endsWith(string);
|
2012-09-01 11:41:12 +02:00
|
|
|
}
|
|
|
|
|
2012-10-19 13:59:40 +02:00
|
|
|
bool matchDomain(const QString &domain, const QString &filter)
|
2012-02-17 18:55:34 +01:00
|
|
|
{
|
2012-10-19 13:59:40 +02:00
|
|
|
// According to RFC 6265
|
|
|
|
|
|
|
|
if (domain == filter) {
|
2012-02-20 17:22:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-19 13:59:40 +02:00
|
|
|
if (!domain.endsWith(filter)) {
|
|
|
|
return false;
|
2012-02-17 18:55:34 +01:00
|
|
|
}
|
|
|
|
|
2012-10-19 13:59:40 +02:00
|
|
|
int index = domain.indexOf(filter);
|
|
|
|
|
|
|
|
return (index > 0 && filter[0] == QLatin1Char('.'));
|
2012-02-17 18:55:34 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
int listContainsDomain(const QStringList &list, const QString &domain)
|
2012-02-17 18:55:34 +01:00
|
|
|
{
|
2012-07-13 16:43:47 +02:00
|
|
|
if (domain.isEmpty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-17 18:55:34 +01:00
|
|
|
foreach(const QString & d, list) {
|
2012-10-19 13:59:40 +02:00
|
|
|
if (matchDomain(domain, d)) {
|
2012-07-13 16:43:47 +02:00
|
|
|
return 1;
|
2012-02-17 18:55:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
return 0;
|
2012-02-17 18:55:34 +01:00
|
|
|
}
|
|
|
|
|
2011-11-05 15:04:29 +01:00
|
|
|
CookieJar::CookieJar(QupZilla* mainClass, QObject* parent)
|
|
|
|
: QNetworkCookieJar(parent)
|
|
|
|
, p_QupZilla(mainClass)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-04 18:48:54 +02:00
|
|
|
m_activeProfil = mApp->currentProfilePath();
|
2011-11-05 15:04:29 +01:00
|
|
|
loadSettings();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2012-02-17 18:55:34 +01:00
|
|
|
settings.beginGroup("Cookie-Settings");
|
2011-11-05 15:04:29 +01:00
|
|
|
m_allowCookies = settings.value("allowCookies", true).toBool();
|
2012-07-13 16:43:47 +02:00
|
|
|
m_blockThirdParty = settings.value("allowCookiesFromVisitedDomainOnly", false).toBool();
|
2011-11-05 15:04:29 +01:00
|
|
|
m_filterTrackingCookie = settings.value("filterTrackingCookie", false).toBool();
|
2011-03-02 16:57:41 +01:00
|
|
|
m_deleteOnClose = settings.value("deleteCookiesOnClose", false).toBool();
|
2012-02-17 18:55:34 +01:00
|
|
|
m_whitelist = settings.value("whitelist", QStringList()).toStringList();
|
|
|
|
m_blacklist = settings.value("blacklist", QStringList()).toStringList();
|
|
|
|
settings.endGroup();
|
2012-12-21 16:25:00 +01:00
|
|
|
|
2013-01-20 12:10:28 +01:00
|
|
|
#if QTWEBKIT_FROM_2_3
|
2012-12-21 16:25:00 +01:00
|
|
|
mApp->webSettings()->setThirdPartyCookiePolicy(m_blockThirdParty ?
|
2012-12-21 16:38:44 +01:00
|
|
|
QWebSettings::AlwaysBlockThirdPartyCookies :
|
|
|
|
QWebSettings::AlwaysAllowThirdPartyCookies);
|
2012-12-21 16:25:00 +01:00
|
|
|
#endif
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::setAllowCookies(bool allow)
|
|
|
|
{
|
|
|
|
m_allowCookies = allow;
|
|
|
|
}
|
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
bool CookieJar::rejectCookie(const QString &domain, const QNetworkCookie &cookie) const
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-12-21 16:25:00 +01:00
|
|
|
Q_UNUSED(domain)
|
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
const QString &cookieDomain = cookie.domain();
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
if (!m_allowCookies) {
|
|
|
|
int result = listContainsDomain(m_whitelist, cookieDomain);
|
|
|
|
if (result != 1) {
|
2011-03-02 16:57:41 +01:00
|
|
|
#ifdef COOKIE_DEBUG
|
2012-02-17 18:55:34 +01:00
|
|
|
qDebug() << "not in whitelist" << cookie;
|
2011-03-02 16:57:41 +01:00
|
|
|
#endif
|
2012-07-13 16:43:47 +02:00
|
|
|
return true;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2012-07-13 16:43:47 +02:00
|
|
|
}
|
2011-11-05 15:04:29 +01:00
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
if (m_allowCookies) {
|
|
|
|
int result = listContainsDomain(m_blacklist, cookieDomain);
|
|
|
|
if (result == 1) {
|
2011-03-02 16:57:41 +01:00
|
|
|
#ifdef COOKIE_DEBUG
|
2012-02-17 18:55:34 +01:00
|
|
|
qDebug() << "found in blacklist" << cookie;
|
2011-03-02 16:57:41 +01:00
|
|
|
#endif
|
2012-07-13 16:43:47 +02:00
|
|
|
return true;
|
2012-02-17 18:55:34 +01:00
|
|
|
}
|
2012-07-13 16:43:47 +02:00
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
|
2012-12-21 16:25:00 +01:00
|
|
|
// This feature is now natively in QtWebKit in Qt 5
|
2013-01-20 12:10:28 +01:00
|
|
|
#if QTWEBKIT_TO_2_3
|
2012-07-13 16:43:47 +02:00
|
|
|
if (m_blockThirdParty) {
|
2012-10-19 13:59:40 +02:00
|
|
|
bool result = blockThirdParty(cookieDomain, domain);
|
2012-07-13 16:43:47 +02:00
|
|
|
if (result) {
|
2012-02-17 18:55:34 +01:00
|
|
|
#ifdef COOKIE_DEBUG
|
2012-07-13 16:43:47 +02:00
|
|
|
qDebug() << "purged for domain mismatch" << cookie << cookieDomain << domain;
|
2012-02-17 18:55:34 +01:00
|
|
|
#endif
|
2012-07-13 16:43:47 +02:00
|
|
|
return true;
|
2012-02-17 18:55:34 +01:00
|
|
|
}
|
2012-07-13 16:43:47 +02:00
|
|
|
}
|
2012-12-21 16:25:00 +01:00
|
|
|
#endif
|
2012-02-17 18:55:34 +01:00
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
if (m_filterTrackingCookie && cookie.name().startsWith("__utm")) {
|
2012-02-17 18:55:34 +01:00
|
|
|
#ifdef COOKIE_DEBUG
|
2012-07-13 16:43:47 +02:00
|
|
|
qDebug() << "purged as tracking " << cookie;
|
2012-02-17 18:55:34 +01:00
|
|
|
#endif
|
2012-07-13 16:43:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
|
|
|
|
{
|
|
|
|
QList<QNetworkCookie> newList = cookieList;
|
|
|
|
|
|
|
|
foreach(const QNetworkCookie & cookie, newList) {
|
|
|
|
if (rejectCookie(url.host(), cookie)) {
|
2012-02-17 18:55:34 +01:00
|
|
|
newList.removeOne(cookie);
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
return QNetworkCookieJar::setCookiesFromUrl(newList, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::saveCookies()
|
|
|
|
{
|
2012-06-26 11:49:39 +02:00
|
|
|
if (m_deleteOnClose || mApp->isPrivateSession()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-06-26 11:49:39 +02:00
|
|
|
QList<QNetworkCookie> allCookies = getAllCookies();
|
2012-01-04 14:09:27 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
QFile file(m_activeProfil + "cookies.dat");
|
2011-03-02 16:57:41 +01:00
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream stream(&file);
|
|
|
|
int count = allCookies.count();
|
|
|
|
|
|
|
|
stream << count;
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2012-02-17 18:55:34 +01:00
|
|
|
const QNetworkCookie &cookie = allCookies.at(i);
|
|
|
|
|
|
|
|
if (cookie.isSessionCookie()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stream << cookie.toRawForm();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::restoreCookies()
|
|
|
|
{
|
2012-06-26 11:49:39 +02:00
|
|
|
if (!QFile::exists(m_activeProfil + "cookies.dat") || mApp->isPrivateSession()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-06-26 11:49:39 +02:00
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
|
|
|
|
QList<QNetworkCookie> restoredCookies;
|
2011-11-06 17:01:23 +01:00
|
|
|
QFile file(m_activeProfil + "cookies.dat");
|
2011-03-02 16:57:41 +01:00
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
QDataStream stream(&file);
|
|
|
|
int count;
|
|
|
|
|
|
|
|
stream >> count;
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2011-03-02 16:57:41 +01:00
|
|
|
QByteArray rawForm;
|
|
|
|
stream >> rawForm;
|
2012-02-17 18:55:34 +01:00
|
|
|
const QList<QNetworkCookie> &cookieList = QNetworkCookie::parseCookies(rawForm);
|
|
|
|
if (cookieList.isEmpty()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
|
|
|
|
const QNetworkCookie &cookie = cookieList.at(0);
|
|
|
|
|
|
|
|
if (cookie.expirationDate() < now) {
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
restoredCookies.append(cookie);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
setAllCookies(restoredCookies);
|
|
|
|
}
|
|
|
|
|
2012-01-12 20:10:51 +01:00
|
|
|
void CookieJar::clearCookies()
|
|
|
|
{
|
2012-06-26 11:49:39 +02:00
|
|
|
setAllCookies(QList<QNetworkCookie>());
|
2012-01-12 20:10:51 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QList<QNetworkCookie> CookieJar::getAllCookies()
|
|
|
|
{
|
|
|
|
return QNetworkCookieJar::allCookies();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::setAllCookies(const QList<QNetworkCookie> &cookieList)
|
|
|
|
{
|
|
|
|
QNetworkCookieJar::setAllCookies(cookieList);
|
|
|
|
}
|