@@ -64,17 +64,17 @@ struct HttpContext
6464 bool send = false ;
6565};
6666
67- void logHttp (const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIODevice *device, bool cached = false )
67+ void logHttp (const QByteArray &verb, const QHttpHeaders &headers, HttpContext *ctx, QJsonObject &&header, QIODevice *device, bool cached = false )
6868{
6969 static const bool redact = !qEnvironmentVariableIsSet (" OWNCLOUD_HTTPLOGGER_NO_REDACT" );
7070 const auto reply = qobject_cast<QNetworkReply *>(device);
7171 const auto contentLength = device ? device->size () : 0 ;
7272
7373 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]" ));
74+ if (headers. contains (QHttpHeaders::WellKnownHeader:: Authorization)) {
75+ const auto auth = QString::fromUtf8 (headers .value (QHttpHeaders::WellKnownHeader::Authorization) );
76+ header. insert ( QStringLiteral ( " Authorization " ),
77+ auth.startsWith (QStringLiteral (" Bearer " )) ? QStringLiteral (" Bearer [redacted]" ) : QStringLiteral (" Basic [redacted]" ));
7878 }
7979 }
8080
@@ -105,10 +105,7 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
105105
106106 QJsonObject body = {{QStringLiteral (" length" ), contentLength}};
107107 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- }
108+ const auto contentType = QString::fromUtf8 (headers.value (QHttpHeaders::WellKnownHeader::ContentType));
112109 if (isTextBody (contentType)) {
113110 if (!device->isOpen ()) {
114111 Q_ASSERT (dynamic_cast <QBuffer *>(device));
@@ -117,8 +114,7 @@ void logHttp(const QByteArray &verb, HttpContext *ctx, QJsonObject &&header, QIO
117114 }
118115 Q_ASSERT (device->pos () == 0 );
119116 QString data = QString::fromUtf8 (device->peek (PeekSize));
120- if (PeekSize < contentLength)
121- {
117+ if (PeekSize < contentLength) {
122118 data += QStringLiteral (" ...(%1 bytes elided)" ).arg (QString::number (contentLength - PeekSize));
123119 }
124120 body[QStringLiteral (" data" )] = data;
@@ -166,7 +162,7 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
166162 for (const auto &key : request.rawHeaderList ()) {
167163 header[QString::fromUtf8 (key)] = QString::fromUtf8 (request.rawHeader (key));
168164 }
169- logHttp (requestVerb (operation, request), ctx, std::move (header), device, cached);
165+ logHttp (requestVerb (operation, request), request. headers (), ctx, std::move (header), device, cached);
170166 };
171167 QObject::connect (reply, &QNetworkReply::requestSent, reply, logSend, Qt::DirectConnection);
172168
@@ -182,7 +178,7 @@ void HttpLogger::logRequest(QNetworkReply *reply, QNetworkAccessManager::Operati
182178 for (const auto &[key, value] : reply->rawHeaderPairs ()) {
183179 header[QString::fromUtf8 (key)] = QString::fromUtf8 (value);
184180 }
185- logHttp (requestVerb (*reply), ctx.get (), std::move (header), reply);
181+ logHttp (requestVerb (*reply), reply-> headers (), ctx.get (), std::move (header), reply);
186182 },
187183 Qt::DirectConnection);
188184}
0 commit comments