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 HTTP2PROTOCOL_P_H |
41 | #define HTTP2PROTOCOL_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 <QtNetwork/qnetworkreply.h> |
55 | #include <QtCore/qloggingcategory.h> |
56 | #include <QtCore/qmetatype.h> |
57 | #include <QtCore/qglobal.h> |
58 | |
59 | // Different HTTP/2 constants/values as defined by RFC 7540. |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | |
63 | class QHttpNetworkRequest; |
64 | class QHttpNetworkReply; |
65 | class QByteArray; |
66 | class QString; |
67 | |
68 | namespace Http2 |
69 | { |
70 | |
71 | enum class Settings : quint16 |
72 | { |
73 | = 0x1, |
74 | ENABLE_PUSH_ID = 0x2, |
75 | MAX_CONCURRENT_STREAMS_ID = 0x3, |
76 | INITIAL_WINDOW_SIZE_ID = 0x4, |
77 | MAX_FRAME_SIZE_ID = 0x5, |
78 | = 0x6 |
79 | }; |
80 | |
81 | enum class FrameType : uchar |
82 | { |
83 | DATA = 0x0, |
84 | = 0x1, |
85 | PRIORITY = 0x2, |
86 | RST_STREAM = 0x3, |
87 | SETTINGS = 0x4, |
88 | PUSH_PROMISE = 0x5, |
89 | PING = 0x6, |
90 | GOAWAY = 0x7, |
91 | WINDOW_UPDATE = 0x8, |
92 | CONTINUATION = 0x9, |
93 | // ATTENTION: enumerators must be sorted. |
94 | // We use LAST_FRAME_TYPE to check if |
95 | // frame type is known, if not - this frame |
96 | // must be ignored, HTTP/2 5.1). |
97 | LAST_FRAME_TYPE |
98 | }; |
99 | |
100 | enum class FrameFlag : uchar |
101 | { |
102 | EMPTY = 0x0, // Valid for any frame type. |
103 | ACK = 0x1, // Valid for PING, SETTINGS |
104 | END_STREAM = 0x1, // Valid for HEADERS, DATA |
105 | = 0x4, // Valid for PUSH_PROMISE, HEADERS, |
106 | PADDED = 0x8, // Valid for PUSH_PROMISE, HEADERS, DATA |
107 | PRIORITY = 0x20 // Valid for HEADERS, |
108 | }; |
109 | |
110 | Q_DECLARE_FLAGS(FrameFlags, FrameFlag) |
111 | Q_DECLARE_OPERATORS_FOR_FLAGS(FrameFlags) |
112 | |
113 | enum Http2PredefinedParameters |
114 | { |
115 | // Old-style enum, so we |
116 | // can use as Http2::frameHeaderSize for example. |
117 | clientPrefaceLength = 24, // HTTP/2, 3.5 |
118 | connectionStreamID = 0, // HTTP/2, 5.1.1 |
119 | = 9, // HTTP/2, 4.1 |
120 | |
121 | // It's our max frame size we send in SETTINGS frame, |
122 | // it's also the default one and we also use it to later |
123 | // validate incoming frames: |
124 | maxFrameSize = 16384, // HTTP/2 6.5.2 |
125 | |
126 | defaultSessionWindowSize = 65535, // HTTP/2 6.5.2 |
127 | maxPayloadSize = (1 << 24) - 1, // HTTP/2 6.5.2 |
128 | // Using 1000 (rather arbitrarily), just to |
129 | // impose *some* upper limit: |
130 | maxPeerConcurrentStreams = 1000, |
131 | maxConcurrentStreams = 100 // HTTP/2, 6.5.2 |
132 | }; |
133 | |
134 | // These are ints, const, they have internal linkage, it's ok to have them in |
135 | // headers - no ODR violation. |
136 | const quint32 lastValidStreamID((quint32(1) << 31) - 1); // HTTP/2, 5.1.1 |
137 | |
138 | // The default size of 64K is too small and limiting: if we use it, we end up |
139 | // sending WINDOW_UPDATE frames on a stream/session all the time, for each |
140 | // 2 DATE frames of size 16K (also default) we'll send a WINDOW_UPDATE frame |
141 | // for a given stream and have a download speed order of magnitude lower than |
142 | // our own HTTP/1.1 protocol handler. We choose a bigger window size: normally, |
143 | // HTTP/2 servers are not afraid to immediately set it to the possible max, |
144 | // we do the same and split this window size between our concurrent streams. |
145 | const qint32 maxSessionReceiveWindowSize((quint32(1) << 31) - 1); |
146 | const qint32 qtDefaultStreamReceiveWindowSize = maxSessionReceiveWindowSize / maxConcurrentStreams; |
147 | |
148 | // The class ProtocolParameters allows client code to customize HTTP/2 protocol |
149 | // handler, if needed. Normally, we use our own default parameters (see below). |
150 | // In 5.10 we can also use setProperty/property on a QNAM object to pass the |
151 | // non-default values to the protocol handler. In 5.11 this will probably become |
152 | // a public API. |
153 | |
154 | using RawSettings = QMap<Settings, quint32>; |
155 | |
156 | struct Q_AUTOTEST_EXPORT ProtocolParameters |
157 | { |
158 | ProtocolParameters(); |
159 | |
160 | bool validate() const; |
161 | QByteArray settingsFrameToBase64() const; |
162 | struct Frame settingsFrame() const; |
163 | void (QHttpNetworkRequest *request) const; |
164 | |
165 | // HPACK: |
166 | // TODO: for now we ignore them (fix it for 5.11, would require changes in HPACK) |
167 | bool useHuffman = true; |
168 | bool indexStrings = true; |
169 | |
170 | // This parameter is not negotiated via SETTINGS frames, so we have it |
171 | // as a member and will convey it to our peer as a WINDOW_UPDATE frame: |
172 | qint32 maxSessionReceiveWindowSize = Http2::maxSessionReceiveWindowSize; |
173 | |
174 | // This is our default SETTINGS frame: |
175 | // |
176 | // SETTINGS_INITIAL_WINDOW_SIZE: (2^31 - 1) / 100 |
177 | // SETTINGS_ENABLE_PUSH: 0. |
178 | // |
179 | // Note, whenever we skip some value in our SETTINGS frame, our peer |
180 | // will assume the defaults recommended by RFC 7540, which in general |
181 | // are good enough, although we (and most browsers) prefer to work |
182 | // with larger window sizes. |
183 | RawSettings settingsFrameData; |
184 | }; |
185 | |
186 | // TODO: remove in 5.11 |
187 | extern const Q_AUTOTEST_EXPORT char *http2ParametersPropertyName; |
188 | |
189 | extern const Q_AUTOTEST_EXPORT char Http2clientPreface[clientPrefaceLength]; |
190 | |
191 | enum class FrameStatus |
192 | { |
193 | protocolError, |
194 | sizeError, |
195 | incompleteFrame, |
196 | goodFrame |
197 | }; |
198 | |
199 | enum Http2Error |
200 | { |
201 | // Old-style enum to avoid excessive name |
202 | // qualification ... |
203 | // NB: |
204 | // I use the last enumerator to check |
205 | // that errorCode (quint32) is valid, |
206 | // so it needs to be the highest-numbered! |
207 | // HTTP/2 7: |
208 | HTTP2_NO_ERROR = 0x0, |
209 | PROTOCOL_ERROR = 0x1, |
210 | INTERNAL_ERROR = 0x2, |
211 | FLOW_CONTROL_ERROR = 0x3, |
212 | SETTINGS_TIMEOUT = 0x4, |
213 | STREAM_CLOSED = 0x5, |
214 | FRAME_SIZE_ERROR = 0x6, |
215 | REFUSE_STREAM = 0x7, |
216 | CANCEL = 0x8, |
217 | COMPRESSION_ERROR = 0x9, |
218 | CONNECT_ERROR = 0xa, |
219 | ENHANCE_YOUR_CALM = 0xb, |
220 | INADEQUATE_SECURITY = 0xc, |
221 | HTTP_1_1_REQUIRED = 0xd |
222 | }; |
223 | |
224 | void qt_error(quint32 errorCode, QNetworkReply::NetworkError &error, QString &errorString); |
225 | QString qt_error_string(quint32 errorCode); |
226 | QNetworkReply::NetworkError qt_error(quint32 errorCode); |
227 | bool is_protocol_upgraded(const QHttpNetworkReply &reply); |
228 | |
229 | } // namespace Http2 |
230 | |
231 | Q_DECLARE_LOGGING_CATEGORY(QT_HTTP2) |
232 | |
233 | QT_END_NAMESPACE |
234 | |
235 | Q_DECLARE_METATYPE(Http2::Settings) |
236 | Q_DECLARE_METATYPE(Http2::ProtocolParameters) |
237 | |
238 | #endif |
239 | |