1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QHTTPNETWORKCONNECTION_H
5#define QHTTPNETWORKCONNECTION_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of the Network Access API. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19#include <QtNetwork/qnetworkrequest.h>
20#include <QtNetwork/qnetworkreply.h>
21#include <QtNetwork/qabstractsocket.h>
22
23#include <qhttp2configuration.h>
24
25#include <private/qobject_p.h>
26#include <qauthenticator.h>
27#include <qnetworkproxy.h>
28#include <qbuffer.h>
29#include <qtimer.h>
30#include <qsharedpointer.h>
31
32#include <private/qhttpnetworkheader_p.h>
33#include <private/qhttpnetworkrequest_p.h>
34#include <private/qhttpnetworkreply_p.h>
35#include <private/qnetconmonitor_p.h>
36#include <private/http2protocol_p.h>
37
38#include <private/qhttpnetworkconnectionchannel_p.h>
39
40QT_REQUIRE_CONFIG(http);
41
42QT_BEGIN_NAMESPACE
43
44class QHttpNetworkRequest;
45class QHttpNetworkReply;
46class QHttpThreadDelegate;
47class QByteArray;
48class QHostInfo;
49#ifndef QT_NO_SSL
50class QSslConfiguration;
51class QSslContext;
52#endif // !QT_NO_SSL
53
54class QHttpNetworkConnectionPrivate;
55class Q_AUTOTEST_EXPORT QHttpNetworkConnection : public QObject
56{
57 Q_OBJECT
58public:
59
60 enum ConnectionType {
61 ConnectionTypeHTTP,
62 ConnectionTypeHTTP2,
63 ConnectionTypeHTTP2Direct
64 };
65
66 explicit QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false,
67 ConnectionType connectionType = ConnectionTypeHTTP,
68 QObject *parent = nullptr);
69 QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80,
70 bool encrypt = false, QObject *parent = nullptr,
71 ConnectionType connectionType = ConnectionTypeHTTP);
72 ~QHttpNetworkConnection();
73
74 //The hostname to which this is connected to.
75 QString hostName() const;
76 //The HTTP port in use.
77 quint16 port() const;
78
79 //add a new HTTP request through this connection
80 QHttpNetworkReply* sendRequest(const QHttpNetworkRequest &request);
81 void fillHttp2Queue();
82
83#ifndef QT_NO_NETWORKPROXY
84 //set the proxy for this connection
85 void setCacheProxy(const QNetworkProxy &networkProxy);
86 QNetworkProxy cacheProxy() const;
87 void setTransparentProxy(const QNetworkProxy &networkProxy);
88 QNetworkProxy transparentProxy() const;
89#endif
90
91 bool isSsl() const;
92
93 QHttpNetworkConnectionChannel *channels() const;
94
95 ConnectionType connectionType();
96 void setConnectionType(ConnectionType type);
97
98 QHttp2Configuration http2Parameters() const;
99 void setHttp2Parameters(const QHttp2Configuration &params);
100
101#ifndef QT_NO_SSL
102 void setSslConfiguration(const QSslConfiguration &config);
103 void ignoreSslErrors(int channel = -1);
104 void ignoreSslErrors(const QList<QSslError> &errors, int channel = -1);
105 std::shared_ptr<QSslContext> sslContext();
106 void setSslContext(std::shared_ptr<QSslContext> context);
107#endif
108
109 void preConnectFinished();
110
111 QString peerVerifyName() const;
112 void setPeerVerifyName(const QString &peerName);
113
114public slots:
115 void onlineStateChanged(bool isOnline);
116
117private:
118 Q_DECLARE_PRIVATE(QHttpNetworkConnection)
119 Q_DISABLE_COPY_MOVE(QHttpNetworkConnection)
120 friend class QHttpThreadDelegate;
121 friend class QHttpNetworkReply;
122 friend class QHttpNetworkReplyPrivate;
123 friend class QHttpNetworkConnectionChannel;
124 friend class QHttp2ProtocolHandler;
125 friend class QHttpProtocolHandler;
126
127 Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
128 Q_PRIVATE_SLOT(d_func(), void _q_hostLookupFinished(QHostInfo))
129 Q_PRIVATE_SLOT(d_func(), void _q_connectDelayedChannel())
130};
131
132
133// private classes
134typedef QPair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair;
135
136
137class QHttpNetworkConnectionPrivate : public QObjectPrivate
138{
139 Q_DECLARE_PUBLIC(QHttpNetworkConnection)
140public:
141 static const int defaultHttpChannelCount;
142 static const int defaultPipelineLength;
143 static const int defaultRePipelineLength;
144
145 enum ConnectionState {
146 RunningState = 0,
147 PausedState = 1
148 };
149
150 enum NetworkLayerPreferenceState {
151 Unknown,
152 HostLookupPending,
153 IPv4,
154 IPv6,
155 IPv4or6
156 };
157
158 QHttpNetworkConnectionPrivate(const QString &hostName, quint16 port, bool encrypt,
159 QHttpNetworkConnection::ConnectionType type);
160 QHttpNetworkConnectionPrivate(quint16 channelCount, const QString &hostName, quint16 port, bool encrypt,
161 QHttpNetworkConnection::ConnectionType type);
162 ~QHttpNetworkConnectionPrivate();
163 void init();
164
165 void pauseConnection();
166 void resumeConnection();
167 ConnectionState state;
168 NetworkLayerPreferenceState networkLayerState;
169
170 enum { ChunkSize = 4096 };
171
172 int indexOf(QAbstractSocket *socket) const;
173
174 QHttpNetworkReply *queueRequest(const QHttpNetworkRequest &request);
175 void requeueRequest(const HttpMessagePair &pair); // e.g. after pipeline broke
176 void fillHttp2Queue();
177 bool dequeueRequest(QAbstractSocket *socket);
178 void prepareRequest(HttpMessagePair &request);
179 void updateChannel(int i, const HttpMessagePair &messagePair);
180 QHttpNetworkRequest predictNextRequest() const;
181 QHttpNetworkReply* predictNextRequestsReply() const;
182
183 void fillPipeline(QAbstractSocket *socket);
184 bool fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel);
185
186 // read more HTTP body after the next event loop spin
187 void readMoreLater(QHttpNetworkReply *reply);
188
189 void copyCredentials(int fromChannel, QAuthenticator *auth, bool isProxy);
190
191 void startHostInfoLookup();
192 void startNetworkLayerStateLookup();
193 void networkLayerDetected(QAbstractSocket::NetworkLayerProtocol protocol);
194
195 // private slots
196 void _q_startNextRequest(); // send the next request from the queue
197
198 void _q_hostLookupFinished(const QHostInfo &info);
199 void _q_connectDelayedChannel();
200
201 void createAuthorization(QAbstractSocket *socket, QHttpNetworkRequest &request);
202
203 QString errorDetail(QNetworkReply::NetworkError errorCode, QAbstractSocket *socket,
204 const QString &extraDetail = QString());
205
206 void removeReply(QHttpNetworkReply *reply);
207
208 QString hostName;
209 quint16 port;
210 bool encrypt;
211 bool delayIpv4;
212
213 // Number of channels we are trying to use at the moment:
214 int activeChannelCount;
215 // The total number of channels we reserved:
216 const int channelCount;
217 QTimer delayedConnectionTimer;
218 QHttpNetworkConnectionChannel *channels; // parallel connections to the server
219 bool shouldEmitChannelError(QAbstractSocket *socket);
220
221 qint64 uncompressedBytesAvailable(const QHttpNetworkReply &reply) const;
222 qint64 uncompressedBytesAvailableNextBlock(const QHttpNetworkReply &reply) const;
223
224
225 void emitReplyError(QAbstractSocket *socket, QHttpNetworkReply *reply, QNetworkReply::NetworkError errorCode);
226 bool handleAuthenticateChallenge(QAbstractSocket *socket, QHttpNetworkReply *reply, bool isProxy, bool &resend);
227 struct ParseRedirectResult {
228 QUrl redirectUrl;
229 QNetworkReply::NetworkError errorCode;
230 };
231 ParseRedirectResult parseRedirectResponse(QHttpNetworkReply *reply);
232 // Used by the HTTP1 code-path
233 QUrl parseRedirectResponse(QAbstractSocket *socket, QHttpNetworkReply *reply);
234
235#ifndef QT_NO_NETWORKPROXY
236 QNetworkProxy networkProxy;
237 void emitProxyAuthenticationRequired(const QHttpNetworkConnectionChannel *chan, const QNetworkProxy &proxy, QAuthenticator* auth);
238#endif
239
240 //The request queues
241 QList<HttpMessagePair> highPriorityQueue;
242 QList<HttpMessagePair> lowPriorityQueue;
243
244 int preConnectRequests;
245
246 QHttpNetworkConnection::ConnectionType connectionType;
247
248#ifndef QT_NO_SSL
249 std::shared_ptr<QSslContext> sslContext;
250#endif
251
252 QHttp2Configuration http2Parameters;
253
254 QString peerVerifyName;
255 // If network status monitoring is enabled, we activate connectionMonitor
256 // as soons as one of channels managed to connect to host (and we
257 // have a pair of addresses (us,peer).
258 // NETMONTODO: consider activating a monitor on a change from
259 // HostLookUp state to ConnectingState (means we have both
260 // local/remote addresses known and can start monitoring this
261 // early).
262 QNetworkConnectionMonitor connectionMonitor;
263
264 friend class QHttpNetworkConnectionChannel;
265};
266
267
268
269QT_END_NAMESPACE
270
271#endif
272

source code of qtbase/src/network/access/qhttpnetworkconnection_p.h