diff --git a/src/creds/tokencredentials.cpp b/src/creds/tokencredentials.cpp index 613dbd808..e8eaa65d9 100644 --- a/src/creds/tokencredentials.cpp +++ b/src/creds/tokencredentials.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "mirall/account.h" #include "mirall/mirallaccessmanager.h" @@ -88,10 +89,9 @@ protected: QByteArray credHash = QByteArray(_cred->user().toUtf8()+":"+_cred->password().toUtf8()).toBase64(); req.setRawHeader(QByteArray("Authorization"), QByteArray("Basic ") + credHash); - if (!req.hasRawHeader("Cookie")) { - // Only use our token if the request doesn't have a cookie already (e.g. because of direct download URL) - req.setRawHeader("Cookie", _cred->_token.toUtf8()); // analogous to neon in syncContextPreStart - } + // A pre-authenticated cookie + QByteArray token = _cred->_token.toUtf8(); + setRawCookie(token, request.url()); return MirallAccessManager::createRequest(op, req, outgoingData); } diff --git a/src/mirall/mirallaccessmanager.cpp b/src/mirall/mirallaccessmanager.cpp index ddb9be013..f2d5bf607 100644 --- a/src/mirall/mirallaccessmanager.cpp +++ b/src/mirall/mirallaccessmanager.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include "mirall/cookiejar.h" #include "mirall/mirallaccessmanager.h" @@ -37,9 +39,27 @@ MirallAccessManager::MirallAccessManager(QObject* parent) 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 cookieList; + cookieList.append(cookie); + + QNetworkCookieJar *jar = cookieJar(); + jar->setCookiesFromUrl(cookieList, url); +} + QNetworkReply* MirallAccessManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& request, QIODevice* outgoingData) { 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()); QByteArray verb = newRequest.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray(); // For PROPFIND (assumed to be a WebDAV op), set xml/utf8 as content type/encoding diff --git a/src/mirall/mirallaccessmanager.h b/src/mirall/mirallaccessmanager.h index 149dd5ae4..adba5eb8b 100644 --- a/src/mirall/mirallaccessmanager.h +++ b/src/mirall/mirallaccessmanager.h @@ -17,6 +17,8 @@ #include "owncloudlib.h" #include +class QByteArray; +class QUrl; namespace Mirall { @@ -27,6 +29,8 @@ class OWNCLOUDSYNC_EXPORT MirallAccessManager : public QNetworkAccessManager public: MirallAccessManager(QObject* parent = 0); + void setRawCookie(const QByteArray &rawCookie, const QUrl &url); + protected: QNetworkReply* createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& request, QIODevice* outgoingData = 0); protected slots: diff --git a/src/mirall/propagator_qnam.cpp b/src/mirall/propagator_qnam.cpp index 1658ac593..66e3f261c 100644 --- a/src/mirall/propagator_qnam.cpp +++ b/src/mirall/propagator_qnam.cpp @@ -528,7 +528,6 @@ void GETFileJob::start() { setReply(davRequest("GET", path(), req)); } else { // Use direct URL - req.setAttribute(QNetworkRequest::CookieLoadControlAttribute, QNetworkRequest::Manual); setReply(davRequest("GET", _directDownloadUrl, req)); } setupConnections(reply());