forked from dgrr/http2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushpromise.go
72 lines (56 loc) · 1.36 KB
/
pushpromise.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
package http2
import (
"github.com/dgrr/http2/http2utils"
)
const FramePushPromise FrameType = 0x5
var _ Frame = &PushPromise{}
// PushPromise https://tools.ietf.org/html/rfc7540#section-6.6
type PushPromise struct {
pad bool
ended bool
stream uint32
header []byte // header block fragment
}
func (pp *PushPromise) Type() FrameType {
return FramePushPromise
}
func (pp *PushPromise) Reset() {
pp.pad = false
pp.ended = false
pp.stream = 0
pp.header = pp.header[:0]
}
func (pp *PushPromise) SetHeader(h []byte) {
pp.header = append(pp.header[:0], h...)
}
func (pp *PushPromise) Write(b []byte) (int, error) {
n := len(b)
pp.header = append(pp.header, b...)
return n, nil
}
func (pp *PushPromise) Deserialize(fr *FrameHeader) error {
payload := fr.payload
if fr.Flags().Has(FlagPadded) {
var err error
payload, err = http2utils.CutPadding(payload, fr.Len())
if err != nil {
return err
}
}
if len(fr.payload) < 4 {
return ErrMissingBytes
}
pp.stream = http2utils.BytesToUint32(payload) & (1<<31 - 1)
pp.header = append(pp.header, payload[4:]...)
pp.ended = fr.Flags().Has(FlagEndHeaders)
return nil
}
func (pp *PushPromise) Serialize(fr *FrameHeader) {
fr.payload = fr.payload[:0]
// if pp.pad {
// fr.Flags().Add(FlagPadded)
// // TODO: Write padding flag
// }
fr.payload = append(fr.payload, pp.header...)
// TODO: write padding
}