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 HTTP2FRAMES_P_H
41#define HTTP2FRAMES_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 "http2protocol_p.h"
55#include "hpack_p.h"
56
57#include <QtCore/qendian.h>
58#include <QtCore/qglobal.h>
59
60#include <algorithm>
61#include <vector>
62
63QT_BEGIN_NAMESPACE
64
65class QHttp2ProtocolHandler;
66class QAbstractSocket;
67
68namespace Http2
69{
70
71struct Q_AUTOTEST_EXPORT Frame
72{
73 Frame();
74 // Reading these values without first forming a valid frame (either reading
75 // it from a socket or building it) will result in undefined behavior:
76 FrameType type() const;
77 quint32 streamID() const;
78 FrameFlags flags() const;
79 quint32 payloadSize() const;
80 uchar padding() const;
81 // In HTTP/2 a stream's priority is specified by its weight and a stream
82 // (id) it depends on:
83 bool priority(quint32 *streamID = nullptr,
84 uchar *weight = nullptr) const;
85
86 FrameStatus validateHeader() const;
87 FrameStatus validatePayload() const;
88
89 // Number of payload bytes without padding and/or priority.
90 quint32 dataSize() const;
91 // HEADERS data size for HEADERS, PUSH_PROMISE and CONTINUATION streams:
92 quint32 hpackBlockSize() const;
93 // Beginning of payload without priority/padding bytes.
94 const uchar *dataBegin() const;
95 // HEADERS data beginning for HEADERS, PUSH_PROMISE and CONTINUATION streams:
96 const uchar *hpackBlockBegin() const;
97
98 std::vector<uchar> buffer;
99};
100
101class Q_AUTOTEST_EXPORT FrameReader
102{
103public:
104 FrameStatus read(QAbstractSocket &socket);
105
106 Frame &inboundFrame()
107 {
108 return frame;
109 }
110private:
111 bool readHeader(QAbstractSocket &socket);
112 bool readPayload(QAbstractSocket &socket);
113
114 quint32 offset = 0;
115 Frame frame;
116};
117
118class Q_AUTOTEST_EXPORT FrameWriter
119{
120public:
121 using payload_type = std::vector<uchar>;
122 using size_type = payload_type::size_type;
123
124 FrameWriter();
125 FrameWriter(FrameType type, FrameFlags flags, quint32 streamID);
126
127 Frame &outboundFrame()
128 {
129 return frame;
130 }
131
132 void setOutboundFrame(Frame &&newFrame);
133
134 // Frame 'builders':
135 void start(FrameType type, FrameFlags flags, quint32 streamID);
136 void setPayloadSize(quint32 size);
137 void setType(FrameType type);
138 void setFlags(FrameFlags flags);
139 void addFlag(FrameFlag flag);
140
141 // All append functions also update frame's payload length.
142 template<typename ValueType>
143 void append(ValueType val)
144 {
145 uchar wired[sizeof val] = {};
146 qToBigEndian(val, wired);
147 append(wired, wired + sizeof val);
148 }
149 void append(uchar val)
150 {
151 frame.buffer.push_back(x: val);
152 updatePayloadSize();
153 }
154 void append(Settings identifier)
155 {
156 append(val: quint16(identifier));
157 }
158 void append(const payload_type &payload)
159 {
160 append(begin: &payload[0], end: &payload[0] + payload.size());
161 }
162
163 void append(const uchar *begin, const uchar *end);
164
165 // Write as a single frame:
166 bool write(QAbstractSocket &socket) const;
167 // Two types of frames we are sending are affected by frame size limits:
168 // HEADERS and DATA. HEADERS' payload (hpacked HTTP headers, following a
169 // frame header) is always in our 'buffer', we send the initial HEADERS
170 // frame first and then CONTINUTATION frame(s) if needed:
171 bool writeHEADERS(QAbstractSocket &socket, quint32 sizeLimit);
172 // With DATA frames the actual payload is never in our 'buffer', it's a
173 // 'readPointer' from QNonContiguousData. We split this payload as needed
174 // into DATA frames with correct payload size fitting into frame size limit:
175 bool writeDATA(QAbstractSocket &socket, quint32 sizeLimit,
176 const uchar *src, quint32 size);
177private:
178 void updatePayloadSize();
179 Frame frame;
180};
181
182}
183
184QT_END_NAMESPACE
185
186#endif
187

source code of qtbase/src/network/access/http2/http2frames_p.h