author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 5 | d3bac044e0f0 |
child 8 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qhttpnetworkconnection_p.h" |
|
43 |
#include "qhttpnetworkconnectionchannel_p.h" |
|
44 |
#include "private/qnoncontiguousbytedevice_p.h" |
|
45 |
||
46 |
#include <qpair.h> |
|
47 |
#include <qdebug.h> |
|
48 |
||
49 |
#ifndef QT_NO_HTTP |
|
50 |
||
51 |
#ifndef QT_NO_OPENSSL |
|
52 |
# include <QtNetwork/qsslkey.h> |
|
53 |
# include <QtNetwork/qsslcipher.h> |
|
54 |
# include <QtNetwork/qsslconfiguration.h> |
|
55 |
#endif |
|
56 |
||
57 |
QT_BEGIN_NAMESPACE |
|
58 |
||
59 |
// TODO: Put channel specific stuff here so it does not polute qhttpnetworkconnection.cpp |
|
60 |
||
61 |
void QHttpNetworkConnectionChannel::init() |
|
62 |
{ |
|
63 |
#ifndef QT_NO_OPENSSL |
|
64 |
if (connection->d_func()->encrypt) |
|
65 |
socket = new QSslSocket; |
|
66 |
else |
|
67 |
socket = new QTcpSocket; |
|
68 |
#else |
|
69 |
socket = new QTcpSocket; |
|
70 |
#endif |
|
71 |
||
72 |
// limit the socket read buffer size. we will read everything into |
|
73 |
// the QHttpNetworkReply anyway, so let's grow only that and not |
|
74 |
// here and there. |
|
75 |
socket->setReadBufferSize(64*1024); |
|
76 |
||
77 |
QObject::connect(socket, SIGNAL(bytesWritten(qint64)), |
|
78 |
this, SLOT(_q_bytesWritten(qint64)), |
|
79 |
Qt::DirectConnection); |
|
80 |
QObject::connect(socket, SIGNAL(connected()), |
|
81 |
this, SLOT(_q_connected()), |
|
82 |
Qt::DirectConnection); |
|
83 |
QObject::connect(socket, SIGNAL(readyRead()), |
|
84 |
this, SLOT(_q_readyRead()), |
|
85 |
Qt::DirectConnection); |
|
86 |
QObject::connect(socket, SIGNAL(disconnected()), |
|
87 |
this, SLOT(_q_disconnected()), |
|
88 |
Qt::DirectConnection); |
|
89 |
QObject::connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), |
|
90 |
this, SLOT(_q_error(QAbstractSocket::SocketError)), |
|
91 |
Qt::DirectConnection); |
|
92 |
#ifndef QT_NO_NETWORKPROXY |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
93 |
QObject::connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
94 |
this, SLOT(_q_proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
0 | 95 |
Qt::DirectConnection); |
96 |
#endif |
|
97 |
||
98 |
#ifndef QT_NO_OPENSSL |
|
99 |
QSslSocket *sslSocket = qobject_cast<QSslSocket*>(socket); |
|
100 |
if (sslSocket) { |
|
101 |
// won't be a sslSocket if encrypt is false |
|
102 |
QObject::connect(sslSocket, SIGNAL(encrypted()), |
|
103 |
this, SLOT(_q_encrypted()), |
|
104 |
Qt::DirectConnection); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
105 |
QObject::connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
106 |
this, SLOT(_q_sslErrors(QList<QSslError>)), |
0 | 107 |
Qt::DirectConnection); |
108 |
QObject::connect(sslSocket, SIGNAL(encryptedBytesWritten(qint64)), |
|
109 |
this, SLOT(_q_encryptedBytesWritten(qint64)), |
|
110 |
Qt::DirectConnection); |
|
111 |
} |
|
112 |
#endif |
|
113 |
} |
|
114 |
||
115 |
||
116 |
void QHttpNetworkConnectionChannel::close() |
|
117 |
{ |
|
118 |
socket->blockSignals(true); |
|
119 |
socket->close(); |
|
120 |
socket->blockSignals(false); |
|
121 |
state = QHttpNetworkConnectionChannel::IdleState; |
|
122 |
} |
|
123 |
||
124 |
||
125 |
bool QHttpNetworkConnectionChannel::sendRequest() |
|
126 |
{ |
|
127 |
switch (state) { |
|
128 |
case QHttpNetworkConnectionChannel::IdleState: { // write the header |
|
129 |
if (!ensureConnection()) { |
|
130 |
// wait for the connection (and encryption) to be done |
|
131 |
// sendRequest will be called again from either |
|
132 |
// _q_connected or _q_encrypted |
|
133 |
return false; |
|
134 |
} |
|
135 |
written = 0; // excluding the header |
|
136 |
bytesTotal = 0; |
|
137 |
if (reply) { |
|
138 |
reply->d_func()->clear(); |
|
139 |
reply->d_func()->connection = connection; |
|
140 |
reply->d_func()->autoDecompress = request.d->autoDecompress; |
|
141 |
reply->d_func()->pipeliningUsed = false; |
|
142 |
} |
|
143 |
state = QHttpNetworkConnectionChannel::WritingState; |
|
144 |
pendingEncrypt = false; |
|
145 |
// if the url contains authentication parameters, use the new ones |
|
146 |
// both channels will use the new authentication parameters |
|
147 |
if (!request.url().userInfo().isEmpty()) { |
|
148 |
QUrl url = request.url(); |
|
149 |
QAuthenticator &auth = authenticator; |
|
150 |
if (url.userName() != auth.user() |
|
151 |
|| (!url.password().isEmpty() && url.password() != auth.password())) { |
|
152 |
auth.setUser(url.userName()); |
|
153 |
auth.setPassword(url.password()); |
|
154 |
connection->d_func()->copyCredentials(connection->d_func()->indexOf(socket), &auth, false); |
|
155 |
} |
|
156 |
// clear the userinfo, since we use the same request for resending |
|
157 |
// userinfo in url can conflict with the one in the authenticator |
|
158 |
url.setUserInfo(QString()); |
|
159 |
request.setUrl(url); |
|
160 |
} |
|
161 |
connection->d_func()->createAuthorization(socket, request); |
|
162 |
#ifndef QT_NO_NETWORKPROXY |
|
163 |
QByteArray header = QHttpNetworkRequestPrivate::header(request, |
|
164 |
(connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy)); |
|
165 |
#else |
|
166 |
QByteArray header = QHttpNetworkRequestPrivate::header(request, false); |
|
167 |
#endif |
|
168 |
socket->write(header); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
169 |
// flushing is dangerous (QSslSocket calls transmit which might read or error) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
170 |
// socket->flush(); |
0 | 171 |
QNonContiguousByteDevice* uploadByteDevice = request.uploadByteDevice(); |
172 |
if (uploadByteDevice) { |
|
173 |
// connect the signals so this function gets called again |
|
174 |
QObject::connect(uploadByteDevice, SIGNAL(readyRead()),this, SLOT(_q_uploadDataReadyRead())); |
|
175 |
||
176 |
bytesTotal = request.contentLength(); |
|
177 |
} else { |
|
178 |
state = QHttpNetworkConnectionChannel::WaitingState; |
|
179 |
sendRequest(); |
|
180 |
break; |
|
181 |
} |
|
182 |
// write the initial chunk together with the headers |
|
183 |
// fall through |
|
184 |
} |
|
185 |
case QHttpNetworkConnectionChannel::WritingState: |
|
186 |
{ |
|
187 |
// write the data |
|
188 |
QNonContiguousByteDevice* uploadByteDevice = request.uploadByteDevice(); |
|
189 |
if (!uploadByteDevice || bytesTotal == written) { |
|
190 |
if (uploadByteDevice) |
|
191 |
emit reply->dataSendProgress(written, bytesTotal); |
|
192 |
state = QHttpNetworkConnectionChannel::WaitingState; // now wait for response |
|
193 |
sendRequest(); |
|
194 |
break; |
|
195 |
} |
|
196 |
||
197 |
// only feed the QTcpSocket buffer when there is less than 32 kB in it |
|
198 |
const qint64 socketBufferFill = 32*1024; |
|
199 |
const qint64 socketWriteMaxSize = 16*1024; |
|
200 |
||
201 |
||
202 |
#ifndef QT_NO_OPENSSL |
|
203 |
QSslSocket *sslSocket = qobject_cast<QSslSocket*>(socket); |
|
204 |
// if it is really an ssl socket, check more than just bytesToWrite() |
|
205 |
while ((socket->bytesToWrite() + (sslSocket ? sslSocket->encryptedBytesToWrite() : 0)) |
|
206 |
<= socketBufferFill && bytesTotal != written) |
|
207 |
#else |
|
208 |
while (socket->bytesToWrite() <= socketBufferFill |
|
209 |
&& bytesTotal != written) |
|
210 |
#endif |
|
211 |
{ |
|
212 |
// get pointer to upload data |
|
213 |
qint64 currentReadSize; |
|
214 |
qint64 desiredReadSize = qMin(socketWriteMaxSize, bytesTotal - written); |
|
215 |
const char *readPointer = uploadByteDevice->readPointer(desiredReadSize, currentReadSize); |
|
216 |
||
217 |
if (currentReadSize == -1) { |
|
218 |
// premature eof happened |
|
219 |
connection->d_func()->emitReplyError(socket, reply, QNetworkReply::UnknownNetworkError); |
|
220 |
return false; |
|
221 |
break; |
|
222 |
} else if (readPointer == 0 || currentReadSize == 0) { |
|
223 |
// nothing to read currently, break the loop |
|
224 |
break; |
|
225 |
} else { |
|
226 |
qint64 currentWriteSize = socket->write(readPointer, currentReadSize); |
|
227 |
if (currentWriteSize == -1 || currentWriteSize != currentReadSize) { |
|
228 |
// socket broke down |
|
229 |
connection->d_func()->emitReplyError(socket, reply, QNetworkReply::UnknownNetworkError); |
|
230 |
return false; |
|
231 |
} else { |
|
232 |
written += currentWriteSize; |
|
233 |
uploadByteDevice->advanceReadPointer(currentWriteSize); |
|
234 |
||
235 |
emit reply->dataSendProgress(written, bytesTotal); |
|
236 |
||
237 |
if (written == bytesTotal) { |
|
238 |
// make sure this function is called once again |
|
239 |
state = QHttpNetworkConnectionChannel::WaitingState; |
|
240 |
sendRequest(); |
|
241 |
break; |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
break; |
|
247 |
} |
|
248 |
||
249 |
case QHttpNetworkConnectionChannel::WaitingState: |
|
250 |
{ |
|
251 |
QNonContiguousByteDevice* uploadByteDevice = request.uploadByteDevice(); |
|
252 |
if (uploadByteDevice) { |
|
253 |
QObject::disconnect(uploadByteDevice, SIGNAL(readyRead()), this, SLOT(_q_uploadDataReadyRead())); |
|
254 |
} |
|
255 |
||
256 |
// HTTP pipelining |
|
257 |
connection->d_func()->fillPipeline(socket); |
|
258 |
socket->flush(); |
|
259 |
||
260 |
// ensure we try to receive a reply in all cases, even if _q_readyRead_ hat not been called |
|
261 |
// this is needed if the sends an reply before we have finished sending the request. In that |
|
262 |
// case receiveReply had been called before but ignored the server reply |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
263 |
QMetaObject::invokeMethod(this, "_q_receiveReply", Qt::QueuedConnection); |
0 | 264 |
break; |
265 |
} |
|
266 |
case QHttpNetworkConnectionChannel::ReadingState: |
|
267 |
case QHttpNetworkConnectionChannel::Wait4AuthState: |
|
268 |
// ignore _q_bytesWritten in these states |
|
269 |
// fall through |
|
270 |
default: |
|
271 |
break; |
|
272 |
} |
|
273 |
return true; |
|
274 |
} |
|
275 |
||
276 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
277 |
void QHttpNetworkConnectionChannel::_q_receiveReply() |
0 | 278 |
{ |
279 |
Q_ASSERT(socket); |
|
280 |
||
281 |
qint64 bytes = 0; |
|
282 |
QAbstractSocket::SocketState socketState = socket->state(); |
|
283 |
||
284 |
// connection might be closed to signal the end of data |
|
285 |
if (socketState == QAbstractSocket::UnconnectedState) { |
|
286 |
if (!socket->bytesAvailable()) { |
|
287 |
if (reply && reply->d_func()->state == QHttpNetworkReplyPrivate::ReadingDataState) { |
|
288 |
reply->d_func()->state = QHttpNetworkReplyPrivate::AllDoneState; |
|
289 |
this->state = QHttpNetworkConnectionChannel::IdleState; |
|
290 |
allDone(); |
|
291 |
} else { |
|
292 |
// try to reconnect/resend before sending an error. |
|
293 |
if (reconnectAttempts-- > 0) { |
|
294 |
closeAndResendCurrentRequest(); |
|
295 |
} else if (reply) { |
|
296 |
reply->d_func()->errorString = connection->d_func()->errorDetail(QNetworkReply::RemoteHostClosedError, socket); |
|
297 |
emit reply->finishedWithError(QNetworkReply::RemoteHostClosedError, reply->d_func()->errorString); |
|
298 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
299 |
} |
|
300 |
} |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
// read loop for the response |
|
305 |
while (socket->bytesAvailable()) { |
|
306 |
QHttpNetworkReplyPrivate::ReplyState state = reply ? reply->d_func()->state : QHttpNetworkReplyPrivate::AllDoneState; |
|
307 |
switch (state) { |
|
308 |
case QHttpNetworkReplyPrivate::NothingDoneState: |
|
309 |
case QHttpNetworkReplyPrivate::ReadingStatusState: { |
|
310 |
eatWhitespace(); |
|
311 |
qint64 statusBytes = reply->d_func()->readStatus(socket); |
|
312 |
if (statusBytes == -1 && reconnectAttempts <= 0) { |
|
313 |
// too many errors reading/receiving/parsing the status, close the socket and emit error |
|
314 |
close(); |
|
315 |
reply->d_func()->errorString = connection->d_func()->errorDetail(QNetworkReply::ProtocolFailure, socket); |
|
316 |
emit reply->finishedWithError(QNetworkReply::ProtocolFailure, reply->d_func()->errorString); |
|
317 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
318 |
break; |
|
319 |
} else if (statusBytes == -1) { |
|
320 |
reconnectAttempts--; |
|
321 |
reply->d_func()->clear(); |
|
322 |
closeAndResendCurrentRequest(); |
|
323 |
break; |
|
324 |
} |
|
325 |
bytes += statusBytes; |
|
326 |
lastStatus = reply->d_func()->statusCode; |
|
327 |
break; |
|
328 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
329 |
case QHttpNetworkReplyPrivate::ReadingHeaderState: { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
330 |
QHttpNetworkReplyPrivate *replyPrivate = reply->d_func(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
331 |
bytes += replyPrivate->readHeader(socket); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
332 |
if (replyPrivate->state == QHttpNetworkReplyPrivate::ReadingDataState) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
333 |
if (replyPrivate->isGzipped() && replyPrivate->autoDecompress) { |
0 | 334 |
// remove the Content-Length from header |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
335 |
replyPrivate->removeAutoDecompressHeader(); |
0 | 336 |
} else { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
337 |
replyPrivate->autoDecompress = false; |
0 | 338 |
} |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
339 |
if (replyPrivate->statusCode == 100) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
340 |
replyPrivate->state = QHttpNetworkReplyPrivate::ReadingStatusState; |
0 | 341 |
break; // ignore |
342 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
343 |
if (replyPrivate->shouldEmitSignals()) |
0 | 344 |
emit reply->headerChanged(); |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
345 |
if (!replyPrivate->expectContent()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
346 |
replyPrivate->state = QHttpNetworkReplyPrivate::AllDoneState; |
0 | 347 |
this->state = QHttpNetworkConnectionChannel::IdleState; |
348 |
allDone(); |
|
349 |
return; |
|
350 |
} |
|
351 |
} |
|
352 |
break; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
353 |
} |
0 | 354 |
case QHttpNetworkReplyPrivate::ReadingDataState: { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
355 |
QHttpNetworkReplyPrivate *replyPrivate = reply->d_func(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
356 |
if (replyPrivate->downstreamLimited && !replyPrivate->responseData.isEmpty() && replyPrivate->shouldEmitSignals()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
357 |
// We already have some HTTP body data. We don't read more from the socket until |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
358 |
// this is fetched by QHttpNetworkAccessHttpBackend. If we would read more, |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
359 |
// we could not limit our read buffer usage. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
360 |
// We only do this when shouldEmitSignals==true because our HTTP parsing |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
361 |
// always needs to parse the 401/407 replies. Therefore they don't really obey |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
362 |
// to the read buffer maximum size, but we don't care since they should be small. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
363 |
return; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
364 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
365 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
366 |
if (!replyPrivate->isChunked() && !replyPrivate->autoDecompress |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
367 |
&& replyPrivate->bodyLength > 0) { |
0 | 368 |
// bulk files like images should fulfill these properties and |
369 |
// we can therefore save on memory copying |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
370 |
bytes = replyPrivate->readBodyFast(socket, &replyPrivate->responseData); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
371 |
replyPrivate->totalProgress += bytes; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
372 |
if (replyPrivate->shouldEmitSignals()) { |
0 | 373 |
QPointer<QHttpNetworkReply> replyPointer = reply; |
374 |
emit reply->readyRead(); |
|
375 |
// make sure that the reply is valid |
|
376 |
if (replyPointer.isNull()) |
|
377 |
return; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
378 |
emit reply->dataReadProgress(replyPrivate->totalProgress, replyPrivate->bodyLength); |
0 | 379 |
// make sure that the reply is valid |
380 |
if (replyPointer.isNull()) |
|
381 |
return; |
|
382 |
} |
|
383 |
} |
|
384 |
else |
|
385 |
{ |
|
386 |
// use the traditional slower reading (for compressed encoding, chunked encoding, |
|
387 |
// no content-length etc) |
|
388 |
QByteDataBuffer byteDatas; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
389 |
bytes = replyPrivate->readBody(socket, &byteDatas); |
0 | 390 |
if (bytes) { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
391 |
if (replyPrivate->autoDecompress) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
392 |
replyPrivate->appendCompressedReplyData(byteDatas); |
0 | 393 |
else |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
394 |
replyPrivate->appendUncompressedReplyData(byteDatas); |
0 | 395 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
396 |
if (!replyPrivate->autoDecompress) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
397 |
replyPrivate->totalProgress += bytes; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
398 |
if (replyPrivate->shouldEmitSignals()) { |
0 | 399 |
QPointer<QHttpNetworkReply> replyPointer = reply; |
400 |
// important: At the point of this readyRead(), the byteDatas list must be empty, |
|
401 |
// else implicit sharing will trigger memcpy when the user is reading data! |
|
402 |
emit reply->readyRead(); |
|
403 |
// make sure that the reply is valid |
|
404 |
if (replyPointer.isNull()) |
|
405 |
return; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
406 |
emit reply->dataReadProgress(replyPrivate->totalProgress, replyPrivate->bodyLength); |
0 | 407 |
// make sure that the reply is valid |
408 |
if (replyPointer.isNull()) |
|
409 |
return; |
|
410 |
} |
|
411 |
} |
|
412 |
#ifndef QT_NO_COMPRESS |
|
413 |
else if (!expand(false)) { // expand a chunk if possible |
|
414 |
return; // ### expand failed |
|
415 |
} |
|
416 |
#endif |
|
417 |
} |
|
418 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
419 |
if (replyPrivate->state == QHttpNetworkReplyPrivate::ReadingDataState) |
0 | 420 |
break; |
421 |
// everything done, fall through |
|
422 |
} |
|
423 |
case QHttpNetworkReplyPrivate::AllDoneState: |
|
424 |
this->state = QHttpNetworkConnectionChannel::IdleState; |
|
425 |
allDone(); |
|
426 |
break; |
|
427 |
default: |
|
428 |
break; |
|
429 |
} |
|
430 |
} |
|
431 |
} |
|
432 |
||
433 |
bool QHttpNetworkConnectionChannel::ensureConnection() |
|
434 |
{ |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
435 |
QAbstractSocket::SocketState socketState = socket->state(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
436 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
437 |
// resend this request after we receive the disconnected signal |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
438 |
if (socketState == QAbstractSocket::ClosingState) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
439 |
resendCurrent = true; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
440 |
return false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
441 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
442 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
443 |
// already trying to connect? |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
444 |
if (socketState == QAbstractSocket::HostLookupState || |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
445 |
socketState == QAbstractSocket::ConnectingState) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
446 |
return false; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
447 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
448 |
|
0 | 449 |
// make sure that this socket is in a connected state, if not initiate |
450 |
// connection to the host. |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
451 |
if (socketState != QAbstractSocket::ConnectedState) { |
0 | 452 |
// connect to the host if not already connected. |
453 |
state = QHttpNetworkConnectionChannel::ConnectingState; |
|
454 |
pendingEncrypt = connection->d_func()->encrypt; |
|
455 |
||
456 |
// reset state |
|
457 |
pipeliningSupported = PipeliningSupportUnknown; |
|
458 |
||
459 |
// This workaround is needed since we use QAuthenticator for NTLM authentication. The "phase == Done" |
|
460 |
// is the usual criteria for emitting authentication signals. The "phase" is set to "Done" when the |
|
461 |
// last header for Authorization is generated by the QAuthenticator. Basic & Digest logic does not |
|
462 |
// check the "phase" for generating the Authorization header. NTLM authentication is a two stage |
|
463 |
// process & needs the "phase". To make sure the QAuthenticator uses the current username/password |
|
464 |
// the phase is reset to Start. |
|
465 |
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(authenticator); |
|
466 |
if (priv && priv->phase == QAuthenticatorPrivate::Done) |
|
467 |
priv->phase = QAuthenticatorPrivate::Start; |
|
468 |
priv = QAuthenticatorPrivate::getPrivate(proxyAuthenticator); |
|
469 |
if (priv && priv->phase == QAuthenticatorPrivate::Done) |
|
470 |
priv->phase = QAuthenticatorPrivate::Start; |
|
471 |
||
472 |
QString connectHost = connection->d_func()->hostName; |
|
473 |
qint16 connectPort = connection->d_func()->port; |
|
474 |
||
475 |
#ifndef QT_NO_NETWORKPROXY |
|
476 |
// HTTPS always use transparent proxy. |
|
477 |
if (connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy && !connection->d_func()->encrypt) { |
|
478 |
connectHost = connection->d_func()->networkProxy.hostName(); |
|
479 |
connectPort = connection->d_func()->networkProxy.port(); |
|
480 |
} |
|
481 |
#endif |
|
482 |
if (connection->d_func()->encrypt) { |
|
483 |
#ifndef QT_NO_OPENSSL |
|
484 |
QSslSocket *sslSocket = qobject_cast<QSslSocket*>(socket); |
|
485 |
sslSocket->connectToHostEncrypted(connectHost, connectPort); |
|
486 |
if (ignoreAllSslErrors) |
|
487 |
sslSocket->ignoreSslErrors(); |
|
488 |
sslSocket->ignoreSslErrors(ignoreSslErrorsList); |
|
489 |
#else |
|
490 |
connection->d_func()->emitReplyError(socket, reply, QNetworkReply::ProtocolUnknownError); |
|
491 |
#endif |
|
492 |
} else { |
|
493 |
socket->connectToHost(connectHost, connectPort); |
|
494 |
} |
|
495 |
return false; |
|
496 |
} |
|
497 |
return true; |
|
498 |
} |
|
499 |
||
500 |
||
501 |
#ifndef QT_NO_COMPRESS |
|
502 |
bool QHttpNetworkConnectionChannel::expand(bool dataComplete) |
|
503 |
{ |
|
504 |
Q_ASSERT(socket); |
|
505 |
Q_ASSERT(reply); |
|
506 |
||
507 |
qint64 total = reply->d_func()->compressedData.size(); |
|
508 |
if (total >= CHUNK || dataComplete) { |
|
509 |
// uncompress the data |
|
510 |
QByteArray content, inflated; |
|
511 |
content = reply->d_func()->compressedData; |
|
512 |
reply->d_func()->compressedData.clear(); |
|
513 |
||
514 |
int ret = Z_OK; |
|
515 |
if (content.size()) |
|
516 |
ret = reply->d_func()->gunzipBodyPartially(content, inflated); |
|
517 |
int retCheck = (dataComplete) ? Z_STREAM_END : Z_OK; |
|
518 |
if (ret >= retCheck) { |
|
519 |
if (inflated.size()) { |
|
520 |
reply->d_func()->totalProgress += inflated.size(); |
|
521 |
reply->d_func()->appendUncompressedReplyData(inflated); |
|
522 |
if (reply->d_func()->shouldEmitSignals()) { |
|
523 |
QPointer<QHttpNetworkReply> replyPointer = reply; |
|
524 |
// important: At the point of this readyRead(), inflated must be cleared, |
|
525 |
// else implicit sharing will trigger memcpy when the user is reading data! |
|
526 |
emit reply->readyRead(); |
|
527 |
// make sure that the reply is valid |
|
528 |
if (replyPointer.isNull()) |
|
529 |
return true; |
|
530 |
emit reply->dataReadProgress(reply->d_func()->totalProgress, 0); |
|
531 |
// make sure that the reply is valid |
|
532 |
if (replyPointer.isNull()) |
|
533 |
return true; |
|
534 |
||
535 |
} |
|
536 |
} |
|
537 |
} else { |
|
538 |
connection->d_func()->emitReplyError(socket, reply, QNetworkReply::ProtocolFailure); |
|
539 |
return false; |
|
540 |
} |
|
541 |
} |
|
542 |
return true; |
|
543 |
} |
|
544 |
#endif |
|
545 |
||
546 |
||
547 |
void QHttpNetworkConnectionChannel::allDone() |
|
548 |
{ |
|
549 |
#ifndef QT_NO_COMPRESS |
|
550 |
// expand the whole data. |
|
551 |
if (reply->d_func()->expectContent() && reply->d_func()->autoDecompress && !reply->d_func()->streamEnd) |
|
552 |
expand(true); // ### if expand returns false, its an error |
|
553 |
#endif |
|
554 |
// while handling 401 & 407, we might reset the status code, so save this. |
|
555 |
bool emitFinished = reply->d_func()->shouldEmitSignals(); |
|
556 |
handleStatus(); |
|
557 |
// ### at this point there should be no more data on the socket |
|
558 |
// close if server requested |
|
559 |
if (reply->d_func()->isConnectionCloseEnabled()) |
|
560 |
close(); |
|
561 |
// queue the finished signal, this is required since we might send new requests from |
|
562 |
// slot connected to it. The socket will not fire readyRead signal, if we are already |
|
563 |
// in the slot connected to readyRead |
|
564 |
if (emitFinished) |
|
565 |
QMetaObject::invokeMethod(reply, "finished", Qt::QueuedConnection); |
|
566 |
// reset the reconnection attempts after we receive a complete reply. |
|
567 |
// in case of failures, each channel will attempt two reconnects before emitting error. |
|
568 |
reconnectAttempts = 2; |
|
569 |
||
570 |
detectPipeliningSupport(); |
|
571 |
||
572 |
// move next from pipeline to current request |
|
573 |
if (!alreadyPipelinedRequests.isEmpty()) { |
|
574 |
if (resendCurrent || reply->d_func()->isConnectionCloseEnabled() || socket->state() != QAbstractSocket::ConnectedState) { |
|
575 |
// move the pipelined ones back to the main queue |
|
576 |
requeueCurrentlyPipelinedRequests(); |
|
577 |
close(); |
|
578 |
} else { |
|
579 |
// there were requests pipelined in and we can continue |
|
580 |
HttpMessagePair messagePair = alreadyPipelinedRequests.takeFirst(); |
|
581 |
||
582 |
request = messagePair.first; |
|
583 |
reply = messagePair.second; |
|
584 |
state = QHttpNetworkConnectionChannel::ReadingState; |
|
585 |
resendCurrent = false; |
|
586 |
||
587 |
written = 0; // message body, excluding the header, irrelevant here |
|
588 |
bytesTotal = 0; // message body total, excluding the header, irrelevant here |
|
589 |
||
590 |
// pipeline even more |
|
591 |
connection->d_func()->fillPipeline(socket); |
|
592 |
||
593 |
// continue reading |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
594 |
_q_receiveReply(); |
0 | 595 |
} |
596 |
} else if (alreadyPipelinedRequests.isEmpty() && socket->bytesAvailable() > 0) { |
|
597 |
eatWhitespace(); |
|
598 |
// this is weird. we had nothing pipelined but still bytes available. better close it. |
|
599 |
if (socket->bytesAvailable() > 0) |
|
600 |
close(); |
|
601 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
602 |
} else if (alreadyPipelinedRequests.isEmpty()) { |
|
603 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
void QHttpNetworkConnectionChannel::detectPipeliningSupport() |
|
608 |
{ |
|
609 |
// detect HTTP Pipelining support |
|
610 |
QByteArray serverHeaderField = reply->headerField("Server"); |
|
611 |
if ( |
|
612 |
// check for broken servers in server reply header |
|
613 |
// this is adapted from http://mxr.mozilla.org/firefox/ident?i=SupportsPipelining |
|
614 |
(!serverHeaderField.contains("Microsoft-IIS/4.")) |
|
615 |
&& (!serverHeaderField.contains("Microsoft-IIS/5.")) |
|
616 |
&& (!serverHeaderField.contains("Netscape-Enterprise/3.")) |
|
617 |
// check for HTTP/1.1 |
|
618 |
&& (reply->d_func()->majorVersion == 1 && reply->d_func()->minorVersion == 1) |
|
619 |
// check for not having connection close |
|
620 |
&& (!reply->d_func()->isConnectionCloseEnabled()) |
|
621 |
// check if it is still connected |
|
622 |
&& (socket->state() == QAbstractSocket::ConnectedState) |
|
623 |
) { |
|
624 |
pipeliningSupported = QHttpNetworkConnectionChannel::PipeliningProbablySupported; |
|
625 |
} else { |
|
626 |
pipeliningSupported = QHttpNetworkConnectionChannel::PipeliningSupportUnknown; |
|
627 |
} |
|
628 |
} |
|
629 |
||
630 |
// called when the connection broke and we need to queue some pipelined requests again |
|
631 |
void QHttpNetworkConnectionChannel::requeueCurrentlyPipelinedRequests() |
|
632 |
{ |
|
633 |
for (int i = 0; i < alreadyPipelinedRequests.length(); i++) |
|
634 |
connection->d_func()->requeueRequest(alreadyPipelinedRequests.at(i)); |
|
635 |
alreadyPipelinedRequests.clear(); |
|
636 |
||
637 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
638 |
} |
|
639 |
||
640 |
void QHttpNetworkConnectionChannel::eatWhitespace() |
|
641 |
{ |
|
642 |
char c; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
643 |
do { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
644 |
qint64 ret = socket->peek(&c, 1); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
645 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
646 |
// nothing read, fine. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
647 |
if (ret == 0) |
0 | 648 |
return; |
649 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
650 |
// EOF from socket? |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
651 |
if (ret == -1) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
652 |
return; // FIXME, we need to stop processing. however the next stuff done will also do that. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
653 |
|
0 | 654 |
// read all whitespace and line endings |
655 |
if (c == 11 || c == '\n' || c == '\r' || c == ' ' || c == 31) { |
|
656 |
socket->read(&c, 1); |
|
657 |
continue; |
|
658 |
} else { |
|
659 |
break; |
|
660 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
661 |
} while(true); |
0 | 662 |
} |
663 |
||
664 |
void QHttpNetworkConnectionChannel::handleStatus() |
|
665 |
{ |
|
666 |
Q_ASSERT(socket); |
|
667 |
Q_ASSERT(reply); |
|
668 |
||
669 |
int statusCode = reply->statusCode(); |
|
670 |
bool resend = false; |
|
671 |
||
672 |
switch (statusCode) { |
|
673 |
case 401: // auth required |
|
674 |
case 407: // proxy auth required |
|
675 |
if (connection->d_func()->handleAuthenticateChallenge(socket, reply, (statusCode == 407), resend)) { |
|
676 |
if (resend) { |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
677 |
if (!resetUploadData()) |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
678 |
break; |
0 | 679 |
|
680 |
reply->d_func()->eraseData(); |
|
681 |
||
682 |
if (alreadyPipelinedRequests.isEmpty()) { |
|
683 |
// this does a re-send without closing the connection |
|
684 |
resendCurrent = true; |
|
685 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
686 |
} else { |
|
687 |
// we had requests pipelined.. better close the connection in closeAndResendCurrentRequest |
|
688 |
closeAndResendCurrentRequest(); |
|
689 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
690 |
} |
|
691 |
} |
|
692 |
} else { |
|
693 |
emit reply->headerChanged(); |
|
694 |
emit reply->readyRead(); |
|
695 |
QNetworkReply::NetworkError errorCode = (statusCode == 407) |
|
696 |
? QNetworkReply::ProxyAuthenticationRequiredError |
|
697 |
: QNetworkReply::AuthenticationRequiredError; |
|
698 |
reply->d_func()->errorString = connection->d_func()->errorDetail(errorCode, socket); |
|
699 |
emit connection->error(errorCode, reply->d_func()->errorString); |
|
700 |
emit reply->finished(); |
|
701 |
} |
|
702 |
break; |
|
703 |
default: |
|
704 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
705 |
} |
|
706 |
} |
|
707 |
||
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
708 |
bool QHttpNetworkConnectionChannel::resetUploadData() |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
709 |
{ |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
710 |
QNonContiguousByteDevice* uploadByteDevice = request.uploadByteDevice(); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
711 |
if (!uploadByteDevice) |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
712 |
return true; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
713 |
|
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
714 |
if (uploadByteDevice->reset()) { |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
715 |
written = 0; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
716 |
return true; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
717 |
} else { |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
718 |
connection->d_func()->emitReplyError(socket, reply, QNetworkReply::ContentReSendError); |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
719 |
return false; |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
720 |
} |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
721 |
} |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
722 |
|
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
723 |
|
0 | 724 |
void QHttpNetworkConnectionChannel::pipelineInto(HttpMessagePair &pair) |
725 |
{ |
|
726 |
// this is only called for simple GET |
|
727 |
||
728 |
QHttpNetworkRequest &request = pair.first; |
|
729 |
QHttpNetworkReply *reply = pair.second; |
|
730 |
if (reply) { |
|
731 |
reply->d_func()->clear(); |
|
732 |
reply->d_func()->connection = connection; |
|
733 |
reply->d_func()->autoDecompress = request.d->autoDecompress; |
|
734 |
reply->d_func()->pipeliningUsed = true; |
|
735 |
} |
|
736 |
||
737 |
#ifndef QT_NO_NETWORKPROXY |
|
738 |
QByteArray header = QHttpNetworkRequestPrivate::header(request, |
|
739 |
(connection->d_func()->networkProxy.type() != QNetworkProxy::NoProxy)); |
|
740 |
#else |
|
741 |
QByteArray header = QHttpNetworkRequestPrivate::header(request, false); |
|
742 |
#endif |
|
743 |
socket->write(header); |
|
744 |
||
745 |
alreadyPipelinedRequests.append(pair); |
|
746 |
} |
|
747 |
||
748 |
void QHttpNetworkConnectionChannel::closeAndResendCurrentRequest() |
|
749 |
{ |
|
750 |
requeueCurrentlyPipelinedRequests(); |
|
751 |
close(); |
|
752 |
resendCurrent = true; |
|
753 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
754 |
} |
|
755 |
||
756 |
bool QHttpNetworkConnectionChannel::isSocketBusy() const |
|
757 |
{ |
|
758 |
return (state & QHttpNetworkConnectionChannel::BusyState); |
|
759 |
} |
|
760 |
||
761 |
bool QHttpNetworkConnectionChannel::isSocketWriting() const |
|
762 |
{ |
|
763 |
return (state & QHttpNetworkConnectionChannel::WritingState); |
|
764 |
} |
|
765 |
||
766 |
bool QHttpNetworkConnectionChannel::isSocketWaiting() const |
|
767 |
{ |
|
768 |
return (state & QHttpNetworkConnectionChannel::WaitingState); |
|
769 |
} |
|
770 |
||
771 |
bool QHttpNetworkConnectionChannel::isSocketReading() const |
|
772 |
{ |
|
773 |
return (state & QHttpNetworkConnectionChannel::ReadingState); |
|
774 |
} |
|
775 |
||
776 |
//private slots |
|
777 |
void QHttpNetworkConnectionChannel::_q_readyRead() |
|
778 |
{ |
|
779 |
if (isSocketWaiting() || isSocketReading()) { |
|
780 |
state = QHttpNetworkConnectionChannel::ReadingState; |
|
781 |
if (reply) |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
782 |
_q_receiveReply(); |
0 | 783 |
} |
784 |
} |
|
785 |
||
786 |
void QHttpNetworkConnectionChannel::_q_bytesWritten(qint64 bytes) |
|
787 |
{ |
|
788 |
Q_UNUSED(bytes); |
|
789 |
// bytes have been written to the socket. write even more of them :) |
|
790 |
if (isSocketWriting()) |
|
791 |
sendRequest(); |
|
792 |
// otherwise we do nothing |
|
793 |
} |
|
794 |
||
795 |
void QHttpNetworkConnectionChannel::_q_disconnected() |
|
796 |
{ |
|
797 |
// read the available data before closing |
|
798 |
if (isSocketWaiting() || isSocketReading()) { |
|
799 |
state = QHttpNetworkConnectionChannel::ReadingState; |
|
800 |
if (reply) |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
801 |
_q_receiveReply(); |
0 | 802 |
} else if (state == QHttpNetworkConnectionChannel::IdleState && resendCurrent) { |
803 |
// re-sending request because the socket was in ClosingState |
|
804 |
QMetaObject::invokeMethod(connection, "_q_startNextRequest", Qt::QueuedConnection); |
|
805 |
} |
|
806 |
state = QHttpNetworkConnectionChannel::IdleState; |
|
807 |
||
808 |
requeueCurrentlyPipelinedRequests(); |
|
809 |
close(); |
|
810 |
} |
|
811 |
||
812 |
||
813 |
void QHttpNetworkConnectionChannel::_q_connected() |
|
814 |
{ |
|
815 |
// improve performance since we get the request sent by the kernel ASAP |
|
816 |
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
817 |
// not sure yet if it helps, but it makes sense |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
818 |
socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1); |
0 | 819 |
|
820 |
pipeliningSupported = QHttpNetworkConnectionChannel::PipeliningSupportUnknown; |
|
821 |
||
822 |
// ### FIXME: if the server closes the connection unexpectedly, we shouldn't send the same broken request again! |
|
823 |
//channels[i].reconnectAttempts = 2; |
|
824 |
if (!pendingEncrypt) { |
|
825 |
state = QHttpNetworkConnectionChannel::IdleState; |
|
826 |
if (reply) |
|
827 |
sendRequest(); |
|
828 |
else |
|
829 |
close(); |
|
830 |
} |
|
831 |
} |
|
832 |
||
833 |
||
834 |
void QHttpNetworkConnectionChannel::_q_error(QAbstractSocket::SocketError socketError) |
|
835 |
{ |
|
836 |
if (!socket) |
|
837 |
return; |
|
838 |
bool send2Reply = false; |
|
839 |
QNetworkReply::NetworkError errorCode = QNetworkReply::UnknownNetworkError; |
|
840 |
||
841 |
switch (socketError) { |
|
842 |
case QAbstractSocket::HostNotFoundError: |
|
843 |
errorCode = QNetworkReply::HostNotFoundError; |
|
844 |
break; |
|
845 |
case QAbstractSocket::ConnectionRefusedError: |
|
846 |
errorCode = QNetworkReply::ConnectionRefusedError; |
|
847 |
break; |
|
848 |
case QAbstractSocket::RemoteHostClosedError: |
|
849 |
// try to reconnect/resend before sending an error. |
|
850 |
// while "Reading" the _q_disconnected() will handle this. |
|
851 |
if (state != QHttpNetworkConnectionChannel::IdleState && state != QHttpNetworkConnectionChannel::ReadingState) { |
|
852 |
if (reconnectAttempts-- > 0) { |
|
853 |
closeAndResendCurrentRequest(); |
|
854 |
return; |
|
855 |
} else { |
|
856 |
send2Reply = true; |
|
857 |
errorCode = QNetworkReply::RemoteHostClosedError; |
|
858 |
} |
|
859 |
} else { |
|
860 |
return; |
|
861 |
} |
|
862 |
break; |
|
863 |
case QAbstractSocket::SocketTimeoutError: |
|
864 |
// try to reconnect/resend before sending an error. |
|
865 |
if (state == QHttpNetworkConnectionChannel::WritingState && (reconnectAttempts-- > 0)) { |
|
866 |
closeAndResendCurrentRequest(); |
|
867 |
return; |
|
868 |
} |
|
869 |
send2Reply = true; |
|
870 |
errorCode = QNetworkReply::TimeoutError; |
|
871 |
break; |
|
872 |
case QAbstractSocket::ProxyAuthenticationRequiredError: |
|
873 |
errorCode = QNetworkReply::ProxyAuthenticationRequiredError; |
|
874 |
break; |
|
875 |
case QAbstractSocket::SslHandshakeFailedError: |
|
876 |
errorCode = QNetworkReply::SslHandshakeFailedError; |
|
877 |
break; |
|
878 |
default: |
|
879 |
// all other errors are treated as NetworkError |
|
880 |
errorCode = QNetworkReply::UnknownNetworkError; |
|
881 |
break; |
|
882 |
} |
|
883 |
QPointer<QObject> that = connection; |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
884 |
QString errorString = connection->d_func()->errorDetail(errorCode, socket, socket->errorString()); |
0 | 885 |
if (send2Reply) { |
886 |
if (reply) { |
|
887 |
reply->d_func()->errorString = errorString; |
|
888 |
// this error matters only to this reply |
|
889 |
emit reply->finishedWithError(errorCode, errorString); |
|
890 |
} |
|
891 |
// send the next request |
|
892 |
QMetaObject::invokeMethod(that, "_q_startNextRequest", Qt::QueuedConnection); |
|
893 |
} else { |
|
894 |
// the failure affects all requests. |
|
895 |
emit connection->error(errorCode, errorString); |
|
896 |
} |
|
897 |
if (that) //signal emission triggered event loop |
|
898 |
close(); |
|
899 |
} |
|
900 |
||
901 |
#ifndef QT_NO_NETWORKPROXY |
|
902 |
void QHttpNetworkConnectionChannel::_q_proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator* auth) |
|
903 |
{ |
|
904 |
connection->d_func()->emitProxyAuthenticationRequired(this, proxy, auth); |
|
905 |
} |
|
906 |
#endif |
|
907 |
||
908 |
void QHttpNetworkConnectionChannel::_q_uploadDataReadyRead() |
|
909 |
{ |
|
910 |
sendRequest(); |
|
911 |
} |
|
912 |
||
913 |
#ifndef QT_NO_OPENSSL |
|
914 |
void QHttpNetworkConnectionChannel::_q_encrypted() |
|
915 |
{ |
|
916 |
if (!socket) |
|
917 |
return; // ### error |
|
918 |
state = QHttpNetworkConnectionChannel::IdleState; |
|
919 |
sendRequest(); |
|
920 |
} |
|
921 |
||
922 |
void QHttpNetworkConnectionChannel::_q_sslErrors(const QList<QSslError> &errors) |
|
923 |
{ |
|
924 |
if (!socket) |
|
925 |
return; |
|
926 |
//QNetworkReply::NetworkError errorCode = QNetworkReply::ProtocolFailure; |
|
927 |
emit connection->sslErrors(errors); |
|
928 |
} |
|
929 |
||
930 |
void QHttpNetworkConnectionChannel::_q_encryptedBytesWritten(qint64 bytes) |
|
931 |
{ |
|
932 |
Q_UNUSED(bytes); |
|
933 |
// bytes have been written to the socket. write even more of them :) |
|
934 |
if (isSocketWriting()) |
|
935 |
sendRequest(); |
|
936 |
// otherwise we do nothing |
|
937 |
} |
|
938 |
#endif |
|
939 |
||
940 |
QT_END_NAMESPACE |
|
941 |
||
942 |
#include "moc_qhttpnetworkconnectionchannel_p.cpp" |
|
943 |
||
944 |
#endif // QT_NO_HTTP |