1818#include " common/utility.h"
1919
2020#include < QBuffer>
21+ #include < QHttpHeaders>
2122#include < QJsonArray>
2223#include < QJsonDocument>
2324#include < QJsonObject>
@@ -64,17 +65,17 @@ struct HttpContext
6465 bool send = false ;
6566};
6667
67- void logHttp (const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIODevice *device, bool cached = false )
68+ void logHttp (const QByteArray &verb, const QHttpHeaders &headers, HttpContext *ctx, QJsonObject &&header, QIODevice *device, bool cached = false )
6869{
6970 static const bool redact = !qEnvironmentVariableIsSet (" OWNCLOUD_HTTPLOGGER_NO_REDACT" );
7071 const auto reply = qobject_cast<QNetworkReply *>(device);
7172 const auto contentLength = device ? device->size () : 0 ;
7273
7374 if (redact) {
74- const QString authKey = QStringLiteral ( " Authorization" );
75- const QString auth = header .value (authKey). toString ( );
76- if (!auth. isEmpty ()) {
77- header. insert (authKey, auth.startsWith (QStringLiteral (" Bearer " )) ? QStringLiteral (" Bearer [redacted]" ) : QStringLiteral (" Basic [redacted]" ));
75+ if (headers. contains (QHttpHeaders::WellKnownHeader:: Authorization)) {
76+ const auto auth = QString::fromUtf8 (headers .value (QHttpHeaders::WellKnownHeader::Authorization) );
77+ header. insert ( QStringLiteral ( " Authorization " ),
78+ auth.startsWith (QStringLiteral (" Bearer " )) ? QStringLiteral (" Bearer [redacted]" ) : QStringLiteral (" Basic [redacted]" ));
7879 }
7980 }
8081
@@ -105,10 +106,7 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
105106
106107 QJsonObject body = {{QStringLiteral (" length" ), contentLength}};
107108 if (contentLength > 0 ) {
108- QString contentType = header.value (QStringLiteral (" Content-Type" )).toString ();
109- if (contentType.isEmpty ()) {
110- contentType = header.value (QStringLiteral (" content-type" )).toString ();
111- }
109+ const auto contentType = QString::fromUtf8 (headers.value (QHttpHeaders::WellKnownHeader::ContentType));
112110 if (isTextBody (contentType)) {
113111 if (!device->isOpen ()) {
114112 Q_ASSERT (dynamic_cast <QBuffer *>(device));
@@ -117,8 +115,7 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
117115 }
118116 Q_ASSERT (device->pos () == 0 );
119117 QString data = QString::fromUtf8 (device->peek (PeekSize));
120- if (PeekSize < contentLength)
121- {
118+ if (PeekSize < contentLength) {
122119 data += QStringLiteral (" ...(%1 bytes elided)" ).arg (QString::number (contentLength - PeekSize));
123120 }
124121 body[QStringLiteral (" data" )] = data;
@@ -163,10 +160,14 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
163160
164161 const auto request = reply->request ();
165162 QJsonObject header;
163+ QHttpHeaders h = {};
166164 for (const auto &key : request.rawHeaderList ()) {
167- header[QString::fromUtf8 (key)] = QString::fromUtf8 (request.rawHeader (key));
165+ auto k = QString::fromUtf8 (key);
166+ auto v = QString::fromUtf8 (request.rawHeader (key));
167+ header[k] = v;
168+ h.append (k, v);
168169 }
169- logHttp (requestVerb (operation, request), ctx, std::move (header), device, cached);
170+ logHttp (requestVerb (operation, request), h, ctx, std::move (header), device, cached);
170171 };
171172 QObject::connect (reply, &QNetworkReply::requestSent, reply, logSend, Qt::DirectConnection);
172173
@@ -179,10 +180,14 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
179180 logSend (true );
180181 }
181182 QJsonObject header;
183+ QHttpHeaders h = {};
182184 for (const auto &[key, value] : reply->rawHeaderPairs ()) {
183- header[QString::fromUtf8 (key)] = QString::fromUtf8 (value);
185+ auto k = QString::fromUtf8 (key);
186+ auto v = QString::fromUtf8 (value);
187+ header[k] = v;
188+ h.append (k, v);
184189 }
185- logHttp (requestVerb (*reply), ctx.get (), std::move (header), reply);
190+ logHttp (requestVerb (*reply), h, ctx.get (), std::move (header), reply);
186191 },
187192 Qt::DirectConnection);
188193}
0 commit comments