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