access/qnetworkaccessauthenticationmanager.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtNetwork module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qnetworkaccessauthenticationmanager_p.h" -
43#include "qnetworkaccessmanager.h" -
44#include "qnetworkaccessmanager_p.h" -
45 -
46#include "QtCore/qbuffer.h" -
47#include "QtCore/qurl.h" -
48#include "QtCore/qvector.h" -
49#include "QtCore/QMutexLocker" -
50#include "QtNetwork/qauthenticator.h" -
51 -
52#include <algorithm> -
53 -
54QT_BEGIN_NAMESPACE -
55 -
56 -
57 -
58 -
59class QNetworkAuthenticationCache: private QVector<QNetworkAuthenticationCredential>, -
60 public QNetworkAccessCache::CacheableObject -
61{ -
62public: -
63 QNetworkAuthenticationCache() -
64 { -
65 setExpires(false);
executed (the execution status of this line is deduced): setExpires(false);
-
66 setShareable(true);
executed (the execution status of this line is deduced): setShareable(true);
-
67 reserve(1);
executed (the execution status of this line is deduced): reserve(1);
-
68 }
executed: }
Execution Count:313
313
69 -
70 QNetworkAuthenticationCredential *findClosestMatch(const QString &domain) -
71 { -
72 iterator it = std::lower_bound(begin(), end(), domain);
executed (the execution status of this line is deduced): iterator it = std::lower_bound(begin(), end(), domain);
-
73 if (it == end() && !isEmpty())
evaluated: it == end()
TRUEFALSE
yes
Evaluation Count:340
yes
Evaluation Count:348
evaluated: !isEmpty()
TRUEFALSE
yes
Evaluation Count:27
yes
Evaluation Count:313
27-348
74 --it;
executed: --it;
Execution Count:27
27
75 if (it == end() || !domain.startsWith(it->domain))
evaluated: it == end()
TRUEFALSE
yes
Evaluation Count:313
yes
Evaluation Count:375
partially evaluated: !domain.startsWith(it->domain)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:375
0-375
76 return 0;
executed: return 0;
Execution Count:313
313
77 return &*it;
executed: return &*it;
Execution Count:375
375
78 } -
79 -
80 void insert(const QString &domain, const QString &user, const QString &password) -
81 { -
82 QNetworkAuthenticationCredential *closestMatch = findClosestMatch(domain);
executed (the execution status of this line is deduced): QNetworkAuthenticationCredential *closestMatch = findClosestMatch(domain);
-
83 if (closestMatch && closestMatch->domain == domain) {
evaluated: closestMatch
TRUEFALSE
yes
Evaluation Count:344
yes
Evaluation Count:313
partially evaluated: closestMatch->domain == domain
TRUEFALSE
yes
Evaluation Count:344
no
Evaluation Count:0
0-344
84 // we're overriding the current credentials -
85 closestMatch->user = user;
executed (the execution status of this line is deduced): closestMatch->user = user;
-
86 closestMatch->password = password;
executed (the execution status of this line is deduced): closestMatch->password = password;
-
87 } else {
executed: }
Execution Count:344
344
88 QNetworkAuthenticationCredential newCredential;
executed (the execution status of this line is deduced): QNetworkAuthenticationCredential newCredential;
-
89 newCredential.domain = domain;
executed (the execution status of this line is deduced): newCredential.domain = domain;
-
90 newCredential.user = user;
executed (the execution status of this line is deduced): newCredential.user = user;
-
91 newCredential.password = password;
executed (the execution status of this line is deduced): newCredential.password = password;
-
92 -
93 if (closestMatch)
partially evaluated: closestMatch
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:313
0-313
94 QVector<QNetworkAuthenticationCredential>::insert(++closestMatch, newCredential);
never executed: QVector<QNetworkAuthenticationCredential>::insert(++closestMatch, newCredential);
0
95 else -
96 QVector<QNetworkAuthenticationCredential>::insert(end(), newCredential);
executed: QVector<QNetworkAuthenticationCredential>::insert(end(), newCredential);
Execution Count:313
313
97 } -
98 } -
99 -
100 virtual void dispose() { delete this; }
executed: }
Execution Count:313
313
101}; -
102 -
103#ifndef QT_NO_NETWORKPROXY -
104static QByteArray proxyAuthenticationKey(const QNetworkProxy &proxy, const QString &realm) -
105{ -
106 QUrl key;
executed (the execution status of this line is deduced): QUrl key;
-
107 -
108 switch (proxy.type()) { -
109 case QNetworkProxy::Socks5Proxy: -
110 key.setScheme(QLatin1String("proxy-socks5"));
executed (the execution status of this line is deduced): key.setScheme(QLatin1String("proxy-socks5"));
-
111 break;
executed: break;
Execution Count:65
65
112 -
113 case QNetworkProxy::HttpProxy: -
114 case QNetworkProxy::HttpCachingProxy: -
115 key.setScheme(QLatin1String("proxy-http"));
executed (the execution status of this line is deduced): key.setScheme(QLatin1String("proxy-http"));
-
116 break;
executed: break;
Execution Count:128
128
117 -
118 case QNetworkProxy::FtpCachingProxy: -
119 key.setScheme(QLatin1String("proxy-ftp"));
never executed (the execution status of this line is deduced): key.setScheme(QLatin1String("proxy-ftp"));
-
120 break;
never executed: break;
0
121 -
122 case QNetworkProxy::DefaultProxy: -
123 case QNetworkProxy::NoProxy: -
124 // shouldn't happen -
125 return QByteArray();
never executed: return QByteArray();
0
126 -
127 // no default: -
128 // let there be errors if a new proxy type is added in the future -
129 } -
130 -
131 if (key.scheme().isEmpty())
partially evaluated: key.scheme().isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:193
0-193
132 // proxy type not handled -
133 return QByteArray();
never executed: return QByteArray();
0
134 -
135 key.setUserName(proxy.user());
executed (the execution status of this line is deduced): key.setUserName(proxy.user());
-
136 key.setHost(proxy.hostName());
executed (the execution status of this line is deduced): key.setHost(proxy.hostName());
-
137 key.setPort(proxy.port());
executed (the execution status of this line is deduced): key.setPort(proxy.port());
-
138 key.setFragment(realm);
executed (the execution status of this line is deduced): key.setFragment(realm);
-
139 return "auth:" + key.toEncoded();
executed: return "auth:" + key.toEncoded();
Execution Count:193
193
140} -
141#endif -
142 -
143static inline QByteArray authenticationKey(const QUrl &url, const QString &realm) -
144{ -
145 QUrl copy = url;
executed (the execution status of this line is deduced): QUrl copy = url;
-
146 copy.setFragment(realm);
executed (the execution status of this line is deduced): copy.setFragment(realm);
-
147 return "auth:" + copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery);
executed: return "auth:" + copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery);
Execution Count:604
604
148} -
149 -
150 -
151#ifndef QT_NO_NETWORKPROXY -
152void QNetworkAccessAuthenticationManager::cacheProxyCredentials(const QNetworkProxy &p, -
153 const QAuthenticator *authenticator) -
154{ -
155 Q_ASSERT(authenticator);
executed (the execution status of this line is deduced): qt_noop();
-
156 Q_ASSERT(p.type() != QNetworkProxy::DefaultProxy);
executed (the execution status of this line is deduced): qt_noop();
-
157 Q_ASSERT(p.type() != QNetworkProxy::NoProxy);
executed (the execution status of this line is deduced): qt_noop();
-
158 -
159 QMutexLocker mutexLocker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker mutexLocker(&mutex);
-
160 -
161 QString realm = authenticator->realm();
executed (the execution status of this line is deduced): QString realm = authenticator->realm();
-
162 QNetworkProxy proxy = p;
executed (the execution status of this line is deduced): QNetworkProxy proxy = p;
-
163 proxy.setUser(authenticator->user());
executed (the execution status of this line is deduced): proxy.setUser(authenticator->user());
-
164 -
165 // don't cache null passwords, empty password may be valid though -
166 if (authenticator->password().isNull())
evaluated: authenticator->password().isNull()
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:52
5-52
167 return;
executed: return;
Execution Count:5
5
168 -
169 // Set two credentials: one with the username and one without -
170 do { -
171 // Set two credentials actually: one with and one without the realm -
172 do { -
173 QByteArray cacheKey = proxyAuthenticationKey(proxy, realm);
executed (the execution status of this line is deduced): QByteArray cacheKey = proxyAuthenticationKey(proxy, realm);
-
174 if (cacheKey.isEmpty())
partially evaluated: cacheKey.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:135
0-135
175 return; // should not happen
never executed: return;
0
176 -
177 QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache;
executed (the execution status of this line is deduced): QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache;
-
178 auth->insert(QString(), authenticator->user(), authenticator->password());
executed (the execution status of this line is deduced): auth->insert(QString(), authenticator->user(), authenticator->password());
-
179 authenticationCache.addEntry(cacheKey, auth); // replace the existing one, if there's any
executed (the execution status of this line is deduced): authenticationCache.addEntry(cacheKey, auth);
-
180 -
181 if (realm.isEmpty()) {
evaluated: realm.isEmpty()
TRUEFALSE
yes
Evaluation Count:104
yes
Evaluation Count:31
31-104
182 break;
executed: break;
Execution Count:104
104
183 } else { -
184 realm.clear();
executed (the execution status of this line is deduced): realm.clear();
-
185 }
executed: }
Execution Count:31
31
186 } while (true);
partially evaluated: true
TRUEFALSE
yes
Evaluation Count:31
no
Evaluation Count:0
0-31
187 -
188 if (proxy.user().isEmpty())
evaluated: proxy.user().isEmpty()
TRUEFALSE
yes
Evaluation Count:52
yes
Evaluation Count:52
52
189 break;
executed: break;
Execution Count:52
52
190 else -
191 proxy.setUser(QString());
executed: proxy.setUser(QString());
Execution Count:52
52
192 } while (true);
partially evaluated: true
TRUEFALSE
yes
Evaluation Count:52
no
Evaluation Count:0
0-52
193}
executed: }
Execution Count:52
52
194 -
195QNetworkAuthenticationCredential -
196QNetworkAccessAuthenticationManager::fetchCachedProxyCredentials(const QNetworkProxy &p, -
197 const QAuthenticator *authenticator) -
198{ -
199 QNetworkProxy proxy = p;
executed (the execution status of this line is deduced): QNetworkProxy proxy = p;
-
200 if (proxy.type() == QNetworkProxy::DefaultProxy) {
partially evaluated: proxy.type() == QNetworkProxy::DefaultProxy
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:58
0-58
201 proxy = QNetworkProxy::applicationProxy();
never executed (the execution status of this line is deduced): proxy = QNetworkProxy::applicationProxy();
-
202 }
never executed: }
0
203 if (!proxy.password().isEmpty())
partially evaluated: !proxy.password().isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:58
0-58
204 return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them
never executed: return QNetworkAuthenticationCredential();
0
205 -
206 QString realm;
executed (the execution status of this line is deduced): QString realm;
-
207 if (authenticator)
evaluated: authenticator
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:57
1-57
208 realm = authenticator->realm();
executed: realm = authenticator->realm();
Execution Count:1
1
209 -
210 QMutexLocker mutexLocker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker mutexLocker(&mutex);
-
211 QByteArray cacheKey = proxyAuthenticationKey(proxy, realm);
executed (the execution status of this line is deduced): QByteArray cacheKey = proxyAuthenticationKey(proxy, realm);
-
212 if (cacheKey.isEmpty())
partially evaluated: cacheKey.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:58
0-58
213 return QNetworkAuthenticationCredential();
never executed: return QNetworkAuthenticationCredential();
0
214 if (!authenticationCache.hasEntry(cacheKey))
evaluated: !authenticationCache.hasEntry(cacheKey)
TRUEFALSE
yes
Evaluation Count:54
yes
Evaluation Count:4
4-54
215 return QNetworkAuthenticationCredential();
executed: return QNetworkAuthenticationCredential();
Execution Count:54
54
216 -
217 QNetworkAuthenticationCache *auth =
executed (the execution status of this line is deduced): QNetworkAuthenticationCache *auth =
-
218 static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey));
executed (the execution status of this line is deduced): static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey));
-
219 QNetworkAuthenticationCredential cred = *auth->findClosestMatch(QString());
executed (the execution status of this line is deduced): QNetworkAuthenticationCredential cred = *auth->findClosestMatch(QString());
-
220 authenticationCache.releaseEntry(cacheKey);
executed (the execution status of this line is deduced): authenticationCache.releaseEntry(cacheKey);
-
221 -
222 // proxy cache credentials always have exactly one item -
223 Q_ASSERT_X(!cred.isNull(), "QNetworkAccessManager",
executed (the execution status of this line is deduced): qt_noop();
-
224 "Internal inconsistency: found a cache key for a proxy, but it's empty"); -
225 return cred;
executed: return cred;
Execution Count:4
4
226} -
227 -
228#endif -
229 -
230void QNetworkAccessAuthenticationManager::cacheCredentials(const QUrl &url, -
231 const QAuthenticator *authenticator) -
232{ -
233 Q_ASSERT(authenticator);
executed (the execution status of this line is deduced): qt_noop();
-
234 QString domain = QString::fromLatin1("/"); // FIXME: make QAuthenticator return the domain
executed (the execution status of this line is deduced): QString domain = QString::fromLatin1("/");
-
235 QString realm = authenticator->realm();
executed (the execution status of this line is deduced): QString realm = authenticator->realm();
-
236 -
237 QMutexLocker mutexLocker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker mutexLocker(&mutex);
-
238 -
239 // Set two credentials actually: one with and one without the username in the URL -
240 QUrl copy = url;
executed (the execution status of this line is deduced): QUrl copy = url;
-
241 copy.setUserName(authenticator->user());
executed (the execution status of this line is deduced): copy.setUserName(authenticator->user());
-
242 do { -
243 QByteArray cacheKey = authenticationKey(copy, realm);
executed (the execution status of this line is deduced): QByteArray cacheKey = authenticationKey(copy, realm);
-
244 if (authenticationCache.hasEntry(cacheKey)) {
evaluated: authenticationCache.hasEntry(cacheKey)
TRUEFALSE
yes
Evaluation Count:344
yes
Evaluation Count:178
178-344
245 QNetworkAuthenticationCache *auth =
executed (the execution status of this line is deduced): QNetworkAuthenticationCache *auth =
-
246 static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey));
executed (the execution status of this line is deduced): static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey));
-
247 auth->insert(domain, authenticator->user(), authenticator->password());
executed (the execution status of this line is deduced): auth->insert(domain, authenticator->user(), authenticator->password());
-
248 authenticationCache.releaseEntry(cacheKey);
executed (the execution status of this line is deduced): authenticationCache.releaseEntry(cacheKey);
-
249 } else {
executed: }
Execution Count:344
344
250 QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache;
executed (the execution status of this line is deduced): QNetworkAuthenticationCache *auth = new QNetworkAuthenticationCache;
-
251 auth->insert(domain, authenticator->user(), authenticator->password());
executed (the execution status of this line is deduced): auth->insert(domain, authenticator->user(), authenticator->password());
-
252 authenticationCache.addEntry(cacheKey, auth);
executed (the execution status of this line is deduced): authenticationCache.addEntry(cacheKey, auth);
-
253 }
executed: }
Execution Count:178
178
254 -
255 if (copy.userName().isEmpty()) {
evaluated: copy.userName().isEmpty()
TRUEFALSE
yes
Evaluation Count:266
yes
Evaluation Count:256
256-266
256 break;
executed: break;
Execution Count:266
266
257 } else { -
258 copy.setUserName(QString());
executed (the execution status of this line is deduced): copy.setUserName(QString());
-
259 }
executed: }
Execution Count:256
256
260 } while (true);
partially evaluated: true
TRUEFALSE
yes
Evaluation Count:256
no
Evaluation Count:0
0-256
261}
executed: }
Execution Count:266
266
262 -
263/*! -
264 Fetch the credential data from the credential cache. -
265 -
266 If auth is 0 (as it is when called from createRequest()), this will try to -
267 look up with an empty realm. That fails in most cases for HTTP (because the -
268 realm is seldom empty for HTTP challenges). In any case, QHttpNetworkConnection -
269 never sends the credentials on the first attempt: it needs to find out what -
270 authentication methods the server supports. -
271 -
272 For FTP, realm is always empty. -
273*/ -
274QNetworkAuthenticationCredential -
275QNetworkAccessAuthenticationManager::fetchCachedCredentials(const QUrl &url, -
276 const QAuthenticator *authentication) -
277{ -
278 if (!url.password().isEmpty())
evaluated: !url.password().isEmpty()
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:82
4-82
279 return QNetworkAuthenticationCredential(); // no need to set credentials if it already has them
executed: return QNetworkAuthenticationCredential();
Execution Count:4
4
280 -
281 QString realm;
executed (the execution status of this line is deduced): QString realm;
-
282 if (authentication)
partially evaluated: authentication
TRUEFALSE
yes
Evaluation Count:82
no
Evaluation Count:0
0-82
283 realm = authentication->realm();
executed: realm = authentication->realm();
Execution Count:82
82
284 -
285 QByteArray cacheKey = authenticationKey(url, realm);
executed (the execution status of this line is deduced): QByteArray cacheKey = authenticationKey(url, realm);
-
286 -
287 QMutexLocker mutexLocker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker mutexLocker(&mutex);
-
288 if (!authenticationCache.hasEntry(cacheKey))
evaluated: !authenticationCache.hasEntry(cacheKey)
TRUEFALSE
yes
Evaluation Count:55
yes
Evaluation Count:27
27-55
289 return QNetworkAuthenticationCredential();
executed: return QNetworkAuthenticationCredential();
Execution Count:55
55
290 -
291 QNetworkAuthenticationCache *auth =
executed (the execution status of this line is deduced): QNetworkAuthenticationCache *auth =
-
292 static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey));
executed (the execution status of this line is deduced): static_cast<QNetworkAuthenticationCache *>(authenticationCache.requestEntryNow(cacheKey));
-
293 QNetworkAuthenticationCredential *cred = auth->findClosestMatch(url.path());
executed (the execution status of this line is deduced): QNetworkAuthenticationCredential *cred = auth->findClosestMatch(url.path());
-
294 QNetworkAuthenticationCredential ret;
executed (the execution status of this line is deduced): QNetworkAuthenticationCredential ret;
-
295 if (cred)
partially evaluated: cred
TRUEFALSE
yes
Evaluation Count:27
no
Evaluation Count:0
0-27
296 ret = *cred;
executed: ret = *cred;
Execution Count:27
27
297 authenticationCache.releaseEntry(cacheKey);
executed (the execution status of this line is deduced): authenticationCache.releaseEntry(cacheKey);
-
298 return ret;
executed: return ret;
Execution Count:27
27
299} -
300 -
301void QNetworkAccessAuthenticationManager::clearCache() -
302{ -
303 authenticationCache.clear();
executed (the execution status of this line is deduced): authenticationCache.clear();
-
304}
executed: }
Execution Count:1176
1176
305 -
306QT_END_NAMESPACE -
307 -
308 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial