Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
http2frames_p.h
Go to the documentation of this file.
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:critical reason:network-protocol
4
5#ifndef HTTP2FRAMES_P_H
6#define HTTP2FRAMES_P_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
20#include "hpack_p.h"
21
22#include <QtCore/qendian.h>
23#include <algorithm>
24#include <vector>
25
26QT_BEGIN_NAMESPACE
27
28class QHttp2ProtocolHandler;
29class QIODevice;
30
31namespace Http2
32{
33
34struct Q_AUTOTEST_EXPORT Frame
35{
36 Frame();
37 // Reading these values without first forming a valid frame (either reading
38 // it from a socket or building it) will result in undefined behavior:
39 FrameType type() const;
40 quint32 streamID() const;
41 FrameFlags flags() const;
42 quint32 payloadSize() const;
43 uchar padding() const;
44 // In HTTP/2 a stream's priority is specified by its weight and a stream
45 // (id) it depends on:
46 bool priority(quint32 *streamID = nullptr,
47 uchar *weight = nullptr) const;
48
51
52 // Number of payload bytes without padding and/or priority.
53 quint32 dataSize() const;
54 // HEADERS data size for HEADERS, PUSH_PROMISE and CONTINUATION streams:
55 quint32 hpackBlockSize() const;
56 // Beginning of payload without priority/padding bytes.
57 const uchar *dataBegin() const;
58 // HEADERS data beginning for HEADERS, PUSH_PROMISE and CONTINUATION streams:
59 const uchar *hpackBlockBegin() const;
60
62};
63
64class Q_AUTOTEST_EXPORT FrameReader
65{
66public:
67 FrameStatus read(QIODevice &socket);
68
69 Frame &inboundFrame()
70 {
71 return frame;
72 }
73private:
74 bool readHeader(QIODevice &socket);
75 bool readPayload(QIODevice &socket);
76
77 quint32 offset = 0;
78 Frame frame;
79};
80
81class Q_AUTOTEST_EXPORT FrameWriter
82{
83public:
84 using payload_type = std::vector<uchar>;
85 using size_type = payload_type::size_type;
86
87 FrameWriter();
88 FrameWriter(FrameType type, FrameFlags flags, quint32 streamID);
89
90 Frame &outboundFrame()
91 {
92 return frame;
93 }
94
95 void setOutboundFrame(Frame &&newFrame);
96
97 // Frame 'builders':
98 void start(FrameType type, FrameFlags flags, quint32 streamID);
99 void setPayloadSize(quint32 size);
100 void setType(FrameType type);
101 void setFlags(FrameFlags flags);
102 void addFlag(FrameFlag flag);
103
104 // All append functions also update frame's payload length.
105 template<typename ValueType>
106 void append(ValueType val)
107 {
108 uchar wired[sizeof val] = {};
109 qToBigEndian(val, wired);
110 append(wired, wired + sizeof val);
111 }
112 void append(uchar val)
113 {
114 frame.buffer.push_back(val);
115 updatePayloadSize();
116 }
117 void append(Settings identifier)
118 {
119 append(quint16(identifier));
120 }
121 void append(const payload_type &payload)
122 {
123 append(&payload[0], &payload[0] + payload.size());
124 }
125 void append(QByteArrayView payload)
126 {
127 append(reinterpret_cast<const uchar *>(payload.begin()),
128 reinterpret_cast<const uchar *>(payload.end()));
129 }
130
131 void append(const uchar *begin, const uchar *end);
132
133 // Write as a single frame:
134 bool write(QIODevice &socket) const;
135 // Two types of frames we are sending are affected by frame size limits:
136 // HEADERS and DATA. HEADERS' payload (hpacked HTTP headers, following a
137 // frame header) is always in our 'buffer', we send the initial HEADERS
138 // frame first and then CONTINUTATION frame(s) if needed:
139 bool writeHEADERS(QIODevice &socket, quint32 sizeLimit);
140 // With DATA frames the actual payload is never in our 'buffer', it's a
141 // 'readPointer' from QNonContiguousData. We split this payload as needed
142 // into DATA frames with correct payload size fitting into frame size limit:
143 bool writeDATA(QIODevice &socket, quint32 sizeLimit,
144 const uchar *src, quint32 size);
145private:
146 void updatePayloadSize();
147 Frame frame;
148};
149
150}
151
152QT_END_NAMESPACE
153
154#endif
FrameStatus validateHeader() const
bool priority(quint32 *streamID=nullptr, uchar *weight=nullptr) const
FrameStatus validatePayload() const
uchar padding() const
const uchar * dataBegin() const
quint32 hpackBlockSize() const
quint32 payloadSize() const
quint32 streamID() const
quint32 dataSize() const
const uchar * hpackBlockBegin() const
std::vector< uchar > buffer
FrameType type() const
FrameFlags flags() const