2013-08-01 18:22:54 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Krzesimir Nowak <krzesimir@endocode.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QNetworkRequest>
|
2014-08-12 21:24:41 +04:00
|
|
|
#include <QNetworkReply>
|
2014-02-05 19:27:57 +04:00
|
|
|
#include <QNetworkProxy>
|
2014-03-06 20:45:02 +04:00
|
|
|
#include <QAuthenticator>
|
2014-05-15 11:43:07 +04:00
|
|
|
#include <QSslConfiguration>
|
2014-08-22 15:46:22 +04:00
|
|
|
#include <QNetworkCookie>
|
|
|
|
#include <QNetworkCookieJar>
|
2013-08-01 18:22:54 +04:00
|
|
|
|
2014-08-12 23:07:14 +04:00
|
|
|
#include "authenticationdialog.h"
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "cookiejar.h"
|
|
|
|
#include "mirallaccessmanager.h"
|
|
|
|
#include "utility.h"
|
2013-08-01 18:22:54 +04:00
|
|
|
|
|
|
|
namespace Mirall
|
|
|
|
{
|
|
|
|
|
|
|
|
MirallAccessManager::MirallAccessManager(QObject* parent)
|
|
|
|
: QNetworkAccessManager (parent)
|
2013-10-21 23:42:52 +04:00
|
|
|
{
|
2014-04-25 12:50:20 +04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) && defined(Q_OS_MAC)
|
2014-02-05 19:27:57 +04:00
|
|
|
// FIXME Workaround http://stackoverflow.com/a/15707366/2941 https://bugreports.qt-project.org/browse/QTBUG-30434
|
|
|
|
QNetworkProxy proxy = this->proxy();
|
|
|
|
proxy.setHostName(" ");
|
|
|
|
setProxy(proxy);
|
|
|
|
#endif
|
2014-05-14 13:11:45 +04:00
|
|
|
setCookieJar(new CookieJar);
|
2014-08-12 21:24:41 +04:00
|
|
|
connect(this, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
|
|
|
|
this, SLOT(slotProxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
|
|
|
|
connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
|
|
|
|
this, SLOT(slotAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
|
|
|
|
|
2013-10-21 23:42:52 +04:00
|
|
|
}
|
2013-08-01 18:22:54 +04:00
|
|
|
|
2014-08-22 15:46:22 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:22:54 +04:00
|
|
|
QNetworkReply* MirallAccessManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest& request, QIODevice* outgoingData)
|
|
|
|
{
|
|
|
|
QNetworkRequest newRequest(request);
|
2014-08-22 15:46:22 +04:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2013-10-21 23:42:52 +04:00
|
|
|
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
|
|
|
|
// This needs extension
|
|
|
|
if (verb == "PROPFIND") {
|
|
|
|
newRequest.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1String("text/xml; charset=utf-8"));
|
|
|
|
}
|
|
|
|
return QNetworkAccessManager::createRequest(op, newRequest, outgoingData);
|
2013-08-01 18:22:54 +04:00
|
|
|
}
|
|
|
|
|
2014-03-06 20:45:02 +04:00
|
|
|
void MirallAccessManager::slotProxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
|
|
|
|
{
|
|
|
|
Q_UNUSED(authenticator);
|
|
|
|
qDebug() << Q_FUNC_INFO << proxy.type();
|
|
|
|
// We put in the password here and in ClientProxy in the proxy itself.
|
|
|
|
if (!proxy.user().isEmpty() || !proxy.password().isEmpty()) {
|
|
|
|
authenticator->setUser(proxy.user());
|
|
|
|
authenticator->setPassword(proxy.password());
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 21:24:41 +04:00
|
|
|
void MirallAccessManager::slotAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
|
|
|
|
{
|
|
|
|
// do not handle 401 created by the networkjobs. We may want
|
|
|
|
// to eventually exempt some, but for now we need
|
|
|
|
// it only for other things, e.g. the browser. Would we handle
|
|
|
|
// network jobs, this would break the wizard logic
|
|
|
|
if (reply->property("doNotHandleAuth").toBool()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QUrl url = reply->url();
|
|
|
|
// show only scheme, host and port
|
|
|
|
QUrl reducedUrl;
|
|
|
|
reducedUrl.setScheme(url.scheme());
|
|
|
|
reducedUrl.setHost(url.host());
|
|
|
|
reducedUrl.setPort(url.port());
|
|
|
|
|
|
|
|
AuthenticationDialog dialog(authenticator->realm(), reducedUrl.toString());
|
|
|
|
if (dialog.exec() == QDialog::Accepted) {
|
|
|
|
authenticator->setUser(dialog.user());
|
|
|
|
authenticator->setPassword(dialog.password());
|
|
|
|
}
|
|
|
|
}
|
2014-03-06 20:45:02 +04:00
|
|
|
|
2013-08-01 18:22:54 +04:00
|
|
|
} // ns Mirall
|