| 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 | |
| 40 | QT_REQUIRE_CONFIG(http); |
| 41 | |
| 42 | QT_BEGIN_NAMESPACE |
| 43 | |
| 44 | class QHttpNetworkRequest; |
| 45 | class QHttpNetworkReply; |
| 46 | class QHttpThreadDelegate; |
| 47 | class QByteArray; |
| 48 | class QHostInfo; |
| 49 | #ifndef QT_NO_SSL |
| 50 | class QSslConfiguration; |
| 51 | class QSslContext; |
| 52 | #endif // !QT_NO_SSL |
| 53 | |
| 54 | class QHttpNetworkConnectionPrivate; |
| 55 | class Q_NETWORK_EXPORT QHttpNetworkConnection : public QObject |
| 56 | { |
| 57 | Q_OBJECT |
| 58 | public: |
| 59 | |
| 60 | enum ConnectionType { |
| 61 | ConnectionTypeHTTP, |
| 62 | ConnectionTypeHTTP2, |
| 63 | ConnectionTypeHTTP2Direct |
| 64 | }; |
| 65 | |
| 66 | QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, |
| 67 | bool encrypt = false, bool isLocalSocket = false, |
| 68 | QObject *parent = nullptr, |
| 69 | ConnectionType connectionType = ConnectionTypeHTTP); |
| 70 | ~QHttpNetworkConnection(); |
| 71 | |
| 72 | //The hostname to which this is connected to. |
| 73 | QString hostName() const; |
| 74 | //The HTTP port in use. |
| 75 | quint16 port() const; |
| 76 | |
| 77 | //add a new HTTP request through this connection |
| 78 | QHttpNetworkReply* sendRequest(const QHttpNetworkRequest &request); |
| 79 | void fillHttp2Queue(); |
| 80 | |
| 81 | #ifndef QT_NO_NETWORKPROXY |
| 82 | //set the proxy for this connection |
| 83 | void setCacheProxy(const QNetworkProxy &networkProxy); |
| 84 | QNetworkProxy cacheProxy() const; |
| 85 | void setTransparentProxy(const QNetworkProxy &networkProxy); |
| 86 | QNetworkProxy transparentProxy() const; |
| 87 | #endif |
| 88 | |
| 89 | bool isSsl() const; |
| 90 | |
| 91 | QHttpNetworkConnectionChannel *channels() const; |
| 92 | |
| 93 | ConnectionType connectionType() const; |
| 94 | void setConnectionType(ConnectionType type); |
| 95 | |
| 96 | QHttp2Configuration http2Parameters() const; |
| 97 | void setHttp2Parameters(const QHttp2Configuration ¶ms); |
| 98 | |
| 99 | #ifndef QT_NO_SSL |
| 100 | void setSslConfiguration(const QSslConfiguration &config); |
| 101 | void ignoreSslErrors(int channel = -1); |
| 102 | void ignoreSslErrors(const QList<QSslError> &errors, int channel = -1); |
| 103 | std::shared_ptr<QSslContext> sslContext() const; |
| 104 | void setSslContext(std::shared_ptr<QSslContext> context); |
| 105 | #endif |
| 106 | |
| 107 | void preConnectFinished(); |
| 108 | |
| 109 | QString peerVerifyName() const; |
| 110 | void setPeerVerifyName(const QString &peerName); |
| 111 | |
| 112 | public slots: |
| 113 | void onlineStateChanged(bool isOnline); |
| 114 | |
| 115 | private: |
| 116 | Q_DECLARE_PRIVATE(QHttpNetworkConnection) |
| 117 | Q_DISABLE_COPY_MOVE(QHttpNetworkConnection) |
| 118 | friend class QHttpThreadDelegate; |
| 119 | friend class QHttpNetworkReply; |
| 120 | friend class QHttpNetworkReplyPrivate; |
| 121 | friend class QHttpNetworkConnectionChannel; |
| 122 | friend class QHttp2ProtocolHandler; |
| 123 | friend class QHttpProtocolHandler; |
| 124 | |
| 125 | Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest()) |
| 126 | Q_PRIVATE_SLOT(d_func(), void _q_hostLookupFinished(QHostInfo)) |
| 127 | Q_PRIVATE_SLOT(d_func(), void _q_connectDelayedChannel()) |
| 128 | }; |
| 129 | |
| 130 | |
| 131 | // private classes |
| 132 | typedef QPair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair; |
| 133 | |
| 134 | |
| 135 | class QHttpNetworkConnectionPrivate : public QObjectPrivate |
| 136 | { |
| 137 | Q_DECLARE_PUBLIC(QHttpNetworkConnection) |
| 138 | Q_DISABLE_COPY_MOVE(QHttpNetworkConnectionPrivate) |
| 139 | public: |
| 140 | // Note: Only used from auto tests, normal usage is via QHttp1Configuration |
| 141 | static constexpr int defaultHttpChannelCount = 6; |
| 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(quint16 connectionCount, const QString &hostName, quint16 port, |
| 159 | bool encrypt, bool isLocalSocket, |
| 160 | QHttpNetworkConnection::ConnectionType type); |
| 161 | ~QHttpNetworkConnectionPrivate(); |
| 162 | void init(); |
| 163 | |
| 164 | void pauseConnection(); |
| 165 | void resumeConnection(); |
| 166 | ConnectionState state = RunningState; |
| 167 | NetworkLayerPreferenceState networkLayerState = Unknown; |
| 168 | |
| 169 | enum { ChunkSize = 4096 }; |
| 170 | |
| 171 | int indexOf(QIODevice *socket) const; |
| 172 | |
| 173 | QHttpNetworkReply *queueRequest(const QHttpNetworkRequest &request); |
| 174 | void requeueRequest(const HttpMessagePair &pair); // e.g. after pipeline broke |
| 175 | void fillHttp2Queue(); |
| 176 | bool dequeueRequest(QIODevice *socket); |
| 177 | void prepareRequest(HttpMessagePair &request); |
| 178 | void updateChannel(int i, const HttpMessagePair &messagePair); |
| 179 | QHttpNetworkRequest predictNextRequest() const; |
| 180 | QHttpNetworkReply* predictNextRequestsReply() const; |
| 181 | |
| 182 | void fillPipeline(QIODevice *socket); |
| 183 | bool fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel); |
| 184 | |
| 185 | // read more HTTP body after the next event loop spin |
| 186 | void readMoreLater(QHttpNetworkReply *reply); |
| 187 | |
| 188 | void copyCredentials(int fromChannel, QAuthenticator *auth, bool isProxy); |
| 189 | |
| 190 | void startHostInfoLookup(); |
| 191 | void startNetworkLayerStateLookup(); |
| 192 | void networkLayerDetected(QAbstractSocket::NetworkLayerProtocol protocol); |
| 193 | |
| 194 | // private slots |
| 195 | void _q_startNextRequest(); // send the next request from the queue |
| 196 | |
| 197 | void _q_hostLookupFinished(const QHostInfo &info); |
| 198 | void _q_connectDelayedChannel(); |
| 199 | |
| 200 | void createAuthorization(QIODevice *socket, QHttpNetworkRequest &request); |
| 201 | |
| 202 | QString errorDetail(QNetworkReply::NetworkError errorCode, QIODevice *socket, |
| 203 | const QString & = QString()); |
| 204 | |
| 205 | void removeReply(QHttpNetworkReply *reply); |
| 206 | |
| 207 | QString hostName; |
| 208 | quint16 port; |
| 209 | bool encrypt; |
| 210 | bool isLocalSocket; |
| 211 | bool delayIpv4 = true; |
| 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 * const channels; // parallel connections to the server |
| 219 | bool shouldEmitChannelError(QIODevice *socket); |
| 220 | |
| 221 | qint64 uncompressedBytesAvailable(const QHttpNetworkReply &reply) const; |
| 222 | qint64 uncompressedBytesAvailableNextBlock(const QHttpNetworkReply &reply) const; |
| 223 | |
| 224 | |
| 225 | void emitReplyError(QIODevice *socket, QHttpNetworkReply *reply, QNetworkReply::NetworkError errorCode); |
| 226 | bool handleAuthenticateChallenge(QIODevice *socket, QHttpNetworkReply *reply, bool isProxy, bool &resend); |
| 227 | struct ParseRedirectResult { |
| 228 | QUrl redirectUrl; |
| 229 | QNetworkReply::NetworkError errorCode; |
| 230 | }; |
| 231 | static ParseRedirectResult parseRedirectResponse(QHttpNetworkReply *reply); |
| 232 | // Used by the HTTP1 code-path |
| 233 | QUrl parseRedirectResponse(QIODevice *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 = 0; |
| 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 | |
| 269 | QT_END_NAMESPACE |
| 270 | |
| 271 | #endif |
| 272 | |