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 | #include "http2frames_p.h" |
41 | |
42 | #include <QtNetwork/qabstractsocket.h> |
43 | |
44 | #include <algorithm> |
45 | #include <utility> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | namespace Http2 |
50 | { |
51 | |
52 | // HTTP/2 frames are defined by RFC7540, clauses 4 and 6. |
53 | |
54 | Frame::Frame() |
55 | : buffer(frameHeaderSize) |
56 | { |
57 | } |
58 | |
59 | FrameType Frame::type() const |
60 | { |
61 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
62 | |
63 | if (int(buffer[3]) >= int(FrameType::LAST_FRAME_TYPE)) |
64 | return FrameType::LAST_FRAME_TYPE; |
65 | |
66 | return FrameType(buffer[3]); |
67 | } |
68 | |
69 | quint32 Frame::streamID() const |
70 | { |
71 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
72 | return qFromBigEndian<quint32>(&buffer[5]); |
73 | } |
74 | |
75 | FrameFlags Frame::flags() const |
76 | { |
77 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
78 | return FrameFlags(buffer[4]); |
79 | } |
80 | |
81 | quint32 Frame::payloadSize() const |
82 | { |
83 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
84 | return buffer[0] << 16 | buffer[1] << 8 | buffer[2]; |
85 | } |
86 | |
87 | uchar Frame::padding() const |
88 | { |
89 | Q_ASSERT(validateHeader() == FrameStatus::goodFrame); |
90 | |
91 | if (!flags().testFlag(FrameFlag::PADDED)) |
92 | return 0; |
93 | |
94 | switch (type()) { |
95 | case FrameType::DATA: |
96 | case FrameType::PUSH_PROMISE: |
97 | case FrameType::HEADERS: |
98 | Q_ASSERT(buffer.size() > frameHeaderSize); |
99 | return buffer[frameHeaderSize]; |
100 | default: |
101 | return 0; |
102 | } |
103 | } |
104 | |
105 | bool Frame::priority(quint32 *streamID, uchar *weight) const |
106 | { |
107 | Q_ASSERT(validatePayload() == FrameStatus::goodFrame); |
108 | |
109 | if (buffer.size() <= frameHeaderSize) |
110 | return false; |
111 | |
112 | const uchar *src = &buffer[0] + frameHeaderSize; |
113 | if (type() == FrameType::HEADERS && flags().testFlag(FrameFlag::PADDED)) |
114 | ++src; |
115 | |
116 | if ((type() == FrameType::HEADERS && flags().testFlag(FrameFlag::PRIORITY)) |
117 | || type() == FrameType::PRIORITY) { |
118 | if (streamID) |
119 | *streamID = qFromBigEndian<quint32>(src); |
120 | if (weight) |
121 | *weight = src[4]; |
122 | return true; |
123 | } |
124 | |
125 | return false; |
126 | } |
127 | |
128 | FrameStatus Frame::() const |
129 | { |
130 | // Should be called only on a frame with |
131 | // a complete header. |
132 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
133 | |
134 | const auto framePayloadSize = payloadSize(); |
135 | // 4.2 Frame Size |
136 | if (framePayloadSize > maxPayloadSize) |
137 | return FrameStatus::sizeError; |
138 | |
139 | switch (type()) { |
140 | case FrameType::SETTINGS: |
141 | // SETTINGS ACK can not have any payload. |
142 | // The payload of a SETTINGS frame consists of zero |
143 | // or more parameters, each consisting of an unsigned |
144 | // 16-bit setting identifier and an unsigned 32-bit value. |
145 | // Thus the payload size must be a multiple of 6. |
146 | if (flags().testFlag(FrameFlag::ACK) ? framePayloadSize : framePayloadSize % 6) |
147 | return FrameStatus::sizeError; |
148 | break; |
149 | case FrameType::PRIORITY: |
150 | // 6.3 PRIORITY |
151 | if (framePayloadSize != 5) |
152 | return FrameStatus::sizeError; |
153 | break; |
154 | case FrameType::PING: |
155 | // 6.7 PING |
156 | if (framePayloadSize != 8) |
157 | return FrameStatus::sizeError; |
158 | break; |
159 | case FrameType::GOAWAY: |
160 | // 6.8 GOAWAY |
161 | if (framePayloadSize < 8) |
162 | return FrameStatus::sizeError; |
163 | break; |
164 | case FrameType::RST_STREAM: |
165 | case FrameType::WINDOW_UPDATE: |
166 | // 6.4 RST_STREAM, 6.9 WINDOW_UPDATE |
167 | if (framePayloadSize != 4) |
168 | return FrameStatus::sizeError; |
169 | break; |
170 | case FrameType::PUSH_PROMISE: |
171 | // 6.6 PUSH_PROMISE |
172 | if (framePayloadSize < 4) |
173 | return FrameStatus::sizeError; |
174 | default: |
175 | // DATA/HEADERS/CONTINUATION will be verified |
176 | // when we have payload. |
177 | // Frames of unknown types are ignored (5.1) |
178 | break; |
179 | } |
180 | |
181 | return FrameStatus::goodFrame; |
182 | } |
183 | |
184 | FrameStatus Frame::validatePayload() const |
185 | { |
186 | // Should be called only on a complete frame with a valid header. |
187 | Q_ASSERT(validateHeader() == FrameStatus::goodFrame); |
188 | |
189 | // Ignored, 5.1 |
190 | if (type() == FrameType::LAST_FRAME_TYPE) |
191 | return FrameStatus::goodFrame; |
192 | |
193 | auto size = payloadSize(); |
194 | Q_ASSERT(buffer.size() >= frameHeaderSize && size == buffer.size() - frameHeaderSize); |
195 | |
196 | const uchar *src = size ? &buffer[0] + frameHeaderSize : nullptr; |
197 | const auto frameFlags = flags(); |
198 | switch (type()) { |
199 | // 6.1 DATA, 6.2 HEADERS |
200 | case FrameType::DATA: |
201 | case FrameType::HEADERS: |
202 | if (frameFlags.testFlag(FrameFlag::PADDED)) { |
203 | if (!size || size < src[0]) |
204 | return FrameStatus::sizeError; |
205 | size -= src[0]; |
206 | } |
207 | if (type() == FrameType::HEADERS && frameFlags.testFlag(FrameFlag::PRIORITY)) { |
208 | if (size < 5) |
209 | return FrameStatus::sizeError; |
210 | } |
211 | break; |
212 | // 6.6 PUSH_PROMISE |
213 | case FrameType::PUSH_PROMISE: |
214 | if (frameFlags.testFlag(FrameFlag::PADDED)) { |
215 | if (!size || size < src[0]) |
216 | return FrameStatus::sizeError; |
217 | size -= src[0]; |
218 | } |
219 | |
220 | if (size < 4) |
221 | return FrameStatus::sizeError; |
222 | break; |
223 | default: |
224 | break; |
225 | } |
226 | |
227 | return FrameStatus::goodFrame; |
228 | } |
229 | |
230 | |
231 | quint32 Frame::dataSize() const |
232 | { |
233 | Q_ASSERT(validatePayload() == FrameStatus::goodFrame); |
234 | |
235 | quint32 size = payloadSize(); |
236 | if (const uchar pad = padding()) { |
237 | // + 1 one for a byte with padding number itself: |
238 | size -= pad + 1; |
239 | } |
240 | |
241 | if (priority()) |
242 | size -= 5; |
243 | |
244 | return size; |
245 | } |
246 | |
247 | quint32 Frame::hpackBlockSize() const |
248 | { |
249 | Q_ASSERT(validatePayload() == FrameStatus::goodFrame); |
250 | |
251 | const auto frameType = type(); |
252 | Q_ASSERT(frameType == FrameType::HEADERS || |
253 | frameType == FrameType::PUSH_PROMISE || |
254 | frameType == FrameType::CONTINUATION); |
255 | |
256 | quint32 size = dataSize(); |
257 | if (frameType == FrameType::PUSH_PROMISE) { |
258 | Q_ASSERT(size >= 4); |
259 | size -= 4; |
260 | } |
261 | |
262 | return size; |
263 | } |
264 | |
265 | const uchar *Frame::dataBegin() const |
266 | { |
267 | Q_ASSERT(validatePayload() == FrameStatus::goodFrame); |
268 | if (buffer.size() <= frameHeaderSize) |
269 | return nullptr; |
270 | |
271 | const uchar *src = &buffer[0] + frameHeaderSize; |
272 | if (padding()) |
273 | ++src; |
274 | |
275 | if (priority()) |
276 | src += 5; |
277 | |
278 | return src; |
279 | } |
280 | |
281 | const uchar *Frame::hpackBlockBegin() const |
282 | { |
283 | Q_ASSERT(validatePayload() == FrameStatus::goodFrame); |
284 | |
285 | const auto frameType = type(); |
286 | Q_ASSERT(frameType == FrameType::HEADERS || |
287 | frameType == FrameType::PUSH_PROMISE || |
288 | frameType == FrameType::CONTINUATION); |
289 | |
290 | const uchar *begin = dataBegin(); |
291 | if (frameType == FrameType::PUSH_PROMISE) |
292 | begin += 4; // That's a promised stream, skip it. |
293 | return begin; |
294 | } |
295 | |
296 | FrameStatus FrameReader::read(QAbstractSocket &socket) |
297 | { |
298 | if (offset < frameHeaderSize) { |
299 | if (!readHeader(socket)) |
300 | return FrameStatus::incompleteFrame; |
301 | |
302 | const auto status = frame.validateHeader(); |
303 | if (status != FrameStatus::goodFrame) { |
304 | // No need to read any payload. |
305 | return status; |
306 | } |
307 | |
308 | if (Http2PredefinedParameters::maxPayloadSize < frame.payloadSize()) |
309 | return FrameStatus::sizeError; |
310 | |
311 | frame.buffer.resize(frame.payloadSize() + frameHeaderSize); |
312 | } |
313 | |
314 | if (offset < frame.buffer.size() && !readPayload(socket)) |
315 | return FrameStatus::incompleteFrame; |
316 | |
317 | // Reset the offset, our frame can be re-used |
318 | // now (re-read): |
319 | offset = 0; |
320 | |
321 | return frame.validatePayload(); |
322 | } |
323 | |
324 | bool FrameReader::(QAbstractSocket &socket) |
325 | { |
326 | Q_ASSERT(offset < frameHeaderSize); |
327 | |
328 | auto &buffer = frame.buffer; |
329 | if (buffer.size() < frameHeaderSize) |
330 | buffer.resize(frameHeaderSize); |
331 | |
332 | const auto chunkSize = socket.read(reinterpret_cast<char *>(&buffer[offset]), |
333 | frameHeaderSize - offset); |
334 | if (chunkSize > 0) |
335 | offset += chunkSize; |
336 | |
337 | return offset == frameHeaderSize; |
338 | } |
339 | |
340 | bool FrameReader::readPayload(QAbstractSocket &socket) |
341 | { |
342 | Q_ASSERT(offset < frame.buffer.size()); |
343 | Q_ASSERT(frame.buffer.size() > frameHeaderSize); |
344 | |
345 | auto &buffer = frame.buffer; |
346 | // Casts and ugliness - to deal with MSVC. Values are guaranteed to fit into quint32. |
347 | const auto chunkSize = socket.read(reinterpret_cast<char *>(&buffer[offset]), |
348 | qint64(buffer.size() - offset)); |
349 | if (chunkSize > 0) |
350 | offset += quint32(chunkSize); |
351 | |
352 | return offset == buffer.size(); |
353 | } |
354 | |
355 | FrameWriter::FrameWriter() |
356 | { |
357 | } |
358 | |
359 | FrameWriter::FrameWriter(FrameType type, FrameFlags flags, quint32 streamID) |
360 | { |
361 | start(type, flags, streamID); |
362 | } |
363 | |
364 | void FrameWriter::setOutboundFrame(Frame &&newFrame) |
365 | { |
366 | frame = std::move(newFrame); |
367 | updatePayloadSize(); |
368 | } |
369 | |
370 | void FrameWriter::start(FrameType type, FrameFlags flags, quint32 streamID) |
371 | { |
372 | auto &buffer = frame.buffer; |
373 | |
374 | buffer.resize(frameHeaderSize); |
375 | // The first three bytes - payload size, which is 0 for now. |
376 | buffer[0] = 0; |
377 | buffer[1] = 0; |
378 | buffer[2] = 0; |
379 | |
380 | buffer[3] = uchar(type); |
381 | buffer[4] = uchar(flags); |
382 | |
383 | qToBigEndian(streamID, &buffer[5]); |
384 | } |
385 | |
386 | void FrameWriter::setPayloadSize(quint32 size) |
387 | { |
388 | auto &buffer = frame.buffer; |
389 | |
390 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
391 | Q_ASSERT(size <= maxPayloadSize); |
392 | |
393 | buffer[0] = size >> 16; |
394 | buffer[1] = size >> 8; |
395 | buffer[2] = size; |
396 | } |
397 | |
398 | void FrameWriter::setType(FrameType type) |
399 | { |
400 | Q_ASSERT(frame.buffer.size() >= frameHeaderSize); |
401 | frame.buffer[3] = uchar(type); |
402 | } |
403 | |
404 | void FrameWriter::setFlags(FrameFlags flags) |
405 | { |
406 | Q_ASSERT(frame.buffer.size() >= frameHeaderSize); |
407 | frame.buffer[4] = uchar(flags); |
408 | } |
409 | |
410 | void FrameWriter::addFlag(FrameFlag flag) |
411 | { |
412 | setFlags(frame.flags() | flag); |
413 | } |
414 | |
415 | void FrameWriter::append(const uchar *begin, const uchar *end) |
416 | { |
417 | Q_ASSERT(begin && end); |
418 | Q_ASSERT(begin < end); |
419 | |
420 | frame.buffer.insert(frame.buffer.end(), begin, end); |
421 | updatePayloadSize(); |
422 | } |
423 | |
424 | void FrameWriter::updatePayloadSize() |
425 | { |
426 | const quint32 size = quint32(frame.buffer.size() - frameHeaderSize); |
427 | Q_ASSERT(size <= maxPayloadSize); |
428 | setPayloadSize(size); |
429 | } |
430 | |
431 | bool FrameWriter::write(QAbstractSocket &socket) const |
432 | { |
433 | auto &buffer = frame.buffer; |
434 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
435 | // Do some sanity check first: |
436 | |
437 | Q_ASSERT(int(frame.type()) < int(FrameType::LAST_FRAME_TYPE)); |
438 | Q_ASSERT(frame.validateHeader() == FrameStatus::goodFrame); |
439 | |
440 | const auto nWritten = socket.write(reinterpret_cast<const char *>(&buffer[0]), |
441 | buffer.size()); |
442 | return nWritten != -1 && size_type(nWritten) == buffer.size(); |
443 | } |
444 | |
445 | bool FrameWriter::(QAbstractSocket &socket, quint32 sizeLimit) |
446 | { |
447 | auto &buffer = frame.buffer; |
448 | Q_ASSERT(buffer.size() >= frameHeaderSize); |
449 | |
450 | if (sizeLimit > quint32(maxPayloadSize)) |
451 | sizeLimit = quint32(maxPayloadSize); |
452 | |
453 | if (quint32(buffer.size() - frameHeaderSize) <= sizeLimit) { |
454 | addFlag(FrameFlag::END_HEADERS); |
455 | updatePayloadSize(); |
456 | return write(socket); |
457 | } |
458 | |
459 | // Our HPACK block does not fit into the size limit, remove |
460 | // END_HEADERS bit from the first frame, we'll later set |
461 | // it on the last CONTINUATION frame: |
462 | setFlags(frame.flags() & ~FrameFlags(FrameFlag::END_HEADERS)); |
463 | // Write a frame's header (not controlled by sizeLimit) and |
464 | // as many bytes of payload as we can within sizeLimit, |
465 | // then send CONTINUATION frames, as needed. |
466 | setPayloadSize(sizeLimit); |
467 | const quint32 firstChunkSize = frameHeaderSize + sizeLimit; |
468 | qint64 written = socket.write(reinterpret_cast<const char *>(&buffer[0]), |
469 | firstChunkSize); |
470 | |
471 | if (written != qint64(firstChunkSize)) |
472 | return false; |
473 | |
474 | FrameWriter continuationWriter(FrameType::CONTINUATION, FrameFlag::EMPTY, frame.streamID()); |
475 | quint32 offset = firstChunkSize; |
476 | |
477 | while (offset != buffer.size()) { |
478 | const auto chunkSize = std::min(sizeLimit, quint32(buffer.size() - offset)); |
479 | if (chunkSize + offset == buffer.size()) |
480 | continuationWriter.addFlag(FrameFlag::END_HEADERS); |
481 | continuationWriter.setPayloadSize(chunkSize); |
482 | if (!continuationWriter.write(socket)) |
483 | return false; |
484 | written = socket.write(reinterpret_cast<const char *>(&buffer[offset]), |
485 | chunkSize); |
486 | if (written != qint64(chunkSize)) |
487 | return false; |
488 | |
489 | offset += chunkSize; |
490 | } |
491 | |
492 | return true; |
493 | } |
494 | |
495 | bool FrameWriter::writeDATA(QAbstractSocket &socket, quint32 sizeLimit, |
496 | const uchar *src, quint32 size) |
497 | { |
498 | // With DATA frame(s) we always have: |
499 | // 1) frame's header (9 bytes) |
500 | // 2) a separate payload (from QNonContiguousByteDevice). |
501 | // We either fit within a sizeLimit, or split into several |
502 | // DATA frames. |
503 | |
504 | Q_ASSERT(src); |
505 | |
506 | if (sizeLimit > quint32(maxPayloadSize)) |
507 | sizeLimit = quint32(maxPayloadSize); |
508 | // We NEVER set END_STREAM, since QHttp2ProtocolHandler works with |
509 | // QNonContiguousByteDevice and this 'writeDATA' is probably |
510 | // not the last one for a given request. |
511 | // This has to be done externally (sending an empty DATA frame with END_STREAM). |
512 | for (quint32 offset = 0; offset != size;) { |
513 | const auto chunkSize = std::min(size - offset, sizeLimit); |
514 | setPayloadSize(chunkSize); |
515 | // Frame's header first: |
516 | if (!write(socket)) |
517 | return false; |
518 | // Payload (if any): |
519 | if (chunkSize) { |
520 | const auto written = socket.write(reinterpret_cast<const char*>(src + offset), |
521 | chunkSize); |
522 | if (written != qint64(chunkSize)) |
523 | return false; |
524 | } |
525 | |
526 | offset += chunkSize; |
527 | } |
528 | |
529 | return true; |
530 | } |
531 | |
532 | } // Namespace Http2 |
533 | |
534 | QT_END_NAMESPACE |
535 | |