Skip to content

Commit

Permalink
Use QNetworkReply::requestSent to improve httplog
Browse files Browse the repository at this point in the history
Fixes: #10313
  • Loading branch information
TheOneRing committed May 30, 2023
1 parent 26476e5 commit bdf6faf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/10313
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Log http request when it is send

We now log the http request when it is send, not when it is created.
This will elable us to print more headers set by the platform and messaure the request time more accurately.

https://github.com/owncloud/client/issues/10313
57 changes: 30 additions & 27 deletions src/libsync/httplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#include "common/chronoelapsedtimer.h"
#include "common/utility.h"

#include <QRegularExpression>
#include <QLoggingCategory>
#include <QBuffer>
#include <QLoggingCategory>
#include <QPointer>
#include <QRegularExpression>

#include <memory>

using namespace std::chrono;

Expand All @@ -29,7 +31,8 @@ Q_LOGGING_CATEGORY(lcNetworkHttp, "sync.httplogger", QtWarningMsg)

const qint64 PeekSize = 1024 * 1024;

const QByteArray XRequestId(){
const QByteArray XRequestId()
{
return QByteArrayLiteral("X-Request-ID");
}

Expand Down Expand Up @@ -110,30 +113,30 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
if (!lcNetworkHttp().isInfoEnabled()) {
return;
}
const auto timer = Utility::ChronoElapsedTimer();
const auto request = reply->request();

const auto keys = request.rawHeaderList();
QList<QNetworkReply::RawHeaderPair> header;
header.reserve(keys.size());
for (const auto &key : keys) {
header << qMakePair(key, request.rawHeader(key));
}
logHttp(requestVerb(operation, request),
request.url().toString(),
request.rawHeader(XRequestId()),
request.header(QNetworkRequest::ContentTypeHeader).toString(),
header,
device);

QObject::connect(reply, &QNetworkReply::finished, reply, [reply, timer] {
logHttp(requestVerb(*reply),
reply->url().toString(),
reply->request().rawHeader(XRequestId()),
reply->header(QNetworkRequest::ContentTypeHeader).toString(),
reply->rawHeaderPairs(),
reply,
timer.duration());
auto timer = std::make_unique<Utility::ChronoElapsedTimer>();

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
// device should still exist, lets still use a qpointer to ensure we have valid data
QObject::connect(
reply, &QNetworkReply::requestSent, reply, [timer = timer.get(), operation, reply, device = QPointer<QIODevice>(device), deviceRaw = device] {
Q_ASSERT(!deviceRaw || device);
timer->reset();

const auto request = reply->request();
const auto keys = request.rawHeaderList();
QList<QNetworkReply::RawHeaderPair> header;
header.reserve(keys.size());
for (const auto &key : keys) {
header << qMakePair(key, request.rawHeader(key));
}
logHttp(requestVerb(operation, request), request.url().toString(), request.rawHeader(XRequestId()),
request.header(QNetworkRequest::ContentTypeHeader).toString(), header, device);
});
#endif

QObject::connect(reply, &QNetworkReply::finished, reply, [reply, timer = std::move(timer)] {
logHttp(requestVerb(*reply), reply->url().toString(), reply->request().rawHeader(XRequestId()),
reply->header(QNetworkRequest::ContentTypeHeader).toString(), reply->rawHeaderPairs(), reply, timer->duration());
});
}

Expand Down

0 comments on commit bdf6faf

Please sign in to comment.