Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkaccessdebugpipebackend.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 "qnetworkaccessdebugpipebackend_p.h" | - | ||||||||||||
35 | #include "QtCore/qdatastream.h" | - | ||||||||||||
36 | #include <QCoreApplication> | - | ||||||||||||
37 | #include <QStringList> | - | ||||||||||||
38 | #include <QUrlQuery> | - | ||||||||||||
39 | #include "private/qnoncontiguousbytedevice_p.h" | - | ||||||||||||
40 | - | |||||||||||||
41 | QT_BEGIN_NAMESPACE | - | ||||||||||||
42 | - | |||||||||||||
43 | #ifdef QT_BUILD_INTERNAL | - | ||||||||||||
44 | - | |||||||||||||
45 | enum { | - | ||||||||||||
46 | ReadBufferSize = 16384, | - | ||||||||||||
47 | WriteBufferSize = ReadBufferSize | - | ||||||||||||
48 | }; | - | ||||||||||||
49 | - | |||||||||||||
50 | QStringList QNetworkAccessDebugPipeBackendFactory::supportedSchemes() const | - | ||||||||||||
51 | { | - | ||||||||||||
52 | return QStringList(QStringLiteral("debugpipe")); never executed: return QStringList(([]() -> QString { enum { Size = sizeof(u"" "debugpipe")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "debugpipe" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())); never executed: return qstring_literal_temp; | 0 | ||||||||||||
53 | } | - | ||||||||||||
54 | - | |||||||||||||
55 | QNetworkAccessBackend * | - | ||||||||||||
56 | QNetworkAccessDebugPipeBackendFactory::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 46 times by 1 test: case QNetworkAccessManager::PutOperation: Executed by:
| 46 | ||||||||||||
63 | break; executed 54 times by 1 test: break; Executed by:
| 54 | ||||||||||||
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() == QLatin1String("debugpipe"))
| 7-47 | ||||||||||||
72 | return new QNetworkAccessDebugPipeBackend; executed 7 times by 1 test: return new QNetworkAccessDebugPipeBackend; Executed by:
| 7 | ||||||||||||
73 | return 0; executed 47 times by 1 test: return 0; Executed by:
| 47 | ||||||||||||
74 | } | - | ||||||||||||
75 | - | |||||||||||||
76 | QNetworkAccessDebugPipeBackend::QNetworkAccessDebugPipeBackend() | - | ||||||||||||
77 | : bareProtocol(false), hasUploadFinished(false), hasDownloadFinished(false), | - | ||||||||||||
78 | hasEverythingFinished(false), bytesDownloaded(0), bytesUploaded(0) | - | ||||||||||||
79 | { | - | ||||||||||||
80 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
81 | - | |||||||||||||
82 | QNetworkAccessDebugPipeBackend::~QNetworkAccessDebugPipeBackend() | - | ||||||||||||
83 | { | - | ||||||||||||
84 | // this is signals disconnect, not network! | - | ||||||||||||
85 | socket.disconnect(this); // we're not interested in the signals at this point | - | ||||||||||||
86 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
87 | - | |||||||||||||
88 | void QNetworkAccessDebugPipeBackend::open() | - | ||||||||||||
89 | { | - | ||||||||||||
90 | socket.connectToHost(url().host(), url().port(12345)); | - | ||||||||||||
91 | socket.setReadBufferSize(ReadBufferSize); | - | ||||||||||||
92 | - | |||||||||||||
93 | // socket ready read -> we can push from socket to downstream | - | ||||||||||||
94 | connect(&socket, SIGNAL(readyRead()), SLOT(socketReadyRead())); | - | ||||||||||||
95 | connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError())); | - | ||||||||||||
96 | connect(&socket, SIGNAL(disconnected()), SLOT(socketDisconnected())); | - | ||||||||||||
97 | connect(&socket, SIGNAL(connected()), SLOT(socketConnected())); | - | ||||||||||||
98 | // socket bytes written -> we can push more from upstream to socket | - | ||||||||||||
99 | connect(&socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64))); | - | ||||||||||||
100 | - | |||||||||||||
101 | bareProtocol = QUrlQuery(url()).queryItemValue(QLatin1String("bare")) == QLatin1String("1"); | - | ||||||||||||
102 | - | |||||||||||||
103 | if (operation() == QNetworkAccessManager::PutOperation) {
| 0-7 | ||||||||||||
104 | uploadByteDevice = createUploadByteDevice(); | - | ||||||||||||
105 | QObject::connect(uploadByteDevice, SIGNAL(readyRead()), this, SLOT(uploadReadyReadSlot())); | - | ||||||||||||
106 | QMetaObject::invokeMethod(this, "uploadReadyReadSlot", Qt::QueuedConnection); | - | ||||||||||||
107 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
108 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
109 | - | |||||||||||||
110 | void QNetworkAccessDebugPipeBackend::socketReadyRead() | - | ||||||||||||
111 | { | - | ||||||||||||
112 | pushFromSocketToDownstream(); | - | ||||||||||||
113 | } never executed: end of block | 0 | ||||||||||||
114 | - | |||||||||||||
115 | void QNetworkAccessDebugPipeBackend::downstreamReadyWrite() | - | ||||||||||||
116 | { | - | ||||||||||||
117 | pushFromSocketToDownstream(); | - | ||||||||||||
118 | } never executed: end of block | 0 | ||||||||||||
119 | - | |||||||||||||
120 | void QNetworkAccessDebugPipeBackend::socketBytesWritten(qint64) | - | ||||||||||||
121 | { | - | ||||||||||||
122 | pushFromUpstreamToSocket(); | - | ||||||||||||
123 | } executed 136 times by 1 test: end of block Executed by:
| 136 | ||||||||||||
124 | - | |||||||||||||
125 | void QNetworkAccessDebugPipeBackend::uploadReadyReadSlot() | - | ||||||||||||
126 | { | - | ||||||||||||
127 | pushFromUpstreamToSocket(); | - | ||||||||||||
128 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
129 | - | |||||||||||||
130 | void QNetworkAccessDebugPipeBackend::pushFromSocketToDownstream() | - | ||||||||||||
131 | { | - | ||||||||||||
132 | QByteArray buffer; | - | ||||||||||||
133 | - | |||||||||||||
134 | if (socket.state() == QAbstractSocket::ConnectingState) {
| 0-1 | ||||||||||||
135 | return; never executed: return; | 0 | ||||||||||||
136 | } | - | ||||||||||||
137 | - | |||||||||||||
138 | forever { | - | ||||||||||||
139 | if (hasDownloadFinished)
| 0-1 | ||||||||||||
140 | return; never executed: return; | 0 | ||||||||||||
141 | - | |||||||||||||
142 | buffer.resize(ReadBufferSize); | - | ||||||||||||
143 | qint64 haveRead = socket.read(buffer.data(), ReadBufferSize); | - | ||||||||||||
144 | - | |||||||||||||
145 | if (haveRead == -1) {
| 0-1 | ||||||||||||
146 | hasDownloadFinished = true; | - | ||||||||||||
147 | // this ensures a good last downloadProgress is emitted | - | ||||||||||||
148 | setHeader(QNetworkRequest::ContentLengthHeader, QVariant()); | - | ||||||||||||
149 | possiblyFinish(); | - | ||||||||||||
150 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||
151 | } else if (haveRead == 0) {
| 0 | ||||||||||||
152 | break; never executed: break; | 0 | ||||||||||||
153 | } else { | - | ||||||||||||
154 | // have read something | - | ||||||||||||
155 | buffer.resize(haveRead); | - | ||||||||||||
156 | bytesDownloaded += haveRead; | - | ||||||||||||
157 | - | |||||||||||||
158 | QByteDataBuffer list; | - | ||||||||||||
159 | list.append(buffer); | - | ||||||||||||
160 | buffer.clear(); // important because of implicit sharing! | - | ||||||||||||
161 | writeDownstreamData(list); | - | ||||||||||||
162 | } never executed: end of block | 0 | ||||||||||||
163 | } | - | ||||||||||||
164 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
165 | - | |||||||||||||
166 | void QNetworkAccessDebugPipeBackend::pushFromUpstreamToSocket() | - | ||||||||||||
167 | { | - | ||||||||||||
168 | // FIXME | - | ||||||||||||
169 | if (operation() == QNetworkAccessManager::PutOperation) {
| 0-143 | ||||||||||||
170 | if (hasUploadFinished)
| 0-143 | ||||||||||||
171 | return; never executed: return; | 0 | ||||||||||||
172 | - | |||||||||||||
173 | forever { | - | ||||||||||||
174 | if (socket.bytesToWrite() >= WriteBufferSize)
| 136-149 | ||||||||||||
175 | return; executed 136 times by 1 test: return; Executed by:
| 136 | ||||||||||||
176 | - | |||||||||||||
177 | qint64 haveRead; | - | ||||||||||||
178 | const char *readPointer = uploadByteDevice->readPointer(WriteBufferSize, haveRead); | - | ||||||||||||
179 | if (haveRead == -1) {
| 7-142 | ||||||||||||
180 | // EOF | - | ||||||||||||
181 | hasUploadFinished = true; | - | ||||||||||||
182 | emitReplyUploadProgress(bytesUploaded, bytesUploaded); | - | ||||||||||||
183 | possiblyFinish(); | - | ||||||||||||
184 | break; executed 7 times by 1 test: break; Executed by:
| 7 | ||||||||||||
185 | } else if (haveRead == 0 || readPointer == 0) {
| 0-142 | ||||||||||||
186 | // nothing to read right now, we will be called again later | - | ||||||||||||
187 | break; never executed: break; | 0 | ||||||||||||
188 | } else { | - | ||||||||||||
189 | qint64 haveWritten; | - | ||||||||||||
190 | haveWritten = socket.write(readPointer, haveRead); | - | ||||||||||||
191 | - | |||||||||||||
192 | if (haveWritten < 0) {
| 0-142 | ||||||||||||
193 | // write error! | - | ||||||||||||
194 | QString msg = QCoreApplication::translate("QNetworkAccessDebugPipeBackend", "Write error writing to %1: %2") | - | ||||||||||||
195 | .arg(url().toString(), socket.errorString()); | - | ||||||||||||
196 | error(QNetworkReply::ProtocolFailure, msg); | - | ||||||||||||
197 | finished(); | - | ||||||||||||
198 | return; never executed: return; | 0 | ||||||||||||
199 | } else { | - | ||||||||||||
200 | uploadByteDevice->advanceReadPointer(haveWritten); | - | ||||||||||||
201 | bytesUploaded += haveWritten; | - | ||||||||||||
202 | emitReplyUploadProgress(bytesUploaded, -1); | - | ||||||||||||
203 | } executed 142 times by 1 test: end of block Executed by:
| 142 | ||||||||||||
204 | - | |||||||||||||
205 | //QCoreApplication::processEvents(); | - | ||||||||||||
206 | - | |||||||||||||
207 | } | - | ||||||||||||
208 | } | - | ||||||||||||
209 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
210 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
211 | - | |||||||||||||
212 | void QNetworkAccessDebugPipeBackend::possiblyFinish() | - | ||||||||||||
213 | { | - | ||||||||||||
214 | if (hasEverythingFinished)
| 1-7 | ||||||||||||
215 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||
216 | hasEverythingFinished = true; | - | ||||||||||||
217 | - | |||||||||||||
218 | if ((operation() == QNetworkAccessManager::GetOperation) && hasDownloadFinished) {
| 0-7 | ||||||||||||
219 | socket.close(); | - | ||||||||||||
220 | finished(); | - | ||||||||||||
221 | } else if ((operation() == QNetworkAccessManager::PutOperation) && hasUploadFinished) { never executed: end of block
| 0-7 | ||||||||||||
222 | socket.close(); | - | ||||||||||||
223 | finished(); | - | ||||||||||||
224 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
225 | - | |||||||||||||
226 | - | |||||||||||||
227 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
228 | - | |||||||||||||
229 | void QNetworkAccessDebugPipeBackend::closeDownstreamChannel() | - | ||||||||||||
230 | { | - | ||||||||||||
231 | qWarning("QNetworkAccessDebugPipeBackend::closeDownstreamChannel() %d",operation());; | - | ||||||||||||
232 | //if (operation() == QNetworkAccessManager::GetOperation) | - | ||||||||||||
233 | // socket.disconnectFromHost(); | - | ||||||||||||
234 | } never executed: end of block | 0 | ||||||||||||
235 | - | |||||||||||||
236 | - | |||||||||||||
237 | void QNetworkAccessDebugPipeBackend::socketError() | - | ||||||||||||
238 | { | - | ||||||||||||
239 | qWarning("QNetworkAccessDebugPipeBackend::socketError() %d",socket.error()); | - | ||||||||||||
240 | QNetworkReply::NetworkError code; | - | ||||||||||||
241 | switch (socket.error()) { | - | ||||||||||||
242 | case QAbstractSocket::RemoteHostClosedError: never executed: case QAbstractSocket::RemoteHostClosedError: | 0 | ||||||||||||
243 | return; // socketDisconnected will be called never executed: return; | 0 | ||||||||||||
244 | - | |||||||||||||
245 | case QAbstractSocket::NetworkError: never executed: case QAbstractSocket::NetworkError: | 0 | ||||||||||||
246 | code = QNetworkReply::UnknownNetworkError; | - | ||||||||||||
247 | break; never executed: break; | 0 | ||||||||||||
248 | - | |||||||||||||
249 | default: never executed: default: | 0 | ||||||||||||
250 | code = QNetworkReply::ProtocolFailure; | - | ||||||||||||
251 | break; never executed: break; | 0 | ||||||||||||
252 | } | - | ||||||||||||
253 | - | |||||||||||||
254 | error(code, QNetworkAccessDebugPipeBackend::tr("Socket error on %1: %2") | - | ||||||||||||
255 | .arg(url().toString(), socket.errorString())); | - | ||||||||||||
256 | finished(); | - | ||||||||||||
257 | disconnect(&socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); | - | ||||||||||||
258 | - | |||||||||||||
259 | } never executed: end of block | 0 | ||||||||||||
260 | - | |||||||||||||
261 | void QNetworkAccessDebugPipeBackend::socketDisconnected() | - | ||||||||||||
262 | { | - | ||||||||||||
263 | pushFromSocketToDownstream(); | - | ||||||||||||
264 | - | |||||||||||||
265 | if (socket.bytesToWrite() == 0) {
| 0-1 | ||||||||||||
266 | // normal close | - | ||||||||||||
267 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
268 | // abnormal close | - | ||||||||||||
269 | QString msg = QNetworkAccessDebugPipeBackend::tr("Remote host closed the connection prematurely on %1") | - | ||||||||||||
270 | .arg(url().toString()); | - | ||||||||||||
271 | error(QNetworkReply::RemoteHostClosedError, msg); | - | ||||||||||||
272 | finished(); | - | ||||||||||||
273 | } never executed: end of block | 0 | ||||||||||||
274 | } | - | ||||||||||||
275 | - | |||||||||||||
276 | void QNetworkAccessDebugPipeBackend::socketConnected() | - | ||||||||||||
277 | { | - | ||||||||||||
278 | } | - | ||||||||||||
279 | - | |||||||||||||
280 | - | |||||||||||||
281 | #endif | - | ||||||||||||
282 | - | |||||||||||||
283 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |