| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qhttpprotocolhandler.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Copyright (C) 2014 BlackBerry Limited. All rights reserved. | - | ||||||||||||||||||
| 5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
| 6 | ** | - | ||||||||||||||||||
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||||||||
| 8 | ** | - | ||||||||||||||||||
| 9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
| 10 | ** Commercial License Usage | - | ||||||||||||||||||
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 15 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 16 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
| 17 | ** | - | ||||||||||||||||||
| 18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 20 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
| 21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
| 22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
| 23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
| 24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
| 25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
| 26 | ** | - | ||||||||||||||||||
| 27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
| 28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
| 29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
| 30 | ** | - | ||||||||||||||||||
| 31 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 32 | ** | - | ||||||||||||||||||
| 33 | ****************************************************************************/ | - | ||||||||||||||||||
| 34 | - | |||||||||||||||||||
| 35 | #include <private/qhttpprotocolhandler_p.h> | - | ||||||||||||||||||
| 36 | #include <private/qnoncontiguousbytedevice_p.h> | - | ||||||||||||||||||
| 37 | #include <private/qhttpnetworkconnectionchannel_p.h> | - | ||||||||||||||||||
| 38 | - | |||||||||||||||||||
| 39 | #ifndef QT_NO_HTTP | - | ||||||||||||||||||
| 40 | - | |||||||||||||||||||
| 41 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 42 | - | |||||||||||||||||||
| 43 | QHttpProtocolHandler::QHttpProtocolHandler(QHttpNetworkConnectionChannel *channel) | - | ||||||||||||||||||
| 44 | : QAbstractProtocolHandler(channel) | - | ||||||||||||||||||
| 45 | { | - | ||||||||||||||||||
| 46 | } executed 730 times by 8 tests: end of blockExecuted by:
| 730 | ||||||||||||||||||
| 47 | - | |||||||||||||||||||
| 48 | void QHttpProtocolHandler::_q_receiveReply() | - | ||||||||||||||||||
| 49 | { | - | ||||||||||||||||||
| 50 | Q_ASSERT(m_socket); | - | ||||||||||||||||||
| 51 | - | |||||||||||||||||||
| 52 | if (!m_reply) {
| 8-9333 | ||||||||||||||||||
| 53 | if (m_socket->bytesAvailable() > 0)
| 0-8 | ||||||||||||||||||
| 54 | qWarning() << "QAbstractProtocolHandler::_q_receiveReply() called without QHttpNetworkReply," never executed: QMessageLogger(__FILE__, 54, __PRETTY_FUNCTION__).warning() << "QAbstractProtocolHandler::_q_receiveReply() called without QHttpNetworkReply," << m_socket->bytesAvailable() << "bytes on socket."; | 0 | ||||||||||||||||||
| 55 | << m_socket->bytesAvailable() << "bytes on socket."; never executed: QMessageLogger(__FILE__, 54, __PRETTY_FUNCTION__).warning() << "QAbstractProtocolHandler::_q_receiveReply() called without QHttpNetworkReply," << m_socket->bytesAvailable() << "bytes on socket."; | 0 | ||||||||||||||||||
| 56 | m_channel->close(); | - | ||||||||||||||||||
| 57 | return; executed 8 times by 1 test: return;Executed by:
| 8 | ||||||||||||||||||
| 58 | } | - | ||||||||||||||||||
| 59 | - | |||||||||||||||||||
| 60 | // only run when the QHttpNetworkConnection is not currently being destructed, e.g. | - | ||||||||||||||||||
| 61 | // this function is called from _q_disconnected which is called because | - | ||||||||||||||||||
| 62 | // of ~QHttpNetworkConnectionPrivate | - | ||||||||||||||||||
| 63 | if (!qobject_cast<QHttpNetworkConnection*>(m_connection)) {
| 0-9333 | ||||||||||||||||||
| 64 | return; never executed: return; | 0 | ||||||||||||||||||
| 65 | } | - | ||||||||||||||||||
| 66 | - | |||||||||||||||||||
| 67 | QAbstractSocket::SocketState socketState = m_socket->state(); | - | ||||||||||||||||||
| 68 | - | |||||||||||||||||||
| 69 | // connection might be closed to signal the end of data | - | ||||||||||||||||||
| 70 | if (socketState == QAbstractSocket::UnconnectedState) {
| 33-9300 | ||||||||||||||||||
| 71 | if (m_socket->bytesAvailable() <= 0) {
| 0-33 | ||||||||||||||||||
| 72 | if (m_reply->d_func()->state == QHttpNetworkReplyPrivate::ReadingDataState) {
| 16-17 | ||||||||||||||||||
| 73 | // finish this reply. this case happens when the server did not send a content length | - | ||||||||||||||||||
| 74 | m_reply->d_func()->state = QHttpNetworkReplyPrivate::AllDoneState; | - | ||||||||||||||||||
| 75 | m_channel->allDone(); | - | ||||||||||||||||||
| 76 | return; executed 17 times by 2 tests: return;Executed by:
| 17 | ||||||||||||||||||
| 77 | } else { | - | ||||||||||||||||||
| 78 | m_channel->handleUnexpectedEOF(); | - | ||||||||||||||||||
| 79 | return; executed 16 times by 2 tests: return;Executed by:
| 16 | ||||||||||||||||||
| 80 | } | - | ||||||||||||||||||
| 81 | } else { | - | ||||||||||||||||||
| 82 | // socket not connected but still bytes for reading.. just continue in this function | - | ||||||||||||||||||
| 83 | } never executed: end of block | 0 | ||||||||||||||||||
| 84 | } | - | ||||||||||||||||||
| 85 | - | |||||||||||||||||||
| 86 | // read loop for the response | - | ||||||||||||||||||
| 87 | qint64 bytes = 0; | - | ||||||||||||||||||
| 88 | qint64 lastBytes = bytes; | - | ||||||||||||||||||
| 89 | do { | - | ||||||||||||||||||
| 90 | lastBytes = bytes; | - | ||||||||||||||||||
| 91 | - | |||||||||||||||||||
| 92 | QHttpNetworkReplyPrivate::ReplyState state = m_reply->d_func()->state; | - | ||||||||||||||||||
| 93 | switch (state) { | - | ||||||||||||||||||
| 94 | case QHttpNetworkReplyPrivate::NothingDoneState: { executed 1694 times by 8 tests: case QHttpNetworkReplyPrivate::NothingDoneState:Executed by:
| 1694 | ||||||||||||||||||
| 95 | m_reply->d_func()->state = QHttpNetworkReplyPrivate::ReadingStatusState; | - | ||||||||||||||||||
| 96 | // fallthrough | - | ||||||||||||||||||
| 97 | } | - | ||||||||||||||||||
| 98 | case QHttpNetworkReplyPrivate::ReadingStatusState: { code before this statement executed 1694 times by 8 tests: case QHttpNetworkReplyPrivate::ReadingStatusState:Executed by:
executed 109 times by 2 tests: case QHttpNetworkReplyPrivate::ReadingStatusState:Executed by:
| 109-1694 | ||||||||||||||||||
| 99 | qint64 statusBytes = m_reply->d_func()->readStatus(m_socket); | - | ||||||||||||||||||
| 100 | if (statusBytes == -1) {
| 70-1733 | ||||||||||||||||||
| 101 | // connection broke while reading status. also handled if later _q_disconnected is called | - | ||||||||||||||||||
| 102 | m_channel->handleUnexpectedEOF(); | - | ||||||||||||||||||
| 103 | return; executed 70 times by 1 test: return;Executed by:
| 70 | ||||||||||||||||||
| 104 | } | - | ||||||||||||||||||
| 105 | bytes += statusBytes; | - | ||||||||||||||||||
| 106 | m_channel->lastStatus = m_reply->d_func()->statusCode; | - | ||||||||||||||||||
| 107 | break; executed 1733 times by 8 tests: break;Executed by:
| 1733 | ||||||||||||||||||
| 108 | } | - | ||||||||||||||||||
| 109 | case QHttpNetworkReplyPrivate::ReadingHeaderState: { executed 1702 times by 8 tests: case QHttpNetworkReplyPrivate::ReadingHeaderState:Executed by:
| 1702 | ||||||||||||||||||
| 110 | QHttpNetworkReplyPrivate *replyPrivate = m_reply->d_func(); | - | ||||||||||||||||||
| 111 | qint64 headerBytes = replyPrivate->readHeader(m_socket); | - | ||||||||||||||||||
| 112 | if (headerBytes == -1) {
| 6-1696 | ||||||||||||||||||
| 113 | // connection broke while reading headers. also handled if later _q_disconnected is called | - | ||||||||||||||||||
| 114 | m_channel->handleUnexpectedEOF(); | - | ||||||||||||||||||
| 115 | return; executed 6 times by 1 test: return;Executed by:
| 6 | ||||||||||||||||||
| 116 | } | - | ||||||||||||||||||
| 117 | bytes += headerBytes; | - | ||||||||||||||||||
| 118 | // If headers were parsed successfully now it is the ReadingDataState | - | ||||||||||||||||||
| 119 | if (replyPrivate->state == QHttpNetworkReplyPrivate::ReadingDataState) {
| 71-1625 | ||||||||||||||||||
| 120 | if (replyPrivate->isCompressed() && replyPrivate->autoDecompress) {
| 1-1617 | ||||||||||||||||||
| 121 | // remove the Content-Length from header | - | ||||||||||||||||||
| 122 | replyPrivate->removeAutoDecompressHeader(); | - | ||||||||||||||||||
| 123 | } else { executed 7 times by 1 test: end of blockExecuted by:
| 7 | ||||||||||||||||||
| 124 | replyPrivate->autoDecompress = false; | - | ||||||||||||||||||
| 125 | } executed 1618 times by 8 tests: end of blockExecuted by:
| 1618 | ||||||||||||||||||
| 126 | if (replyPrivate->statusCode == 100) {
| 7-1618 | ||||||||||||||||||
| 127 | replyPrivate->clearHttpLayerInformation(); | - | ||||||||||||||||||
| 128 | replyPrivate->state = QHttpNetworkReplyPrivate::ReadingStatusState; | - | ||||||||||||||||||
| 129 | break; // ignore executed 7 times by 1 test: break;Executed by:
| 7 | ||||||||||||||||||
| 130 | } | - | ||||||||||||||||||
| 131 | if (replyPrivate->shouldEmitSignals())
| 199-1419 | ||||||||||||||||||
| 132 | emit m_reply->headerChanged(); executed 1419 times by 8 tests: m_reply->headerChanged();Executed by:
| 1419 | ||||||||||||||||||
| 133 | // After headerChanged had been emitted | - | ||||||||||||||||||
| 134 | // we can suddenly have a replyPrivate->userProvidedDownloadBuffer | - | ||||||||||||||||||
| 135 | // this is handled in the ReadingDataState however | - | ||||||||||||||||||
| 136 | - | |||||||||||||||||||
| 137 | if (!replyPrivate->expectContent()) {
| 151-1467 | ||||||||||||||||||
| 138 | replyPrivate->state = QHttpNetworkReplyPrivate::AllDoneState; | - | ||||||||||||||||||
| 139 | m_channel->allDone(); | - | ||||||||||||||||||
| 140 | break; executed 151 times by 4 tests: break;Executed by:
| 151 | ||||||||||||||||||
| 141 | } | - | ||||||||||||||||||
| 142 | } executed 1467 times by 7 tests: end of blockExecuted by:
| 1467 | ||||||||||||||||||
| 143 | break; executed 1538 times by 7 tests: break;Executed by:
| 1538 | ||||||||||||||||||
| 144 | } | - | ||||||||||||||||||
| 145 | case QHttpNetworkReplyPrivate::ReadingDataState: { executed 16925 times by 7 tests: case QHttpNetworkReplyPrivate::ReadingDataState:Executed by:
| 16925 | ||||||||||||||||||
| 146 | QHttpNetworkReplyPrivate *replyPrivate = m_reply->d_func(); | - | ||||||||||||||||||
| 147 | if (m_socket->state() == QAbstractSocket::ConnectedState &&
| 0-16925 | ||||||||||||||||||
| 148 | replyPrivate->downstreamLimited && !replyPrivate->responseData.isEmpty() && replyPrivate->shouldEmitSignals()) {
| 0-16194 | ||||||||||||||||||
| 149 | // (only do the following when still connected, not when we have already been disconnected and there is still data) | - | ||||||||||||||||||
| 150 | // We already have some HTTP body data. We don't read more from the socket until | - | ||||||||||||||||||
| 151 | // this is fetched by QHttpNetworkAccessHttpBackend. If we would read more, | - | ||||||||||||||||||
| 152 | // we could not limit our read buffer usage. | - | ||||||||||||||||||
| 153 | // We only do this when shouldEmitSignals==true because our HTTP parsing | - | ||||||||||||||||||
| 154 | // always needs to parse the 401/407 replies. Therefore they don't really obey | - | ||||||||||||||||||
| 155 | // to the read buffer maximum size, but we don't care since they should be small. | - | ||||||||||||||||||
| 156 | return; executed 227 times by 2 tests: return;Executed by:
| 227 | ||||||||||||||||||
| 157 | } | - | ||||||||||||||||||
| 158 | - | |||||||||||||||||||
| 159 | if (replyPrivate->userProvidedDownloadBuffer) {
| 2661-14037 | ||||||||||||||||||
| 160 | // the user provided a direct buffer where we should put all our data in. | - | ||||||||||||||||||
| 161 | // this only works when we can tell the user the content length and he/she can allocate | - | ||||||||||||||||||
| 162 | // the buffer in that size. | - | ||||||||||||||||||
| 163 | // note that this call will read only from the still buffered data | - | ||||||||||||||||||
| 164 | qint64 haveRead = replyPrivate->readBodyVeryFast(m_socket, replyPrivate->userProvidedDownloadBuffer + replyPrivate->totalProgress); | - | ||||||||||||||||||
| 165 | if (haveRead > 0) {
| 1166-1495 | ||||||||||||||||||
| 166 | bytes += haveRead; | - | ||||||||||||||||||
| 167 | replyPrivate->totalProgress += haveRead; | - | ||||||||||||||||||
| 168 | // the user will get notified of it via progress signal | - | ||||||||||||||||||
| 169 | emit m_reply->dataReadProgress(replyPrivate->totalProgress, replyPrivate->bodyLength); | - | ||||||||||||||||||
| 170 | } else if (haveRead == 0) { executed 1495 times by 3 tests: end of blockExecuted by:
| 2-1495 | ||||||||||||||||||
| 171 | // Happens since this called in a loop. Currently no bytes available. | - | ||||||||||||||||||
| 172 | } else if (haveRead < 0) { executed 1164 times by 1 test: end of blockExecuted by:
| 0-1164 | ||||||||||||||||||
| 173 | m_connection->d_func()->emitReplyError(m_socket, m_reply, QNetworkReply::RemoteHostClosedError); | - | ||||||||||||||||||
| 174 | break; executed 2 times by 2 tests: break;Executed by:
| 2 | ||||||||||||||||||
| 175 | } | - | ||||||||||||||||||
| 176 | } else if (!replyPrivate->isChunked() && !replyPrivate->autoDecompress executed 2659 times by 3 tests: end of blockExecuted by:
| 9-13555 | ||||||||||||||||||
| 177 | && replyPrivate->bodyLength > 0) {
| 20-13526 | ||||||||||||||||||
| 178 | // bulk files like images should fulfill these properties and | - | ||||||||||||||||||
| 179 | // we can therefore save on memory copying | - | ||||||||||||||||||
| 180 | qint64 haveRead = replyPrivate->readBodyFast(m_socket, &replyPrivate->responseData); | - | ||||||||||||||||||
| 181 | bytes += haveRead; | - | ||||||||||||||||||
| 182 | replyPrivate->totalProgress += haveRead; | - | ||||||||||||||||||
| 183 | if (replyPrivate->shouldEmitSignals()) {
| 243-13283 | ||||||||||||||||||
| 184 | emit m_reply->readyRead(); | - | ||||||||||||||||||
| 185 | emit m_reply->dataReadProgress(replyPrivate->totalProgress, replyPrivate->bodyLength); | - | ||||||||||||||||||
| 186 | } executed 13283 times by 3 tests: end of blockExecuted by:
| 13283 | ||||||||||||||||||
| 187 | } executed 13526 times by 3 tests: end of blockExecuted by:
| 13526 | ||||||||||||||||||
| 188 | else | - | ||||||||||||||||||
| 189 | { | - | ||||||||||||||||||
| 190 | // use the traditional slower reading (for compressed encoding, chunked encoding, | - | ||||||||||||||||||
| 191 | // no content-length etc) | - | ||||||||||||||||||
| 192 | qint64 haveRead = replyPrivate->readBody(m_socket, &replyPrivate->responseData); | - | ||||||||||||||||||
| 193 | if (haveRead > 0) {
| 206-305 | ||||||||||||||||||
| 194 | bytes += haveRead; | - | ||||||||||||||||||
| 195 | replyPrivate->totalProgress += haveRead; | - | ||||||||||||||||||
| 196 | if (replyPrivate->shouldEmitSignals()) {
| 4-301 | ||||||||||||||||||
| 197 | emit m_reply->readyRead(); | - | ||||||||||||||||||
| 198 | emit m_reply->dataReadProgress(replyPrivate->totalProgress, replyPrivate->bodyLength); | - | ||||||||||||||||||
| 199 | } executed 301 times by 4 tests: end of blockExecuted by:
| 301 | ||||||||||||||||||
| 200 | } else if (haveRead == -1) { executed 305 times by 4 tests: end of blockExecuted by:
| 1-305 | ||||||||||||||||||
| 201 | // Some error occurred | - | ||||||||||||||||||
| 202 | m_connection->d_func()->emitReplyError(m_socket, m_reply, QNetworkReply::ProtocolFailure); | - | ||||||||||||||||||
| 203 | break; executed 1 time by 1 test: break;Executed by:
| 1 | ||||||||||||||||||
| 204 | } | - | ||||||||||||||||||
| 205 | } executed 510 times by 5 tests: end of blockExecuted by:
| 510 | ||||||||||||||||||
| 206 | // still in ReadingDataState? This function will be called again by the socket's readyRead | - | ||||||||||||||||||
| 207 | if (replyPrivate->state == QHttpNetworkReplyPrivate::ReadingDataState)
| 1438-15257 | ||||||||||||||||||
| 208 | break; executed 15257 times by 7 tests: break;Executed by:
| 15257 | ||||||||||||||||||
| 209 | - | |||||||||||||||||||
| 210 | // everything done, fall through | - | ||||||||||||||||||
| 211 | } | - | ||||||||||||||||||
| 212 | case QHttpNetworkReplyPrivate::AllDoneState: code before this statement executed 1438 times by 6 tests: case QHttpNetworkReplyPrivate::AllDoneState:Executed by:
executed 157 times by 2 tests: case QHttpNetworkReplyPrivate::AllDoneState:Executed by:
| 157-1438 | ||||||||||||||||||
| 213 | m_channel->allDone(); | - | ||||||||||||||||||
| 214 | break; executed 1595 times by 6 tests: break;Executed by:
| 1595 | ||||||||||||||||||
| 215 | default: never executed: default: | 0 | ||||||||||||||||||
| 216 | break; never executed: break; | 0 | ||||||||||||||||||
| 217 | } | - | ||||||||||||||||||
| 218 | } while (bytes != lastBytes && m_reply);
| 1031-12318 | ||||||||||||||||||
| 219 | } executed 8997 times by 8 tests: end of blockExecuted by:
| 8997 | ||||||||||||||||||
| 220 | - | |||||||||||||||||||
| 221 | void QHttpProtocolHandler::_q_readyRead() | - | ||||||||||||||||||
| 222 | { | - | ||||||||||||||||||
| 223 | if (m_socket->state() == QAbstractSocket::ConnectedState && m_socket->bytesAvailable() == 0) {
| 0-9155 | ||||||||||||||||||
| 224 | // We got a readyRead but no bytes are available.. | - | ||||||||||||||||||
| 225 | // This happens for the Unbuffered QTcpSocket | - | ||||||||||||||||||
| 226 | // Also check if socket is in ConnectedState since | - | ||||||||||||||||||
| 227 | // this function may also be invoked via the event loop. | - | ||||||||||||||||||
| 228 | char c; | - | ||||||||||||||||||
| 229 | qint64 ret = m_socket->peek(&c, 1); | - | ||||||||||||||||||
| 230 | if (ret < 0) {
| 45-156 | ||||||||||||||||||
| 231 | m_channel->_q_error(m_socket->error()); | - | ||||||||||||||||||
| 232 | // We still need to handle the reply so it emits its signals etc. | - | ||||||||||||||||||
| 233 | if (m_reply)
| 12-33 | ||||||||||||||||||
| 234 | _q_receiveReply(); executed 33 times by 3 tests: _q_receiveReply();Executed by:
| 33 | ||||||||||||||||||
| 235 | return; executed 45 times by 4 tests: return;Executed by:
| 45 | ||||||||||||||||||
| 236 | } | - | ||||||||||||||||||
| 237 | } executed 156 times by 1 test: end of blockExecuted by:
| 156 | ||||||||||||||||||
| 238 | - | |||||||||||||||||||
| 239 | if (m_channel->isSocketWaiting() || m_channel->isSocketReading()) {
| 4-7949 | ||||||||||||||||||
| 240 | if (m_socket->bytesAvailable()) {
| 152-8954 | ||||||||||||||||||
| 241 | // We might get a spurious call from readMoreLater() | - | ||||||||||||||||||
| 242 | // call of the QHttpNetworkConnection even while the socket is disconnecting. | - | ||||||||||||||||||
| 243 | // Therefore check if there is actually bytes available before changing the channel state. | - | ||||||||||||||||||
| 244 | m_channel->state = QHttpNetworkConnectionChannel::ReadingState; | - | ||||||||||||||||||
| 245 | } executed 8954 times by 6 tests: end of blockExecuted by:
| 8954 | ||||||||||||||||||
| 246 | if (m_reply)
| 0-9106 | ||||||||||||||||||
| 247 | _q_receiveReply(); executed 9106 times by 6 tests: _q_receiveReply();Executed by:
| 9106 | ||||||||||||||||||
| 248 | } executed 9106 times by 6 tests: end of blockExecuted by:
| 9106 | ||||||||||||||||||
| 249 | } executed 9110 times by 6 tests: end of blockExecuted by:
| 9110 | ||||||||||||||||||
| 250 | - | |||||||||||||||||||
| 251 | bool QHttpProtocolHandler::sendRequest() | - | ||||||||||||||||||
| 252 | { | - | ||||||||||||||||||
| 253 | m_reply = m_channel->reply; | - | ||||||||||||||||||
| 254 | - | |||||||||||||||||||
| 255 | if (!m_reply) {
| 0-6076 | ||||||||||||||||||
| 256 | // heh, how should that happen! | - | ||||||||||||||||||
| 257 | qWarning() << "QAbstractProtocolHandler::sendRequest() called without QHttpNetworkReply"; | - | ||||||||||||||||||
| 258 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 259 | } | - | ||||||||||||||||||
| 260 | - | |||||||||||||||||||
| 261 | switch (m_channel->state) { | - | ||||||||||||||||||
| 262 | case QHttpNetworkConnectionChannel::IdleState: { // write the header executed 1447 times by 9 tests: case QHttpNetworkConnectionChannel::IdleState:Executed by:
| 1447 | ||||||||||||||||||
| 263 | if (!m_channel->ensureConnection()) {
| 122-1325 | ||||||||||||||||||
| 264 | // wait for the connection (and encryption) to be done | - | ||||||||||||||||||
| 265 | // sendRequest will be called again from either | - | ||||||||||||||||||
| 266 | // _q_connected or _q_encrypted | - | ||||||||||||||||||
| 267 | return false; executed 122 times by 3 tests: return false;Executed by:
| 122 | ||||||||||||||||||
| 268 | } | - | ||||||||||||||||||
| 269 | QString scheme = m_channel->request.url().scheme(); | - | ||||||||||||||||||
| 270 | if (scheme == QLatin1String("preconnect-http")
| 0-1325 | ||||||||||||||||||
| 271 | || scheme == QLatin1String("preconnect-https")) {
| 0-1325 | ||||||||||||||||||
| 272 | m_channel->state = QHttpNetworkConnectionChannel::IdleState; | - | ||||||||||||||||||
| 273 | m_reply->d_func()->state = QHttpNetworkReplyPrivate::AllDoneState; | - | ||||||||||||||||||
| 274 | m_channel->allDone(); | - | ||||||||||||||||||
| 275 | m_connection->preConnectFinished(); // will only decrease the counter | - | ||||||||||||||||||
| 276 | m_reply = 0; // so we can reuse this channel | - | ||||||||||||||||||
| 277 | return true; // we have a working connection and are done never executed: return true; | 0 | ||||||||||||||||||
| 278 | } | - | ||||||||||||||||||
| 279 | - | |||||||||||||||||||
| 280 | m_channel->written = 0; // excluding the header | - | ||||||||||||||||||
| 281 | m_channel->bytesTotal = 0; | - | ||||||||||||||||||
| 282 | - | |||||||||||||||||||
| 283 | QHttpNetworkReplyPrivate *replyPrivate = m_reply->d_func(); | - | ||||||||||||||||||
| 284 | replyPrivate->clear(); | - | ||||||||||||||||||
| 285 | replyPrivate->connection = m_connection; | - | ||||||||||||||||||
| 286 | replyPrivate->connectionChannel = m_channel; | - | ||||||||||||||||||
| 287 | replyPrivate->autoDecompress = m_channel->request.d->autoDecompress; | - | ||||||||||||||||||
| 288 | replyPrivate->pipeliningUsed = false; | - | ||||||||||||||||||
| 289 | - | |||||||||||||||||||
| 290 | // if the url contains authentication parameters, use the new ones | - | ||||||||||||||||||
| 291 | // both channels will use the new authentication parameters | - | ||||||||||||||||||
| 292 | if (!m_channel->request.url().userInfo().isEmpty() && m_channel->request.withCredentials()) {
| 0-1299 | ||||||||||||||||||
| 293 | QUrl url = m_channel->request.url(); | - | ||||||||||||||||||
| 294 | QAuthenticator &auth = m_channel->authenticator; | - | ||||||||||||||||||
| 295 | if (url.userName() != auth.user()
| 10-16 | ||||||||||||||||||
| 296 | || (!url.password().isEmpty() && url.password() != auth.password())) {
| 0-10 | ||||||||||||||||||
| 297 | auth.setUser(url.userName()); | - | ||||||||||||||||||
| 298 | auth.setPassword(url.password()); | - | ||||||||||||||||||
| 299 | m_connection->d_func()->copyCredentials(m_connection->d_func()->indexOf(m_socket), &auth, false); | - | ||||||||||||||||||
| 300 | } executed 18 times by 1 test: end of blockExecuted by:
| 18 | ||||||||||||||||||
| 301 | // clear the userinfo, since we use the same request for resending | - | ||||||||||||||||||
| 302 | // userinfo in url can conflict with the one in the authenticator | - | ||||||||||||||||||
| 303 | url.setUserInfo(QString()); | - | ||||||||||||||||||
| 304 | m_channel->request.setUrl(url); | - | ||||||||||||||||||
| 305 | } executed 26 times by 1 test: end of blockExecuted by:
| 26 | ||||||||||||||||||
| 306 | // Will only be false if Qt WebKit is performing a cross-origin XMLHttpRequest | - | ||||||||||||||||||
| 307 | // and withCredentials has not been set to true. | - | ||||||||||||||||||
| 308 | if (m_channel->request.withCredentials())
| 1-1324 | ||||||||||||||||||
| 309 | m_connection->d_func()->createAuthorization(m_socket, m_channel->request); executed 1324 times by 9 tests: m_connection->d_func()->createAuthorization(m_socket, m_channel->request);Executed by:
| 1324 | ||||||||||||||||||
| 310 | #ifndef QT_NO_NETWORKPROXY | - | ||||||||||||||||||
| 311 | QByteArray header = QHttpNetworkRequestPrivate::header(m_channel->request, | - | ||||||||||||||||||
| 312 | (m_connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy)); | - | ||||||||||||||||||
| 313 | #else | - | ||||||||||||||||||
| 314 | QByteArray header = QHttpNetworkRequestPrivate::header(m_channel->request, false); | - | ||||||||||||||||||
| 315 | #endif | - | ||||||||||||||||||
| 316 | m_socket->write(header); | - | ||||||||||||||||||
| 317 | // flushing is dangerous (QSslSocket calls transmit which might read or error) | - | ||||||||||||||||||
| 318 | // m_socket->flush(); | - | ||||||||||||||||||
| 319 | QNonContiguousByteDevice* uploadByteDevice = m_channel->request.uploadByteDevice(); | - | ||||||||||||||||||
| 320 | if (uploadByteDevice) {
| 227-1098 | ||||||||||||||||||
| 321 | // connect the signals so this function gets called again | - | ||||||||||||||||||
| 322 | QObject::connect(uploadByteDevice, SIGNAL(readyRead()), m_channel, SLOT(_q_uploadDataReadyRead())); | - | ||||||||||||||||||
| 323 | - | |||||||||||||||||||
| 324 | m_channel->bytesTotal = m_channel->request.contentLength(); | - | ||||||||||||||||||
| 325 | - | |||||||||||||||||||
| 326 | m_channel->state = QHttpNetworkConnectionChannel::WritingState; // start writing data | - | ||||||||||||||||||
| 327 | sendRequest(); //recurse | - | ||||||||||||||||||
| 328 | } else { executed 227 times by 3 tests: end of blockExecuted by:
| 227 | ||||||||||||||||||
| 329 | m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response | - | ||||||||||||||||||
| 330 | sendRequest(); //recurse | - | ||||||||||||||||||
| 331 | } executed 1098 times by 8 tests: end of blockExecuted by:
| 1098 | ||||||||||||||||||
| 332 | - | |||||||||||||||||||
| 333 | break; executed 1325 times by 9 tests: break;Executed by:
| 1325 | ||||||||||||||||||
| 334 | } | - | ||||||||||||||||||
| 335 | case QHttpNetworkConnectionChannel::WritingState: executed 3304 times by 3 tests: case QHttpNetworkConnectionChannel::WritingState:Executed by:
| 3304 | ||||||||||||||||||
| 336 | { | - | ||||||||||||||||||
| 337 | // write the data | - | ||||||||||||||||||
| 338 | QNonContiguousByteDevice* uploadByteDevice = m_channel->request.uploadByteDevice(); | - | ||||||||||||||||||
| 339 | if (!uploadByteDevice || m_channel->bytesTotal == m_channel->written) {
| 0-3304 | ||||||||||||||||||
| 340 | if (uploadByteDevice)
| 0-31 | ||||||||||||||||||
| 341 | emit m_reply->dataSendProgress(m_channel->written, m_channel->bytesTotal); executed 31 times by 1 test: m_reply->dataSendProgress(m_channel->written, m_channel->bytesTotal);Executed by:
| 31 | ||||||||||||||||||
| 342 | m_channel->state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response | - | ||||||||||||||||||
| 343 | sendRequest(); // recurse | - | ||||||||||||||||||
| 344 | break; executed 31 times by 1 test: break;Executed by:
| 31 | ||||||||||||||||||
| 345 | } | - | ||||||||||||||||||
| 346 | - | |||||||||||||||||||
| 347 | // only feed the QTcpSocket buffer when there is less than 32 kB in it | - | ||||||||||||||||||
| 348 | const qint64 socketBufferFill = 32*1024; | - | ||||||||||||||||||
| 349 | const qint64 socketWriteMaxSize = 16*1024; | - | ||||||||||||||||||
| 350 | - | |||||||||||||||||||
| 351 | - | |||||||||||||||||||
| 352 | #ifndef QT_NO_SSL | - | ||||||||||||||||||
| 353 | QSslSocket *sslSocket = qobject_cast<QSslSocket*>(m_socket); | - | ||||||||||||||||||
| 354 | // if it is really an ssl socket, check more than just bytesToWrite() | - | ||||||||||||||||||
| 355 | while ((m_socket->bytesToWrite() + (sslSocket ? sslSocket->encryptedBytesToWrite() : 0))
| 350-5403 | ||||||||||||||||||
| 356 | <= socketBufferFill && m_channel->bytesTotal != m_channel->written)
| 0-5403 | ||||||||||||||||||
| 357 | #else | - | ||||||||||||||||||
| 358 | while (m_socket->bytesToWrite() <= socketBufferFill | - | ||||||||||||||||||
| 359 | && m_channel->bytesTotal != m_channel->written) | - | ||||||||||||||||||
| 360 | #endif | - | ||||||||||||||||||
| 361 | { | - | ||||||||||||||||||
| 362 | // get pointer to upload data | - | ||||||||||||||||||
| 363 | qint64 currentReadSize = 0; | - | ||||||||||||||||||
| 364 | qint64 desiredReadSize = qMin(socketWriteMaxSize, m_channel->bytesTotal - m_channel->written); | - | ||||||||||||||||||
| 365 | const char *readPointer = uploadByteDevice->readPointer(desiredReadSize, currentReadSize); | - | ||||||||||||||||||
| 366 | - | |||||||||||||||||||
| 367 | if (currentReadSize == -1) {
| 0-5403 | ||||||||||||||||||
| 368 | // premature eof happened | - | ||||||||||||||||||
| 369 | m_connection->d_func()->emitReplyError(m_socket, m_reply, QNetworkReply::UnknownNetworkError); | - | ||||||||||||||||||
| 370 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 371 | } else if (readPointer == 0 || currentReadSize == 0) {
| 0-2727 | ||||||||||||||||||
| 372 | // nothing to read currently, break the loop | - | ||||||||||||||||||
| 373 | break; executed 2727 times by 2 tests: break;Executed by:
| 2727 | ||||||||||||||||||
| 374 | } else { | - | ||||||||||||||||||
| 375 | if (m_channel->written != uploadByteDevice->pos()) {
| 0-2676 | ||||||||||||||||||
| 376 | // Sanity check. This was useful in tracking down an upload corruption. | - | ||||||||||||||||||
| 377 | qWarning() << "QHttpProtocolHandler: Internal error in sendRequest. Expected to write at position" << m_channel->written << "but read device is at" << uploadByteDevice->pos(); | - | ||||||||||||||||||
| 378 | Q_ASSERT(m_channel->written == uploadByteDevice->pos()); | - | ||||||||||||||||||
| 379 | m_connection->d_func()->emitReplyError(m_socket, m_reply, QNetworkReply::ProtocolFailure); | - | ||||||||||||||||||
| 380 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 381 | } | - | ||||||||||||||||||
| 382 | qint64 currentWriteSize = m_socket->write(readPointer, currentReadSize); | - | ||||||||||||||||||
| 383 | if (currentWriteSize == -1 || currentWriteSize != currentReadSize) {
| 0-2676 | ||||||||||||||||||
| 384 | // socket broke down | - | ||||||||||||||||||
| 385 | m_connection->d_func()->emitReplyError(m_socket, m_reply, QNetworkReply::UnknownNetworkError); | - | ||||||||||||||||||
| 386 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 387 | } else { | - | ||||||||||||||||||
| 388 | m_channel->written += currentWriteSize; | - | ||||||||||||||||||
| 389 | uploadByteDevice->advanceReadPointer(currentWriteSize); | - | ||||||||||||||||||
| 390 | - | |||||||||||||||||||
| 391 | emit m_reply->dataSendProgress(m_channel->written, m_channel->bytesTotal); | - | ||||||||||||||||||
| 392 | - | |||||||||||||||||||
| 393 | if (m_channel->written == m_channel->bytesTotal) {
| 196-2480 | ||||||||||||||||||
| 394 | // make sure this function is called once again | - | ||||||||||||||||||
| 395 | m_channel->state = QHttpNetworkConnectionChannel::WaitingState; | - | ||||||||||||||||||
| 396 | sendRequest(); | - | ||||||||||||||||||
| 397 | break; executed 196 times by 3 tests: break;Executed by:
| 196 | ||||||||||||||||||
| 398 | } | - | ||||||||||||||||||
| 399 | } executed 2480 times by 1 test: end of blockExecuted by:
| 2480 | ||||||||||||||||||
| 400 | } | - | ||||||||||||||||||
| 401 | } | - | ||||||||||||||||||
| 402 | break; executed 3273 times by 3 tests: break;Executed by:
| 3273 | ||||||||||||||||||
| 403 | } | - | ||||||||||||||||||
| 404 | - | |||||||||||||||||||
| 405 | case QHttpNetworkConnectionChannel::WaitingState: executed 1325 times by 9 tests: case QHttpNetworkConnectionChannel::WaitingState:Executed by:
| 1325 | ||||||||||||||||||
| 406 | { | - | ||||||||||||||||||
| 407 | QNonContiguousByteDevice* uploadByteDevice = m_channel->request.uploadByteDevice(); | - | ||||||||||||||||||
| 408 | if (uploadByteDevice) {
| 227-1098 | ||||||||||||||||||
| 409 | QObject::disconnect(uploadByteDevice, SIGNAL(readyRead()), m_channel, SLOT(_q_uploadDataReadyRead())); | - | ||||||||||||||||||
| 410 | } executed 227 times by 3 tests: end of blockExecuted by:
| 227 | ||||||||||||||||||
| 411 | - | |||||||||||||||||||
| 412 | // HTTP pipelining | - | ||||||||||||||||||
| 413 | //m_connection->d_func()->fillPipeline(m_socket); | - | ||||||||||||||||||
| 414 | //m_socket->flush(); | - | ||||||||||||||||||
| 415 | - | |||||||||||||||||||
| 416 | // ensure we try to receive a reply in all cases, even if _q_readyRead_ hat not been called | - | ||||||||||||||||||
| 417 | // this is needed if the sends an reply before we have finished sending the request. In that | - | ||||||||||||||||||
| 418 | // case receiveReply had been called before but ignored the server reply | - | ||||||||||||||||||
| 419 | if (m_socket->bytesAvailable())
| 197-1128 | ||||||||||||||||||
| 420 | QMetaObject::invokeMethod(m_channel, "_q_receiveReply", Qt::QueuedConnection); executed 197 times by 3 tests: QMetaObject::invokeMethod(m_channel, "_q_receiveReply", Qt::QueuedConnection);Executed by:
| 197 | ||||||||||||||||||
| 421 | break; executed 1325 times by 9 tests: break;Executed by:
| 1325 | ||||||||||||||||||
| 422 | } | - | ||||||||||||||||||
| 423 | case QHttpNetworkConnectionChannel::ReadingState: never executed: case QHttpNetworkConnectionChannel::ReadingState: | 0 | ||||||||||||||||||
| 424 | // ignore _q_bytesWritten in these states | - | ||||||||||||||||||
| 425 | // fall through | - | ||||||||||||||||||
| 426 | default: never executed: default: | 0 | ||||||||||||||||||
| 427 | break; never executed: break; | 0 | ||||||||||||||||||
| 428 | } | - | ||||||||||||||||||
| 429 | return true; executed 5954 times by 9 tests: return true;Executed by:
| 5954 | ||||||||||||||||||
| 430 | } | - | ||||||||||||||||||
| 431 | - | |||||||||||||||||||
| 432 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 433 | - | |||||||||||||||||||
| 434 | #endif // QT_NO_HTTP | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |