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 QHTTPNETWORKCONNECTION_H
41#define QHTTPNETWORKCONNECTION_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 <QtNetwork/qnetworkrequest.h>
56#include <QtNetwork/qnetworkreply.h>
57#include <QtNetwork/qabstractsocket.h>
58
59#include <qhttp2configuration.h>
60
61#include <private/qobject_p.h>
62#include <qauthenticator.h>
63#include <qnetworkproxy.h>
64#include <qbuffer.h>
65#include <qtimer.h>
66#include <qsharedpointer.h>
67
68#include <private/qhttpnetworkheader_p.h>
69#include <private/qhttpnetworkrequest_p.h>
70#include <private/qhttpnetworkreply_p.h>
71#include <private/qnetconmonitor_p.h>
72#include <private/http2protocol_p.h>
73
74#include <private/qhttpnetworkconnectionchannel_p.h>
75
76QT_REQUIRE_CONFIG(http);
77
78QT_BEGIN_NAMESPACE
79
80class QHttpNetworkRequest;
81class QHttpNetworkReply;
82class QHttpThreadDelegate;
83class QByteArray;
84class QHostInfo;
85#ifndef QT_NO_SSL
86class QSslConfiguration;
87class QSslContext;
88#endif // !QT_NO_SSL
89
90class QHttpNetworkConnectionPrivate;
91class Q_AUTOTEST_EXPORT QHttpNetworkConnection : public QObject
92{
93 Q_OBJECT
94public:
95
96 enum ConnectionType {
97 ConnectionTypeHTTP,
98 ConnectionTypeHTTP2,
99 ConnectionTypeHTTP2Direct
100 };
101
102 explicit QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false,
103 ConnectionType connectionType = ConnectionTypeHTTP,
104 QObject *parent = nullptr);
105 QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80,
106 bool encrypt = false, QObject *parent = nullptr,
107 ConnectionType connectionType = ConnectionTypeHTTP);
108 ~QHttpNetworkConnection();
109
110 //The hostname to which this is connected to.
111 QString hostName() const;
112 //The HTTP port in use.
113 quint16 port() const;
114
115 //add a new HTTP request through this connection
116 QHttpNetworkReply* sendRequest(const QHttpNetworkRequest &request);
117 void fillHttp2Queue();
118
119#ifndef QT_NO_NETWORKPROXY
120 //set the proxy for this connection
121 void setCacheProxy(const QNetworkProxy &networkProxy);
122 QNetworkProxy cacheProxy() const;
123 void setTransparentProxy(const QNetworkProxy &networkProxy);
124 QNetworkProxy transparentProxy() const;
125#endif
126
127 bool isSsl() const;
128
129 QHttpNetworkConnectionChannel *channels() const;
130
131 ConnectionType connectionType();
132 void setConnectionType(ConnectionType type);
133
134 QHttp2Configuration http2Parameters() const;
135 void setHttp2Parameters(const QHttp2Configuration &params);
136
137#ifndef QT_NO_SSL
138 void setSslConfiguration(const QSslConfiguration &config);
139 void ignoreSslErrors(int channel = -1);
140 void ignoreSslErrors(const QList<QSslError> &errors, int channel = -1);
141 QSharedPointer<QSslContext> sslContext();
142 void setSslContext(QSharedPointer<QSslContext> context);
143#endif
144
145 void preConnectFinished();
146
147 QString peerVerifyName() const;
148 void setPeerVerifyName(const QString &peerName);
149
150public slots:
151 void onlineStateChanged(bool isOnline);
152
153private:
154 Q_DECLARE_PRIVATE(QHttpNetworkConnection)
155 Q_DISABLE_COPY_MOVE(QHttpNetworkConnection)
156 friend class QHttpThreadDelegate;
157 friend class QHttpNetworkReply;
158 friend class QHttpNetworkReplyPrivate;
159 friend class QHttpNetworkConnectionChannel;
160 friend class QHttp2ProtocolHandler;
161 friend class QHttpProtocolHandler;
162
163 Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
164 Q_PRIVATE_SLOT(d_func(), void _q_hostLookupFinished(QHostInfo))
165 Q_PRIVATE_SLOT(d_func(), void _q_connectDelayedChannel())
166};
167
168
169// private classes
170typedef QPair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair;
171
172
173class QHttpNetworkConnectionPrivate : public QObjectPrivate
174{
175 Q_DECLARE_PUBLIC(QHttpNetworkConnection)
176public:
177 static const int defaultHttpChannelCount;
178 static const int defaultPipelineLength;
179 static const int defaultRePipelineLength;
180
181 enum ConnectionState {
182 RunningState = 0,
183 PausedState = 1
184 };
185
186 enum NetworkLayerPreferenceState {
187 Unknown,
188 HostLookupPending,
189 IPv4,
190 IPv6,
191 IPv4or6
192 };
193
194 QHttpNetworkConnectionPrivate(const QString &hostName, quint16 port, bool encrypt,
195 QHttpNetworkConnection::ConnectionType type);
196 QHttpNetworkConnectionPrivate(quint16 channelCount, const QString &hostName, quint16 port, bool encrypt,
197 QHttpNetworkConnection::ConnectionType type);
198 ~QHttpNetworkConnectionPrivate();
199 void init();
200
201 void pauseConnection();
202 void resumeConnection();
203 ConnectionState state;
204 NetworkLayerPreferenceState networkLayerState;
205
206 enum { ChunkSize = 4096 };
207
208 int indexOf(QAbstractSocket *socket) const;
209
210 QHttpNetworkReply *queueRequest(const QHttpNetworkRequest &request);
211 void requeueRequest(const HttpMessagePair &pair); // e.g. after pipeline broke
212 void fillHttp2Queue();
213 bool dequeueRequest(QAbstractSocket *socket);
214 void prepareRequest(HttpMessagePair &request);
215 void updateChannel(int i, const HttpMessagePair &messagePair);
216 QHttpNetworkRequest predictNextRequest() const;
217
218 void fillPipeline(QAbstractSocket *socket);
219 bool fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel);
220
221 // read more HTTP body after the next event loop spin
222 void readMoreLater(QHttpNetworkReply *reply);
223
224 void copyCredentials(int fromChannel, QAuthenticator *auth, bool isProxy);
225
226 void startHostInfoLookup();
227 void startNetworkLayerStateLookup();
228 void networkLayerDetected(QAbstractSocket::NetworkLayerProtocol protocol);
229
230 // private slots
231 void _q_startNextRequest(); // send the next request from the queue
232
233 void _q_hostLookupFinished(const QHostInfo &info);
234 void _q_connectDelayedChannel();
235
236 void createAuthorization(QAbstractSocket *socket, QHttpNetworkRequest &request);
237
238 QString errorDetail(QNetworkReply::NetworkError errorCode, QAbstractSocket *socket,
239 const QString &extraDetail = QString());
240
241 void removeReply(QHttpNetworkReply *reply);
242
243 QString hostName;
244 quint16 port;
245 bool encrypt;
246 bool delayIpv4;
247
248 // Number of channels we are trying to use at the moment:
249 int activeChannelCount;
250 // The total number of channels we reserved:
251 const int channelCount;
252 QTimer delayedConnectionTimer;
253 QHttpNetworkConnectionChannel *channels; // parallel connections to the server
254 bool shouldEmitChannelError(QAbstractSocket *socket);
255
256 qint64 uncompressedBytesAvailable(const QHttpNetworkReply &reply) const;
257 qint64 uncompressedBytesAvailableNextBlock(const QHttpNetworkReply &reply) const;
258
259
260 void emitReplyError(QAbstractSocket *socket, QHttpNetworkReply *reply, QNetworkReply::NetworkError errorCode);
261 bool handleAuthenticateChallenge(QAbstractSocket *socket, QHttpNetworkReply *reply, bool isProxy, bool &resend);
262 QUrl parseRedirectResponse(QAbstractSocket *socket, QHttpNetworkReply *reply);
263
264#ifndef QT_NO_NETWORKPROXY
265 QNetworkProxy networkProxy;
266 void emitProxyAuthenticationRequired(const QHttpNetworkConnectionChannel *chan, const QNetworkProxy &proxy, QAuthenticator* auth);
267#endif
268
269 //The request queues
270 QList<HttpMessagePair> highPriorityQueue;
271 QList<HttpMessagePair> lowPriorityQueue;
272
273 int preConnectRequests;
274
275 QHttpNetworkConnection::ConnectionType connectionType;
276
277#ifndef QT_NO_SSL
278 QSharedPointer<QSslContext> sslContext;
279#endif
280
281 QHttp2Configuration http2Parameters;
282
283 QString peerVerifyName;
284 // If network status monitoring is enabled, we activate connectionMonitor
285 // as soons as one of channels managed to connect to host (and we
286 // have a pair of addresses (us,peer).
287 // NETMONTODO: consider activating a monitor on a change from
288 // HostLookUp state to ConnectingState (means we have both
289 // local/remote addresses known and can start monitoring this
290 // early).
291 QNetworkConnectionMonitor connectionMonitor;
292
293 friend class QHttpNetworkConnectionChannel;
294};
295
296
297
298QT_END_NAMESPACE
299
300#endif
301