1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* This file is part of buteo-sync-plugin-caldav package
*
* Copyright (C) 2013 Jolla Ltd. and/or its subsidiary(-ies).
*
* Contributors: Bea Lam <bea.lam@jollamobile.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "request_p.h"
#include "logging_p.h"
Request::Request(QNetworkAccessManager *manager,
Settings *settings,
const QString &requestType,
QObject *parent)
: QObject(parent)
, mNAManager(manager)
, REQUEST_TYPE(requestType)
, mSettings(settings)
, mNetworkError(QNetworkReply::NoError)
{
}
bool Request::hasError() const
{
return mErrorOccurred;
}
QString Request::errorMessage() const
{
return mErrorMessage;
}
QByteArray Request::errorData() const
{
return mErrorData;
}
QNetworkReply::NetworkError Request::networkError() const
{
return mNetworkError;
}
QString Request::command() const
{
return REQUEST_TYPE;
}
void Request::finishedWithReplyResult(const QString &uri, QNetworkReply *reply)
{
mNetworkError = reply->error();
if (reply->error() == QNetworkReply::NoError) {
debugReplyAndReadAll(reply);
finishedWithSuccess(uri);
} else {
int code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qCWarning(lcDav) << "The" << command()
<< "operation failed with error:" << reply->error()
<< ", HTTP code:" << code;
const QByteArray data(reply->readAll());
debugReply(*reply, data);
finishedWithError(uri,
QString("Network request failed with QNetworkReply::NetworkError: %1").arg(reply->error()),
data);
}
}
void Request::slotSslErrors(QList<QSslError> errors)
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (!reply) {
return;
}
debugReplyAndReadAll(reply);
if (mSettings->ignoreSSLErrors()) {
qCDebug(lcDav) << "Ignoring SSL error response";
reply->ignoreSslErrors(errors);
} else {
qCWarning(lcDav) << command() << "request received SSL error response!";
}
}
void Request::requestFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (!reply) {
finishedWithInternalError(QString());
return;
}
reply->deleteLater();
qCDebug(lcDav) << command() << "request finished:" << reply->error();
handleReply(reply);
}
void Request::finishedWithError(const QString &uri, const QString &errorString, const QByteArray &errorData)
{
mErrorOccurred = true;
mErrorMessage = errorString;
if (mErrorMessage.isEmpty()) {
mErrorMessage = QString::fromLatin1("request %1 failure at %2").arg(command()).arg(uri);
}
mErrorData = errorData;
emit finished(uri);
}
void Request::finishedWithInternalError(const QString &uri, const QString &errorString)
{
finishedWithError(uri, errorString.isEmpty() ? QStringLiteral("Internal error") : errorString,
QByteArray());
}
void Request::finishedWithSuccess(const QString &uri)
{
mErrorOccurred = false;
emit finished(uri);
}
void Request::prepareRequest(QNetworkRequest *request, const QString &requestPath)
{
QUrl url(mSettings->serverAddress());
if (!mSettings->authToken().isEmpty()) {
request->setRawHeader(QString("Authorization").toLatin1(),
QString("Bearer " + mSettings->authToken()).toLatin1());
} else if (mSettings->serverAddress().endsWith(QStringLiteral(".yahoo.com"))
|| mSettings->serverAddress().endsWith(QStringLiteral(".icloud.com"))) {
request->setRawHeader(QString("Authorization").toLatin1(),
QByteArray("Basic ") + QString::fromLatin1("%1:%2").arg(mSettings->username()).arg(mSettings->password()).toLatin1().toBase64());
} else {
url.setUserName(mSettings->username());
url.setPassword(mSettings->password());
}
url.setPath(requestPath);
request->setUrl(url);
}
void Request::debugRequest(const QNetworkRequest &request, const QByteArray &data)
{
const QStringList lines = debuggingString(request, data).split('\n', QString::SkipEmptyParts);
for (QString line : lines) {
qCDebug(lcDav) << line.replace('\r', ' ');
}
}
void Request::debugRequest(const QNetworkRequest &request, const QString &data)
{
const QStringList lines = debuggingString(request, data.toUtf8()).split('\n', QString::SkipEmptyParts);
for (QString line : lines) {
qCDebug(lcDav) << line.replace('\r', ' ');
}
}
void Request::debugReply(const QNetworkReply &reply, const QByteArray &data)
{
const QStringList lines = debuggingString(reply, data).split('\n', QString::SkipEmptyParts);
for (QString line : lines) {
qCDebug(lcDav) << line.replace('\r', ' ');
}
}
void Request::debugReplyAndReadAll(QNetworkReply *reply)
{
const QStringList lines = debuggingString(*reply, reply->readAll()).split('\n', QString::SkipEmptyParts);
for (QString line : lines) {
qCDebug(lcDav) << line.replace('\r', ' ');
}
}
QString Request::debuggingString(const QNetworkRequest &request, const QByteArray &data)
{
QStringList text;
text += "---------------------------------------------------------------------";
const QList<QByteArray> &rawHeaderList = request.rawHeaderList();
for (const QByteArray &rawHeader : rawHeaderList) {
const QByteArray &rawHeaderData = request.rawHeader(rawHeader);
if (rawHeader == "Authorization" && rawHeaderData.startsWith("Basic")) {
text += rawHeader + " : " + "Basic user:password";
} else {
text += rawHeader + " : " + rawHeaderData;
}
}
QUrl censoredUrl = request.url();
censoredUrl.setUserName(QStringLiteral("user"));
censoredUrl.setPassword(QStringLiteral("pass"));
text += "URL = " + censoredUrl.toString();
text += "Request : " + REQUEST_TYPE + "\n" + data;
text += "---------------------------------------------------------------------\n";
return text.join(QChar('\n'));
}
QString Request::debuggingString(const QNetworkReply &reply, const QByteArray &data)
{
QStringList text;
text += "---------------------------------------------------------------------";
text += REQUEST_TYPE + " response status code: " + reply.attribute(QNetworkRequest::HttpStatusCodeAttribute).toString();
const QList<QNetworkReply::RawHeaderPair> headers = reply.rawHeaderPairs();
text += REQUEST_TYPE + " response headers:";
for (const QNetworkReply::RawHeaderPair header : headers) {
text += "\t" + header.first + " : " + header.second;
}
if (!data.isEmpty()) {
text += REQUEST_TYPE + " response data:" + data;
}
text += "---------------------------------------------------------------------\n";
return text.join(QChar('\n'));
}
|