-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.go
217 lines (195 loc) · 5.31 KB
/
control.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
package framestream
import (
"bytes"
"errors"
"fmt"
"io"
)
const (
ControlFrameMaxLength = 512
ControlFieldContentTypeMaxLength = 256
)
type ControlType uint32
const (
ControlTypeAccept ControlType = iota + 1
ControlTypeStart
ControlTypeStop
ControlTypeReady
ControlTypeFinish
)
func (ct *ControlType) Unmarshal(r io.Reader) error {
ft, err := read32(r)
if err != nil {
return err
}
switch xct := ControlType(ft); xct {
case ControlTypeAccept, ControlTypeStart, ControlTypeStop, ControlTypeReady, ControlTypeFinish:
*ct = xct
default:
return errors.New(fmt.Sprintf("unknown control frame type %d", ft))
}
return nil
}
type ControlField uint32
const (
ControlFieldContentType ControlField = iota + 1
)
type ControlFlag int
const (
ControlFlagWithHeader ControlFlag = 1 << iota
)
//TODO(jdef) why is this a []byte? should it be a string?
type ContentType []byte
type Control struct {
Type ControlType
ContentTypes []ContentType
}
func (c *Control) MatchFieldContentType(match ContentType) bool {
// STOP and FINISH frames don't have content types
switch c.Type {
case ControlTypeStop, ControlTypeFinish:
return false
}
if len(c.ContentTypes) == 0 {
return true
}
// control frame has >= 1 content type which cannot match an unset type
if match == nil {
return false
}
for _, t := range c.ContentTypes {
if bytes.Compare(match, t) == 0 {
return true
}
}
return false
}
// Marshall (fstrm_control_encode)
func (c *Control) Marshal(flags ControlFlag) ([]byte, error) {
buf := &bytes.Buffer{}
sz, err := c.calcEncodedSize(flags)
if err != nil {
return nil, err
}
buf.Grow(int(sz))
w32 := func(v uint32) {
if err == nil {
err = write32(buf, v)
}
}
if (flags & ControlFlagWithHeader) > 0 {
w32(0) // escape
w32(sz - 8) // frame-length: does not include escape field, nor this one
}
w32(uint32(c.Type))
for _, t := range c.ContentTypes {
if c.Type == ControlTypeStop || c.Type == ControlTypeFinish {
break // no content type fields for these control frames
}
w32(uint32(ControlFieldContentType))
w32(uint32(len(t)))
if err == nil {
_, err = buf.Write(t)
}
if c.Type == ControlTypeStart {
break // only one allowed
}
}
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (c *Control) calcEncodedSize(flags ControlFlag) (uint32, error) {
result := uint32(0)
if (flags & ControlFlagWithHeader) > 0 {
result += 8 // 2x uint32 :: escape, frame-length
}
result += 4 // control-type
for _, t := range c.ContentTypes {
if c.Type == ControlTypeStop || c.Type == ControlTypeFinish {
break // no content type fields for these control frames
}
result += 8 // 2x uint32 :: control-field-content-type, payload length field
if len(t) > ControlFieldContentTypeMaxLength {
return 0, errors.New(fmt.Sprintf("content type field length %d exceeds max allowed %d", len(t), ControlFieldContentTypeMaxLength))
}
result += uint32(len(t))
if c.Type == ControlTypeStart {
break // only one allowed
}
}
if result > ControlFrameMaxLength {
return 0, errors.New(fmt.Sprintf("control frame length %d exceeds max allowed %d", result, ControlFrameMaxLength))
}
return result, nil
}
// Unmarshal (fstrm_control_decode)
func (c *Control) Unmarshal(buf []byte, flags ControlFlag) error {
p := bytes.NewBuffer(buf)
if (flags & ControlFlagWithHeader) > 0 {
// read outer frame len
outer, err := read32(p)
if err != nil {
return err
}
if outer != 0 {
return errors.New(fmt.Sprintf("expected outer frame length 0 for control frame instead of %d", outer))
}
if outer > ControlFrameMaxLength {
return errors.New(fmt.Sprintf("outer frame length %d exceeds max control frame length %d", outer, ControlFrameMaxLength))
}
if outer != uint32(p.Len()) {
return errors.New(fmt.Sprintf("remaining frame buffer len %d != frame length %d", p.Len(), outer))
}
} else {
if p.Len() > ControlFrameMaxLength {
return errors.New(fmt.Sprintf("control frame length %d exceeds max length %d", p.Len(), ControlFrameMaxLength))
}
}
// read frame type
err := (&c.Type).Unmarshal(p)
if err != nil {
return err
}
for p.Len() > 0 {
// read control frame field type
cft, err := read32(p)
if err != nil {
return err
}
switch cft := ControlField(cft); cft {
case ControlFieldContentType:
// payload length
plen, err := read32(p)
if err != nil {
return err
}
if plen > uint32(p.Len()) {
return errors.New(fmt.Sprintf("content type payload len exceeds control frame"))
}
if plen > ControlFieldContentTypeMaxLength {
return errors.New(fmt.Sprintf("content type field len %d exceeds max allowed %d", plen, ControlFieldContentTypeMaxLength))
}
buf := p.Next(int(plen))
if uint32(len(buf)) != plen {
return errors.New("not enough bytes remain in buffer for content type field payload")
}
c.ContentTypes = append(c.ContentTypes, ContentType(buf))
default:
return errors.New(fmt.Sprintf("unknown control field frame type: %d", cft))
}
}
// enforce limits of content type fields
switch tlen := len(c.ContentTypes); c.Type {
case ControlTypeStart:
if tlen > 1 {
return errors.New("too many content type payloads for control-start")
}
case ControlTypeStop, ControlTypeFinish:
if tlen > 0 {
return errors.New("too many content type payloads for control-start")
}
}
return nil
}