Skip to content

Commit

Permalink
HttpLogger: Log redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Apr 10, 2024
1 parent 0f41121 commit 15913ee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/11581
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Support logging redirect

We now log when all urls when a request was redirected.

https://github.com/owncloud/client/pull/11581
62 changes: 44 additions & 18 deletions src/libsync/httplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "common/utility.h"

#include <QBuffer>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLoggingCategory>
Expand All @@ -42,12 +43,21 @@ bool isTextBody(const QString &s)
struct HttpContext
{
HttpContext(const QNetworkRequest &request)
: url(request.url().toString())
: originalUrl(request.url().toString())
, lastUrl(request.url())
, id(QString::fromUtf8(request.rawHeader(QByteArrayLiteral("X-Request-ID"))))
{
}

const QString url;
void addRedirect(const QUrl &url)
{
lastUrl = url;
redirectUrls.append(url.toString());
}

const QString originalUrl;
QUrl lastUrl;
QStringList redirectUrls;
const QString id;

OCC::Utility::ChronoElapsedTimer timer;
Expand All @@ -66,7 +76,11 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
header.insert(authKey, auth.startsWith(QStringLiteral("Bearer ")) ? QStringLiteral("Bearer [redacted]") : QStringLiteral("Basic [redacted]"));
}

QJsonObject info{{QStringLiteral("method"), QString::fromUtf8(verb)}, {QStringLiteral("id"), ctx->id}, {QStringLiteral("url"), ctx->url}};
QJsonObject info{{QStringLiteral("method"), QString::fromUtf8(verb)}, {QStringLiteral("id"), ctx->id}, {QStringLiteral("url"), ctx->originalUrl}};

if (!ctx->redirectUrls.isEmpty()) {
info.insert(QStringLiteral("redirects"), QJsonArray::fromStringList(ctx->redirectUrls));
}

if (reply) {
// respond
Expand Down Expand Up @@ -130,9 +144,17 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
// device should still exist, lets still use a qpointer to ensure we have valid data
const auto logSend = [ctx = ctx.get(), operation, reply, device = QPointer<QIODevice>(device), deviceRaw = device](bool cached = false) {
Q_ASSERT(!deviceRaw || device);
Q_ASSERT(!ctx->send);
ctx->send = true;
ctx->timer.reset();
if (!ctx->send) {
ctx->send = true;
ctx->timer.reset();
} else {
// this is a redirect
if (ctx->lastUrl != reply->url()) {
ctx->addRedirect(reply->url());
} else {
Q_UNREACHABLE();
}
}

const auto request = reply->request();
QJsonObject header;
Expand All @@ -141,19 +163,23 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
}
logHttp(requestVerb(operation, request), ctx, std::move(header), device, cached);
};
QObject::connect(reply, &QNetworkReply::requestSent, reply, logSend);
QObject::connect(reply, &QNetworkReply::requestSent, reply, logSend, Qt::DirectConnection);

QObject::connect(reply, &QNetworkReply::finished, reply, [reply, ctx = std::move(ctx), logSend] {
ctx->timer.stop();
if (!ctx->send) {
logSend(true);
}
QJsonObject header;
for (const auto &[key, value] : reply->rawHeaderPairs()) {
header[QString::fromUtf8(key)] = QString::fromUtf8(value);
}
logHttp(requestVerb(*reply), ctx.get(), std::move(header), reply);
});

QObject::connect(
reply, &QNetworkReply::finished, reply,
[reply, ctx = std::move(ctx), logSend] {
ctx->timer.stop();
if (!ctx->send) {
logSend(true);
}
QJsonObject header;
for (const auto &[key, value] : reply->rawHeaderPairs()) {
header[QString::fromUtf8(key)] = QString::fromUtf8(value);
}
logHttp(requestVerb(*reply), ctx.get(), std::move(header), reply);
},
Qt::DirectConnection);
}

QByteArray HttpLogger::requestVerb(QNetworkAccessManager::Operation operation, const QNetworkRequest &request)
Expand Down

0 comments on commit 15913ee

Please sign in to comment.