TokenCredentials, DirectDownload: Rework cookie handling

That way we don't override QNAM's cookie jar behaviour
This commit is contained in:
Markus Goetz 2014-08-22 13:46:22 +02:00
parent 824628061b
commit b037e6356e
4 changed files with 28 additions and 5 deletions

View file

@ -17,6 +17,7 @@
#include <QDebug> #include <QDebug>
#include <QNetworkReply> #include <QNetworkReply>
#include <QSettings> #include <QSettings>
#include <QNetworkCookieJar>
#include "mirall/account.h" #include "mirall/account.h"
#include "mirall/mirallaccessmanager.h" #include "mirall/mirallaccessmanager.h"
@ -88,10 +89,9 @@ protected:
QByteArray credHash = QByteArray(_cred->user().toUtf8()+":"+_cred->password().toUtf8()).toBase64(); QByteArray credHash = QByteArray(_cred->user().toUtf8()+":"+_cred->password().toUtf8()).toBase64();
req.setRawHeader(QByteArray("Authorization"), QByteArray("Basic ") + credHash); req.setRawHeader(QByteArray("Authorization"), QByteArray("Basic ") + credHash);
if (!req.hasRawHeader("Cookie")) { // A pre-authenticated cookie
// Only use our token if the request doesn't have a cookie already (e.g. because of direct download URL) QByteArray token = _cred->_token.toUtf8();
req.setRawHeader("Cookie", _cred->_token.toUtf8()); // analogous to neon in syncContextPreStart setRawCookie(token, request.url());
}
return MirallAccessManager::createRequest(op, req, outgoingData); return MirallAccessManager::createRequest(op, req, outgoingData);
} }

View file

@ -15,6 +15,8 @@
#include <QNetworkProxy> #include <QNetworkProxy>
#include <QAuthenticator> #include <QAuthenticator>
#include <QSslConfiguration> #include <QSslConfiguration>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
#include "mirall/cookiejar.h" #include "mirall/cookiejar.h"
#include "mirall/mirallaccessmanager.h" #include "mirall/mirallaccessmanager.h"
@ -37,9 +39,27 @@ MirallAccessManager::MirallAccessManager(QObject* parent)
this, SLOT(slotProxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); this, SLOT(slotProxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
} }
void MirallAccessManager::setRawCookie(const QByteArray &rawCookie, const QUrl &url)
{
QNetworkCookie cookie(rawCookie.left(rawCookie.indexOf('=')),
rawCookie.mid(rawCookie.indexOf('=')+1));
qDebug() << Q_FUNC_INFO << cookie.name() << cookie.value();
QList<QNetworkCookie> cookieList;
cookieList.append(cookie);
QNetworkCookieJar *jar = cookieJar();
jar->setCookiesFromUrl(cookieList, url);
}
QNetworkReply* MirallAccessManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& request, QIODevice* outgoingData) QNetworkReply* MirallAccessManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& request, QIODevice* outgoingData)
{ {
QNetworkRequest newRequest(request); QNetworkRequest newRequest(request);
if (newRequest.hasRawHeader("cookie")) {
// This will set the cookie into the QNetworkCookieJar which will then override the cookie header
setRawCookie(request.rawHeader("cookie"), request.url());
}
newRequest.setRawHeader(QByteArray("User-Agent"), Utility::userAgentString()); newRequest.setRawHeader(QByteArray("User-Agent"), Utility::userAgentString());
QByteArray verb = newRequest.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray(); QByteArray verb = newRequest.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray();
// For PROPFIND (assumed to be a WebDAV op), set xml/utf8 as content type/encoding // For PROPFIND (assumed to be a WebDAV op), set xml/utf8 as content type/encoding

View file

@ -17,6 +17,8 @@
#include "owncloudlib.h" #include "owncloudlib.h"
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
class QByteArray;
class QUrl;
namespace Mirall namespace Mirall
{ {
@ -27,6 +29,8 @@ class OWNCLOUDSYNC_EXPORT MirallAccessManager : public QNetworkAccessManager
public: public:
MirallAccessManager(QObject* parent = 0); MirallAccessManager(QObject* parent = 0);
void setRawCookie(const QByteArray &rawCookie, const QUrl &url);
protected: protected:
QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& request, QIODevice* outgoingData = 0); QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& request, QIODevice* outgoingData = 0);
protected slots: protected slots:

View file

@ -528,7 +528,6 @@ void GETFileJob::start() {
setReply(davRequest("GET", path(), req)); setReply(davRequest("GET", path(), req));
} else { } else {
// Use direct URL // Use direct URL
req.setAttribute(QNetworkRequest::CookieLoadControlAttribute, QNetworkRequest::Manual);
setReply(davRequest("GET", _directDownloadUrl, req)); setReply(davRequest("GET", _directDownloadUrl, req));
} }
setupConnections(reply()); setupConnections(reply());