This repository has been archived by the owner on May 13, 2023. It is now read-only.
forked from dgrr/http2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
frameHeader.go
266 lines (215 loc) · 5.68 KB
/
frameHeader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package http2
import (
"bufio"
"fmt"
"io"
"sync"
"github.com/dgrr/http2/http2utils"
)
const (
// DefaultFrameSize FrameHeader default size
// http://httpwg.org/specs/rfc7540.html#FrameHeader
DefaultFrameSize = 9
// https://httpwg.org/specs/rfc7540.html#SETTINGS_MAX_FRAME_SIZE
defaultMaxLen = 1 << 14
)
// Frame Flag (described along the frame types)
// More flags have been ignored due to redundancy.
const (
FlagAck FrameFlags = 0x1
FlagEndStream FrameFlags = 0x1
FlagEndHeaders FrameFlags = 0x4
FlagPadded FrameFlags = 0x8
FlagPriority FrameFlags = 0x20
)
// TODO: Develop methods for FrameFlags
var frameHeaderPool = sync.Pool{
New: func() interface{} {
return &FrameHeader{}
},
}
// FrameHeader is frame representation of HTTP2 protocol
//
// Use AcquireFrameHeader instead of creating FrameHeader every time
// if you are going to use FrameHeader as your own and ReleaseFrameHeader to
// delete the FrameHeader
//
// FrameHeader instance MUST NOT be used from different goroutines.
//
// https://tools.ietf.org/html/rfc7540#section-4.1
type FrameHeader struct {
length int // 24 bits
kind FrameType // 8 bits
flags FrameFlags // 8 bits
stream uint32 // 31 bits
maxLen uint32
rawHeader [DefaultFrameSize]byte
payload []byte
fr Frame
}
// AcquireFrameHeader gets a FrameHeader from pool.
func AcquireFrameHeader() *FrameHeader {
fr := frameHeaderPool.Get().(*FrameHeader)
fr.Reset()
return fr
}
// ReleaseFrameHeader reset and puts fr to the pool.
func ReleaseFrameHeader(fr *FrameHeader) {
ReleaseFrame(fr.Body())
frameHeaderPool.Put(fr)
}
// Reset resets header values.
func (f *FrameHeader) Reset() {
f.kind = 0
f.flags = 0
f.stream = 0
f.length = 0
f.maxLen = defaultMaxLen
f.fr = nil
f.payload = f.payload[:0]
}
// Type returns the frame type (https://httpwg.org/specs/rfc7540.html#Frame_types)
func (f *FrameHeader) Type() FrameType {
return f.kind
}
func (f *FrameHeader) Flags() FrameFlags {
return f.flags
}
func (f *FrameHeader) SetFlags(flags FrameFlags) {
f.flags = flags
}
// Stream returns the stream id of the current frame.
func (f *FrameHeader) Stream() uint32 {
return f.stream
}
// SetStream sets the stream id on the current frame.
//
// This function DOESN'T delete the reserved bit (first bit)
// in order to support personalized implementations of the protocol.
func (f *FrameHeader) SetStream(stream uint32) {
f.stream = stream
}
// Len returns the payload length.
func (f *FrameHeader) Len() int {
return f.length
}
// MaxLen returns max negotiated payload length.
func (f *FrameHeader) MaxLen() uint32 {
return f.maxLen
}
func (f *FrameHeader) parseValues(header []byte) {
f.length = int(http2utils.BytesToUint24(header[:3])) // & (1<<24 - 1) // 3
f.kind = FrameType(header[3]) // 1
f.flags = FrameFlags(header[4]) // 1
f.stream = http2utils.BytesToUint32(header[5:]) & (1<<31 - 1) // 4
}
func (f *FrameHeader) parseHeader(header []byte) {
http2utils.Uint24ToBytes(header[:3], uint32(f.length)) // 2
header[3] = byte(f.kind) // 1
header[4] = byte(f.flags) // 1
http2utils.Uint32ToBytes(header[5:], f.stream) // 4
}
func ReadFrameFrom(br *bufio.Reader) (*FrameHeader, error) {
fr := AcquireFrameHeader()
_, err := fr.ReadFrom(br)
if err != nil {
if fr.Body() != nil {
ReleaseFrameHeader(fr)
} else {
frameHeaderPool.Put(fr)
}
fr = nil
}
return fr, err
}
func ReadFrameFromWithSize(br *bufio.Reader, max uint32) (*FrameHeader, error) {
fr := AcquireFrameHeader()
fr.maxLen = max
_, err := fr.ReadFrom(br)
if err != nil {
if fr.Body() != nil {
ReleaseFrameHeader(fr)
} else {
frameHeaderPool.Put(fr)
}
fr = nil
}
return fr, err
}
// ReadFrom reads frame from Reader.
//
// This function returns read bytes and/or error.
//
// Unlike io.ReaderFrom this method does not read until io.EOF.
func (f *FrameHeader) ReadFrom(br *bufio.Reader) (int64, error) {
return f.readFrom(br)
}
// TODO: Delete rb?
func (f *FrameHeader) readFrom(br *bufio.Reader) (int64, error) {
header, err := br.Peek(DefaultFrameSize)
if err != nil {
return -1, err
}
_, _ = br.Discard(DefaultFrameSize)
rn := int64(DefaultFrameSize)
// Parsing FrameHeader's Header field.
f.parseValues(header)
if err = f.checkLen(); err != nil {
return 0, err
}
if f.kind > FrameContinuation {
_, _ = br.Discard(f.length)
return 0, ErrUnknownFrameType
}
f.fr = AcquireFrame(f.kind)
// if max > 0 && frh.length > max {
// TODO: Discard bytes and return an error
if f.length > 0 {
n := f.length
if n < 0 {
panic(fmt.Sprintf("length is less than 0 (%d). Overflow? (%d)", n, f.length))
}
f.payload = http2utils.Resize(f.payload, n)
n, err = io.ReadFull(br, f.payload[:n])
if err != nil {
ReleaseFrame(f.fr)
return 0, err
}
rn += int64(n)
}
return rn, f.fr.Deserialize(f)
}
// WriteTo writes frame to the Writer.
//
// This function returns FrameHeader bytes written and/or error.
func (f *FrameHeader) WriteTo(w *bufio.Writer) (wb int64, err error) {
f.fr.Serialize(f)
f.length = len(f.payload)
f.parseHeader(f.rawHeader[:])
n, err := w.Write(f.rawHeader[:])
if err == nil {
wb += int64(n)
n, err = w.Write(f.payload)
wb += int64(n)
}
return wb, err
}
func (f *FrameHeader) Body() Frame {
return f.fr
}
func (f *FrameHeader) SetBody(fr Frame) {
if fr == nil {
panic("Body cannot be nil")
}
f.kind = fr.Type()
f.fr = fr
}
func (f *FrameHeader) setPayload(payload []byte) {
f.payload = append(f.payload[:0], payload...)
}
func (f *FrameHeader) checkLen() error {
if f.maxLen != 0 && f.length > int(f.maxLen) {
return ErrPayloadExceeds
}
return nil
}