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