qnetworkreplyfileimpl.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9