Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkreplyfileimpl.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 "qnetworkreplyfileimpl_p.h" | - | ||||||||||||
35 | - | |||||||||||||
36 | #include "QtCore/qdatetime.h" | - | ||||||||||||
37 | #include <QtCore/QCoreApplication> | - | ||||||||||||
38 | #include <QtCore/QFileInfo> | - | ||||||||||||
39 | - | |||||||||||||
40 | QT_BEGIN_NAMESPACE | - | ||||||||||||
41 | - | |||||||||||||
42 | QNetworkReplyFileImplPrivate::QNetworkReplyFileImplPrivate() | - | ||||||||||||
43 | : QNetworkReplyPrivate(), realFileSize(0) | - | ||||||||||||
44 | { | - | ||||||||||||
45 | } executed 30 times by 2 tests: end of block Executed by:
| 30 | ||||||||||||
46 | - | |||||||||||||
47 | QNetworkReplyFileImpl::~QNetworkReplyFileImpl() | - | ||||||||||||
48 | { | - | ||||||||||||
49 | } | - | ||||||||||||
50 | - | |||||||||||||
51 | QNetworkReplyFileImpl::QNetworkReplyFileImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op) | - | ||||||||||||
52 | : QNetworkReply(*new QNetworkReplyFileImplPrivate(), parent) | - | ||||||||||||
53 | { | - | ||||||||||||
54 | setRequest(req); | - | ||||||||||||
55 | setUrl(req.url()); | - | ||||||||||||
56 | setOperation(op); | - | ||||||||||||
57 | setFinished(true); | - | ||||||||||||
58 | QNetworkReply::open(QIODevice::ReadOnly); | - | ||||||||||||
59 | - | |||||||||||||
60 | QNetworkReplyFileImplPrivate *d = (QNetworkReplyFileImplPrivate*) d_func(); | - | ||||||||||||
61 | - | |||||||||||||
62 | QUrl url = req.url(); | - | ||||||||||||
63 | if (url.host() == QLatin1String("localhost"))
| 1-29 | ||||||||||||
64 | url.setHost(QString()); executed 1 time by 1 test: url.setHost(QString()); Executed by:
| 1 | ||||||||||||
65 | - | |||||||||||||
66 | #if !defined(Q_OS_WIN) | - | ||||||||||||
67 | // do not allow UNC paths on Unix | - | ||||||||||||
68 | if (!url.host().isEmpty()) {
| 1-29 | ||||||||||||
69 | // we handle only local files | - | ||||||||||||
70 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString()); | - | ||||||||||||
71 | setError(QNetworkReply::ProtocolInvalidOperationError, msg); | - | ||||||||||||
72 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, | - | ||||||||||||
73 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError)); | - | ||||||||||||
74 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); | - | ||||||||||||
75 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||
76 | } | - | ||||||||||||
77 | #endif | - | ||||||||||||
78 | if (url.path().isEmpty())
| 1-28 | ||||||||||||
79 | url.setPath(QLatin1String("/")); executed 1 time by 1 test: url.setPath(QLatin1String("/")); Executed by:
| 1 | ||||||||||||
80 | setUrl(url); | - | ||||||||||||
81 | - | |||||||||||||
82 | - | |||||||||||||
83 | QString fileName = url.toLocalFile(); | - | ||||||||||||
84 | if (fileName.isEmpty()) {
| 2-27 | ||||||||||||
85 | if (url.scheme() == QLatin1String("qrc")) {
| 0-2 | ||||||||||||
86 | fileName = QLatin1Char(':') + url.path(); | - | ||||||||||||
87 | } else { executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
88 | #if defined(Q_OS_ANDROID) | - | ||||||||||||
89 | if (url.scheme() == QLatin1String("assets")) | - | ||||||||||||
90 | fileName = QLatin1String("assets:") + url.path(); | - | ||||||||||||
91 | else | - | ||||||||||||
92 | #endif | - | ||||||||||||
93 | fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); | - | ||||||||||||
94 | } never executed: end of block | 0 | ||||||||||||
95 | } | - | ||||||||||||
96 | - | |||||||||||||
97 | QFileInfo fi(fileName); | - | ||||||||||||
98 | if (fi.isDir()) {
| 3-26 | ||||||||||||
99 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString()); | - | ||||||||||||
100 | setError(QNetworkReply::ContentOperationNotPermittedError, msg); | - | ||||||||||||
101 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, | - | ||||||||||||
102 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError)); | - | ||||||||||||
103 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); | - | ||||||||||||
104 | return; executed 3 times by 1 test: return; Executed by:
| 3 | ||||||||||||
105 | } | - | ||||||||||||
106 | - | |||||||||||||
107 | d->realFile.setFileName(fileName); | - | ||||||||||||
108 | bool opened = d->realFile.open(QIODevice::ReadOnly | QIODevice::Unbuffered); | - | ||||||||||||
109 | - | |||||||||||||
110 | // could we open the file? | - | ||||||||||||
111 | if (!opened) {
| 3-23 | ||||||||||||
112 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2") | - | ||||||||||||
113 | .arg(d->realFile.fileName(), d->realFile.errorString()); | - | ||||||||||||
114 | - | |||||||||||||
115 | if (d->realFile.exists()) {
| 1-2 | ||||||||||||
116 | setError(QNetworkReply::ContentAccessDenied, msg); | - | ||||||||||||
117 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, | - | ||||||||||||
118 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied)); | - | ||||||||||||
119 | } else { executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
120 | setError(QNetworkReply::ContentNotFoundError, msg); | - | ||||||||||||
121 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, | - | ||||||||||||
122 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError)); | - | ||||||||||||
123 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
124 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); | - | ||||||||||||
125 | return; executed 3 times by 1 test: return; Executed by:
| 3 | ||||||||||||
126 | } | - | ||||||||||||
127 | - | |||||||||||||
128 | setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified()); | - | ||||||||||||
129 | d->realFileSize = fi.size(); | - | ||||||||||||
130 | setHeader(QNetworkRequest::ContentLengthHeader, d->realFileSize); | - | ||||||||||||
131 | - | |||||||||||||
132 | QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection); | - | ||||||||||||
133 | QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection, | - | ||||||||||||
134 | Q_ARG(qint64, d->realFileSize), Q_ARG(qint64, d->realFileSize)); | - | ||||||||||||
135 | QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection); | - | ||||||||||||
136 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); | - | ||||||||||||
137 | } executed 23 times by 2 tests: end of block Executed by:
| 23 | ||||||||||||
138 | - | |||||||||||||
139 | void QNetworkReplyFileImpl::close() | - | ||||||||||||
140 | { | - | ||||||||||||
141 | Q_D(QNetworkReplyFileImpl); | - | ||||||||||||
142 | QNetworkReply::close(); | - | ||||||||||||
143 | d->realFile.close(); | - | ||||||||||||
144 | } never executed: end of block | 0 | ||||||||||||
145 | - | |||||||||||||
146 | void QNetworkReplyFileImpl::abort() | - | ||||||||||||
147 | { | - | ||||||||||||
148 | Q_D(QNetworkReplyFileImpl); | - | ||||||||||||
149 | QNetworkReply::close(); | - | ||||||||||||
150 | d->realFile.close(); | - | ||||||||||||
151 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
152 | - | |||||||||||||
153 | qint64 QNetworkReplyFileImpl::bytesAvailable() const | - | ||||||||||||
154 | { | - | ||||||||||||
155 | Q_D(const QNetworkReplyFileImpl); | - | ||||||||||||
156 | if (!d->realFile.isOpen())
| 2-184 | ||||||||||||
157 | return QNetworkReply::bytesAvailable(); executed 2 times by 1 test: return QNetworkReply::bytesAvailable(); Executed by:
| 2 | ||||||||||||
158 | return QNetworkReply::bytesAvailable() + d->realFile.bytesAvailable(); executed 184 times by 2 tests: return QNetworkReply::bytesAvailable() + d->realFile.bytesAvailable(); Executed by:
| 184 | ||||||||||||
159 | } | - | ||||||||||||
160 | - | |||||||||||||
161 | bool QNetworkReplyFileImpl::isSequential () const | - | ||||||||||||
162 | { | - | ||||||||||||
163 | return true; executed 183 times by 2 tests: return true; Executed by:
| 183 | ||||||||||||
164 | } | - | ||||||||||||
165 | - | |||||||||||||
166 | qint64 QNetworkReplyFileImpl::size() const | - | ||||||||||||
167 | { | - | ||||||||||||
168 | Q_D(const QNetworkReplyFileImpl); | - | ||||||||||||
169 | return d->realFileSize; never executed: return d->realFileSize; | 0 | ||||||||||||
170 | } | - | ||||||||||||
171 | - | |||||||||||||
172 | /*! | - | ||||||||||||
173 | \internal | - | ||||||||||||
174 | */ | - | ||||||||||||
175 | qint64 QNetworkReplyFileImpl::readData(char *data, qint64 maxlen) | - | ||||||||||||
176 | { | - | ||||||||||||
177 | Q_D(QNetworkReplyFileImpl); | - | ||||||||||||
178 | if (!d->realFile.isOpen())
| 17-166 | ||||||||||||
179 | return -1; executed 17 times by 1 test: return -1; Executed by:
| 17 | ||||||||||||
180 | qint64 ret = d->realFile.read(data, maxlen); | - | ||||||||||||
181 | if (bytesAvailable() == 0 && d->realFile.isOpen())
| 0-151 | ||||||||||||
182 | d->realFile.close(); executed 15 times by 1 test: d->realFile.close(); Executed by:
| 15 | ||||||||||||
183 | if (ret == 0 && bytesAvailable() == 0)
| 1-163 | ||||||||||||
184 | return -1; executed 2 times by 1 test: return -1; Executed by:
| 2 | ||||||||||||
185 | else { | - | ||||||||||||
186 | setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200); | - | ||||||||||||
187 | setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, QLatin1String("OK")); | - | ||||||||||||
188 | return ret; executed 164 times by 2 tests: return ret; Executed by:
| 164 | ||||||||||||
189 | } | - | ||||||||||||
190 | } | - | ||||||||||||
191 | - | |||||||||||||
192 | - | |||||||||||||
193 | QT_END_NAMESPACE | - | ||||||||||||
194 | - | |||||||||||||
195 | #include "moc_qnetworkreplyfileimpl_p.cpp" | - | ||||||||||||
196 | - | |||||||||||||
Source code | Switch to Preprocessed file |