Эх сурвалжийг харах

Add better cookie managment support

Antialize 16 жил өмнө
parent
commit
8e059c11a3

+ 5 - 3
src/arguments.cc

@@ -116,9 +116,9 @@ public:
 /*!
   Putting values into a map
 */
-struct AHMapSetter: public ArgHandler {
+struct MapSetter: public ArgHandler {
 	QHash<QString, QString> & dst;
-	AHMapSetter(QHash<QString, QString> & a, QString keyName, QString valueName) : dst(a) {
+	MapSetter(QHash<QString, QString> & a, QString keyName, QString valueName) : dst(a) {
 		argn.push_back(keyName);
 		argn.push_back(valueName);
 	}
@@ -432,7 +432,7 @@ CommandLineParserPrivate::CommandLineParserPrivate(Settings & s):
 	addarg("proxy",'p',"Use a proxy", new ProxySetter(s.proxy, "proxy"));
 	addarg("username",0,"HTTP Authentication username", new QStrSetter(s.username, "username",""));
 	addarg("password",0,"HTTP Authentication password", new QStrSetter(s.password, "password",""));
-	addarg("custom-header",0,"Set an additional HTTP header (repeatable)", new AHMapSetter(s.customHeaders, "name", "value"));
+	addarg("custom-header",0,"Set an additional HTTP header (repeatable)", new MapSetter(s.customHeaders, "name", "value"));
 	qthack(true);
 	addarg("book",'b',"Set the options one would usually set when printing a book", new Caller<BookFunc>());
 	addarg("cover",0,"Use html document as cover. It will be inserted before the toc with no headers and footers",new QStrSetter(s.cover,"url",""));
@@ -458,6 +458,8 @@ CommandLineParserPrivate::CommandLineParserPrivate(Settings & s):
 	addarg("enable-plugins",0,"Enable installed plugins (such as flash", new ConstSetter<bool>(s.enablePlugins,true,false));
 	addarg("zoom",0,"Use this zoom factor", new FloatSetter(s.zoomFactor,"float",1.0));
 	addarg("read-args-from-stdin",0,"Read command line arguments from stdin", new ConstSetter<bool>(s.readArgsFromStdin,true,false));
+	addarg("cookie-jar", 0, "Read and write cookies from and to the supplied cookie jar file", new QStrSetter(s.cookieJar, "path", "") );
+	addarg("cookie",0,"Set an additional cookie (repeatable)", new MapSetter(s.cookies, "name", "value"));
 
 	qthack(true);
 	addarg("disable-internal-links",0,"Do no make local links", new ConstSetter<bool>(s.useLocalLinks,false,true));

+ 40 - 2
src/multipageloader.cc

@@ -17,6 +17,7 @@
 #include <QFile>
 #include <QFileInfo>
 #include <QTimer>
+#include <QNetworkCookie>
 
 /*!
   \file multipageloader.hh
@@ -28,6 +29,32 @@
   \brief Defines the MultiPageLoaderPrivate class
 */
 
+void MyCookieJar::addGlobalCookie(const QString & name, const QString & value) {
+	globalCookies.append(QNetworkCookie(name.toUtf8(), value.toUtf8()));
+}
+
+QList<QNetworkCookie> MyCookieJar::cookiesForUrl(const QUrl & url) {
+	QList<QNetworkCookie> list = QNetworkCookieJar::cookiesForUrl(url);
+	list.append(globalCookies);
+	return list;
+}
+
+void MyCookieJar::loadFromFile(const QString & path) {
+	QFile cookieJar(path);
+	if (cookieJar.open(QIODevice::ReadOnly | QIODevice::Text) )
+		setAllCookies(QNetworkCookie::parseCookies(cookieJar.readAll()));
+}
+
+void MyCookieJar::saveToFile(const QString & path) {
+	QFile cookieJar(path);
+	if (cookieJar.open(QIODevice::WriteOnly | QIODevice::Text) )
+		foreach (const QNetworkCookie & cookie, allCookies()) {
+			cookieJar.write(cookie.toRawForm());
+			cookieJar.write(";\n");
+		}
+}
+
+
 /*!
  * Track and handle network errors
  * \param reply The networkreply that has finished
@@ -106,7 +133,15 @@ MultiPageLoaderPrivate::MultiPageLoaderPrivate(Settings & s, MultiPageLoader & o
 	connect(&networkAccessManager, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator *)),this,
 	        SLOT(authenticationRequired(QNetworkReply *, QAuthenticator *)));
 
-		//If we must use a proxy, create a host of objects
+	networkAccessManager.setCookieJar(&cookieJar);
+
+	if (!settings.cookieJar.isEmpty()) 
+		cookieJar.loadFromFile(settings.cookieJar);
+	
+	foreach (const QString & name, settings.cookies )
+		cookieJar.addGlobalCookie(name, settings.cookies[name]);
+		
+	//If we must use a proxy, create a host of objects
 	if (!settings.proxy.host.isEmpty()) {
 		QNetworkProxy proxy;
 		proxy.setHostName(settings.proxy.host);
@@ -233,8 +268,11 @@ void MultiPageLoaderPrivate::loadFinished(bool ok) {
 }
 
 void MultiPageLoaderPrivate::timedFinished() {
-	if(loadingPages == 0) 
+	if(loadingPages == 0) {
+		if (!settings.cookieJar.isEmpty())
+			cookieJar.saveToFile(settings.cookieJar);
 		emit outer.loadFinished(!error);
+	}
 }
 
 /*!

+ 12 - 0
src/multipageloader_p.hh

@@ -21,10 +21,22 @@
 #include <QNetworkAccessManager>
 #include <QNetworkReply>
 #include <QWebFrame>
+#include <QNetworkCookieJar>
+
+class MyCookieJar: public QNetworkCookieJar {
+private:
+	QList<QNetworkCookie> globalCookies;
+public:
+	void addGlobalCookie(const QString & name, const QString & value);
+	QList<QNetworkCookie> cookiesForUrl(const QUrl & url);
+	void loadFromFile(const QString & path);
+	void saveToFile(const QString & path);
+};
 
 class MultiPageLoaderPrivate: public QObject {
 	Q_OBJECT
 public:
+	MyCookieJar cookieJar;
 	MultiPageLoader & outer;
 	Settings & settings;
 	int httpErrorCode;

+ 5 - 0
src/settings.hh

@@ -177,6 +177,11 @@ struct Settings {
 	QHash<QString, QString> customHeaders;
 	//! Should we read arguments from stdin
 	bool readArgsFromStdin;
+	//! Map of cookies
+	QHash<QString, QString> cookies;
+	//! Path of the cookie jar file
+	QString cookieJar;
+	
 
 	static QPrinter::PageSize strToPageSize(const char * s, bool * ok=0);
 	static QPair<qreal, QPrinter::Unit> strToUnitReal(const char * s, bool * ok=0);