author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 29 Apr 2010 15:15:16 +0300 | |
branch | RCL_3 |
changeset 17 | 4b6ee5efea19 |
parent 14 | c0432d11811c |
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 |
#include <private/qnetworkrequest_p.h> |
|
46 |
#include <private/qobject_p.h> |
|
47 |
#include <private/qauthenticator_p.h> |
|
48 |
#include <qnetworkproxy.h> |
|
49 |
#include <qauthenticator.h> |
|
50 |
||
51 |
#include <qbuffer.h> |
|
52 |
#include <qpair.h> |
|
53 |
#include <qhttp.h> |
|
54 |
#include <qdebug.h> |
|
55 |
||
56 |
#ifndef QT_NO_HTTP |
|
57 |
||
58 |
#ifndef QT_NO_OPENSSL |
|
59 |
# include <QtNetwork/qsslkey.h> |
|
60 |
# include <QtNetwork/qsslcipher.h> |
|
61 |
# include <QtNetwork/qsslconfiguration.h> |
|
62 |
#endif |
|
63 |
||
64 |
||
65 |
||
66 |
QT_BEGIN_NAMESPACE |
|
67 |
||
68 |
#ifdef Q_OS_SYMBIAN |
|
69 |
const int QHttpNetworkConnectionPrivate::defaultChannelCount = 3; |
|
70 |
#else |
|
71 |
const int QHttpNetworkConnectionPrivate::defaultChannelCount = 6; |
|
72 |
#endif |
|
73 |
||
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
74 |
// The pipeline length. So there will be 4 requests in flight. |
0 | 75 |
const int QHttpNetworkConnectionPrivate::defaultPipelineLength = 3; |
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
76 |
// Only re-fill the pipeline if there's defaultRePipelineLength slots free in the pipeline. |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
77 |
// This means that there are 2 requests in flight and 2 slots free that will be re-filled. |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
78 |
const int QHttpNetworkConnectionPrivate::defaultRePipelineLength = 2; |
0 | 79 |
|
80 |
||
81 |
QHttpNetworkConnectionPrivate::QHttpNetworkConnectionPrivate(const QString &hostName, quint16 port, bool encrypt) |
|
82 |
: hostName(hostName), port(port), encrypt(encrypt), |
|
83 |
channelCount(defaultChannelCount), |
|
84 |
pendingAuthSignal(false), pendingProxyAuthSignal(false) |
|
85 |
#ifndef QT_NO_NETWORKPROXY |
|
86 |
, networkProxy(QNetworkProxy::NoProxy) |
|
87 |
#endif |
|
88 |
{ |
|
89 |
channels = new QHttpNetworkConnectionChannel[channelCount]; |
|
90 |
} |
|
91 |
||
92 |
QHttpNetworkConnectionPrivate::QHttpNetworkConnectionPrivate(quint16 channelCount, const QString &hostName, quint16 port, bool encrypt) |
|
93 |
: hostName(hostName), port(port), encrypt(encrypt), |
|
94 |
channelCount(channelCount), |
|
95 |
pendingAuthSignal(false), pendingProxyAuthSignal(false) |
|
96 |
#ifndef QT_NO_NETWORKPROXY |
|
97 |
, networkProxy(QNetworkProxy::NoProxy) |
|
98 |
#endif |
|
99 |
{ |
|
100 |
channels = new QHttpNetworkConnectionChannel[channelCount]; |
|
101 |
} |
|
102 |
||
103 |
||
104 |
||
105 |
QHttpNetworkConnectionPrivate::~QHttpNetworkConnectionPrivate() |
|
106 |
{ |
|
107 |
for (int i = 0; i < channelCount; ++i) { |
|
108 |
if (channels[i].socket) { |
|
109 |
channels[i].socket->close(); |
|
110 |
delete channels[i].socket; |
|
111 |
} |
|
112 |
} |
|
113 |
delete []channels; |
|
114 |
} |
|
115 |
||
116 |
void QHttpNetworkConnectionPrivate::init() |
|
117 |
{ |
|
118 |
for (int i = 0; i < channelCount; i++) { |
|
119 |
channels[i].setConnection(this->q_func()); |
|
120 |
channels[i].init(); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
int QHttpNetworkConnectionPrivate::indexOf(QAbstractSocket *socket) const |
|
125 |
{ |
|
126 |
for (int i = 0; i < channelCount; ++i) |
|
127 |
if (channels[i].socket == socket) |
|
128 |
return i; |
|
129 |
||
130 |
qFatal("Called with unknown socket object."); |
|
131 |
return 0; |
|
132 |
} |
|
133 |
||
134 |
qint64 QHttpNetworkConnectionPrivate::uncompressedBytesAvailable(const QHttpNetworkReply &reply) const |
|
135 |
{ |
|
136 |
return reply.d_func()->responseData.byteAmount(); |
|
137 |
} |
|
138 |
||
139 |
qint64 QHttpNetworkConnectionPrivate::uncompressedBytesAvailableNextBlock(const QHttpNetworkReply &reply) const |
|
140 |
{ |
|
141 |
return reply.d_func()->responseData.sizeNextBlock(); |
|
142 |
} |
|
143 |
||
144 |
void QHttpNetworkConnectionPrivate::prepareRequest(HttpMessagePair &messagePair) |
|
145 |
{ |
|
146 |
QHttpNetworkRequest &request = messagePair.first; |
|
147 |
QHttpNetworkReply *reply = messagePair.second; |
|
148 |
||
149 |
// add missing fields for the request |
|
150 |
QByteArray value; |
|
151 |
// check if Content-Length is provided |
|
152 |
QNonContiguousByteDevice* uploadByteDevice = request.uploadByteDevice(); |
|
153 |
if (uploadByteDevice) { |
|
154 |
if (request.contentLength() != -1 && uploadByteDevice->size() != -1) { |
|
155 |
// both values known, take the smaller one. |
|
156 |
request.setContentLength(qMin(uploadByteDevice->size(), request.contentLength())); |
|
157 |
} else if (request.contentLength() == -1 && uploadByteDevice->size() != -1) { |
|
158 |
// content length not supplied by user, but the upload device knows it |
|
159 |
request.setContentLength(uploadByteDevice->size()); |
|
160 |
} else if (request.contentLength() != -1 && uploadByteDevice->size() == -1) { |
|
161 |
// everything OK, the user supplied us the contentLength |
|
162 |
} else if (request.contentLength() == -1 && uploadByteDevice->size() == -1) { |
|
163 |
qFatal("QHttpNetworkConnectionPrivate: Neither content-length nor upload device size were given"); |
|
164 |
} |
|
165 |
} |
|
166 |
// set the Connection/Proxy-Connection: Keep-Alive headers |
|
167 |
#ifndef QT_NO_NETWORKPROXY |
|
168 |
if (networkProxy.type() == QNetworkProxy::HttpCachingProxy) { |
|
169 |
value = request.headerField("proxy-connection"); |
|
170 |
if (value.isEmpty()) |
|
171 |
request.setHeaderField("Proxy-Connection", "Keep-Alive"); |
|
172 |
} else { |
|
173 |
#endif |
|
174 |
value = request.headerField("connection"); |
|
175 |
if (value.isEmpty()) |
|
176 |
request.setHeaderField("Connection", "Keep-Alive"); |
|
177 |
#ifndef QT_NO_NETWORKPROXY |
|
178 |
} |
|
179 |
#endif |
|
180 |
||
181 |
// If the request had a accept-encoding set, we better not mess |
|
182 |
// with it. If it was not set, we announce that we understand gzip |
|
183 |
// and remember this fact in request.d->autoDecompress so that |
|
184 |
// we can later decompress the HTTP reply if it has such an |
|
185 |
// encoding. |
|
186 |
value = request.headerField("accept-encoding"); |
|
187 |
if (value.isEmpty()) { |
|
188 |
#ifndef QT_NO_COMPRESS |
|
189 |
request.setHeaderField("Accept-Encoding", "gzip"); |
|
190 |
request.d->autoDecompress = true; |
|
191 |
#else |
|
192 |
// if zlib is not available set this to false always |
|
193 |
request.d->autoDecompress = false; |
|
194 |
#endif |
|
195 |
} |
|
196 |
||
197 |
// some websites mandate an accept-language header and fail |
|
198 |
// if it is not sent. This is a problem with the website and |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
199 |
// not with us, but we work around this by setting |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
// one always. |
0 | 201 |
value = request.headerField("accept-language"); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
202 |
if (value.isEmpty()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
203 |
QString systemLocale = QLocale::system().name().replace(QChar::fromAscii('_'),QChar::fromAscii('-')); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
QString acceptLanguage; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
205 |
if (systemLocale == QLatin1String("C")) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
206 |
acceptLanguage = QString::fromAscii("en,*"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
207 |
else if (systemLocale.startsWith(QLatin1String("en-"))) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
208 |
acceptLanguage = QString::fromAscii("%1,*").arg(systemLocale); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
209 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
210 |
acceptLanguage = QString::fromAscii("%1,en,*").arg(systemLocale); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
211 |
request.setHeaderField("Accept-Language", acceptLanguage.toAscii()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
} |
0 | 213 |
|
214 |
// set the User Agent |
|
215 |
value = request.headerField("user-agent"); |
|
216 |
if (value.isEmpty()) |
|
217 |
request.setHeaderField("User-Agent", "Mozilla/5.0"); |
|
218 |
// set the host |
|
219 |
value = request.headerField("host"); |
|
220 |
if (value.isEmpty()) { |
|
221 |
QByteArray host = QUrl::toAce(hostName); |
|
222 |
||
223 |
int port = request.url().port(); |
|
224 |
if (port != -1) { |
|
225 |
host += ':'; |
|
226 |
host += QByteArray::number(port); |
|
227 |
} |
|
228 |
||
229 |
request.setHeaderField("Host", host); |
|
230 |
} |
|
231 |
||
232 |
reply->d_func()->requestIsPrepared = true; |
|
233 |
} |
|
234 |
||
235 |
||
236 |
||
237 |
||
238 |
void QHttpNetworkConnectionPrivate::emitReplyError(QAbstractSocket *socket, |
|
239 |
QHttpNetworkReply *reply, |
|
240 |
QNetworkReply::NetworkError errorCode) |
|
241 |
{ |
|
242 |
Q_Q(QHttpNetworkConnection); |
|
243 |
if (socket && reply) { |
|
244 |
// this error matters only to this reply |
|
245 |
reply->d_func()->errorString = errorDetail(errorCode, socket); |
|
246 |
emit reply->finishedWithError(errorCode, reply->d_func()->errorString); |
|
247 |
int i = indexOf(socket); |
|
248 |
// remove the corrupt data if any |
|
249 |
reply->d_func()->eraseData(); |
|
250 |
channels[i].close(); |
|
251 |
// send the next request |
|
252 |
QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
void QHttpNetworkConnectionPrivate::copyCredentials(int fromChannel, QAuthenticator *auth, bool isProxy) |
|
257 |
{ |
|
258 |
Q_ASSERT(auth); |
|
259 |
||
260 |
// select another channel |
|
261 |
QAuthenticator* otherAuth = 0; |
|
262 |
for (int i = 0; i < channelCount; ++i) { |
|
263 |
if (i == fromChannel) |
|
264 |
continue; |
|
265 |
if (isProxy) |
|
266 |
otherAuth = &channels[i].proxyAuthenticator; |
|
267 |
else |
|
268 |
otherAuth = &channels[i].authenticator; |
|
269 |
// if the credentials are different, copy them |
|
270 |
if (otherAuth->user().compare(auth->user())) |
|
271 |
otherAuth->setUser(auth->user()); |
|
272 |
if (otherAuth->password().compare(auth->password())) |
|
273 |
otherAuth->setPassword(auth->password()); |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
||
278 |
// handles the authentication for one channel and eventually re-starts the other channels |
|
279 |
bool QHttpNetworkConnectionPrivate::handleAuthenticateChallenge(QAbstractSocket *socket, QHttpNetworkReply *reply, |
|
280 |
bool isProxy, bool &resend) |
|
281 |
{ |
|
282 |
Q_ASSERT(socket); |
|
283 |
Q_ASSERT(reply); |
|
284 |
||
285 |
Q_Q(QHttpNetworkConnection); |
|
286 |
||
287 |
resend = false; |
|
288 |
//create the response header to be used with QAuthenticatorPrivate. |
|
289 |
QHttpResponseHeader responseHeader; |
|
290 |
QList<QPair<QByteArray, QByteArray> > fields = reply->header(); |
|
291 |
QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin(); |
|
292 |
while (it != fields.constEnd()) { |
|
293 |
responseHeader.addValue(QString::fromLatin1(it->first), QString::fromUtf8(it->second)); |
|
294 |
it++; |
|
295 |
} |
|
296 |
//find out the type of authentication protocol requested. |
|
297 |
QAuthenticatorPrivate::Method authMethod = reply->d_func()->authenticationMethod(isProxy); |
|
298 |
if (authMethod != QAuthenticatorPrivate::None) { |
|
299 |
int i = indexOf(socket); |
|
300 |
//Use a single authenticator for all domains. ### change later to use domain/realm |
|
301 |
QAuthenticator* auth = 0; |
|
302 |
if (isProxy) { |
|
303 |
auth = &channels[i].proxyAuthenticator; |
|
304 |
channels[i].proxyAuthMehtod = authMethod; |
|
305 |
} else { |
|
306 |
auth = &channels[i].authenticator; |
|
307 |
channels[i].authMehtod = authMethod; |
|
308 |
} |
|
309 |
//proceed with the authentication. |
|
310 |
if (auth->isNull()) |
|
311 |
auth->detach(); |
|
312 |
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(*auth); |
|
313 |
priv->parseHttpResponse(responseHeader, isProxy); |
|
314 |
||
315 |
if (priv->phase == QAuthenticatorPrivate::Done) { |
|
316 |
if ((isProxy && pendingProxyAuthSignal) ||(!isProxy && pendingAuthSignal)) { |
|
317 |
// drop the request |
|
318 |
reply->d_func()->eraseData(); |
|
319 |
channels[i].close(); |
|
320 |
channels[i].lastStatus = 0; |
|
321 |
channels[i].state = QHttpNetworkConnectionChannel::Wait4AuthState; |
|
322 |
return false; |
|
323 |
} |
|
324 |
// cannot use this socket until the slot returns |
|
325 |
channels[i].state = QHttpNetworkConnectionChannel::WaitingState; |
|
326 |
socket->blockSignals(true); |
|
327 |
if (!isProxy) { |
|
328 |
pendingAuthSignal = true; |
|
329 |
emit q->authenticationRequired(reply->request(), auth, q); |
|
330 |
pendingAuthSignal = false; |
|
331 |
#ifndef QT_NO_NETWORKPROXY |
|
332 |
} else { |
|
333 |
pendingProxyAuthSignal = true; |
|
334 |
emit q->proxyAuthenticationRequired(networkProxy, auth, q); |
|
335 |
pendingProxyAuthSignal = false; |
|
336 |
#endif |
|
337 |
} |
|
338 |
socket->blockSignals(false); |
|
339 |
// socket free to use |
|
340 |
channels[i].state = QHttpNetworkConnectionChannel::IdleState; |
|
341 |
if (priv->phase != QAuthenticatorPrivate::Done) { |
|
342 |
// send any pending requests |
|
343 |
copyCredentials(i, auth, isProxy); |
|
344 |
QMetaObject::invokeMethod(q, "_q_restartAuthPendingRequests", Qt::QueuedConnection); |
|
345 |
} |
|
346 |
} |
|
347 |
// changing values in QAuthenticator will reset the 'phase' |
|
348 |
if (priv->phase == QAuthenticatorPrivate::Done) { |
|
349 |
// authentication is cancelled, send the current contents to the user. |
|
350 |
emit channels[i].reply->headerChanged(); |
|
351 |
emit channels[i].reply->readyRead(); |
|
352 |
QNetworkReply::NetworkError errorCode = |
|
353 |
isProxy |
|
354 |
? QNetworkReply::ProxyAuthenticationRequiredError |
|
355 |
: QNetworkReply::AuthenticationRequiredError; |
|
356 |
reply->d_func()->errorString = errorDetail(errorCode, socket); |
|
357 |
emit q->error(errorCode, reply->d_func()->errorString); |
|
358 |
emit channels[i].reply->finished(); |
|
359 |
// ### at this point the reply could be deleted |
|
360 |
socket->close(); |
|
361 |
// remove pending request on the other channels |
|
362 |
for (int j = 0; j < channelCount; ++j) { |
|
363 |
if (j != i && channels[j].state == QHttpNetworkConnectionChannel::Wait4AuthState) |
|
364 |
channels[j].state = QHttpNetworkConnectionChannel::IdleState; |
|
365 |
} |
|
366 |
return true; |
|
367 |
} |
|
368 |
//resend the request |
|
369 |
resend = true; |
|
370 |
return true; |
|
371 |
} |
|
372 |
return false; |
|
373 |
} |
|
374 |
||
375 |
void QHttpNetworkConnectionPrivate::createAuthorization(QAbstractSocket *socket, QHttpNetworkRequest &request) |
|
376 |
{ |
|
377 |
Q_ASSERT(socket); |
|
378 |
||
379 |
int i = indexOf(socket); |
|
380 |
||
381 |
if (channels[i].authMehtod != QAuthenticatorPrivate::None) { |
|
382 |
if (!(channels[i].authMehtod == QAuthenticatorPrivate::Ntlm && channels[i].lastStatus != 401)) { |
|
383 |
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(channels[i].authenticator); |
|
384 |
if (priv && priv->method != QAuthenticatorPrivate::None) { |
|
385 |
QByteArray response = priv->calculateResponse(request.d->methodName(), request.d->uri(false)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
386 |
request.setHeaderField("Authorization", response); |
0 | 387 |
} |
388 |
} |
|
389 |
} |
|
390 |
if (channels[i].proxyAuthMehtod != QAuthenticatorPrivate::None) { |
|
391 |
if (!(channels[i].proxyAuthMehtod == QAuthenticatorPrivate::Ntlm && channels[i].lastStatus != 407)) { |
|
392 |
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(channels[i].proxyAuthenticator); |
|
393 |
if (priv && priv->method != QAuthenticatorPrivate::None) { |
|
394 |
QByteArray response = priv->calculateResponse(request.d->methodName(), request.d->uri(false)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
395 |
request.setHeaderField("Proxy-Authorization", response); |
0 | 396 |
} |
397 |
} |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
QHttpNetworkReply* QHttpNetworkConnectionPrivate::queueRequest(const QHttpNetworkRequest &request) |
|
402 |
{ |
|
403 |
Q_Q(QHttpNetworkConnection); |
|
404 |
||
405 |
// The reply component of the pair is created initially. |
|
406 |
QHttpNetworkReply *reply = new QHttpNetworkReply(request.url()); |
|
407 |
reply->setRequest(request); |
|
408 |
reply->d_func()->connection = q; |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
409 |
reply->d_func()->connectionChannel = &channels[0]; // will have the correct one set later |
0 | 410 |
HttpMessagePair pair = qMakePair(request, reply); |
411 |
||
412 |
switch (request.priority()) { |
|
413 |
case QHttpNetworkRequest::HighPriority: |
|
414 |
highPriorityQueue.prepend(pair); |
|
415 |
break; |
|
416 |
case QHttpNetworkRequest::NormalPriority: |
|
417 |
case QHttpNetworkRequest::LowPriority: |
|
418 |
lowPriorityQueue.prepend(pair); |
|
419 |
break; |
|
420 |
} |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
421 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
422 |
// this used to be called via invokeMethod and a QueuedConnection |
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
423 |
// It is the only place _q_startNextRequest is called directly without going |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
424 |
// through the event loop using a QueuedConnection. |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
425 |
// This is dangerous because of recursion that might occur when emitting |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
426 |
// signals as DirectConnection from this code path. Therefore all signal |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
427 |
// emissions that can come out from this code path need to |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
428 |
// be QueuedConnection. |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
429 |
// We are currently trying to fine-tune this. |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
430 |
_q_startNextRequest(); |
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
431 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
432 |
|
0 | 433 |
return reply; |
434 |
} |
|
435 |
||
436 |
void QHttpNetworkConnectionPrivate::requeueRequest(const HttpMessagePair &pair) |
|
437 |
{ |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
438 |
Q_Q(QHttpNetworkConnection); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
439 |
|
0 | 440 |
QHttpNetworkRequest request = pair.first; |
441 |
switch (request.priority()) { |
|
442 |
case QHttpNetworkRequest::HighPriority: |
|
443 |
highPriorityQueue.prepend(pair); |
|
444 |
break; |
|
445 |
case QHttpNetworkRequest::NormalPriority: |
|
446 |
case QHttpNetworkRequest::LowPriority: |
|
447 |
lowPriorityQueue.prepend(pair); |
|
448 |
break; |
|
449 |
} |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
450 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
451 |
QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); |
0 | 452 |
} |
453 |
||
454 |
void QHttpNetworkConnectionPrivate::dequeueAndSendRequest(QAbstractSocket *socket) |
|
455 |
{ |
|
456 |
Q_ASSERT(socket); |
|
457 |
||
458 |
int i = indexOf(socket); |
|
459 |
||
460 |
if (!highPriorityQueue.isEmpty()) { |
|
461 |
// remove from queue before sendRequest! else we might pipeline the same request again |
|
462 |
HttpMessagePair messagePair = highPriorityQueue.takeLast(); |
|
463 |
if (!messagePair.second->d_func()->requestIsPrepared) |
|
464 |
prepareRequest(messagePair); |
|
465 |
channels[i].request = messagePair.first; |
|
466 |
channels[i].reply = messagePair.second; |
|
467 |
channels[i].sendRequest(); |
|
468 |
return; |
|
469 |
} |
|
470 |
||
471 |
if (!lowPriorityQueue.isEmpty()) { |
|
472 |
// remove from queue before sendRequest! else we might pipeline the same request again |
|
473 |
HttpMessagePair messagePair = lowPriorityQueue.takeLast(); |
|
474 |
if (!messagePair.second->d_func()->requestIsPrepared) |
|
475 |
prepareRequest(messagePair); |
|
476 |
channels[i].request = messagePair.first; |
|
477 |
channels[i].reply = messagePair.second; |
|
478 |
channels[i].sendRequest(); |
|
479 |
return; |
|
480 |
} |
|
481 |
} |
|
482 |
||
483 |
// this is called from _q_startNextRequest and when a request has been sent down a socket from the channel |
|
484 |
void QHttpNetworkConnectionPrivate::fillPipeline(QAbstractSocket *socket) |
|
485 |
{ |
|
486 |
// return fast if there is nothing to pipeline |
|
487 |
if (highPriorityQueue.isEmpty() && lowPriorityQueue.isEmpty()) |
|
488 |
return; |
|
489 |
||
490 |
int i = indexOf(socket); |
|
491 |
||
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
492 |
// return fast if there was no reply right now processed |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
493 |
if (channels[i].reply == 0) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
494 |
return; |
0 | 495 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
496 |
if (! (defaultPipelineLength - channels[i].alreadyPipelinedRequests.length() >= defaultRePipelineLength)) { |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
497 |
return; |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
498 |
} |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
499 |
|
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
500 |
if (channels[i].pipeliningSupported != QHttpNetworkConnectionChannel::PipeliningProbablySupported) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
501 |
return; |
0 | 502 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
503 |
// the current request that is in must already support pipelining |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
504 |
if (!channels[i].request.isPipeliningAllowed()) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
505 |
return; |
0 | 506 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
507 |
// the current request must be a idempotent (right now we only check GET) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
508 |
if (channels[i].request.operation() != QHttpNetworkRequest::Get) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
509 |
return; |
0 | 510 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
511 |
// check if socket is connected |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
512 |
if (socket->state() != QAbstractSocket::ConnectedState) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
513 |
return; |
0 | 514 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
515 |
// check for resendCurrent |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
516 |
if (channels[i].resendCurrent) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
517 |
return; |
0 | 518 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
519 |
// we do not like authentication stuff |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
520 |
// ### make sure to be OK with this in later releases |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
521 |
if (!channels[i].authenticator.isNull() || !channels[i].authenticator.user().isEmpty()) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
522 |
return; |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
523 |
if (!channels[i].proxyAuthenticator.isNull() || !channels[i].proxyAuthenticator.user().isEmpty()) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
524 |
return; |
0 | 525 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
526 |
// must be in ReadingState or WaitingState |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
527 |
if (! (channels[i].state == QHttpNetworkConnectionChannel::WaitingState |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
528 |
|| channels[i].state == QHttpNetworkConnectionChannel::ReadingState)) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
529 |
return; |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
530 |
|
0 | 531 |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
532 |
//qDebug() << "QHttpNetworkConnectionPrivate::fillPipeline processing highPriorityQueue, size=" << highPriorityQueue.size() << " alreadyPipelined=" << channels[i].alreadyPipelinedRequests.length(); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
533 |
int lengthBefore; |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
534 |
while (!highPriorityQueue.isEmpty()) { |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
535 |
lengthBefore = channels[i].alreadyPipelinedRequests.length(); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
536 |
fillPipeline(highPriorityQueue, channels[i]); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
537 |
|
0 | 538 |
if (channels[i].alreadyPipelinedRequests.length() >= defaultPipelineLength) |
539 |
return; |
|
540 |
||
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
541 |
if (lengthBefore == channels[i].alreadyPipelinedRequests.length()) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
542 |
break; // did not process anything, now do the low prio queue |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
543 |
} |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
544 |
|
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
545 |
//qDebug() << "QHttpNetworkConnectionPrivate::fillPipeline processing lowPriorityQueue, size=" << lowPriorityQueue.size() << " alreadyPipelined=" << channels[i].alreadyPipelinedRequests.length(); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
546 |
while (!lowPriorityQueue.isEmpty()) { |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
547 |
lengthBefore = channels[i].alreadyPipelinedRequests.length(); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
548 |
fillPipeline(lowPriorityQueue, channels[i]); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
549 |
|
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
550 |
if (channels[i].alreadyPipelinedRequests.length() >= defaultPipelineLength) |
0 | 551 |
return; |
552 |
||
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
553 |
if (lengthBefore == channels[i].alreadyPipelinedRequests.length()) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
554 |
break; // did not process anything |
0 | 555 |
} |
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
556 |
|
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
557 |
|
0 | 558 |
} |
559 |
||
560 |
// returns true when the processing of a queue has been done |
|
561 |
bool QHttpNetworkConnectionPrivate::fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel) |
|
562 |
{ |
|
563 |
if (queue.isEmpty()) |
|
564 |
return true; |
|
565 |
||
566 |
for (int i = queue.count() - 1; i >= 0; --i) { |
|
567 |
HttpMessagePair messagePair = queue.at(i); |
|
568 |
const QHttpNetworkRequest &request = messagePair.first; |
|
569 |
||
570 |
// we currently do not support pipelining if HTTP authentication is used |
|
571 |
if (!request.url().userInfo().isEmpty()) |
|
572 |
continue; |
|
573 |
||
574 |
// take only GET requests |
|
575 |
if (request.operation() != QHttpNetworkRequest::Get) |
|
576 |
continue; |
|
577 |
||
578 |
if (!request.isPipeliningAllowed()) |
|
579 |
continue; |
|
580 |
||
581 |
// remove it from the queue |
|
582 |
queue.takeAt(i); |
|
583 |
// we modify the queue we iterate over here, but since we return from the function |
|
584 |
// afterwards this is fine. |
|
585 |
||
586 |
// actually send it |
|
587 |
if (!messagePair.second->d_func()->requestIsPrepared) |
|
588 |
prepareRequest(messagePair); |
|
589 |
channel.pipelineInto(messagePair); |
|
590 |
||
591 |
// return false because we processed something and need to process again |
|
592 |
return false; |
|
593 |
} |
|
594 |
||
595 |
// return true, the queue has been processed and not changed |
|
596 |
return true; |
|
597 |
} |
|
598 |
||
599 |
||
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
600 |
QString QHttpNetworkConnectionPrivate::errorDetail(QNetworkReply::NetworkError errorCode, QAbstractSocket* socket, |
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
601 |
const QString &extraDetail) |
0 | 602 |
{ |
603 |
Q_ASSERT(socket); |
|
604 |
||
605 |
QString errorString; |
|
606 |
switch (errorCode) { |
|
607 |
case QNetworkReply::HostNotFoundError: |
|
608 |
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QHttp", "Host %1 not found")) |
|
609 |
.arg(socket->peerName()); |
|
610 |
break; |
|
611 |
case QNetworkReply::ConnectionRefusedError: |
|
612 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Connection refused")); |
|
613 |
break; |
|
614 |
case QNetworkReply::RemoteHostClosedError: |
|
615 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Connection closed")); |
|
616 |
break; |
|
617 |
case QNetworkReply::TimeoutError: |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
618 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QAbstractSocket", "Socket operation timed out")); |
0 | 619 |
break; |
620 |
case QNetworkReply::ProxyAuthenticationRequiredError: |
|
621 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Proxy requires authentication")); |
|
622 |
break; |
|
623 |
case QNetworkReply::AuthenticationRequiredError: |
|
624 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Host requires authentication")); |
|
625 |
break; |
|
626 |
case QNetworkReply::ProtocolFailure: |
|
627 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Data corrupted")); |
|
628 |
break; |
|
629 |
case QNetworkReply::ProtocolUnknownError: |
|
630 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Unknown protocol specified")); |
|
631 |
break; |
|
632 |
case QNetworkReply::SslHandshakeFailedError: |
|
633 |
errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "SSL handshake failed")); |
|
634 |
break; |
|
635 |
default: |
|
636 |
// all other errors are treated as QNetworkReply::UnknownNetworkError |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
637 |
errorString = extraDetail; |
0 | 638 |
break; |
639 |
} |
|
640 |
return errorString; |
|
641 |
} |
|
642 |
||
643 |
// this is called from the destructor of QHttpNetworkReply. It is called when |
|
644 |
// the reply was finished correctly or when it was aborted. |
|
645 |
void QHttpNetworkConnectionPrivate::removeReply(QHttpNetworkReply *reply) |
|
646 |
{ |
|
647 |
Q_Q(QHttpNetworkConnection); |
|
648 |
||
649 |
// check if the reply is currently being processed or it is pipelined in |
|
650 |
for (int i = 0; i < channelCount; ++i) { |
|
651 |
// is the reply associated the currently processing of this channel? |
|
652 |
if (channels[i].reply == reply) { |
|
653 |
channels[i].reply = 0; |
|
654 |
||
655 |
if (!reply->isFinished() && !channels[i].alreadyPipelinedRequests.isEmpty()) { |
|
656 |
// the reply had to be prematurely removed, e.g. it was not finished |
|
657 |
// therefore we have to requeue the already pipelined requests. |
|
658 |
channels[i].requeueCurrentlyPipelinedRequests(); |
|
659 |
} |
|
660 |
||
661 |
// if HTTP mandates we should close |
|
662 |
// or the reply is not finished yet, e.g. it was aborted |
|
663 |
// we have to close that connection |
|
664 |
if (reply->d_func()->isConnectionCloseEnabled() || !reply->isFinished()) |
|
665 |
channels[i].close(); |
|
666 |
||
667 |
QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); |
|
668 |
return; |
|
669 |
} |
|
670 |
||
671 |
// is the reply inside the pipeline of this channel already? |
|
672 |
for (int j = 0; j < channels[i].alreadyPipelinedRequests.length(); j++) { |
|
673 |
if (channels[i].alreadyPipelinedRequests.at(j).second == reply) { |
|
674 |
// Remove that HttpMessagePair |
|
675 |
channels[i].alreadyPipelinedRequests.removeAt(j); |
|
676 |
||
677 |
channels[i].requeueCurrentlyPipelinedRequests(); |
|
678 |
||
679 |
// Since some requests had already been pipelined, but we removed |
|
680 |
// one and re-queued the others |
|
681 |
// we must force a connection close after the request that is |
|
682 |
// currently in processing has been finished. |
|
683 |
if (channels[i].reply) |
|
684 |
channels[i].reply->d_func()->forceConnectionCloseEnabled = true; |
|
685 |
||
686 |
QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); |
|
687 |
return; |
|
688 |
} |
|
689 |
} |
|
690 |
} |
|
691 |
// remove from the high priority queue |
|
692 |
if (!highPriorityQueue.isEmpty()) { |
|
693 |
for (int j = highPriorityQueue.count() - 1; j >= 0; --j) { |
|
694 |
HttpMessagePair messagePair = highPriorityQueue.at(j); |
|
695 |
if (messagePair.second == reply) { |
|
696 |
highPriorityQueue.removeAt(j); |
|
697 |
QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); |
|
698 |
return; |
|
699 |
} |
|
700 |
} |
|
701 |
} |
|
702 |
// remove from the low priority queue |
|
703 |
if (!lowPriorityQueue.isEmpty()) { |
|
704 |
for (int j = lowPriorityQueue.count() - 1; j >= 0; --j) { |
|
705 |
HttpMessagePair messagePair = lowPriorityQueue.at(j); |
|
706 |
if (messagePair.second == reply) { |
|
707 |
lowPriorityQueue.removeAt(j); |
|
708 |
QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); |
|
709 |
return; |
|
710 |
} |
|
711 |
} |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
||
716 |
||
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
717 |
// This function must be called from the event loop. The only |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
718 |
// exception is documented in QHttpNetworkConnectionPrivate::queueRequest |
0 | 719 |
void QHttpNetworkConnectionPrivate::_q_startNextRequest() |
720 |
{ |
|
721 |
//resend the necessary ones. |
|
722 |
for (int i = 0; i < channelCount; ++i) { |
|
723 |
if (channels[i].resendCurrent) { |
|
724 |
channels[i].resendCurrent = false; |
|
725 |
channels[i].state = QHttpNetworkConnectionChannel::IdleState; |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
726 |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
727 |
// if this is not possible, error will be emitted and connection terminated |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
728 |
if (!channels[i].resetUploadData()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
729 |
continue; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
5
diff
changeset
|
730 |
channels[i].sendRequest(); |
0 | 731 |
} |
732 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
733 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
734 |
// dequeue new ones |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
735 |
|
0 | 736 |
QAbstractSocket *socket = 0; |
737 |
for (int i = 0; i < channelCount; ++i) { |
|
738 |
QAbstractSocket *chSocket = channels[i].socket; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
739 |
// try to get a free AND connected socket |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
740 |
if (!channels[i].isSocketBusy() && channels[i].socket->state() == QAbstractSocket::ConnectedState) { |
0 | 741 |
socket = chSocket; |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
742 |
dequeueAndSendRequest(socket); |
0 | 743 |
break; |
744 |
} |
|
745 |
} |
|
746 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
747 |
if (!socket) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
748 |
for (int i = 0; i < channelCount; ++i) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
749 |
QAbstractSocket *chSocket = channels[i].socket; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
750 |
// try to get a free unconnected socket |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
751 |
if (!channels[i].isSocketBusy()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
752 |
socket = chSocket; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
753 |
dequeueAndSendRequest(socket); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
754 |
break; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
755 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
756 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
757 |
} |
0 | 758 |
|
759 |
// try to push more into all sockets |
|
760 |
// ### FIXME we should move this to the beginning of the function |
|
761 |
// as soon as QtWebkit is properly using the pipelining |
|
762 |
// (e.g. not for XMLHttpRequest or the first page load) |
|
763 |
// ### FIXME we should also divide the requests more even |
|
764 |
// on the connected sockets |
|
765 |
//tryToFillPipeline(socket); |
|
766 |
// return fast if there is nothing to pipeline |
|
767 |
if (highPriorityQueue.isEmpty() && lowPriorityQueue.isEmpty()) |
|
768 |
return; |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
769 |
for (int i = 0; i < channelCount; i++) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
770 |
if (channels[i].socket->state() == QAbstractSocket::ConnectedState) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
771 |
fillPipeline(channels[i].socket); |
0 | 772 |
} |
773 |
||
774 |
void QHttpNetworkConnectionPrivate::_q_restartAuthPendingRequests() |
|
775 |
{ |
|
776 |
// send the request using the idle socket |
|
777 |
for (int i = 0 ; i < channelCount; ++i) { |
|
778 |
if (channels[i].state == QHttpNetworkConnectionChannel::Wait4AuthState) { |
|
779 |
channels[i].state = QHttpNetworkConnectionChannel::IdleState; |
|
780 |
if (channels[i].reply) |
|
781 |
channels[i].sendRequest(); |
|
782 |
} |
|
783 |
} |
|
784 |
} |
|
785 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
786 |
void QHttpNetworkConnectionPrivate::readMoreLater(QHttpNetworkReply *reply) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
787 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
788 |
for (int i = 0 ; i < channelCount; ++i) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
789 |
if (channels[i].reply == reply) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
790 |
// emulate a readyRead() from the socket |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
791 |
QMetaObject::invokeMethod(&channels[i], "_q_readyRead", Qt::QueuedConnection); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
792 |
return; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
793 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
794 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
795 |
} |
0 | 796 |
|
797 |
QHttpNetworkConnection::QHttpNetworkConnection(const QString &hostName, quint16 port, bool encrypt, QObject *parent) |
|
798 |
: QObject(*(new QHttpNetworkConnectionPrivate(hostName, port, encrypt)), parent) |
|
799 |
{ |
|
800 |
Q_D(QHttpNetworkConnection); |
|
801 |
d->init(); |
|
802 |
} |
|
803 |
||
804 |
QHttpNetworkConnection::QHttpNetworkConnection(quint16 connectionCount, const QString &hostName, quint16 port, bool encrypt, QObject *parent) |
|
805 |
: QObject(*(new QHttpNetworkConnectionPrivate(connectionCount, hostName, port, encrypt)), parent) |
|
806 |
{ |
|
807 |
Q_D(QHttpNetworkConnection); |
|
808 |
d->init(); |
|
809 |
} |
|
810 |
||
811 |
QHttpNetworkConnection::~QHttpNetworkConnection() |
|
812 |
{ |
|
813 |
} |
|
814 |
||
815 |
QString QHttpNetworkConnection::hostName() const |
|
816 |
{ |
|
817 |
Q_D(const QHttpNetworkConnection); |
|
818 |
return d->hostName; |
|
819 |
} |
|
820 |
||
821 |
quint16 QHttpNetworkConnection::port() const |
|
822 |
{ |
|
823 |
Q_D(const QHttpNetworkConnection); |
|
824 |
return d->port; |
|
825 |
} |
|
826 |
||
827 |
QHttpNetworkReply* QHttpNetworkConnection::sendRequest(const QHttpNetworkRequest &request) |
|
828 |
{ |
|
829 |
Q_D(QHttpNetworkConnection); |
|
830 |
return d->queueRequest(request); |
|
831 |
} |
|
832 |
||
833 |
void QHttpNetworkConnection::enableEncryption() |
|
834 |
{ |
|
835 |
Q_D(QHttpNetworkConnection); |
|
836 |
d->encrypt = true; |
|
837 |
} |
|
838 |
||
839 |
bool QHttpNetworkConnection::isEncrypted() const |
|
840 |
{ |
|
841 |
Q_D(const QHttpNetworkConnection); |
|
842 |
return d->encrypt; |
|
843 |
} |
|
844 |
||
845 |
void QHttpNetworkConnection::setProxyAuthentication(QAuthenticator *authenticator) |
|
846 |
{ |
|
847 |
Q_D(QHttpNetworkConnection); |
|
848 |
for (int i = 0; i < d->channelCount; ++i) |
|
849 |
d->channels[i].proxyAuthenticator = *authenticator; |
|
850 |
} |
|
851 |
||
852 |
void QHttpNetworkConnection::setAuthentication(const QString &domain, QAuthenticator *authenticator) |
|
853 |
{ |
|
854 |
Q_UNUSED(domain); // ### domain ? |
|
855 |
Q_D(QHttpNetworkConnection); |
|
856 |
for (int i = 0; i < d->channelCount; ++i) |
|
857 |
d->channels[i].authenticator = *authenticator; |
|
858 |
} |
|
859 |
||
860 |
#ifndef QT_NO_NETWORKPROXY |
|
861 |
void QHttpNetworkConnection::setCacheProxy(const QNetworkProxy &networkProxy) |
|
862 |
{ |
|
863 |
Q_D(QHttpNetworkConnection); |
|
864 |
d->networkProxy = networkProxy; |
|
865 |
// update the authenticator |
|
866 |
if (!d->networkProxy.user().isEmpty()) { |
|
867 |
for (int i = 0; i < d->channelCount; ++i) { |
|
868 |
d->channels[i].proxyAuthenticator.setUser(d->networkProxy.user()); |
|
869 |
d->channels[i].proxyAuthenticator.setPassword(d->networkProxy.password()); |
|
870 |
} |
|
871 |
} |
|
872 |
} |
|
873 |
||
874 |
QNetworkProxy QHttpNetworkConnection::cacheProxy() const |
|
875 |
{ |
|
876 |
Q_D(const QHttpNetworkConnection); |
|
877 |
return d->networkProxy; |
|
878 |
} |
|
879 |
||
880 |
void QHttpNetworkConnection::setTransparentProxy(const QNetworkProxy &networkProxy) |
|
881 |
{ |
|
882 |
Q_D(QHttpNetworkConnection); |
|
883 |
for (int i = 0; i < d->channelCount; ++i) |
|
884 |
d->channels[i].socket->setProxy(networkProxy); |
|
885 |
} |
|
886 |
||
887 |
QNetworkProxy QHttpNetworkConnection::transparentProxy() const |
|
888 |
{ |
|
889 |
Q_D(const QHttpNetworkConnection); |
|
890 |
return d->channels[0].socket->proxy(); |
|
891 |
} |
|
892 |
#endif |
|
893 |
||
894 |
||
895 |
// SSL support below |
|
896 |
#ifndef QT_NO_OPENSSL |
|
897 |
void QHttpNetworkConnection::setSslConfiguration(const QSslConfiguration &config) |
|
898 |
{ |
|
899 |
Q_D(QHttpNetworkConnection); |
|
900 |
if (!d->encrypt) |
|
901 |
return; |
|
902 |
||
903 |
// set the config on all channels |
|
904 |
for (int i = 0; i < d->channelCount; ++i) |
|
905 |
static_cast<QSslSocket *>(d->channels[i].socket)->setSslConfiguration(config); |
|
906 |
} |
|
907 |
||
908 |
void QHttpNetworkConnection::ignoreSslErrors(int channel) |
|
909 |
{ |
|
910 |
Q_D(QHttpNetworkConnection); |
|
911 |
if (!d->encrypt) |
|
912 |
return; |
|
913 |
||
914 |
if (channel == -1) { // ignore for all channels |
|
915 |
for (int i = 0; i < d->channelCount; ++i) { |
|
916 |
static_cast<QSslSocket *>(d->channels[i].socket)->ignoreSslErrors(); |
|
917 |
d->channels[i].ignoreAllSslErrors = true; |
|
918 |
} |
|
919 |
||
920 |
} else { |
|
921 |
static_cast<QSslSocket *>(d->channels[channel].socket)->ignoreSslErrors(); |
|
922 |
d->channels[channel].ignoreAllSslErrors = true; |
|
923 |
} |
|
924 |
} |
|
925 |
||
926 |
void QHttpNetworkConnection::ignoreSslErrors(const QList<QSslError> &errors, int channel) |
|
927 |
{ |
|
928 |
Q_D(QHttpNetworkConnection); |
|
929 |
if (!d->encrypt) |
|
930 |
return; |
|
931 |
||
932 |
if (channel == -1) { // ignore for all channels |
|
933 |
for (int i = 0; i < d->channelCount; ++i) { |
|
934 |
static_cast<QSslSocket *>(d->channels[i].socket)->ignoreSslErrors(errors); |
|
935 |
d->channels[i].ignoreSslErrorsList = errors; |
|
936 |
} |
|
937 |
||
938 |
} else { |
|
939 |
static_cast<QSslSocket *>(d->channels[channel].socket)->ignoreSslErrors(errors); |
|
940 |
d->channels[channel].ignoreSslErrorsList = errors; |
|
941 |
} |
|
942 |
} |
|
943 |
||
944 |
#endif //QT_NO_OPENSSL |
|
945 |
||
946 |
#ifndef QT_NO_NETWORKPROXY |
|
947 |
// only called from QHttpNetworkConnectionChannel::_q_proxyAuthenticationRequired, not |
|
948 |
// from QHttpNetworkConnectionChannel::handleAuthenticationChallenge |
|
949 |
// e.g. it is for SOCKS proxies which require authentication. |
|
950 |
void QHttpNetworkConnectionPrivate::emitProxyAuthenticationRequired(const QHttpNetworkConnectionChannel *chan, const QNetworkProxy &proxy, QAuthenticator* auth) |
|
951 |
{ |
|
952 |
Q_Q(QHttpNetworkConnection); |
|
953 |
emit q->proxyAuthenticationRequired(proxy, auth, q); |
|
954 |
int i = indexOf(chan->socket); |
|
955 |
copyCredentials(i, auth, true); |
|
956 |
} |
|
957 |
#endif |
|
958 |
||
959 |
||
960 |
QT_END_NAMESPACE |
|
961 |
||
962 |
#include "moc_qhttpnetworkconnection_p.cpp" |
|
963 |
||
964 |
#endif // QT_NO_HTTP |