| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #include "qnetworkreplydataimpl_p.h" |
| 6 | #include "private/qdataurl_p.h" |
| 7 | #include <QtCore/QCoreApplication> |
| 8 | #include <QtCore/QMetaObject> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | QNetworkReplyDataImplPrivate::QNetworkReplyDataImplPrivate() |
| 13 | : QNetworkReplyPrivate() |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | QNetworkReplyDataImplPrivate::~QNetworkReplyDataImplPrivate() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | QNetworkReplyDataImpl::~QNetworkReplyDataImpl() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | QNetworkReplyDataImpl::QNetworkReplyDataImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op) |
| 26 | : QNetworkReply(*new QNetworkReplyDataImplPrivate(), parent) |
| 27 | { |
| 28 | Q_D(QNetworkReplyDataImpl); |
| 29 | setRequest(req); |
| 30 | setUrl(req.url()); |
| 31 | setOperation(op); |
| 32 | setFinished(true); |
| 33 | QNetworkReply::open(mode: QIODevice::ReadOnly); |
| 34 | |
| 35 | QUrl url = req.url(); |
| 36 | QString mimeType; |
| 37 | QByteArray payload; |
| 38 | if (qDecodeDataUrl(url, mimeType, payload)) { |
| 39 | qint64 size = payload.size(); |
| 40 | auto h = headers(); |
| 41 | h.replaceOrAppend(name: QHttpHeaders::WellKnownHeader::ContentType, newValue: mimeType); |
| 42 | h.replaceOrAppend(name: QHttpHeaders::WellKnownHeader::ContentLength, newValue: QByteArray::number(size)); |
| 43 | setHeaders(std::move(h)); |
| 44 | |
| 45 | QMetaObject::invokeMethod(obj: this, member: "metaDataChanged" , c: Qt::QueuedConnection); |
| 46 | |
| 47 | d->decodedData.setData(payload); |
| 48 | d->decodedData.open(openMode: QIODevice::ReadOnly); |
| 49 | |
| 50 | QMetaObject::invokeMethod(obj: this, member: "downloadProgress" , c: Qt::QueuedConnection, |
| 51 | Q_ARG(qint64,size), Q_ARG(qint64, size)); |
| 52 | QMetaObject::invokeMethod(obj: this, member: "readyRead" , c: Qt::QueuedConnection); |
| 53 | QMetaObject::invokeMethod(obj: this, member: "finished" , c: Qt::QueuedConnection); |
| 54 | } else { |
| 55 | // something wrong with this URI |
| 56 | const QString msg = QCoreApplication::translate(context: "QNetworkAccessDataBackend" , |
| 57 | key: "Invalid URI: %1" ).arg(a: url.toString()); |
| 58 | setError(errorCode: QNetworkReply::ProtocolFailure, errorString: msg); |
| 59 | QMetaObject::invokeMethod(obj: this, member: "errorOccurred" , c: Qt::QueuedConnection, |
| 60 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolFailure)); |
| 61 | QMetaObject::invokeMethod(obj: this, member: "finished" , c: Qt::QueuedConnection); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void QNetworkReplyDataImpl::close() |
| 66 | { |
| 67 | QNetworkReply::close(); |
| 68 | } |
| 69 | |
| 70 | void QNetworkReplyDataImpl::abort() |
| 71 | { |
| 72 | QNetworkReply::close(); |
| 73 | } |
| 74 | |
| 75 | qint64 QNetworkReplyDataImpl::bytesAvailable() const |
| 76 | { |
| 77 | Q_D(const QNetworkReplyDataImpl); |
| 78 | return QNetworkReply::bytesAvailable() + d->decodedData.bytesAvailable(); |
| 79 | } |
| 80 | |
| 81 | bool QNetworkReplyDataImpl::isSequential () const |
| 82 | { |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | qint64 QNetworkReplyDataImpl::size() const |
| 87 | { |
| 88 | Q_D(const QNetworkReplyDataImpl); |
| 89 | return d->decodedData.size(); |
| 90 | } |
| 91 | |
| 92 | /*! |
| 93 | \internal |
| 94 | */ |
| 95 | qint64 QNetworkReplyDataImpl::readData(char *data, qint64 maxlen) |
| 96 | { |
| 97 | Q_D(QNetworkReplyDataImpl); |
| 98 | |
| 99 | // TODO idea: |
| 100 | // Instead of decoding the whole data into new memory, we could decode on demand. |
| 101 | // Note that this might be tricky to do. |
| 102 | |
| 103 | return d->decodedData.read(data, maxlen); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | QT_END_NAMESPACE |
| 108 | |
| 109 | #include "moc_qnetworkreplydataimpl_p.cpp" |
| 110 | |
| 111 | |