File indexing completed on 2025-02-16 04:51:24

0001 /*
0002     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "httpresponse.h"
0007 #include "logging.h"
0008 
0009 #include <QJsonArray>
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #ifdef QT_NETWORK_LIB
0013 #include <QNetworkReply>
0014 #endif
0015 
0016 using namespace KItinerary;
0017 
0018 namespace KItinerary {
0019 class HttpResponsePrivate : public QSharedData {
0020 public:
0021     QUrl url;
0022     QByteArray content;
0023     QDateTime requestDateTime;
0024     // expand with HTTP headers, status code, etc as necessary
0025 };
0026 }
0027 
0028 HttpResponse::HttpResponse()
0029     : d(new HttpResponsePrivate)
0030 {
0031 }
0032 
0033 HttpResponse::~HttpResponse() = default;
0034 HttpResponse::HttpResponse(const HttpResponse&) = default;
0035 HttpResponse& HttpResponse::operator=(const HttpResponse&) = default;
0036 
0037 HttpResponse::operator QVariant() const
0038 {
0039     return QVariant::fromValue(*this);
0040 }
0041 
0042 QUrl HttpResponse::url() const
0043 {
0044     return d->url;
0045 }
0046 
0047 QByteArray HttpResponse::content() const
0048 {
0049     return d->content;
0050 }
0051 
0052 QDateTime HttpResponse::requestDateTime() const
0053 {
0054     return d->requestDateTime;
0055 }
0056 
0057 HttpResponse HttpResponse::fromNetworkReply(QNetworkReply *reply)
0058 {
0059     HttpResponse r;
0060 #ifdef QT_NETWORK_LIB
0061     r.d->url = reply->url();
0062     r.d->content = reply->readAll();
0063     r.d->requestDateTime = QDateTime::currentDateTime();
0064 #endif
0065     return r;
0066 }
0067 
0068 HttpResponse HttpResponse::fromHarEntry(const QJsonObject &harEntry)
0069 {
0070     HttpResponse r;
0071     r.d->url = QUrl(harEntry.value(QLatin1StringView("request"))
0072                         .toObject()
0073                         .value(QLatin1String("url"))
0074                         .toString());
0075     const auto content = harEntry.value(QLatin1StringView("response"))
0076                              .toObject()
0077                              .value(QLatin1String("content"))
0078                              .toObject();
0079 
0080     r.d->content = content.value(QLatin1StringView("text")).toString().toUtf8();
0081     if (content.value(QLatin1StringView("encoding")).toString() ==
0082         QLatin1String("base64")) {
0083       r.d->content = QByteArray::fromBase64(r.d->content);
0084     }
0085 
0086     r.d->requestDateTime = QDateTime::fromString(
0087         harEntry.value(QLatin1StringView("startedDateTime")).toString(),
0088         Qt::ISODateWithMs);
0089     return r;
0090 }
0091 
0092 QList<HttpResponse> HttpResponse::fromHarFile(const QByteArray &harFile)
0093 {
0094     QJsonParseError error;
0095     const auto doc = QJsonDocument::fromJson(harFile, &error);
0096     if (error.error != QJsonParseError::NoError) {
0097         qCWarning(Log) << error.errorString() << error.offset;
0098         return {};
0099     }
0100 
0101     const auto entries = QJsonDocument::fromJson(harFile)
0102                              .object()
0103                              .value(QLatin1StringView("log"))
0104                              .toObject()
0105                              .value(QLatin1String("entries"))
0106                              .toArray();
0107     QList<HttpResponse> result;
0108     result.reserve(entries.size());
0109     for (const auto &entry : entries) {
0110         result.push_back(HttpResponse::fromHarEntry(entry.toObject()));
0111     }
0112     return result;
0113 }
0114 
0115 #include "moc_httpresponse.cpp"