| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtNetwork module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qnetworkaccessfilebackend_p.h" |
| 41 | #include "qfileinfo.h" |
| 42 | #include "qdir.h" |
| 43 | #include "private/qnoncontiguousbytedevice_p.h" |
| 44 | |
| 45 | #include <QtCore/QCoreApplication> |
| 46 | #include <QtCore/QDateTime> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | QStringList QNetworkAccessFileBackendFactory::supportedSchemes() const |
| 51 | { |
| 52 | QStringList schemes; |
| 53 | schemes << QStringLiteral("file" ) |
| 54 | << QStringLiteral("qrc" ); |
| 55 | #if defined(Q_OS_ANDROID) |
| 56 | schemes << QStringLiteral("assets" ); |
| 57 | #endif |
| 58 | return schemes; |
| 59 | } |
| 60 | |
| 61 | QNetworkAccessBackend * |
| 62 | QNetworkAccessFileBackendFactory::create(QNetworkAccessManager::Operation op, |
| 63 | const QNetworkRequest &request) const |
| 64 | { |
| 65 | // is it an operation we know of? |
| 66 | switch (op) { |
| 67 | case QNetworkAccessManager::GetOperation: |
| 68 | case QNetworkAccessManager::PutOperation: |
| 69 | break; |
| 70 | |
| 71 | default: |
| 72 | // no, we can't handle this operation |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | QUrl url = request.url(); |
| 77 | if (url.scheme().compare(QLatin1String("qrc" ), Qt::CaseInsensitive) == 0 |
| 78 | #if defined(Q_OS_ANDROID) |
| 79 | || url.scheme().compare(QLatin1String("assets" ), Qt::CaseInsensitive) == 0 |
| 80 | #endif |
| 81 | || url.isLocalFile()) { |
| 82 | return new QNetworkAccessFileBackend; |
| 83 | } else if (!url.scheme().isEmpty() && url.authority().isEmpty() && (url.scheme().length() > 1)) { |
| 84 | // check if QFile could, in theory, open this URL via the file engines |
| 85 | // it has to be in the format: |
| 86 | // prefix:path/to/file |
| 87 | // or prefix:/path/to/file |
| 88 | // |
| 89 | // this construct here must match the one below in open() |
| 90 | QFileInfo fi(url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery)); |
| 91 | if (fi.exists() || (op == QNetworkAccessManager::PutOperation && fi.dir().exists())) |
| 92 | return new QNetworkAccessFileBackend; |
| 93 | } |
| 94 | |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | // We pass TargetType::Local even though it's kind of Networked but we're using a QFile to access |
| 99 | // the resource so it cannot use proxies anyway |
| 100 | QNetworkAccessFileBackend::QNetworkAccessFileBackend() |
| 101 | : QNetworkAccessBackend(QNetworkAccessBackend::TargetType::Local), |
| 102 | totalBytes(0), |
| 103 | hasUploadFinished(false) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | QNetworkAccessFileBackend::~QNetworkAccessFileBackend() |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | void QNetworkAccessFileBackend::open() |
| 112 | { |
| 113 | QUrl url = this->url(); |
| 114 | |
| 115 | if (url.host() == QLatin1String("localhost" )) |
| 116 | url.setHost(QString()); |
| 117 | #if !defined(Q_OS_WIN) |
| 118 | // do not allow UNC paths on Unix |
| 119 | if (!url.host().isEmpty()) { |
| 120 | // we handle only local files |
| 121 | error(QNetworkReply::ProtocolInvalidOperationError, |
| 122 | QCoreApplication::translate("QNetworkAccessFileBackend" , "Request for opening non-local file %1" ).arg(url.toString())); |
| 123 | finished(); |
| 124 | return; |
| 125 | } |
| 126 | #endif // !defined(Q_OS_WIN) |
| 127 | if (url.path().isEmpty()) |
| 128 | url.setPath(QLatin1String("/" )); |
| 129 | setUrl(url); |
| 130 | |
| 131 | QString fileName = url.toLocalFile(); |
| 132 | if (fileName.isEmpty()) { |
| 133 | if (url.scheme() == QLatin1String("qrc" )) { |
| 134 | fileName = QLatin1Char(':') + url.path(); |
| 135 | } else { |
| 136 | #if defined(Q_OS_ANDROID) |
| 137 | if (url.scheme() == QLatin1String("assets" )) |
| 138 | fileName = QLatin1String("assets:" ) + url.path(); |
| 139 | else |
| 140 | #endif |
| 141 | fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); |
| 142 | } |
| 143 | } |
| 144 | file.setFileName(fileName); |
| 145 | |
| 146 | if (operation() == QNetworkAccessManager::GetOperation) { |
| 147 | if (!loadFileInfo()) |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | QIODevice::OpenMode mode; |
| 152 | switch (operation()) { |
| 153 | case QNetworkAccessManager::GetOperation: |
| 154 | mode = QIODevice::ReadOnly; |
| 155 | break; |
| 156 | case QNetworkAccessManager::PutOperation: |
| 157 | mode = QIODevice::WriteOnly | QIODevice::Truncate; |
| 158 | createUploadByteDevice(); |
| 159 | QObject::connect(uploadByteDevice(), SIGNAL(readyRead()), this, SLOT(uploadReadyReadSlot())); |
| 160 | QMetaObject::invokeMethod(this, "uploadReadyReadSlot" , Qt::QueuedConnection); |
| 161 | break; |
| 162 | default: |
| 163 | Q_ASSERT_X(false, "QNetworkAccessFileBackend::open" , |
| 164 | "Got a request operation I cannot handle!!" ); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | mode |= QIODevice::Unbuffered; |
| 169 | bool opened = file.open(mode); |
| 170 | if (file.isSequential()) |
| 171 | connect(&file, &QIODevice::readChannelFinished, this, [this]() { finished(); }); |
| 172 | |
| 173 | // could we open the file? |
| 174 | if (!opened) { |
| 175 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend" , "Error opening %1: %2" ) |
| 176 | .arg(this->url().toString(), file.errorString()); |
| 177 | |
| 178 | // why couldn't we open the file? |
| 179 | // if we're opening for reading, either it doesn't exist, or it's access denied |
| 180 | // if we're opening for writing, not existing means it's access denied too |
| 181 | if (file.exists() || operation() == QNetworkAccessManager::PutOperation) |
| 182 | error(QNetworkReply::ContentAccessDenied, msg); |
| 183 | else |
| 184 | error(QNetworkReply::ContentNotFoundError, msg); |
| 185 | finished(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void QNetworkAccessFileBackend::uploadReadyReadSlot() |
| 190 | { |
| 191 | if (hasUploadFinished) |
| 192 | return; |
| 193 | |
| 194 | forever { |
| 195 | QByteArray data(16 * 1024, Qt::Uninitialized); |
| 196 | qint64 haveRead = uploadByteDevice()->peek(data.data(), data.size()); |
| 197 | if (haveRead == -1) { |
| 198 | // EOF |
| 199 | hasUploadFinished = true; |
| 200 | file.flush(); |
| 201 | file.close(); |
| 202 | finished(); |
| 203 | break; |
| 204 | } else if (haveRead == 0) { |
| 205 | // nothing to read right now, we will be called again later |
| 206 | break; |
| 207 | } else { |
| 208 | qint64 haveWritten; |
| 209 | data.truncate(haveRead); |
| 210 | haveWritten = file.write(data); |
| 211 | |
| 212 | if (haveWritten < 0) { |
| 213 | // write error! |
| 214 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend" , "Write error writing to %1: %2" ) |
| 215 | .arg(url().toString(), file.errorString()); |
| 216 | error(QNetworkReply::ProtocolFailure, msg); |
| 217 | |
| 218 | finished(); |
| 219 | return; |
| 220 | } else { |
| 221 | uploadByteDevice()->skip(haveWritten); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | file.flush(); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void QNetworkAccessFileBackend::close() |
| 231 | { |
| 232 | if (operation() == QNetworkAccessManager::GetOperation) { |
| 233 | file.close(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | bool QNetworkAccessFileBackend::loadFileInfo() |
| 238 | { |
| 239 | QFileInfo fi(file); |
| 240 | setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified()); |
| 241 | setHeader(QNetworkRequest::ContentLengthHeader, fi.size()); |
| 242 | |
| 243 | // signal we're open |
| 244 | metaDataChanged(); |
| 245 | |
| 246 | if (fi.isDir()) { |
| 247 | error(QNetworkReply::ContentOperationNotPermittedError, |
| 248 | QCoreApplication::translate("QNetworkAccessFileBackend" , "Cannot open %1: Path is a directory" ).arg(url().toString())); |
| 249 | finished(); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | qint64 QNetworkAccessFileBackend::bytesAvailable() const |
| 257 | { |
| 258 | if (operation() != QNetworkAccessManager::GetOperation) |
| 259 | return 0; |
| 260 | return file.bytesAvailable(); |
| 261 | } |
| 262 | |
| 263 | qint64 QNetworkAccessFileBackend::read(char *data, qint64 maxlen) |
| 264 | { |
| 265 | if (operation() != QNetworkAccessManager::GetOperation) |
| 266 | return 0; |
| 267 | qint64 actuallyRead = file.read(data, maxlen); |
| 268 | if (actuallyRead <= 0) { |
| 269 | // EOF or error |
| 270 | if (file.error() != QFile::NoError) { |
| 271 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend" , "Read error reading from %1: %2" ) |
| 272 | .arg(url().toString(), file.errorString()); |
| 273 | error(QNetworkReply::ProtocolFailure, msg); |
| 274 | |
| 275 | finished(); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | finished(); |
| 280 | return actuallyRead; |
| 281 | } |
| 282 | if (!file.isSequential() && file.atEnd()) |
| 283 | finished(); |
| 284 | totalBytes += actuallyRead; |
| 285 | return actuallyRead; |
| 286 | } |
| 287 | |
| 288 | QT_END_NAMESPACE |
| 289 | |