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

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