39QNetworkReplyFileImpl::QNetworkReplyFileImpl(QNetworkAccessManager *manager,
const QNetworkRequest &req,
const QNetworkAccessManager::Operation op)
40 : QNetworkReply(*
new QNetworkReplyFileImplPrivate(), manager)
45 QNetworkReply::open(QIODevice::ReadOnly);
52 if (url.host() ==
"localhost"_L1)
53 url.setHost(QString());
57 if (!url.host().isEmpty()) {
59 QString msg = QCoreApplication::translate(
"QNetworkAccessFileBackend",
"Request for opening non-local file %1").arg(url.toString());
60 setError(QNetworkReply::ProtocolInvalidOperationError, msg);
62 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
63 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError));
64 QMetaObject::invokeMethod(
this, &QNetworkReplyFileImpl::fileOpenFinished, Qt::QueuedConnection,
false);
68 if (url.path().isEmpty())
72 QString fileName = url.toLocalFile();
73 if (fileName.isEmpty()) {
74 const QString scheme = url.scheme();
75 if (scheme ==
"qrc"_L1) {
76 fileName = u':' + url.path();
78#if defined(Q_OS_ANDROID)
79 if (scheme ==
"assets"_L1)
80 fileName =
"assets:"_L1 + url.path();
83 fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
87 if (req.attribute(QNetworkRequest::BackgroundRequestAttribute).toBool()) {
88 auto realFile =
new QNetworkFile(fileName);
89 connect(realFile, &QNetworkFile::headerRead,
this, &QNetworkReplyFileImpl::setWellKnownHeader,
90 Qt::QueuedConnection);
91 connect(realFile, &QNetworkFile::networkError,
this, &QNetworkReplyFileImpl::setError,
92 Qt::QueuedConnection);
93 connect(realFile, SIGNAL(finished(
bool)), SLOT(fileOpenFinished(
bool)),
94 Qt::QueuedConnection);
97 QMetaObject::invokeMethod(realFile,
"open", Qt::QueuedConnection);
99 d->realFile = realFile;
103 QFileInfo fi(fileName);
105 QString msg = QCoreApplication::translate(
"QNetworkAccessFileBackend",
"Cannot open %1: Path is a directory").arg(url.toString());
106 setError(QNetworkReply::ContentOperationNotPermittedError, msg);
107 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
108 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError));
109 QMetaObject::invokeMethod(
this,
"finished", Qt::QueuedConnection);
112 d->realFile =
new QFile(fileName,
this);
113 bool opened = d->realFile->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
117 QString msg = QCoreApplication::translate(
"QNetworkAccessFileBackend",
"Error opening %1: %2")
118 .arg(d->realFile->fileName(), d->realFile->errorString());
121 setError(QNetworkReply::ContentAccessDenied, msg);
122 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
123 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied));
125 setError(QNetworkReply::ContentNotFoundError, msg);
126 QMetaObject::invokeMethod(
this,
"errorOccurred", Qt::QueuedConnection,
127 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError));
129 QMetaObject::invokeMethod(
this,
"finished", Qt::QueuedConnection);
133 h.replaceOrAppend(QHttpHeaders::WellKnownHeader::LastModified,
134 QNetworkHeadersPrivate::toHttpDate(fi.lastModified()));
135 h.replaceOrAppend(QHttpHeaders::WellKnownHeader::ContentLength,
136 QByteArray::number(fi.size()));
137 setHeaders(
std::move(h));
139 QMetaObject::invokeMethod(
this,
"metaDataChanged", Qt::QueuedConnection);
140 QMetaObject::invokeMethod(
this,
"downloadProgress", Qt::QueuedConnection,
141 Q_ARG(qint64, fi.size()), Q_ARG(qint64, fi.size()));
142 QMetaObject::invokeMethod(
this,
"readyRead", Qt::QueuedConnection);
143 QMetaObject::invokeMethod(
this,
"finished", Qt::QueuedConnection);