Merge pull request #112 from ErikDubbelboer/persistent-cookies

Persistent cookie support
1.3
Ariya Hidayat 2011-07-20 11:07:36 -07:00
commit 853052da93
7 changed files with 137 additions and 3 deletions

View File

@ -12,6 +12,10 @@ Version 1.3.0
* Added Filesystem API, based on CommonJS Filesystem draft specs (issue 129)
New features
* Presistent cookie support using --cookies=cookies.ini
Examples
* Added a new example on using Modernizr to detect features.

70
src/cookiejar.cpp Normal file
View File

@ -0,0 +1,70 @@
/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cookiejar.h"
#include <QSettings>
#include <QStringList>
CookieJar::CookieJar(QString cookieFile)
: QNetworkCookieJar()
{
m_cookieFile = cookieFile;
}
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> & cookieList, const QUrl & url) {
QSettings settings(m_cookieFile, QSettings::IniFormat);
settings.beginGroup(url.host());
for (QList<QNetworkCookie>::const_iterator i = cookieList.begin(); i != cookieList.end(); i++) {
settings.setValue((*i).name(), QString((*i).value()));
}
settings.sync();
return true;
}
QList<QNetworkCookie> CookieJar::cookiesForUrl(const QUrl & url) const {
QSettings settings(m_cookieFile, QSettings::IniFormat);
QList<QNetworkCookie> cookieList;
settings.beginGroup(url.host());
QStringList keys = settings.childKeys();
for (QStringList::iterator i = keys.begin(); i != keys.end(); i++) {
cookieList.push_back(QNetworkCookie((*i).toLocal8Bit(), settings.value(*i).toByteArray()));
}
return cookieList;
}

48
src/cookiejar.h Normal file
View File

@ -0,0 +1,48 @@
/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef COOKIEJAR_H
#define COOKIEJAR_H
#include <QNetworkCookieJar>
class CookieJar: public QNetworkCookieJar
{
private:
QString m_cookieFile;
public:
CookieJar(QString cookieFile);
bool setCookiesFromUrl(const QList<QNetworkCookie> & cookieList, const QUrl & url);
QList<QNetworkCookie> cookiesForUrl (const QUrl & url) const;
};
#endif // COOKIEJAR_H

View File

@ -35,6 +35,7 @@
#include <QNetworkDiskCache>
#include "networkaccessmanager.h"
#include "cookiejar.h"
static const char *toString(QNetworkAccessManager::Operation op)
{
@ -63,12 +64,16 @@ static const char *toString(QNetworkAccessManager::Operation op)
}
// public:
NetworkAccessManager::NetworkAccessManager(QObject *parent, bool diskCacheEnabled, bool ignoreSslErrors)
NetworkAccessManager::NetworkAccessManager(QObject *parent, bool diskCacheEnabled, QString cookieFile, bool ignoreSslErrors)
: QNetworkAccessManager(parent)
, m_networkDiskCache(0)
, m_ignoreSslErrors(ignoreSslErrors)
, m_idCounter(0)
{
if (!cookieFile.isEmpty()) {
setCookieJar(new CookieJar(cookieFile));
}
if (diskCacheEnabled) {
m_networkDiskCache = new QNetworkDiskCache();
m_networkDiskCache->setCacheDirectory(QDesktopServices::storageLocation(QDesktopServices::CacheLocation));

View File

@ -41,7 +41,7 @@ class NetworkAccessManager : public QNetworkAccessManager
Q_OBJECT
QNetworkDiskCache* m_networkDiskCache;
public:
NetworkAccessManager(QObject *parent = 0, bool diskCacheEnabled = false, bool ignoreSslErrors = false);
NetworkAccessManager(QObject *parent = 0, bool diskCacheEnabled = false, QString cookieFile = "", bool ignoreSslErrors = false);
virtual ~NetworkAccessManager();
protected:

View File

@ -54,6 +54,7 @@ Phantom::Phantom(QObject *parent)
m_pages.append(m_page);
QString proxyHost;
QString cookieFile;
int proxyPort = 1080;
bool autoLoadImages = true;
bool pluginsEnabled = false;
@ -128,6 +129,10 @@ Phantom::Phantom(QObject *parent)
}
continue;
}
if (arg.startsWith("--cookies=")) {
cookieFile = arg.mid(10).trimmed();
continue;
}
if (arg.startsWith("--")) {
std::cerr << "Unknown option '" << qPrintable(arg) << "'" << std::endl;
m_terminated = true;
@ -157,7 +162,7 @@ Phantom::Phantom(QObject *parent)
}
// Provide WebPage with a non-standard Network Access Manager
m_netAccessMan = new NetworkAccessManager(this, diskCacheEnabled, ignoreSslErrors);
m_netAccessMan = new NetworkAccessManager(this, diskCacheEnabled, cookieFile, ignoreSslErrors);
m_page->setNetworkAccessManager(m_netAccessMan);
connect(m_page, SIGNAL(javaScriptConsoleMessageSent(QString, int, QString)),

View File

@ -16,6 +16,7 @@ HEADERS += csconverter.h \
consts.h \
utils.h \
networkaccessmanager.h \
cookiejar.h \
filesystem.h
SOURCES += phantom.cpp \
webpage.cpp \
@ -23,6 +24,7 @@ SOURCES += phantom.cpp \
csconverter.cpp \
utils.cpp \
networkaccessmanager.cpp \
cookiejar.cpp \
filesystem.cpp
OTHER_FILES = bootstrap.js