-
Notifications
You must be signed in to change notification settings - Fork 21
/
frame.go
349 lines (306 loc) · 8.95 KB
/
frame.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2013-14, Amahi. All rights reserved.
// Use of this source code is governed by the
// license that can be found in the LICENSE file.
// Frame related functions for generic for control/data frames
// as well as specific frame related functions
package spdy
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"strings"
)
// ========================================
// Control Frames
// ========================================
func (f controlFrame) Flags() frameFlags { return f.flags }
func (f controlFrame) Data() []byte { return f.data }
func (f controlFrame) String() string {
s := fmt.Sprintf("Control frame %s, flags: %s, size: %d\n", f.kind, f.flags, len(f.data))
return s
}
func (f controlFrame) isFIN() bool { return f.flags&FLAG_FIN == FLAG_FIN }
func (f frameFlags) String() string {
if f == FLAG_NONE {
return "-"
}
if f == FLAG_FIN {
return "FIN"
}
return fmt.Sprintf("0x%02x", int(f))
}
func (f controlFrameKind) String() string {
switch f {
case FRAME_SYN_STREAM:
return "SYN_STREAM"
case FRAME_SYN_REPLY:
return "SYN_REPLY"
case FRAME_RST_STREAM:
return "RST_STREAM"
case FRAME_SETTINGS:
return "SETTINGS"
case FRAME_PING:
return "PING"
case FRAME_GOAWAY:
return "GOAWAY"
case FRAME_HEADERS:
return "HEADERS"
case FRAME_WINDOW_UPDATE:
return "WINDOW_UPDATE"
}
return fmt.Sprintf("Type(%#04x)", uint16(f))
}
func (f controlFrame) Write(w io.Writer) (n int64, err error) {
total := len(f.data) + 8
debug.Printf("Writing control frame %s, flags: %s, payload: %d", f.kind, f.flags, len(f.data))
nn, err := writeFrame(w, []interface{}{uint16(0x8000) | uint16(0x0003), f.kind, f.flags}, f.data)
if nn != total {
log.Println("WARNING in controlFrame.Write, wrote", nn, "vs. frame size", total)
}
return int64(nn), err
}
// read the stream ID from the payload
// applies to SYN_STREAM, SYN_REPLY, RST_STREAM, GOAWAY, HEADERS and WINDOWS_UPDATE
func (f *controlFrame) streamID() (id streamID) {
data := bytes.NewBuffer(f.data[0:4])
err := binary.Read(data, binary.BigEndian, &id)
if err != nil {
log.Println("ERROR: Cannot read stream ID from a control frame that is supposed to have a stream ID:", err)
id = 0
return
}
id &= 0x7fffffff
return
}
// ========================================
// Data Frames
// ========================================
func (f dataFrame) Flags() frameFlags { return f.flags }
func (f dataFrame) Data() []byte { return f.data }
func (f dataFrame) isFIN() bool { return f.flags&FLAG_FIN == FLAG_FIN }
func (f dataFrame) Write(w io.Writer) (n int64, err error) {
total := len(f.data) + 8
debug.Printf("Writing data frame, flags: %s, size: %d", f.flags, len(f.data))
nn, err := writeFrame(w, []interface{}{f.stream & 0x7fffffff, f.flags}, f.data)
if nn != total {
log.Println("WARNING in dataFrame.Write, wrote", nn, "vs. frame size", total)
}
return int64(nn), err
}
func (f dataFrame) String() string {
l := len(f.data)
s := fmt.Sprintf("\n\tFrame: DATA of size %d, for stream #%d", l, f.stream)
s += fmt.Sprintf(", Flags: %s", f.flags)
if l > 16 {
s += fmt.Sprintf("\n\tData: [%x .. %x]", f.data[0:6], f.data[l-6:])
} else if l > 0 {
s += fmt.Sprintf("\n\tData: [%x]", f.data)
}
return s
}
// ========================================
// Generic frame-writing utilities
// ========================================
func writeFrame(w io.Writer, head []interface{}, data []byte) (n int, err error) {
var nn int
// Header (40 bits)
err = writeBinary(w, head...)
if err != nil {
return
}
n += 5 // frame head, in bytes, without the length field
// Length (24 bits)
length := len(data)
nn, err = w.Write([]byte{
byte(length & 0x00ff0000 >> 16),
byte(length & 0x0000ff00 >> 8),
byte(length & 0x000000ff),
})
n += nn
if err != nil {
log.Println("Write of length failed:", err)
return
}
// Data
if length > 0 {
nn, err = w.Write(data)
if err != nil {
log.Println("Write of data failed:", err)
return
}
n += nn
}
return
}
func writeBinary(r io.Writer, args ...interface{}) (err error) {
for _, a := range args {
err = binary.Write(r, binary.BigEndian, a)
if err != nil {
return
}
}
return
}
// ========================================
// Generic frame-reading utilities
// ========================================
// readFrame reads an entire frame into memory
func readFrame(r io.Reader) (f frame, err error) {
headBuffer := new(bytes.Buffer)
_, err = io.CopyN(headBuffer, r, 5)
if err != nil {
return
}
if headBuffer.Bytes()[0]&0x80 == 0 {
// Data
df := dataFrame{}
err = readBinary(headBuffer, &df.stream, &df.flags)
if err != nil {
return
}
df.data, err = readData(r)
f = df
} else {
// Control
cf := controlFrame{}
headBuffer.ReadByte() // FIXME skip version word
headBuffer.ReadByte()
err = readBinary(headBuffer, &cf.kind, &cf.flags)
if err != nil {
return
}
cf.data, err = readData(r)
f = cf
}
return
}
func readBinary(r io.Reader, args ...interface{}) (err error) {
for _, a := range args {
err = binary.Read(r, binary.BigEndian, a)
if err != nil {
return
}
}
return
}
func readData(r io.Reader) (data []byte, err error) {
lengthField := make([]byte, 3)
_, err = io.ReadFull(r, lengthField)
if err != nil {
return
}
var length uint32
length |= uint32(lengthField[0]) << 16
length |= uint32(lengthField[1]) << 8
length |= uint32(lengthField[2])
if length > 0 {
data = make([]byte, int(length))
_, err = io.ReadFull(r, data)
if err != nil {
data = nil
return
}
} else {
data = []byte{}
}
return
}
// ========================================
// SYN_STREAM frame
// ========================================
func (frame frameSynStream) Flags() frameFlags {
return frame.flags
}
func (frame frameSynStream) Data() []byte {
buf := new(bytes.Buffer)
// stream-id
binary.Write(buf, binary.BigEndian, frame.stream&0x7fffffff)
// associated-to-stream-id FIXME in the long term
binary.Write(buf, binary.BigEndian, frame.associated_stream&0x7fffffff)
// Priority & unused/reserved
var misc uint16 = uint16((frame.priority & 0x7) << 13)
binary.Write(buf, binary.BigEndian, misc)
// debug.Println("Before header:", buf.Bytes())
frame.session.headerWriter.writeHeader(buf, frame.header)
// debug.Println("Compressed header:", buf.Bytes())
return buf.Bytes()
}
func (frame frameSynStream) Write(w io.Writer) (n int64, err error) {
f := controlFrame{kind: FRAME_SYN_STREAM, flags: frame.flags, data: frame.Data()}
return f.Write(w)
}
// print details of the frame to a string
func (frame frameSynStream) String() string {
s := fmt.Sprintf("\n\tFrame: SYN_STREAM, Stream #%d", frame.stream)
s += fmt.Sprintf(", Flags: %s", frame.flags)
s += fmt.Sprintf("\n\tHeaders:\n")
for i := range frame.header {
s += fmt.Sprintf("\t\t%s: %s\n", i, strings.Join(frame.header[i], ", "))
}
return s
}
// ========================================
// SYN_REPLY frame
// ========================================
func (frame frameSynReply) Flags() frameFlags {
return frame.flags
}
func (frame frameSynReply) Data() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, frame.stream&0x7fffffff)
frame.session.headerWriter.writeHeader(buf, frame.headers)
return buf.Bytes()
}
func (frame frameSynReply) Write(w io.Writer) (n int64, err error) {
cf := controlFrame{kind: FRAME_SYN_REPLY, data: frame.Data()}
return cf.Write(w)
}
// print details of the frame to a string
func (frame frameSynReply) String() string {
s := fmt.Sprintf("\n\tFrame: SYN_REPLY, Stream #%d", frame.stream)
s += fmt.Sprintf(", Flags: %s", frame.flags)
s += fmt.Sprintf("\n\tHeaders:\n")
for i := range frame.headers {
s += fmt.Sprintf("\t\t%s: %s\n", i, strings.Join(frame.headers[i], ", "))
}
return s
}
// ========================================
// SETTINGS frame
// ========================================
func (s settings) Flags() frameFlags {
return s.flags
}
func (s settings) Data() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, s.count)
for i := range s.svp {
binary.Write(buf, binary.BigEndian, s.svp[i].flags)
binary.Write(buf, binary.BigEndian, s.svp[i].id)
binary.Write(buf, binary.BigEndian, s.svp[i].value)
}
return buf.Bytes()
}
func (s settings) Write(w io.Writer) (n int64, err error) {
cf := controlFrame{kind: FRAME_SETTINGS, flags: s.flags, data: s.Data()}
return cf.Write(w)
}
func (s settings) String() (r string) {
r = fmt.Sprintf("\n\tFrame: SETTINGS, Flags: %s\n", s.flags)
r += fmt.Sprintf("\tValue/Pairs (%d):\n", s.count)
for i := range s.svp {
r += fmt.Sprintf("\t\t%d: %d\tflags: %d\n", s.svp[i].id, s.svp[i].value, s.svp[i].flags)
}
return
}
// ========================================
// WINDOW_UPDATE frame
// ========================================
// takes a frame and the delta window size and returns a WINDOW_UPDATE frame
func windowUpdateFor(id streamID, dws int) frame {
data := new(bytes.Buffer)
binary.Write(data, binary.BigEndian, id)
binary.Write(data, binary.BigEndian, uint32(dws))
return controlFrame{kind: FRAME_WINDOW_UPDATE, data: data.Bytes()}
}