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 <QNetworkReply>
#include <QSettings>
#include <QNetworkCookieJar>
#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);
}

View file

@ -15,6 +15,8 @@
#include <QNetworkProxy>
#include <QAuthenticator>
#include <QSslConfiguration>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
#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<QNetworkCookie> 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

View file

@ -17,6 +17,8 @@
#include "owncloudlib.h"
#include <QNetworkAccessManager>
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:

View file

@ -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());