35QNetworkReplyFileImpl::QNetworkReplyFileImpl(QNetworkAccessManager *manager,
const QNetworkRequest &req,
const QNetworkAccessManager::Operation op)
36 : QNetworkReply(*
new QNetworkReplyFileImplPrivate(), manager)
41 QNetworkReply::open(QIODevice::ReadOnly);
48 if (url.host() ==
"localhost"_L1)
49 url.setHost(QString());
53 if (!url.host().isEmpty()) {
55 QString msg = QCoreApplication::translate(
"QNetworkAccessFileBackend",
"Request for opening non-local file %1").arg(url.toString());
56 setError(QNetworkReply::ProtocolInvalidOperationError, msg);
58 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
59 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError));
60 QMetaObject::invokeMethod(
this, &QNetworkReplyFileImpl::fileOpenFinished, Qt::QueuedConnection,
false);
64 if (url.path().isEmpty())
68 QString fileName = url.toLocalFile();
69 if (fileName.isEmpty()) {
70 const QString scheme = url.scheme();
71 if (scheme ==
"qrc"_L1) {
72 fileName = u':' + url.path();
74#if defined(Q_OS_ANDROID)
75 if (scheme ==
"assets"_L1)
76 fileName =
"assets:"_L1 + url.path();
79 fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
83 if (req.attribute(QNetworkRequest::BackgroundRequestAttribute).toBool()) {
84 auto realFile =
new QNetworkFile(fileName);
85 connect(realFile, &QNetworkFile::headerRead,
this, &QNetworkReplyFileImpl::setWellKnownHeader,
86 Qt::QueuedConnection);
87 connect(realFile, &QNetworkFile::networkError,
this, &QNetworkReplyFileImpl::setError,
88 Qt::QueuedConnection);
89 connect(realFile, SIGNAL(finished(
bool)), SLOT(fileOpenFinished(
bool)),
90 Qt::QueuedConnection);
93 QMetaObject::invokeMethod(realFile,
"open", Qt::QueuedConnection);
95 d->realFile = realFile;
99 QFileInfo fi(fileName);
101 QString msg = QCoreApplication::translate(
"QNetworkAccessFileBackend",
"Cannot open %1: Path is a directory").arg(url.toString());
102 setError(QNetworkReply::ContentOperationNotPermittedError, msg);
103 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
104 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError));
105 QMetaObject::invokeMethod(
this,
"finished", Qt::QueuedConnection);
108 d->realFile =
new QFile(fileName,
this);
109 bool opened = d->realFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
113 QString msg = QCoreApplication::translate(
"QNetworkAccessFileBackend",
"Error opening %1: %2")
114 .arg(d->realFile->fileName(), d->realFile->errorString());
117 setError(QNetworkReply::ContentAccessDenied, msg);
118 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
119 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied));
121 setError(QNetworkReply::ContentNotFoundError, msg);
122 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
123 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError));
125 QMetaObject::invokeMethod(
this,
"finished", Qt::QueuedConnection);
129 h.replaceOrAppend(QHttpHeaders::WellKnownHeader::LastModified,
130 QNetworkHeadersPrivate::toHttpDate(fi.lastModified()));
131 h.replaceOrAppend(QHttpHeaders::WellKnownHeader::ContentLength,
132 QByteArray::number(fi.size()));
133 setHeaders(
std::move(h));
135 QMetaObject::invokeMethod(
this,
"metaDataChanged", Qt::QueuedConnection);
136 QMetaObject::invokeMethod(
this,
"downloadProgress", Qt::QueuedConnection,
137 Q_ARG(qint64, fi.size()), Q_ARG(qint64, fi.size()));
138 QMetaObject::invokeMethod(
this,
"readyRead", Qt::QueuedConnection);
139 QMetaObject::invokeMethod(
this,
"finished", Qt::QueuedConnection);