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