Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnetworkreplydataimpl.cpp
Go to the documentation of this file.
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
6#include "private/qdataurl_p.h"
7#include <QtCore/QCoreApplication>
8#include <QtCore/QMetaObject>
9
11
12QNetworkReplyDataImplPrivate::QNetworkReplyDataImplPrivate()
13 : QNetworkReplyPrivate()
14{
15}
16
20
24
25QNetworkReplyDataImpl::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(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(QHttpHeaders::WellKnownHeader::ContentType, mimeType);
42 h.replaceOrAppend(QHttpHeaders::WellKnownHeader::ContentLength, QByteArray::number(size));
43 setHeaders(std::move(h));
44
45 QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
46
47 d->decodedData.setData(payload);
48 d->decodedData.open(QIODevice::ReadOnly);
49
50 QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
51 Q_ARG(qint64,size), Q_ARG(qint64, size));
52 QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
53 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
54 } else {
55 // something wrong with this URI
56 const QString msg = QCoreApplication::translate("QNetworkAccessDataBackend",
57 "Invalid URI: %1").arg(url.toString());
58 setError(QNetworkReply::ProtocolFailure, msg);
59 QMetaObject::invokeMethod(this, "errorOccurred", Qt::QueuedConnection,
60 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolFailure));
61 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
62 }
63}
64
66{
67 QNetworkReply::close();
68}
69
71{
72 QNetworkReply::close();
73}
74
76{
77 Q_D(const QNetworkReplyDataImpl);
78 return QNetworkReply::bytesAvailable() + d->decodedData.bytesAvailable();
79}
80
82{
83 return true;
84}
85
87{
88 Q_D(const QNetworkReplyDataImpl);
89 return d->decodedData.size();
90}
91
92/*!
93 \internal
94*/
95qint64 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
107QT_END_NAMESPACE
108
109#include "moc_qnetworkreplydataimpl_p.cpp"
virtual void close() override
Closes this device for reading.
virtual void abort() override
Aborts the operation immediately and closes any network connections still open.
virtual qint64 readData(char *data, qint64 maxlen) override
qint64 size() const override
For open random-access devices, this function returns the size of the device.
virtual qint64 bytesAvailable() const override
Returns the number of bytes that are available for reading.
virtual bool isSequential() const override