Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkaccessfilebackend.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qnetworkaccessfilebackend_p.h" | - | ||||||||||||||||||
35 | #include "qfileinfo.h" | - | ||||||||||||||||||
36 | #include "qurlinfo_p.h" | - | ||||||||||||||||||
37 | #include "qdir.h" | - | ||||||||||||||||||
38 | #include "private/qnoncontiguousbytedevice_p.h" | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | #include <QtCore/QCoreApplication> | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
43 | - | |||||||||||||||||||
44 | QStringList QNetworkAccessFileBackendFactory::supportedSchemes() const | - | ||||||||||||||||||
45 | { | - | ||||||||||||||||||
46 | QStringList schemes; | - | ||||||||||||||||||
47 | schemes << QStringLiteral("file") never executed: return qstring_literal_temp; | 0 | ||||||||||||||||||
48 | << QStringLiteral("qrc"); never executed: return qstring_literal_temp; | 0 | ||||||||||||||||||
49 | #if defined(Q_OS_ANDROID) | - | ||||||||||||||||||
50 | schemes << QStringLiteral("assets"); | - | ||||||||||||||||||
51 | #endif | - | ||||||||||||||||||
52 | return schemes; never executed: return schemes; | 0 | ||||||||||||||||||
53 | } | - | ||||||||||||||||||
54 | - | |||||||||||||||||||
55 | QNetworkAccessBackend * | - | ||||||||||||||||||
56 | QNetworkAccessFileBackendFactory::create(QNetworkAccessManager::Operation op, | - | ||||||||||||||||||
57 | const QNetworkRequest &request) const | - | ||||||||||||||||||
58 | { | - | ||||||||||||||||||
59 | // is it an operation we know of? | - | ||||||||||||||||||
60 | switch (op) { | - | ||||||||||||||||||
61 | case QNetworkAccessManager::GetOperation: executed 8 times by 1 test: case QNetworkAccessManager::GetOperation: Executed by:
| 8 | ||||||||||||||||||
62 | case QNetworkAccessManager::PutOperation: executed 39 times by 1 test: case QNetworkAccessManager::PutOperation: Executed by:
| 39 | ||||||||||||||||||
63 | break; executed 47 times by 1 test: break; Executed by:
| 47 | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | default: never executed: default: | 0 | ||||||||||||||||||
66 | // no, we can't handle this operation | - | ||||||||||||||||||
67 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
68 | } | - | ||||||||||||||||||
69 | - | |||||||||||||||||||
70 | QUrl url = request.url(); | - | ||||||||||||||||||
71 | if (url.scheme().compare(QLatin1String("qrc"), Qt::CaseInsensitive) == 0
| 0-47 | ||||||||||||||||||
72 | #if defined(Q_OS_ANDROID) | - | ||||||||||||||||||
73 | || url.scheme().compare(QLatin1String("assets"), Qt::CaseInsensitive) == 0 | - | ||||||||||||||||||
74 | #endif | - | ||||||||||||||||||
75 | || url.isLocalFile()) {
| 8-39 | ||||||||||||||||||
76 | return new QNetworkAccessFileBackend; executed 39 times by 1 test: return new QNetworkAccessFileBackend; Executed by:
| 39 | ||||||||||||||||||
77 | } else if (!url.scheme().isEmpty() && url.authority().isEmpty() && (url.scheme().length() > 1)) {
| 0-5 | ||||||||||||||||||
78 | // check if QFile could, in theory, open this URL via the file engines | - | ||||||||||||||||||
79 | // it has to be in the format: | - | ||||||||||||||||||
80 | // prefix:path/to/file | - | ||||||||||||||||||
81 | // or prefix:/path/to/file | - | ||||||||||||||||||
82 | // | - | ||||||||||||||||||
83 | // this construct here must match the one below in open() | - | ||||||||||||||||||
84 | QFileInfo fi(url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery)); | - | ||||||||||||||||||
85 | if (fi.exists() || (op == QNetworkAccessManager::PutOperation && fi.dir().exists()))
| 0-4 | ||||||||||||||||||
86 | return new QNetworkAccessFileBackend; executed 4 times by 1 test: return new QNetworkAccessFileBackend; Executed by:
| 4 | ||||||||||||||||||
87 | } never executed: end of block | 0 | ||||||||||||||||||
88 | - | |||||||||||||||||||
89 | return 0; executed 4 times by 1 test: return 0; Executed by:
| 4 | ||||||||||||||||||
90 | } | - | ||||||||||||||||||
91 | - | |||||||||||||||||||
92 | QNetworkAccessFileBackend::QNetworkAccessFileBackend() | - | ||||||||||||||||||
93 | : uploadByteDevice(0), totalBytes(0), hasUploadFinished(false) | - | ||||||||||||||||||
94 | { | - | ||||||||||||||||||
95 | } executed 43 times by 1 test: end of block Executed by:
| 43 | ||||||||||||||||||
96 | - | |||||||||||||||||||
97 | QNetworkAccessFileBackend::~QNetworkAccessFileBackend() | - | ||||||||||||||||||
98 | { | - | ||||||||||||||||||
99 | } | - | ||||||||||||||||||
100 | - | |||||||||||||||||||
101 | void QNetworkAccessFileBackend::open() | - | ||||||||||||||||||
102 | { | - | ||||||||||||||||||
103 | QUrl url = this->url(); | - | ||||||||||||||||||
104 | - | |||||||||||||||||||
105 | if (url.host() == QLatin1String("localhost"))
| 0-43 | ||||||||||||||||||
106 | url.setHost(QString()); never executed: url.setHost(QString()); | 0 | ||||||||||||||||||
107 | #if !defined(Q_OS_WIN) | - | ||||||||||||||||||
108 | // do not allow UNC paths on Unix | - | ||||||||||||||||||
109 | if (!url.host().isEmpty()) {
| 0-43 | ||||||||||||||||||
110 | // we handle only local files | - | ||||||||||||||||||
111 | error(QNetworkReply::ProtocolInvalidOperationError, | - | ||||||||||||||||||
112 | QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString())); | - | ||||||||||||||||||
113 | finished(); | - | ||||||||||||||||||
114 | return; never executed: return; | 0 | ||||||||||||||||||
115 | } | - | ||||||||||||||||||
116 | #endif // !defined(Q_OS_WIN) | - | ||||||||||||||||||
117 | if (url.path().isEmpty())
| 0-43 | ||||||||||||||||||
118 | url.setPath(QLatin1String("/")); never executed: url.setPath(QLatin1String("/")); | 0 | ||||||||||||||||||
119 | setUrl(url); | - | ||||||||||||||||||
120 | - | |||||||||||||||||||
121 | QString fileName = url.toLocalFile(); | - | ||||||||||||||||||
122 | if (fileName.isEmpty()) {
| 4-39 | ||||||||||||||||||
123 | if (url.scheme() == QLatin1String("qrc")) {
| 0-4 | ||||||||||||||||||
124 | fileName = QLatin1Char(':') + url.path(); | - | ||||||||||||||||||
125 | } else { never executed: end of block | 0 | ||||||||||||||||||
126 | #if defined(Q_OS_ANDROID) | - | ||||||||||||||||||
127 | if (url.scheme() == QLatin1String("assets")) | - | ||||||||||||||||||
128 | fileName = QLatin1String("assets:") + url.path(); | - | ||||||||||||||||||
129 | else | - | ||||||||||||||||||
130 | #endif | - | ||||||||||||||||||
131 | fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); | - | ||||||||||||||||||
132 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
133 | } | - | ||||||||||||||||||
134 | file.setFileName(fileName); | - | ||||||||||||||||||
135 | - | |||||||||||||||||||
136 | if (operation() == QNetworkAccessManager::GetOperation) {
| 4-39 | ||||||||||||||||||
137 | if (!loadFileInfo())
| 0-4 | ||||||||||||||||||
138 | return; never executed: return; | 0 | ||||||||||||||||||
139 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | QIODevice::OpenMode mode; | - | ||||||||||||||||||
142 | switch (operation()) { | - | ||||||||||||||||||
143 | case QNetworkAccessManager::GetOperation: executed 4 times by 1 test: case QNetworkAccessManager::GetOperation: Executed by:
| 4 | ||||||||||||||||||
144 | mode = QIODevice::ReadOnly; | - | ||||||||||||||||||
145 | break; executed 4 times by 1 test: break; Executed by:
| 4 | ||||||||||||||||||
146 | case QNetworkAccessManager::PutOperation: executed 39 times by 1 test: case QNetworkAccessManager::PutOperation: Executed by:
| 39 | ||||||||||||||||||
147 | mode = QIODevice::WriteOnly | QIODevice::Truncate; | - | ||||||||||||||||||
148 | uploadByteDevice = createUploadByteDevice(); | - | ||||||||||||||||||
149 | QObject::connect(uploadByteDevice, SIGNAL(readyRead()), this, SLOT(uploadReadyReadSlot())); | - | ||||||||||||||||||
150 | QMetaObject::invokeMethod(this, "uploadReadyReadSlot", Qt::QueuedConnection); | - | ||||||||||||||||||
151 | break; executed 39 times by 1 test: break; Executed by:
| 39 | ||||||||||||||||||
152 | default: never executed: default: | 0 | ||||||||||||||||||
153 | Q_ASSERT_X(false, "QNetworkAccessFileBackend::open", | - | ||||||||||||||||||
154 | "Got a request operation I cannot handle!!"); | - | ||||||||||||||||||
155 | return; never executed: return; | 0 | ||||||||||||||||||
156 | } | - | ||||||||||||||||||
157 | - | |||||||||||||||||||
158 | mode |= QIODevice::Unbuffered; | - | ||||||||||||||||||
159 | bool opened = file.open(mode); | - | ||||||||||||||||||
160 | - | |||||||||||||||||||
161 | // could we open the file? | - | ||||||||||||||||||
162 | if (!opened) {
| 0-43 | ||||||||||||||||||
163 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2") | - | ||||||||||||||||||
164 | .arg(this->url().toString(), file.errorString()); | - | ||||||||||||||||||
165 | - | |||||||||||||||||||
166 | // why couldn't we open the file? | - | ||||||||||||||||||
167 | // if we're opening for reading, either it doesn't exist, or it's access denied | - | ||||||||||||||||||
168 | // if we're opening for writing, not existing means it's access denied too | - | ||||||||||||||||||
169 | if (file.exists() || operation() == QNetworkAccessManager::PutOperation)
| 0 | ||||||||||||||||||
170 | error(QNetworkReply::ContentAccessDenied, msg); never executed: error(QNetworkReply::ContentAccessDenied, msg); | 0 | ||||||||||||||||||
171 | else | - | ||||||||||||||||||
172 | error(QNetworkReply::ContentNotFoundError, msg); never executed: error(QNetworkReply::ContentNotFoundError, msg); | 0 | ||||||||||||||||||
173 | finished(); | - | ||||||||||||||||||
174 | } never executed: end of block | 0 | ||||||||||||||||||
175 | } executed 43 times by 1 test: end of block Executed by:
| 43 | ||||||||||||||||||
176 | - | |||||||||||||||||||
177 | void QNetworkAccessFileBackend::uploadReadyReadSlot() | - | ||||||||||||||||||
178 | { | - | ||||||||||||||||||
179 | if (hasUploadFinished)
| 2-124 | ||||||||||||||||||
180 | return; executed 2 times by 1 test: return; Executed by:
| 2 | ||||||||||||||||||
181 | - | |||||||||||||||||||
182 | forever { | - | ||||||||||||||||||
183 | qint64 haveRead; | - | ||||||||||||||||||
184 | const char *readPointer = uploadByteDevice->readPointer(-1, haveRead); | - | ||||||||||||||||||
185 | if (haveRead == -1) {
| 39-672 | ||||||||||||||||||
186 | // EOF | - | ||||||||||||||||||
187 | hasUploadFinished = true; | - | ||||||||||||||||||
188 | file.flush(); | - | ||||||||||||||||||
189 | file.close(); | - | ||||||||||||||||||
190 | finished(); | - | ||||||||||||||||||
191 | break; executed 39 times by 1 test: break; Executed by:
| 39 | ||||||||||||||||||
192 | } else if (haveRead == 0 || readPointer == 0) {
| 0-587 | ||||||||||||||||||
193 | // nothing to read right now, we will be called again later | - | ||||||||||||||||||
194 | break; executed 85 times by 1 test: break; Executed by:
| 85 | ||||||||||||||||||
195 | } else { | - | ||||||||||||||||||
196 | qint64 haveWritten; | - | ||||||||||||||||||
197 | haveWritten = file.write(readPointer, haveRead); | - | ||||||||||||||||||
198 | - | |||||||||||||||||||
199 | if (haveWritten < 0) {
| 0-587 | ||||||||||||||||||
200 | // write error! | - | ||||||||||||||||||
201 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Write error writing to %1: %2") | - | ||||||||||||||||||
202 | .arg(url().toString(), file.errorString()); | - | ||||||||||||||||||
203 | error(QNetworkReply::ProtocolFailure, msg); | - | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | finished(); | - | ||||||||||||||||||
206 | return; never executed: return; | 0 | ||||||||||||||||||
207 | } else { | - | ||||||||||||||||||
208 | uploadByteDevice->advanceReadPointer(haveWritten); | - | ||||||||||||||||||
209 | } executed 587 times by 1 test: end of block Executed by:
| 587 | ||||||||||||||||||
210 | - | |||||||||||||||||||
211 | - | |||||||||||||||||||
212 | file.flush(); | - | ||||||||||||||||||
213 | } executed 587 times by 1 test: end of block Executed by:
| 587 | ||||||||||||||||||
214 | } | - | ||||||||||||||||||
215 | } executed 124 times by 1 test: end of block Executed by:
| 124 | ||||||||||||||||||
216 | - | |||||||||||||||||||
217 | void QNetworkAccessFileBackend::closeDownstreamChannel() | - | ||||||||||||||||||
218 | { | - | ||||||||||||||||||
219 | if (operation() == QNetworkAccessManager::GetOperation) {
| 0 | ||||||||||||||||||
220 | file.close(); | - | ||||||||||||||||||
221 | } never executed: end of block | 0 | ||||||||||||||||||
222 | } never executed: end of block | 0 | ||||||||||||||||||
223 | - | |||||||||||||||||||
224 | void QNetworkAccessFileBackend::downstreamReadyWrite() | - | ||||||||||||||||||
225 | { | - | ||||||||||||||||||
226 | Q_ASSERT_X(operation() == QNetworkAccessManager::GetOperation, "QNetworkAccessFileBackend", | - | ||||||||||||||||||
227 | "We're being told to download data but operation isn't GET!"); | - | ||||||||||||||||||
228 | - | |||||||||||||||||||
229 | readMoreFromFile(); | - | ||||||||||||||||||
230 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
231 | - | |||||||||||||||||||
232 | bool QNetworkAccessFileBackend::loadFileInfo() | - | ||||||||||||||||||
233 | { | - | ||||||||||||||||||
234 | QFileInfo fi(file); | - | ||||||||||||||||||
235 | setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified()); | - | ||||||||||||||||||
236 | setHeader(QNetworkRequest::ContentLengthHeader, fi.size()); | - | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | // signal we're open | - | ||||||||||||||||||
239 | metaDataChanged(); | - | ||||||||||||||||||
240 | - | |||||||||||||||||||
241 | if (fi.isDir()) {
| 0-4 | ||||||||||||||||||
242 | error(QNetworkReply::ContentOperationNotPermittedError, | - | ||||||||||||||||||
243 | QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url().toString())); | - | ||||||||||||||||||
244 | finished(); | - | ||||||||||||||||||
245 | return false; never executed: return false; | 0 | ||||||||||||||||||
246 | } | - | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | return true; executed 4 times by 1 test: return true; Executed by:
| 4 | ||||||||||||||||||
249 | } | - | ||||||||||||||||||
250 | - | |||||||||||||||||||
251 | bool QNetworkAccessFileBackend::readMoreFromFile() | - | ||||||||||||||||||
252 | { | - | ||||||||||||||||||
253 | qint64 wantToRead; | - | ||||||||||||||||||
254 | while ((wantToRead = nextDownstreamBlockSize()) > 0) {
| 0-38 | ||||||||||||||||||
255 | // ### FIXME!! | - | ||||||||||||||||||
256 | // Obtain a pointer from the ringbuffer! | - | ||||||||||||||||||
257 | // Avoid extra copy | - | ||||||||||||||||||
258 | QByteArray data; | - | ||||||||||||||||||
259 | data.reserve(wantToRead); | - | ||||||||||||||||||
260 | qint64 actuallyRead = file.read(data.data(), wantToRead); | - | ||||||||||||||||||
261 | if (actuallyRead <= 0) {
| 4-34 | ||||||||||||||||||
262 | // EOF or error | - | ||||||||||||||||||
263 | if (file.error() != QFile::NoError) {
| 0-4 | ||||||||||||||||||
264 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Read error reading from %1: %2") | - | ||||||||||||||||||
265 | .arg(url().toString(), file.errorString()); | - | ||||||||||||||||||
266 | error(QNetworkReply::ProtocolFailure, msg); | - | ||||||||||||||||||
267 | - | |||||||||||||||||||
268 | finished(); | - | ||||||||||||||||||
269 | return false; never executed: return false; | 0 | ||||||||||||||||||
270 | } | - | ||||||||||||||||||
271 | - | |||||||||||||||||||
272 | finished(); | - | ||||||||||||||||||
273 | return true; executed 4 times by 1 test: return true; Executed by:
| 4 | ||||||||||||||||||
274 | } | - | ||||||||||||||||||
275 | - | |||||||||||||||||||
276 | data.resize(actuallyRead); | - | ||||||||||||||||||
277 | totalBytes += actuallyRead; | - | ||||||||||||||||||
278 | - | |||||||||||||||||||
279 | QByteDataBuffer list; | - | ||||||||||||||||||
280 | list.append(data); | - | ||||||||||||||||||
281 | data.clear(); // important because of implicit sharing! | - | ||||||||||||||||||
282 | writeDownstreamData(list); | - | ||||||||||||||||||
283 | } executed 34 times by 1 test: end of block Executed by:
| 34 | ||||||||||||||||||
284 | return true; never executed: return true; | 0 | ||||||||||||||||||
285 | } | - | ||||||||||||||||||
286 | - | |||||||||||||||||||
287 | QT_END_NAMESPACE | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |