1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QNETWORKACCESSMANAGER_P_H
41 #define QNETWORKACCESSMANAGER_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists for the convenience
48 // of the Network Access API.  This header file may change from
49 // version to version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtNetwork/private/qtnetworkglobal_p.h>
55 #include "qnetworkaccessmanager.h"
56 #include "qnetworkaccesscache_p.h"
57 #include "qnetworkaccessbackend_p.h"
58 #include "private/qnetconmonitor_p.h"
59 #include "qnetworkrequest.h"
60 #include "qhsts_p.h"
61 #include "private/qobject_p.h"
62 #include "QtNetwork/qnetworkproxy.h"
63 #include "QtNetwork/qnetworksession.h"
64 #include "qnetworkaccessauthenticationmanager_p.h"
65 #ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section
66 #include "QtNetwork/qnetworkconfigmanager.h"
67 #endif
68 
69 #if QT_CONFIG(settings)
70 #include "qhstsstore_p.h"
71 #endif // QT_CONFIG(settings)
72 
73 QT_BEGIN_NAMESPACE
74 
75 class QAuthenticator;
76 class QAbstractNetworkCache;
77 class QNetworkAuthenticationCredential;
78 class QNetworkCookieJar;
79 
80 class QNetworkAccessManagerPrivate: public QObjectPrivate
81 {
82 public:
QNetworkAccessManagerPrivate()83     QNetworkAccessManagerPrivate()
84         : networkCache(nullptr),
85           cookieJar(nullptr),
86           thread(nullptr),
87 #ifndef QT_NO_NETWORKPROXY
88           proxyFactory(nullptr),
89 #endif
90 #ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section
91           lastSessionState(QNetworkSession::Invalid),
92           networkConfiguration(networkConfigurationManager.defaultConfiguration()),
93           customNetworkConfiguration(false),
94           networkSessionRequired(networkConfigurationManager.capabilities()
95                                  & QNetworkConfigurationManager::NetworkSessionRequired),
96           activeReplyCount(0),
97           online(false),
98           initializeSession(true),
99 #endif
100           cookieJarCreated(false),
101           defaultAccessControl(true),
102           redirectPolicy(QNetworkRequest::ManualRedirectPolicy),
103           authenticationManager(QSharedPointer<QNetworkAccessAuthenticationManager>::create())
104     {
105 #ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section
106         // we would need all active configurations to check for
107         // d->networkConfigurationManager.isOnline(), which is asynchronous
108         // and potentially expensive. We can just check the configuration here
109         online = (networkConfiguration.state().testFlag(QNetworkConfiguration::Active));
110         if (online)
111             networkAccessible = QNetworkAccessManager::Accessible;
112         else if (networkConfiguration.state().testFlag(QNetworkConfiguration::Undefined))
113             networkAccessible = QNetworkAccessManager::UnknownAccessibility;
114         else
115             networkAccessible = QNetworkAccessManager::NotAccessible;
116 #endif
117     }
118     ~QNetworkAccessManagerPrivate();
119 
120     QThread * createThread();
121     void destroyThread();
122 
123     void _q_replyFinished(QNetworkReply *reply);
124     void _q_replyEncrypted(QNetworkReply *reply);
125     void _q_replySslErrors(const QList<QSslError> &errors);
126     void _q_replyPreSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator);
127     QNetworkReply *postProcess(QNetworkReply *reply);
128     void createCookieJar() const;
129 
130     void authenticationRequired(QAuthenticator *authenticator,
131                                 QNetworkReply *reply,
132                                 bool synchronous,
133                                 QUrl &url,
134                                 QUrl *urlForLastAuthentication,
135                                 bool allowAuthenticationReuse = true);
136     void cacheCredentials(const QUrl &url, const QAuthenticator *auth);
137     QNetworkAuthenticationCredential *fetchCachedCredentials(const QUrl &url,
138                                                              const QAuthenticator *auth = nullptr);
139 
140 #ifndef QT_NO_NETWORKPROXY
141     void proxyAuthenticationRequired(const QUrl &url,
142                                 const QNetworkProxy &proxy,
143                                 bool synchronous,
144                                 QAuthenticator *authenticator,
145                                 QNetworkProxy *lastProxyAuthentication);
146     void cacheProxyCredentials(const QNetworkProxy &proxy, const QAuthenticator *auth);
147     QNetworkAuthenticationCredential *fetchCachedProxyCredentials(const QNetworkProxy &proxy,
148                                                              const QAuthenticator *auth = nullptr);
149     QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query);
150 #endif
151 
152     QNetworkAccessBackend *findBackend(QNetworkAccessManager::Operation op, const QNetworkRequest &request);
153     QStringList backendSupportedSchemes() const;
154 
155     void _q_onlineStateChanged(bool isOnline);
156 #ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section
157     void createSession(const QNetworkConfiguration &config);
158     QSharedPointer<QNetworkSession> getNetworkSession() const;
159 
160     void _q_networkSessionClosed();
161     void _q_networkSessionNewConfigurationActivated();
162     void _q_networkSessionPreferredConfigurationChanged(const QNetworkConfiguration &config,
163                                                         bool isSeamless);
164     void _q_networkSessionStateChanged(QNetworkSession::State state);
165 
166     void _q_configurationChanged(const QNetworkConfiguration &configuration);
167     void _q_networkSessionFailed(QNetworkSession::SessionError error);
168 
169     QSet<QString> onlineConfigurations;
170 #endif
171 
172 #if QT_CONFIG(http)
173     QNetworkRequest prepareMultipart(const QNetworkRequest &request, QHttpMultiPart *multiPart);
174 #endif
175 
176     // this is the cache for storing downloaded files
177     QAbstractNetworkCache *networkCache;
178 
179     QNetworkCookieJar *cookieJar;
180 
181     QThread *thread;
182 
183 
184 #ifndef QT_NO_NETWORKPROXY
185     QNetworkProxy proxy;
186     QNetworkProxyFactory *proxyFactory;
187 #endif
188 
189 #ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section
190     QSharedPointer<QNetworkSession> networkSessionStrongRef;
191     QWeakPointer<QNetworkSession> networkSessionWeakRef;
192     QNetworkSession::State lastSessionState;
193     QNetworkConfigurationManager networkConfigurationManager;
194     QNetworkConfiguration networkConfiguration;
195     // we need to track whether the user set a config or not,
196     // because the default config might change
197     bool customNetworkConfiguration;
198     bool networkSessionRequired;
199     QNetworkAccessManager::NetworkAccessibility networkAccessible;
200     int activeReplyCount;
201     bool online;
202     bool initializeSession;
203 #else
204     bool networkAccessible = true;
205 #endif
206 
207     bool cookieJarCreated;
208     bool defaultAccessControl;
209     QNetworkRequest::RedirectPolicy redirectPolicy;
210 
211     // The cache with authorization data:
212     QSharedPointer<QNetworkAccessAuthenticationManager> authenticationManager;
213 
214     // this cache can be used by individual backends to cache e.g. their TCP connections to a server
215     // and use the connections for multiple requests.
216     QNetworkAccessCache objectCache;
getObjectCache(QNetworkAccessBackend * backend)217     static inline QNetworkAccessCache *getObjectCache(QNetworkAccessBackend *backend)
218     { return &backend->manager->objectCache; }
219 
220     Q_AUTOTEST_EXPORT static void clearAuthenticationCache(QNetworkAccessManager *manager);
221     Q_AUTOTEST_EXPORT static void clearConnectionCache(QNetworkAccessManager *manager);
222 
223     QHstsCache stsCache;
224 #if QT_CONFIG(settings)
225     QScopedPointer<QHstsStore> stsStore;
226 #endif // QT_CONFIG(settings)
227     bool stsEnabled = false;
228     QNetworkStatusMonitor *statusMonitor = nullptr;
229 
230     bool autoDeleteReplies = false;
231 
232     int transferTimeout = 0;
233 
234 #ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section
235     Q_AUTOTEST_EXPORT static const QWeakPointer<const QNetworkSession> getNetworkSession(const QNetworkAccessManager *manager);
236 #endif
237     Q_DECLARE_PUBLIC(QNetworkAccessManager)
238 };
239 
240 QT_END_NAMESPACE
241 
242 #endif
243