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 4 | 3b1da2848fc7 |
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 "qnetworkaccessmanager.h" |
|
43 |
#include "qnetworkaccessmanager_p.h" |
|
44 |
#include "qnetworkrequest.h" |
|
45 |
#include "qnetworkreply.h" |
|
46 |
#include "qnetworkreply_p.h" |
|
47 |
#include "qnetworkcookie.h" |
|
48 |
#include "qabstractnetworkcache.h" |
|
49 |
||
50 |
#include "qnetworkaccesshttpbackend_p.h" |
|
51 |
#include "qnetworkaccessftpbackend_p.h" |
|
52 |
#include "qnetworkaccessfilebackend_p.h" |
|
53 |
#include "qnetworkaccessdatabackend_p.h" |
|
54 |
#include "qnetworkaccessdebugpipebackend_p.h" |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
#include "qfilenetworkreply_p.h" |
0 | 56 |
|
57 |
#include "QtCore/qbuffer.h" |
|
58 |
#include "QtCore/qurl.h" |
|
59 |
#include "QtCore/qvector.h" |
|
60 |
#include "QtNetwork/qauthenticator.h" |
|
61 |
#include "QtNetwork/qsslconfiguration.h" |
|
62 |
||
63 |
QT_BEGIN_NAMESPACE |
|
64 |
||
65 |
#ifndef QT_NO_HTTP |
|
66 |
Q_GLOBAL_STATIC(QNetworkAccessHttpBackendFactory, httpBackend) |
|
67 |
#endif // QT_NO_HTTP |
|
68 |
Q_GLOBAL_STATIC(QNetworkAccessFileBackendFactory, fileBackend) |
|
69 |
Q_GLOBAL_STATIC(QNetworkAccessDataBackendFactory, dataBackend) |
|
70 |
#ifndef QT_NO_FTP |
|
71 |
Q_GLOBAL_STATIC(QNetworkAccessFtpBackendFactory, ftpBackend) |
|
72 |
#endif // QT_NO_FTP |
|
73 |
||
74 |
#ifdef QT_BUILD_INTERNAL |
|
75 |
Q_GLOBAL_STATIC(QNetworkAccessDebugPipeBackendFactory, debugpipeBackend) |
|
76 |
#endif |
|
77 |
||
78 |
static void ensureInitialized() |
|
79 |
{ |
|
80 |
#ifndef QT_NO_HTTP |
|
81 |
(void) httpBackend(); |
|
82 |
#endif // QT_NO_HTTP |
|
83 |
(void) dataBackend(); |
|
84 |
#ifndef QT_NO_FTP |
|
85 |
(void) ftpBackend(); |
|
86 |
#endif |
|
87 |
||
88 |
#ifdef QT_BUILD_INTERNAL |
|
89 |
(void) debugpipeBackend(); |
|
90 |
#endif |
|
91 |
||
92 |
// leave this one last since it will query the special QAbstractFileEngines |
|
93 |
(void) fileBackend(); |
|
94 |
} |
|
95 |
||
96 |
/*! |
|
97 |
\class QNetworkAccessManager |
|
98 |
\brief The QNetworkAccessManager class allows the application to |
|
99 |
send network requests and receive replies |
|
100 |
\since 4.4 |
|
101 |
||
102 |
\ingroup network |
|
103 |
\inmodule QtNetwork |
|
104 |
\reentrant |
|
105 |
||
106 |
The Network Access API is constructed around one QNetworkAccessManager |
|
107 |
object, which holds the common configuration and settings for the requests |
|
108 |
it sends. It contains the proxy and cache configuration, as well as the |
|
109 |
signals related to such issues, and reply signals that can be used to |
|
110 |
monitor the progress of a network operation. |
|
111 |
||
112 |
Once a QNetworkAccessManager object has been created, the application can |
|
113 |
use it to send requests over the network. A group of standard functions |
|
114 |
are supplied that take a request and optional data, and each return a |
|
115 |
QNetworkReply object. The returned object is used to obtain any data |
|
116 |
returned in response to the corresponding request. |
|
117 |
||
118 |
A simple download off the network could be accomplished with: |
|
119 |
\snippet doc/src/snippets/code/src_network_access_qnetworkaccessmanager.cpp 0 |
|
120 |
||
121 |
When the \tt replyFinished slot above is called, the parameter it |
|
122 |
takes is the QNetworkReply object containing the downloaded data |
|
123 |
as well as meta-data (headers, etc.). |
|
124 |
||
125 |
\note After the request has finished, it is the responsibility of the user |
|
126 |
to delete the QNetworkReply object at an appropriate time. Do not directly |
|
127 |
delete it inside the slot connected to finished(). You can use the |
|
128 |
deleteLater() function. |
|
129 |
||
130 |
A more involved example, assuming the manager is already existent, |
|
131 |
can be: |
|
132 |
\snippet doc/src/snippets/code/src_network_access_qnetworkaccessmanager.cpp 1 |
|
133 |
||
134 |
\sa QNetworkRequest, QNetworkReply, QNetworkProxy |
|
135 |
*/ |
|
136 |
||
137 |
/*! |
|
138 |
\enum QNetworkAccessManager::Operation |
|
139 |
||
140 |
Indicates the operation this reply is processing. |
|
141 |
||
142 |
\value HeadOperation retrieve headers operation (created |
|
143 |
with head()) |
|
144 |
||
145 |
\value GetOperation retrieve headers and download contents |
|
146 |
(created with get()) |
|
147 |
||
148 |
\value PutOperation upload contents operation (created |
|
149 |
with put()) |
|
150 |
||
151 |
\value PostOperation send the contents of an HTML form for |
|
152 |
processing via HTTP POST (created with post()) |
|
153 |
||
154 |
\value DeleteOperation delete contents operation (created with |
|
155 |
deleteResource()) |
|
156 |
||
157 |
\omitvalue UnknownOperation |
|
158 |
||
159 |
\sa QNetworkReply::operation() |
|
160 |
*/ |
|
161 |
||
162 |
/*! |
|
163 |
\fn void QNetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) |
|
164 |
||
165 |
This signal is emitted whenever a proxy requests authentication |
|
166 |
and QNetworkAccessManager cannot find a valid, cached |
|
167 |
credential. The slot connected to this signal should fill in the |
|
168 |
credentials for the proxy \a proxy in the \a authenticator object. |
|
169 |
||
170 |
QNetworkAccessManager will cache the credentials internally. The |
|
171 |
next time the proxy requests authentication, QNetworkAccessManager |
|
172 |
will automatically send the same credential without emitting the |
|
173 |
proxyAuthenticationRequired signal again. |
|
174 |
||
175 |
If the proxy rejects the credentials, QNetworkAccessManager will |
|
176 |
emit the signal again. |
|
177 |
||
178 |
\sa proxy(), setProxy(), authenticationRequired() |
|
179 |
*/ |
|
180 |
||
181 |
/*! |
|
182 |
\fn void QNetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator) |
|
183 |
||
184 |
This signal is emitted whenever a final server requests |
|
185 |
authentication before it delivers the requested contents. The slot |
|
186 |
connected to this signal should fill the credentials for the |
|
187 |
contents (which can be determined by inspecting the \a reply |
|
188 |
object) in the \a authenticator object. |
|
189 |
||
190 |
QNetworkAccessManager will cache the credentials internally and |
|
191 |
will send the same values if the server requires authentication |
|
192 |
again, without emitting the authenticationRequired() signal. If it |
|
193 |
rejects the credentials, this signal will be emitted again. |
|
194 |
||
195 |
\sa proxyAuthenticationRequired() |
|
196 |
*/ |
|
197 |
||
198 |
/*! |
|
199 |
\fn void QNetworkAccessManager::finished(QNetworkReply *reply) |
|
200 |
||
201 |
This signal is emitted whenever a pending network reply is |
|
202 |
finished. The \a reply parameter will contain a pointer to the |
|
203 |
reply that has just finished. This signal is emitted in tandem |
|
204 |
with the QNetworkReply::finished() signal. |
|
205 |
||
206 |
See QNetworkReply::finished() for information on the status that |
|
207 |
the object will be in. |
|
208 |
||
209 |
\note Do not delete the \a reply object in the slot connected to this |
|
210 |
signal. Use deleteLater(). |
|
211 |
||
212 |
\sa QNetworkReply::finished(), QNetworkReply::error() |
|
213 |
*/ |
|
214 |
||
215 |
/*! |
|
216 |
\fn void QNetworkAccessManager::sslErrors(QNetworkReply *reply, const QList<QSslError> &errors) |
|
217 |
||
218 |
This signal is emitted if the SSL/TLS session encountered errors |
|
219 |
during the set up, including certificate verification errors. The |
|
220 |
\a errors parameter contains the list of errors and \a reply is |
|
221 |
the QNetworkReply that is encountering these errors. |
|
222 |
||
223 |
To indicate that the errors are not fatal and that the connection |
|
224 |
should proceed, the QNetworkReply::ignoreSslErrors() function should be called |
|
225 |
from the slot connected to this signal. If it is not called, the |
|
226 |
SSL session will be torn down before any data is exchanged |
|
227 |
(including the URL). |
|
228 |
||
229 |
This signal can be used to display an error message to the user |
|
230 |
indicating that security may be compromised and display the |
|
231 |
SSL settings (see sslConfiguration() to obtain it). If the user |
|
232 |
decides to proceed after analyzing the remote certificate, the |
|
233 |
slot should call ignoreSslErrors(). |
|
234 |
||
235 |
\sa QSslSocket::sslErrors(), QNetworkReply::sslErrors(), |
|
236 |
QNetworkReply::sslConfiguration(), QNetworkReply::ignoreSslErrors() |
|
237 |
*/ |
|
238 |
||
239 |
class QNetworkAuthenticationCredential |
|
240 |
{ |
|
241 |
public: |
|
242 |
QString domain; |
|
243 |
QString user; |
|
244 |
QString password; |
|
245 |
}; |
|
246 |
Q_DECLARE_TYPEINFO(QNetworkAuthenticationCredential, Q_MOVABLE_TYPE); |
|
247 |
inline bool operator<(const QNetworkAuthenticationCredential &t1, const QString &t2) |
|
248 |
{ return t1.domain < t2; } |
|
249 |
||
250 |
class QNetworkAuthenticationCache: private QVector<QNetworkAuthenticationCredential>, |
|
251 |
public QNetworkAccessCache::CacheableObject |
|
252 |
{ |
|
253 |
public: |
|
254 |
QNetworkAuthenticationCache() |
|
255 |
{ |
|
256 |
setExpires(false); |
|
257 |
setShareable(true); |
|
258 |
reserve(1); |
|
259 |
} |
|
260 |
||
261 |
QNetworkAuthenticationCredential *findClosestMatch(const QString &domain) |
|
262 |
{ |
|
263 |
iterator it = qLowerBound(begin(), end(), domain); |
|
264 |
if (it == end() && !isEmpty()) |
|
265 |
--it; |
|
266 |
if (it == end() || !domain.startsWith(it->domain)) |
|
267 |
return 0; |
|
268 |
return &*it; |
|
269 |
} |
|
270 |
||
271 |
void insert(const QString &domain, const QString &user, const QString &password) |
|
272 |
{ |
|
273 |
QNetworkAuthenticationCredential *closestMatch = findClosestMatch(domain); |
|
274 |
if (closestMatch && closestMatch->domain == domain) { |
|
275 |
// we're overriding the current credentials |
|
276 |
closestMatch->user = user; |
|
277 |
closestMatch->password = password; |
|
278 |
} else { |
|
279 |
QNetworkAuthenticationCredential newCredential; |
|
280 |
newCredential.domain = domain; |
|
281 |
newCredential.user = user; |
|
282 |
newCredential.password = password; |
|
283 |
||
284 |
if (closestMatch) |
|
285 |
QVector<QNetworkAuthenticationCredential>::insert(++closestMatch, newCredential); |
|
286 |
else |
|
287 |
QVector<QNetworkAuthenticationCredential>::insert(end(), newCredential); |
|
288 |
} |
|
289 |
} |
|
290 |
||
291 |
virtual void dispose() { delete this; } |
|
292 |
}; |
|
293 |
||
294 |
#ifndef QT_NO_NETWORKPROXY |
|
295 |
static QByteArray proxyAuthenticationKey(const QNetworkProxy &proxy, const QString &realm) |
|
296 |
{ |
|
297 |
QUrl key; |
|
298 |
||
299 |
switch (proxy.type()) { |
|
300 |
case QNetworkProxy::Socks5Proxy: |
|
301 |
key.setScheme(QLatin1String("proxy-socks5")); |
|
302 |
break; |
|
303 |
||
304 |
case QNetworkProxy::HttpProxy: |
|
305 |
case QNetworkProxy::HttpCachingProxy: |
|
306 |
key.setScheme(QLatin1String("proxy-http")); |
|
307 |
break; |
|
308 |
||
309 |
case QNetworkProxy::FtpCachingProxy: |
|
310 |
key.setScheme(QLatin1String("proxy-ftp")); |
|
311 |
break; |
|
312 |
||
313 |
case QNetworkProxy::DefaultProxy: |
|
314 |
case QNetworkProxy::NoProxy: |
|
315 |
// shouldn't happen |
|
316 |
return QByteArray(); |
|
317 |
||
318 |
// no default: |
|
319 |
// let there be errors if a new proxy type is added in the future |
|
320 |
} |
|
321 |
||
322 |
if (key.scheme().isEmpty()) |
|
323 |
// proxy type not handled |
|
324 |
return QByteArray(); |
|
325 |
||
326 |
key.setUserName(proxy.user()); |
|
327 |
key.setHost(proxy.hostName()); |
|
328 |
key.setPort(proxy.port()); |
|
329 |
key.setFragment(realm); |
|
330 |
return "auth:" + key.toEncoded(); |
|
331 |
} |
|
332 |
#endif |
|
333 |
||
334 |
static inline QByteArray authenticationKey(const QUrl &url, const QString &realm) |
|
335 |
{ |
|
336 |
QUrl copy = url; |
|
337 |
copy.setFragment(realm); |
|
338 |
return "auth:" + copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery); |
|
339 |
} |
|
340 |
||
341 |
/*! |
|
342 |
Constructs a QNetworkAccessManager object that is the center of |
|
343 |
the Network Access API and sets \a parent as the parent object. |
|
344 |
*/ |
|
345 |
QNetworkAccessManager::QNetworkAccessManager(QObject *parent) |
|
346 |
: QObject(*new QNetworkAccessManagerPrivate, parent) |
|
347 |
{ |
|
348 |
ensureInitialized(); |
|
349 |
} |
|
350 |
||
351 |
/*! |
|
352 |
Destroys the QNetworkAccessManager object and frees up any |
|
353 |
resources. Note that QNetworkReply objects that are returned from |
|
354 |
this class have this object set as their parents, which means that |
|
355 |
they will be deleted along with it if you don't call |
|
356 |
QObject::setParent() on them. |
|
357 |
*/ |
|
358 |
QNetworkAccessManager::~QNetworkAccessManager() |
|
359 |
{ |
|
360 |
#ifndef QT_NO_NETWORKPROXY |
|
361 |
delete d_func()->proxyFactory; |
|
362 |
#endif |
|
363 |
} |
|
364 |
||
365 |
#ifndef QT_NO_NETWORKPROXY |
|
366 |
/*! |
|
367 |
Returns the QNetworkProxy that the requests sent using this |
|
368 |
QNetworkAccessManager object will use. The default value for the |
|
369 |
proxy is QNetworkProxy::DefaultProxy. |
|
370 |
||
371 |
\sa setProxy(), setProxyFactory(), proxyAuthenticationRequired() |
|
372 |
*/ |
|
373 |
QNetworkProxy QNetworkAccessManager::proxy() const |
|
374 |
{ |
|
375 |
return d_func()->proxy; |
|
376 |
} |
|
377 |
||
378 |
/*! |
|
379 |
Sets the proxy to be used in future requests to be \a proxy. This |
|
380 |
does not affect requests that have already been sent. The |
|
381 |
proxyAuthenticationRequired() signal will be emitted if the proxy |
|
382 |
requests authentication. |
|
383 |
||
384 |
A proxy set with this function will be used for all requests |
|
385 |
issued by QNetworkAccessManager. In some cases, it might be |
|
386 |
necessary to select different proxies depending on the type of |
|
387 |
request being sent or the destination host. If that's the case, |
|
388 |
you should consider using setProxyFactory(). |
|
389 |
||
390 |
\sa proxy(), proxyAuthenticationRequired() |
|
391 |
*/ |
|
392 |
void QNetworkAccessManager::setProxy(const QNetworkProxy &proxy) |
|
393 |
{ |
|
394 |
Q_D(QNetworkAccessManager); |
|
395 |
delete d->proxyFactory; |
|
396 |
d->proxy = proxy; |
|
397 |
d->proxyFactory = 0; |
|
398 |
} |
|
399 |
||
400 |
/*! |
|
401 |
\fn QNetworkProxyFactory *QNetworkAccessManager::proxyFactory() const |
|
402 |
\since 4.5 |
|
403 |
||
404 |
Returns the proxy factory that this QNetworkAccessManager object |
|
405 |
is using to determine the proxies to be used for requests. |
|
406 |
||
407 |
Note that the pointer returned by this function is managed by |
|
408 |
QNetworkAccessManager and could be deleted at any time. |
|
409 |
||
410 |
\sa setProxyFactory(), proxy() |
|
411 |
*/ |
|
412 |
QNetworkProxyFactory *QNetworkAccessManager::proxyFactory() const |
|
413 |
{ |
|
414 |
return d_func()->proxyFactory; |
|
415 |
} |
|
416 |
||
417 |
/*! |
|
418 |
\since 4.5 |
|
419 |
||
420 |
Sets the proxy factory for this class to be \a factory. A proxy |
|
421 |
factory is used to determine a more specific list of proxies to be |
|
422 |
used for a given request, instead of trying to use the same proxy |
|
423 |
value for all requests. |
|
424 |
||
425 |
All queries sent by QNetworkAccessManager will have type |
|
426 |
QNetworkProxyQuery::UrlRequest. |
|
427 |
||
428 |
For example, a proxy factory could apply the following rules: |
|
429 |
\list |
|
430 |
\o if the target address is in the local network (for example, |
|
431 |
if the hostname contains no dots or if it's an IP address in |
|
432 |
the organization's range), return QNetworkProxy::NoProxy |
|
433 |
\o if the request is FTP, return an FTP proxy |
|
434 |
\o if the request is HTTP or HTTPS, then return an HTTP proxy |
|
435 |
\o otherwise, return a SOCKSv5 proxy server |
|
436 |
\endlist |
|
437 |
||
438 |
The lifetime of the object \a factory will be managed by |
|
439 |
QNetworkAccessManager. It will delete the object when necessary. |
|
440 |
||
441 |
\note If a specific proxy is set with setProxy(), the factory will not |
|
442 |
be used. |
|
443 |
||
444 |
\sa proxyFactory(), setProxy(), QNetworkProxyQuery |
|
445 |
*/ |
|
446 |
void QNetworkAccessManager::setProxyFactory(QNetworkProxyFactory *factory) |
|
447 |
{ |
|
448 |
Q_D(QNetworkAccessManager); |
|
449 |
delete d->proxyFactory; |
|
450 |
d->proxyFactory = factory; |
|
451 |
d->proxy = QNetworkProxy(); |
|
452 |
} |
|
453 |
#endif |
|
454 |
||
455 |
/*! |
|
456 |
\since 4.5 |
|
457 |
||
458 |
Returns the cache that is used to store data obtained from the network. |
|
459 |
||
460 |
\sa setCache() |
|
461 |
*/ |
|
462 |
QAbstractNetworkCache *QNetworkAccessManager::cache() const |
|
463 |
{ |
|
464 |
Q_D(const QNetworkAccessManager); |
|
465 |
return d->networkCache; |
|
466 |
} |
|
467 |
||
468 |
/*! |
|
469 |
\since 4.5 |
|
470 |
||
471 |
Sets the manager's network cache to be the \a cache specified. The cache |
|
472 |
is used for all requests dispatched by the manager. |
|
473 |
||
474 |
Use this function to set the network cache object to a class that implements |
|
475 |
additional features, like saving the cookies to permanent storage. |
|
476 |
||
477 |
\note QNetworkAccessManager takes ownership of the \a cache object. |
|
478 |
||
479 |
QNetworkAccessManager by default does not have a set cache. |
|
480 |
Qt provides a simple disk cache, QNetworkDiskCache, which can be used. |
|
481 |
||
482 |
\sa cache(), QNetworkRequest::CacheLoadControl |
|
483 |
*/ |
|
484 |
void QNetworkAccessManager::setCache(QAbstractNetworkCache *cache) |
|
485 |
{ |
|
486 |
Q_D(QNetworkAccessManager); |
|
487 |
if (d->networkCache != cache) { |
|
488 |
delete d->networkCache; |
|
489 |
d->networkCache = cache; |
|
490 |
if (d->networkCache) |
|
491 |
d->networkCache->setParent(this); |
|
492 |
} |
|
493 |
} |
|
494 |
||
495 |
/*! |
|
496 |
Returns the QNetworkCookieJar that is used to store cookies |
|
497 |
obtained from the network as well as cookies that are about to be |
|
498 |
sent. |
|
499 |
||
500 |
\sa setCookieJar() |
|
501 |
*/ |
|
502 |
QNetworkCookieJar *QNetworkAccessManager::cookieJar() const |
|
503 |
{ |
|
504 |
Q_D(const QNetworkAccessManager); |
|
505 |
if (!d->cookieJar) |
|
506 |
d->createCookieJar(); |
|
507 |
return d->cookieJar; |
|
508 |
} |
|
509 |
||
510 |
/*! |
|
511 |
Sets the manager's cookie jar to be the \a cookieJar specified. |
|
512 |
The cookie jar is used by all requests dispatched by the manager. |
|
513 |
||
514 |
Use this function to set the cookie jar object to a class that |
|
515 |
implements additional features, like saving the cookies to permanent |
|
516 |
storage. |
|
517 |
||
518 |
\note QNetworkAccessManager takes ownership of the \a cookieJar object. |
|
519 |
||
520 |
QNetworkAccessManager will set the parent of the \a cookieJar |
|
521 |
passed to itself, so that the cookie jar is deleted when this |
|
522 |
object is deleted as well. If you want to share cookie jars |
|
523 |
between different QNetworkAccessManager objects, you may want to |
|
524 |
set the cookie jar's parent to 0 after calling this function. |
|
525 |
||
526 |
QNetworkAccessManager by default does not implement any cookie |
|
527 |
policy of its own: it accepts all cookies sent by the server, as |
|
528 |
long as they are well formed and meet the minimum security |
|
529 |
requirements (cookie domain matches the request's and cookie path |
|
530 |
matches the request's). In order to implement your own security |
|
531 |
policy, override the QNetworkCookieJar::cookiesForUrl() and |
|
532 |
QNetworkCookieJar::setCookiesFromUrl() virtual functions. Those |
|
533 |
functions are called by QNetworkAccessManager when it detects a |
|
534 |
new cookie. |
|
535 |
||
536 |
\sa cookieJar(), QNetworkCookieJar::cookiesForUrl(), QNetworkCookieJar::setCookiesFromUrl() |
|
537 |
*/ |
|
538 |
void QNetworkAccessManager::setCookieJar(QNetworkCookieJar *cookieJar) |
|
539 |
{ |
|
540 |
Q_D(QNetworkAccessManager); |
|
541 |
d->cookieJarCreated = true; |
|
542 |
if (d->cookieJar != cookieJar) { |
|
543 |
if (d->cookieJar && d->cookieJar->parent() == this) |
|
544 |
delete d->cookieJar; |
|
545 |
d->cookieJar = cookieJar; |
|
546 |
d->cookieJar->setParent(this); |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
/*! |
|
551 |
Posts a request to obtain the network headers for \a request |
|
552 |
and returns a new QNetworkReply object which will contain such headers |
|
553 |
||
554 |
The function is named after the HTTP request associated (HEAD). |
|
555 |
*/ |
|
556 |
QNetworkReply *QNetworkAccessManager::head(const QNetworkRequest &request) |
|
557 |
{ |
|
558 |
return d_func()->postProcess(createRequest(QNetworkAccessManager::HeadOperation, request)); |
|
559 |
} |
|
560 |
||
561 |
/*! |
|
562 |
Posts a request to obtain the contents of the target \a request |
|
563 |
and returns a new QNetworkReply object opened for reading which emits the |
|
564 |
\l{QIODevice::readyRead()}{readyRead()} signal whenever new data |
|
565 |
arrives. |
|
566 |
||
567 |
The contents as well as associated headers will be downloaded. |
|
568 |
||
569 |
\sa post(), put(), deleteResource() |
|
570 |
*/ |
|
571 |
QNetworkReply *QNetworkAccessManager::get(const QNetworkRequest &request) |
|
572 |
{ |
|
573 |
return d_func()->postProcess(createRequest(QNetworkAccessManager::GetOperation, request)); |
|
574 |
} |
|
575 |
||
576 |
/*! |
|
577 |
Sends an HTTP POST request to the destination specified by \a request |
|
578 |
and returns a new QNetworkReply object opened for reading that will |
|
579 |
contain the reply sent by the server. The contents of the \a data |
|
580 |
device will be uploaded to the server. |
|
581 |
||
582 |
\a data must be open for reading and must remain valid until the |
|
583 |
finished() signal is emitted for this reply. |
|
584 |
||
585 |
\note Sending a POST request on protocols other than HTTP and |
|
586 |
HTTPS is undefined and will probably fail. |
|
587 |
||
588 |
\sa get(), put(), deleteResource() |
|
589 |
*/ |
|
590 |
QNetworkReply *QNetworkAccessManager::post(const QNetworkRequest &request, QIODevice *data) |
|
591 |
{ |
|
592 |
return d_func()->postProcess(createRequest(QNetworkAccessManager::PostOperation, request, data)); |
|
593 |
} |
|
594 |
||
595 |
/*! |
|
596 |
\overload |
|
597 |
||
598 |
Sends the contents of the \a data byte array to the destination |
|
599 |
specified by \a request. |
|
600 |
*/ |
|
601 |
QNetworkReply *QNetworkAccessManager::post(const QNetworkRequest &request, const QByteArray &data) |
|
602 |
{ |
|
603 |
QBuffer *buffer = new QBuffer; |
|
604 |
buffer->setData(data); |
|
605 |
buffer->open(QIODevice::ReadOnly); |
|
606 |
||
607 |
QNetworkReply *reply = post(request, buffer); |
|
608 |
buffer->setParent(reply); |
|
609 |
return reply; |
|
610 |
} |
|
611 |
||
612 |
/*! |
|
613 |
Uploads the contents of \a data to the destination \a request and |
|
614 |
returnes a new QNetworkReply object that will be open for reply. |
|
615 |
||
616 |
\a data must be opened for reading when this function is called |
|
617 |
and must remain valid until the finished() signal is emitted for |
|
618 |
this reply. |
|
619 |
||
620 |
Whether anything will be available for reading from the returned |
|
621 |
object is protocol dependent. For HTTP, the server may send a |
|
622 |
small HTML page indicating the upload was successful (or not). |
|
623 |
Other protocols will probably have content in their replies. |
|
624 |
||
625 |
\note For HTTP, this request will send a PUT request, which most servers |
|
626 |
do not allow. Form upload mechanisms, including that of uploading |
|
627 |
files through HTML forms, use the POST mechanism. |
|
628 |
||
629 |
\sa get(), post() |
|
630 |
*/ |
|
631 |
QNetworkReply *QNetworkAccessManager::put(const QNetworkRequest &request, QIODevice *data) |
|
632 |
{ |
|
633 |
return d_func()->postProcess(createRequest(QNetworkAccessManager::PutOperation, request, data)); |
|
634 |
} |
|
635 |
||
636 |
/*! |
|
637 |
\overload |
|
638 |
Sends the contents of the \a data byte array to the destination |
|
639 |
specified by \a request. |
|
640 |
*/ |
|
641 |
QNetworkReply *QNetworkAccessManager::put(const QNetworkRequest &request, const QByteArray &data) |
|
642 |
{ |
|
643 |
QBuffer *buffer = new QBuffer; |
|
644 |
buffer->setData(data); |
|
645 |
buffer->open(QIODevice::ReadOnly); |
|
646 |
||
647 |
QNetworkReply *reply = put(request, buffer); |
|
648 |
buffer->setParent(reply); |
|
649 |
return reply; |
|
650 |
} |
|
651 |
||
652 |
/*! |
|
653 |
\since 4.6 |
|
654 |
||
655 |
Sends a request to delete the resource identified by the URL of \a request. |
|
656 |
||
657 |
\note This feature is currently available for HTTP only, performing an |
|
658 |
HTTP DELETE request. |
|
659 |
||
660 |
\sa get(), post(), put() |
|
661 |
*/ |
|
662 |
QNetworkReply *QNetworkAccessManager::deleteResource(const QNetworkRequest &request) |
|
663 |
{ |
|
664 |
return d_func()->postProcess(createRequest(QNetworkAccessManager::DeleteOperation, request)); |
|
665 |
} |
|
666 |
||
667 |
/*! |
|
668 |
Returns a new QNetworkReply object to handle the operation \a op |
|
669 |
and request \a req. The device \a outgoingData is always 0 for Get and |
|
670 |
Head requests, but is the value passed to post() and put() in |
|
671 |
those operations (the QByteArray variants will pass a QBuffer |
|
672 |
object). |
|
673 |
||
674 |
The default implementation calls QNetworkCookieJar::cookiesForUrl() |
|
675 |
on the cookie jar set with setCookieJar() to obtain the cookies to |
|
676 |
be sent to the remote server. |
|
677 |
||
678 |
The returned object must be in an open state. |
|
679 |
*/ |
|
680 |
QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Operation op, |
|
681 |
const QNetworkRequest &req, |
|
682 |
QIODevice *outgoingData) |
|
683 |
{ |
|
684 |
Q_D(QNetworkAccessManager); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
685 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
686 |
// fast path for GET on file:// URLs |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
687 |
// Also if the scheme is empty we consider it a file. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
688 |
// The QNetworkAccessFileBackend will right now only be used |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
689 |
// for PUT or qrc:// |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
690 |
if ((op == QNetworkAccessManager::GetOperation || op == QNetworkAccessManager::HeadOperation) |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
691 |
&& (req.url().scheme() == QLatin1String("file") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
692 |
|| req.url().scheme().isEmpty())) { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
693 |
return new QFileNetworkReply(this, req, op); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
694 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
695 |
|
0 | 696 |
QNetworkRequest request = req; |
697 |
if (!request.header(QNetworkRequest::ContentLengthHeader).isValid() && |
|
698 |
outgoingData && !outgoingData->isSequential()) { |
|
699 |
// request has no Content-Length |
|
700 |
// but the data that is outgoing is random-access |
|
701 |
request.setHeader(QNetworkRequest::ContentLengthHeader, outgoingData->size()); |
|
702 |
} |
|
703 |
if (d->cookieJar) { |
|
704 |
QList<QNetworkCookie> cookies = d->cookieJar->cookiesForUrl(request.url()); |
|
705 |
if (!cookies.isEmpty()) |
|
706 |
request.setHeader(QNetworkRequest::CookieHeader, qVariantFromValue(cookies)); |
|
707 |
} |
|
708 |
||
709 |
// first step: create the reply |
|
710 |
QUrl url = request.url(); |
|
711 |
QNetworkReplyImpl *reply = new QNetworkReplyImpl(this); |
|
712 |
QNetworkReplyImplPrivate *priv = reply->d_func(); |
|
713 |
priv->manager = this; |
|
714 |
||
715 |
// second step: fetch cached credentials |
|
716 |
QNetworkAuthenticationCredential *cred = d->fetchCachedCredentials(url); |
|
717 |
if (cred) { |
|
718 |
url.setUserName(cred->user); |
|
719 |
url.setPassword(cred->password); |
|
720 |
priv->urlForLastAuthentication = url; |
|
721 |
} |
|
722 |
||
723 |
// third step: find a backend |
|
724 |
priv->backend = d->findBackend(op, request); |
|
725 |
||
726 |
#ifndef QT_NO_NETWORKPROXY |
|
727 |
QList<QNetworkProxy> proxyList = d->queryProxy(QNetworkProxyQuery(request.url())); |
|
728 |
priv->proxyList = proxyList; |
|
729 |
#endif |
|
730 |
if (priv->backend) { |
|
731 |
priv->backend->setParent(reply); |
|
732 |
priv->backend->reply = priv; |
|
733 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
734 |
// fourth step: setup the reply |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
735 |
priv->setup(op, request, outgoingData); |
0 | 736 |
|
737 |
#ifndef QT_NO_OPENSSL |
|
738 |
reply->setSslConfiguration(request.sslConfiguration()); |
|
739 |
#endif |
|
740 |
return reply; |
|
741 |
} |
|
742 |
||
743 |
void QNetworkAccessManagerPrivate::_q_replyFinished() |
|
744 |
{ |
|
745 |
Q_Q(QNetworkAccessManager); |
|
746 |
QNetworkReply *reply = qobject_cast<QNetworkReply *>(q->sender()); |
|
747 |
if (reply) |
|
748 |
emit q->finished(reply); |
|
749 |
} |
|
750 |
||
751 |
void QNetworkAccessManagerPrivate::_q_replySslErrors(const QList<QSslError> &errors) |
|
752 |
{ |
|
753 |
#ifndef QT_NO_OPENSSL |
|
754 |
Q_Q(QNetworkAccessManager); |
|
755 |
QNetworkReply *reply = qobject_cast<QNetworkReply *>(q->sender()); |
|
756 |
if (reply) |
|
757 |
emit q->sslErrors(reply, errors); |
|
758 |
#else |
|
759 |
Q_UNUSED(errors); |
|
760 |
#endif |
|
761 |
} |
|
762 |
||
763 |
QNetworkReply *QNetworkAccessManagerPrivate::postProcess(QNetworkReply *reply) |
|
764 |
{ |
|
765 |
Q_Q(QNetworkAccessManager); |
|
766 |
QNetworkReplyPrivate::setManager(reply, q); |
|
767 |
q->connect(reply, SIGNAL(finished()), SLOT(_q_replyFinished())); |
|
768 |
#ifndef QT_NO_OPENSSL |
|
769 |
/* In case we're compiled without SSL support, we don't have this signal and we need to |
|
770 |
* avoid getting a connection error. */ |
|
771 |
q->connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(_q_replySslErrors(QList<QSslError>))); |
|
772 |
#endif |
|
773 |
||
774 |
return reply; |
|
775 |
} |
|
776 |
||
777 |
void QNetworkAccessManagerPrivate::createCookieJar() const |
|
778 |
{ |
|
779 |
if (!cookieJarCreated) { |
|
780 |
// keep the ugly hack in here |
|
781 |
QNetworkAccessManagerPrivate *that = const_cast<QNetworkAccessManagerPrivate *>(this); |
|
782 |
that->cookieJar = new QNetworkCookieJar(that->q_func()); |
|
783 |
that->cookieJarCreated = true; |
|
784 |
} |
|
785 |
} |
|
786 |
||
787 |
void QNetworkAccessManagerPrivate::authenticationRequired(QNetworkAccessBackend *backend, |
|
788 |
QAuthenticator *authenticator) |
|
789 |
{ |
|
790 |
Q_Q(QNetworkAccessManager); |
|
791 |
||
792 |
// FIXME: Add support for domains (i.e., the leading path) |
|
793 |
QUrl url = backend->reply->url; |
|
794 |
||
795 |
// don't try the cache for the same URL twice in a row |
|
796 |
// being called twice for the same URL means the authentication failed |
|
797 |
if (url != backend->reply->urlForLastAuthentication) { |
|
798 |
QNetworkAuthenticationCredential *cred = fetchCachedCredentials(url, authenticator); |
|
799 |
if (cred) { |
|
800 |
authenticator->setUser(cred->user); |
|
801 |
authenticator->setPassword(cred->password); |
|
802 |
backend->reply->urlForLastAuthentication = url; |
|
803 |
return; |
|
804 |
} |
|
805 |
} |
|
806 |
||
807 |
backend->reply->urlForLastAuthentication = url; |
|
808 |
emit q->authenticationRequired(backend->reply->q_func(), authenticator); |
|
809 |
addCredentials(url, authenticator); |
|
810 |
} |
|
811 |
||
812 |
#ifndef QT_NO_NETWORKPROXY |
|
813 |
void QNetworkAccessManagerPrivate::proxyAuthenticationRequired(QNetworkAccessBackend *backend, |
|
814 |
const QNetworkProxy &proxy, |
|
815 |
QAuthenticator *authenticator) |
|
816 |
{ |
|
817 |
Q_Q(QNetworkAccessManager); |
|
818 |
// ### FIXME Tracking of successful authentications |
|
819 |
// This code is a bit broken right now for SOCKS authentication |
|
820 |
// first request: proxyAuthenticationRequired gets emitted, credentials gets saved |
|
821 |
// second request: (proxy != backend->reply->lastProxyAuthentication) does not evaluate to true, |
|
822 |
// proxyAuthenticationRequired gets emitted again |
|
823 |
// possible solution: some tracking inside the authenticator |
|
824 |
// or a new function proxyAuthenticationSucceeded(true|false) |
|
825 |
if (proxy != backend->reply->lastProxyAuthentication) { |
|
826 |
QNetworkAuthenticationCredential *cred = fetchCachedCredentials(proxy); |
|
827 |
if (cred) { |
|
828 |
authenticator->setUser(cred->user); |
|
829 |
authenticator->setPassword(cred->password); |
|
830 |
return; |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
backend->reply->lastProxyAuthentication = proxy; |
|
835 |
emit q->proxyAuthenticationRequired(proxy, authenticator); |
|
836 |
addCredentials(proxy, authenticator); |
|
837 |
} |
|
838 |
||
839 |
void QNetworkAccessManagerPrivate::addCredentials(const QNetworkProxy &p, |
|
840 |
const QAuthenticator *authenticator) |
|
841 |
{ |
|
842 |
Q_ASSERT(authenticator); |
|
843 |
Q_ASSERT(p.type() != QNetworkProxy::DefaultProxy); |
|
844 |
Q_ASSERT(p.type() != QNetworkProxy::NoProxy); |
|
845 |
||
846 |
QString realm = authenticator->realm(); |
|
847 |
QNetworkProxy proxy = p; |
|
848 |
proxy.setUser(authenticator->user()); |
|
849 |
// Set two credentials: one with the username and one without |
|
850 |
do { |
|
851 |
// Set two credentials actually: one with and one without the realm |
|
852 |
do { |
|
853 |
QByteArray cacheKey = proxyAuthenticationKey(proxy, realm); |
|
854 |
if (cacheKey.isEmpty()) |
|
855 |
return; // should not happen |
|
856 |
||
857 |
QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache; |
|
858 |
auth->insert(QString(), authenticator->user(), authenticator->password()); |
|
859 |
objectCache.addEntry(cacheKey, auth); // replace the existing one, if there's any |
|
860 |
||
861 |
if (realm.isEmpty()) { |
|
862 |
break; |
|
863 |
} else { |
|
864 |
realm.clear(); |
|
865 |
} |
|
866 |
} while (true); |
|
867 |
||
868 |
if (proxy.user().isEmpty()) |
|
869 |
break; |
|
870 |
else |
|
871 |
proxy.setUser(QString()); |
|
872 |
} while (true); |
|
873 |
} |
|
874 |
||
875 |
QNetworkAuthenticationCredential * |
|
876 |
QNetworkAccessManagerPrivate::fetchCachedCredentials(const QNetworkProxy &p, |
|
877 |
const QAuthenticator *authenticator) |
|
878 |
{ |
|
879 |
QNetworkProxy proxy = p; |
|
880 |
if (proxy.type() == QNetworkProxy::DefaultProxy) { |
|
881 |
proxy = QNetworkProxy::applicationProxy(); |
|
882 |
} |
|
883 |
if (!proxy.password().isEmpty()) |
|
884 |
return 0; // no need to set credentials if it already has them |
|
885 |
||
886 |
QString realm; |
|
887 |
if (authenticator) |
|
888 |
realm = authenticator->realm(); |
|
889 |
||
890 |
QByteArray cacheKey = proxyAuthenticationKey(proxy, realm); |
|
891 |
if (cacheKey.isEmpty()) |
|
892 |
return 0; |
|
893 |
if (!objectCache.hasEntry(cacheKey)) |
|
894 |
return 0; |
|
895 |
||
896 |
QNetworkAuthenticationCache *auth = |
|
897 |
static_cast<QNetworkAuthenticationCache *>(objectCache.requestEntryNow(cacheKey)); |
|
898 |
QNetworkAuthenticationCredential *cred = auth->findClosestMatch(QString()); |
|
899 |
objectCache.releaseEntry(cacheKey); |
|
900 |
||
901 |
// proxy cache credentials always have exactly one item |
|
902 |
Q_ASSERT_X(cred, "QNetworkAccessManager", |
|
903 |
"Internal inconsistency: found a cache key for a proxy, but it's empty"); |
|
904 |
return cred; |
|
905 |
} |
|
906 |
||
907 |
QList<QNetworkProxy> QNetworkAccessManagerPrivate::queryProxy(const QNetworkProxyQuery &query) |
|
908 |
{ |
|
909 |
QList<QNetworkProxy> proxies; |
|
910 |
if (proxyFactory) { |
|
911 |
proxies = proxyFactory->queryProxy(query); |
|
912 |
if (proxies.isEmpty()) { |
|
913 |
qWarning("QNetworkAccessManager: factory %p has returned an empty result set", |
|
914 |
proxyFactory); |
|
915 |
proxies << QNetworkProxy::NoProxy; |
|
916 |
} |
|
917 |
} else if (proxy.type() == QNetworkProxy::DefaultProxy) { |
|
918 |
// no proxy set, query the application |
|
919 |
return QNetworkProxyFactory::proxyForQuery(query); |
|
920 |
} else { |
|
921 |
proxies << proxy; |
|
922 |
} |
|
923 |
||
924 |
return proxies; |
|
925 |
} |
|
926 |
#endif |
|
927 |
||
928 |
void QNetworkAccessManagerPrivate::addCredentials(const QUrl &url, |
|
929 |
const QAuthenticator *authenticator) |
|
930 |
{ |
|
931 |
Q_ASSERT(authenticator); |
|
932 |
QString domain = QString::fromLatin1("/"); // FIXME: make QAuthenticator return the domain |
|
933 |
QString realm = authenticator->realm(); |
|
934 |
||
935 |
// Set two credentials actually: one with and one without the username in the URL |
|
936 |
QUrl copy = url; |
|
937 |
copy.setUserName(authenticator->user()); |
|
938 |
do { |
|
939 |
QByteArray cacheKey = authenticationKey(copy, realm); |
|
940 |
if (objectCache.hasEntry(cacheKey)) { |
|
941 |
QNetworkAuthenticationCache *auth = |
|
942 |
static_cast<QNetworkAuthenticationCache *>(objectCache.requestEntryNow(cacheKey)); |
|
943 |
auth->insert(domain, authenticator->user(), authenticator->password()); |
|
944 |
objectCache.releaseEntry(cacheKey); |
|
945 |
} else { |
|
946 |
QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache; |
|
947 |
auth->insert(domain, authenticator->user(), authenticator->password()); |
|
948 |
objectCache.addEntry(cacheKey, auth); |
|
949 |
} |
|
950 |
||
951 |
if (copy.userName().isEmpty()) { |
|
952 |
break; |
|
953 |
} else { |
|
954 |
copy.setUserName(QString()); |
|
955 |
} |
|
956 |
} while (true); |
|
957 |
} |
|
958 |
||
959 |
/*! |
|
960 |
Fetch the credential data from the credential cache. |
|
961 |
||
962 |
If auth is 0 (as it is when called from createRequest()), this will try to |
|
963 |
look up with an empty realm. That fails in most cases for HTTP (because the |
|
964 |
realm is seldom empty for HTTP challenges). In any case, QHttpNetworkConnection |
|
965 |
never sends the credentials on the first attempt: it needs to find out what |
|
966 |
authentication methods the server supports. |
|
967 |
||
968 |
For FTP, realm is always empty. |
|
969 |
*/ |
|
970 |
QNetworkAuthenticationCredential * |
|
971 |
QNetworkAccessManagerPrivate::fetchCachedCredentials(const QUrl &url, |
|
972 |
const QAuthenticator *authentication) |
|
973 |
{ |
|
974 |
if (!url.password().isEmpty()) |
|
975 |
return 0; // no need to set credentials if it already has them |
|
976 |
||
977 |
QString realm; |
|
978 |
if (authentication) |
|
979 |
realm = authentication->realm(); |
|
980 |
||
981 |
QByteArray cacheKey = authenticationKey(url, realm); |
|
982 |
if (!objectCache.hasEntry(cacheKey)) |
|
983 |
return 0; |
|
984 |
||
985 |
QNetworkAuthenticationCache *auth = |
|
986 |
static_cast<QNetworkAuthenticationCache *>(objectCache.requestEntryNow(cacheKey)); |
|
987 |
QNetworkAuthenticationCredential *cred = auth->findClosestMatch(url.path()); |
|
988 |
objectCache.releaseEntry(cacheKey); |
|
989 |
return cred; |
|
990 |
} |
|
991 |
||
992 |
void QNetworkAccessManagerPrivate::clearCache(QNetworkAccessManager *manager) |
|
993 |
{ |
|
994 |
manager->d_func()->objectCache.clear(); |
|
995 |
} |
|
996 |
||
997 |
QNetworkAccessManagerPrivate::~QNetworkAccessManagerPrivate() |
|
998 |
{ |
|
999 |
} |
|
1000 |
||
1001 |
QT_END_NAMESPACE |
|
1002 |
||
1003 |
#include "moc_qnetworkaccessmanager.cpp" |