Permalink
Browse files

Add cookiejar load/save, convert last use of wget

  • Loading branch information...
1 parent 3988d57 commit 5588b9ab41c9010f017366543bfdb300c2c5a1c8 @Beirdo Beirdo committed Feb 9, 2012
@@ -7,6 +7,7 @@
#include <QDir>
#include <QNetworkCookieJar>
#include <QAuthenticator>
+#include <QTextStream>
#include "stdlib.h"
@@ -1324,6 +1325,55 @@ QDateTime MythDownloadManager::GetLastModified(const QString &url)
return result;
}
+class MythCookieJar : public QNetworkCookieJar
+{
+ public:
+ void load(const QString &filename);
+ void save(const QString &filename);
+};
+
+void MythCookieJar::load(const QString &filename)
+{
+ QList<QNetworkCookie> cookieList;
+ QTextStream stream((QString *)&filename, QIODevice::ReadOnly);
+ while (!stream.atEnd())
+ {
+ QString cookie = stream.readLine();
+ cookieList << QNetworkCookie::parseCookies(cookie.toLocal8Bit());
+ }
+
+ setAllCookies(cookieList);
+}
+
+void MythCookieJar::save(const QString &filename)
+{
+ QList<QNetworkCookie> cookieList = allCookies();
+ QTextStream stream((QString *)&filename, QIODevice::WriteOnly);
+
+ for (QList<QNetworkCookie>::iterator it = cookieList.begin();
+ it != cookieList.end(); ++it)
+ {
+ stream << (*it).toRawForm() << endl;
+ }
+}
+
+
+void MythDownloadManager::loadCookieJar(const QString &filename)
+{
+ MythCookieJar *jar = new MythCookieJar;
+ jar->load(filename);
+ m_manager->setCookieJar(jar);
+}
+
+void MythDownloadManager::saveCookieJar(const QString &filename)
+{
+ if (!m_manager->cookieJar())
+ return;
+
+ MythCookieJar *jar = static_cast<MythCookieJar *>(m_manager->cookieJar());
+ jar->save(filename);
+}
+
QString MythDownloadManager::getHeader(const QNetworkCacheMetaData &cacheData,
const QString& header)
{
@@ -68,8 +68,11 @@ class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
void removeListener(QObject *caller);
QDateTime GetLastModified(const QString &url);
+ void loadCookieJar(const QString &filename);
+ void saveCookieJar(const QString &filename);
QNetworkCookieJar *getCookieJar(void) { return m_manager->cookieJar(); }
void setCookieJar(QNetworkCookieJar *cookieJar) { m_manager->setCookieJar(cookieJar); }
+
QString getHeader(const QUrl &url, const QString &header) { return getHeader(m_manager->cache()->metaData(url), header); }
QString getHeader(const QNetworkCacheMetaData &cacheData, const QString &header);
@@ -965,7 +965,6 @@ void DataDirectProcessor::DataDirectProgramUpdate(void)
void authenticationCallback(QNetworkReply *reply, QAuthenticator *auth,
void *arg)
{
- LOG(VB_GENERAL, LOG_DEBUG, "main callback");
if (!arg)
return;
@@ -976,7 +975,6 @@ void authenticationCallback(QNetworkReply *reply, QAuthenticator *auth,
void DataDirectProcessor::authenticationCallback(QNetworkReply *reply,
QAuthenticator *auth)
{
- LOG(VB_GENERAL, LOG_DEBUG, "class callback");
(void)reply;
auth->setUser(GetUserID());
auth->setPassword(GetPassword());
@@ -1844,56 +1842,32 @@ bool DataDirectProcessor::Post(QString url, const PostList &list,
QString documentFile,
QString inCookieFile, QString outCookieFile)
{
- QString dfile = QString("'%1' ").arg(documentFile);
- QString command = "wget ";
+ MythDownloadManager *manager = GetMythDownloadManager();
if (!inCookieFile.isEmpty())
- command += QString("--load-cookies=%1 ").arg(inCookieFile);
+ manager->loadCookieJar(inCookieFile);
- if (!outCookieFile.isEmpty())
- {
- command += "--keep-session-cookies ";
- command += QString("--save-cookies=%1 ").arg(outCookieFile);
- }
-
- QString post_data = "";
+ QByteArray postdata;
for (uint i = 0; i < list.size(); i++)
{
- post_data += ((i) ? "&" : "") + list[i].key + "=";
- post_data += html_escape(list[i].value);
+ postdata += ((i) ? "&" : "") + list[i].key + "=";
+ postdata += html_escape(list[i].value);
}
- if (post_data.length())
- command += "--post-data='" + post_data + "' ";
-
- command += url;
- command += " ";
-
- {
- QMutexLocker locker(&user_agent_lock);
- command += QString("--user-agent='%1' ").arg(user_agent);
- }
-
- command += "--output-document=";
- command += (documentFile.isEmpty()) ? "- " : dfile;
-
-#ifdef USING_MINGW
- command.replace("'", "\"");
-#endif
-
- if (SHOW_WGET_OUTPUT)
- LOG(VB_GENERAL, LOG_DEBUG, LOC + "command: " + command);
- else
- {
- command += (documentFile.isEmpty()) ? "&> " : "2> ";
- command += "/dev/null ";
- }
+ if (!manager->post(url, &postdata))
+ return false;
- myth_system(command);
+ if (!outCookieFile.isEmpty())
+ manager->saveCookieJar(outCookieFile);
if (documentFile.isEmpty())
return true;
+ QFile file(documentFile);
+ file.open(QIODevice::WriteOnly);
+ file.write(postdata);
+ file.close();
+
QFileInfo fi(documentFile);
return fi.size();
}

0 comments on commit 5588b9a

Please sign in to comment.