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