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 QHTTP2PROTOCOLHANDLER_P_H |
41 | #define QHTTP2PROTOCOLHANDLER_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 <private/qhttpnetworkconnectionchannel_p.h> |
55 | #include <private/qabstractprotocolhandler_p.h> |
56 | #include <private/qhttpnetworkrequest_p.h> |
57 | |
58 | #include <access/qhttp2configuration.h> |
59 | |
60 | #include <private/http2protocol_p.h> |
61 | #include <private/http2streams_p.h> |
62 | #include <private/http2frames_p.h> |
63 | #include <private/hpacktable_p.h> |
64 | #include <private/hpack_p.h> |
65 | |
66 | #include <QtCore/qnamespace.h> |
67 | #include <QtCore/qbytearray.h> |
68 | #include <QtCore/qglobal.h> |
69 | #include <QtCore/qobject.h> |
70 | #include <QtCore/qflags.h> |
71 | #include <QtCore/qhash.h> |
72 | |
73 | #include <vector> |
74 | #include <limits> |
75 | #include <deque> |
76 | #include <set> |
77 | |
78 | QT_REQUIRE_CONFIG(http); |
79 | |
80 | QT_BEGIN_NAMESPACE |
81 | |
82 | class QHttp2ProtocolHandler : public QObject, public QAbstractProtocolHandler |
83 | { |
84 | Q_OBJECT |
85 | |
86 | public: |
87 | QHttp2ProtocolHandler(QHttpNetworkConnectionChannel *channel); |
88 | |
89 | QHttp2ProtocolHandler(const QHttp2ProtocolHandler &rhs) = delete; |
90 | QHttp2ProtocolHandler(QHttp2ProtocolHandler &&rhs) = delete; |
91 | |
92 | QHttp2ProtocolHandler &operator = (const QHttp2ProtocolHandler &rhs) = delete; |
93 | QHttp2ProtocolHandler &operator = (QHttp2ProtocolHandler &&rhs) = delete; |
94 | |
95 | Q_INVOKABLE void handleConnectionClosure(); |
96 | Q_INVOKABLE void ensureClientPrefaceSent(); |
97 | |
98 | private slots: |
99 | void _q_uploadDataReadyRead(); |
100 | void _q_replyDestroyed(QObject* reply); |
101 | void _q_uploadDataDestroyed(QObject* uploadData); |
102 | |
103 | private: |
104 | using Stream = Http2::Stream; |
105 | |
106 | void _q_readyRead() override; |
107 | Q_INVOKABLE void _q_receiveReply() override; |
108 | Q_INVOKABLE bool sendRequest() override; |
109 | |
110 | bool sendClientPreface(); |
111 | bool sendSETTINGS_ACK(); |
112 | bool sendHEADERS(Stream &stream); |
113 | bool sendDATA(Stream &stream); |
114 | Q_INVOKABLE bool sendWINDOW_UPDATE(quint32 streamID, quint32 delta); |
115 | bool sendRST_STREAM(quint32 streamID, quint32 errorCoder); |
116 | bool sendGOAWAY(quint32 errorCode); |
117 | |
118 | void handleDATA(); |
119 | void handleHEADERS(); |
120 | void handlePRIORITY(); |
121 | void handleRST_STREAM(); |
122 | void handleSETTINGS(); |
123 | void handlePUSH_PROMISE(); |
124 | void handlePING(); |
125 | void handleGOAWAY(); |
126 | void handleWINDOW_UPDATE(); |
127 | void handleCONTINUATION(); |
128 | |
129 | void handleContinuedHEADERS(); |
130 | |
131 | bool acceptSetting(Http2::Settings identifier, quint32 newValue); |
132 | |
133 | void updateStream(Stream &stream, const HPack::HttpHeader &, |
134 | Qt::ConnectionType connectionType = Qt::DirectConnection); |
135 | void updateStream(Stream &stream, const Http2::Frame &dataFrame, |
136 | Qt::ConnectionType connectionType = Qt::DirectConnection); |
137 | void finishStream(Stream &stream, Qt::ConnectionType connectionType = Qt::DirectConnection); |
138 | // Error code send by a peer (GOAWAY/RST_STREAM): |
139 | void finishStreamWithError(Stream &stream, quint32 errorCode); |
140 | // Locally encountered error: |
141 | void finishStreamWithError(Stream &stream, QNetworkReply::NetworkError error, |
142 | const QString &message); |
143 | |
144 | // Stream's lifecycle management: |
145 | quint32 createNewStream(const HttpMessagePair &message, bool uploadDone = false); |
146 | void addToSuspended(Stream &stream); |
147 | void markAsReset(quint32 streamID); |
148 | quint32 popStreamToResume(); |
149 | void removeFromSuspended(quint32 streamID); |
150 | void deleteActiveStream(quint32 streamID); |
151 | bool streamWasReset(quint32 streamID) const; |
152 | |
153 | bool prefaceSent = false; |
154 | // In the current implementation we send |
155 | // SETTINGS only once, immediately after |
156 | // the client's preface 24-byte message. |
157 | bool waitingForSettingsACK = false; |
158 | |
159 | static const quint32 maxAcceptableTableSize = 16 * HPack::FieldLookupTable::DefaultSize; |
160 | // HTTP/2 4.3: Header compression is stateful. One compression context and |
161 | // one decompression context are used for the entire connection. |
162 | HPack::Decoder decoder; |
163 | HPack::Encoder encoder; |
164 | |
165 | QHash<QObject *, int> streamIDs; |
166 | QHash<quint32, Stream> activeStreams; |
167 | std::deque<quint32> suspendedStreams[3]; // 3 for priorities: High, Normal, Low. |
168 | static const std::deque<quint32>::size_type maxRecycledStreams; |
169 | std::deque<quint32> recycledStreams; |
170 | |
171 | // Peer's max frame size (this min is the default value |
172 | // we start with, that can be updated by SETTINGS frame): |
173 | quint32 maxFrameSize = Http2::minPayloadLimit; |
174 | |
175 | Http2::FrameReader frameReader; |
176 | Http2::Frame inboundFrame; |
177 | Http2::FrameWriter frameWriter; |
178 | // Temporary storage to assemble HEADERS' block |
179 | // from several CONTINUATION frames ... |
180 | bool continuationExpected = false; |
181 | std::vector<Http2::Frame> continuedFrames; |
182 | |
183 | // Control flow: |
184 | |
185 | // This is how many concurrent streams our peer allows us, 100 is the |
186 | // initial value, can be updated by the server's SETTINGS frame(s): |
187 | quint32 maxConcurrentStreams = Http2::maxConcurrentStreams; |
188 | // While we allow sending SETTTINGS_MAX_CONCURRENT_STREAMS to limit our peer, |
189 | // it's just a hint and we do not actually enforce it (and we can continue |
190 | // sending requests and creating streams while maxConcurrentStreams allows). |
191 | |
192 | // This is our (client-side) maximum possible receive window size, we set |
193 | // it in a ctor from QHttp2Configuration, it does not change after that. |
194 | // The default is 64Kb: |
195 | qint32 maxSessionReceiveWindowSize = Http2::defaultSessionWindowSize; |
196 | |
197 | // Our session current receive window size, updated in a ctor from |
198 | // QHttp2Configuration. Signed integer since it can become negative |
199 | // (it's still a valid window size). |
200 | qint32 sessionReceiveWindowSize = Http2::defaultSessionWindowSize; |
201 | // Our per-stream receive window size, default is 64 Kb, will be updated |
202 | // from QHttp2Configuration. Again, signed - can become negative. |
203 | qint32 streamInitialReceiveWindowSize = Http2::defaultSessionWindowSize; |
204 | |
205 | // These are our peer's receive window sizes, they will be updated by the |
206 | // peer's SETTINGS and WINDOW_UPDATE frames, defaults presumed to be 64Kb. |
207 | qint32 sessionSendWindowSize = Http2::defaultSessionWindowSize; |
208 | qint32 streamInitialSendWindowSize = Http2::defaultSessionWindowSize; |
209 | |
210 | // Our peer's header size limitations. It's unlimited by default, but can |
211 | // be changed via peer's SETTINGS frame. |
212 | quint32 maxHeaderListSize = (std::numeric_limits<quint32>::max)(); |
213 | // While we can send SETTINGS_MAX_HEADER_LIST_SIZE value (our limit on |
214 | // the headers size), we never enforce it, it's just a hint to our peer. |
215 | |
216 | Q_INVOKABLE void resumeSuspendedStreams(); |
217 | // Our stream IDs (all odd), the first valid will be 1. |
218 | quint32 nextID = 1; |
219 | quint32 allocateStreamID(); |
220 | bool validPeerStreamID() const; |
221 | bool goingAway = false; |
222 | bool pushPromiseEnabled = false; |
223 | quint32 lastPromisedID = Http2::connectionStreamID; |
224 | QHash<QString, Http2::PushPromise> promisedData; |
225 | bool tryReserveStream(const Http2::Frame &pushPromiseFrame, |
226 | const HPack::HttpHeader &); |
227 | void resetPromisedStream(const Http2::Frame &pushPromiseFrame, |
228 | Http2::Http2Error reason); |
229 | void initReplyFromPushPromise(const HttpMessagePair &message, |
230 | const QString &cacheKey); |
231 | // Errors: |
232 | void connectionError(Http2::Http2Error errorCode, |
233 | const char *message); |
234 | void closeSession(); |
235 | }; |
236 | |
237 | QT_END_NAMESPACE |
238 | |
239 | #endif |
240 | |