aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/http2_interop/settings.go
blob: 97914d960faa2a5a06ad5ddc50ede8c83e4cbdee (plain)
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
package http2interop

import (
	"encoding/binary"
	"fmt"
	"io"
)

const (
	SETTINGS_ACK = 1
)

type SettingsFrame struct {
	Header FrameHeader
	Params []SettingsParameter
}

type SettingsIdentifier uint16

const (
	SettingsHeaderTableSize      SettingsIdentifier = 1
	SettingsEnablePush           SettingsIdentifier = 2
	SettingsMaxConcurrentStreams SettingsIdentifier = 3
	SettingsInitialWindowSize    SettingsIdentifier = 4
	SettingsMaxFrameSize         SettingsIdentifier = 5
	SettingsMaxHeaderListSize    SettingsIdentifier = 6
)

func (si SettingsIdentifier) String() string {
	switch si {
	case SettingsHeaderTableSize:
		return "SETTINGS_HEADER_TABLE_SIZE"
	case SettingsEnablePush:
		return "SETTINGS_ENABLE_PUSH"
	case SettingsMaxConcurrentStreams:
		return "SETTINGS_MAX_CONCURRENT_STREAMS"
	case SettingsInitialWindowSize:
		return "SETTINGS_INITIAL_WINDOW_SIZE"
	case SettingsMaxFrameSize:
		return "SETTINGS_MAX_FRAME_SIZE"
	case SettingsMaxHeaderListSize:
		return "SETTINGS_MAX_HEADER_LIST_SIZE"
	default:
		return fmt.Sprintf("SETTINGS_UNKNOWN(%d)", uint16(si))
	}
}

type SettingsParameter struct {
	Identifier SettingsIdentifier
	Value      uint32
}

func (f *SettingsFrame) GetHeader() *FrameHeader {
	return &f.Header
}

func (f *SettingsFrame) ParsePayload(r io.Reader) error {
	raw := make([]byte, f.Header.Length)
	if _, err := io.ReadFull(r, raw); err != nil {
		return err
	}
	return f.UnmarshalPayload(raw)
}

func (f *SettingsFrame) UnmarshalPayload(raw []byte) error {
	if f.Header.Length != len(raw) {
		return fmt.Errorf("Invalid Payload length %d != %d", f.Header.Length, len(raw))
	}

	if f.Header.Length%6 != 0 {
		return fmt.Errorf("Invalid Payload length %d", f.Header.Length)
	}

	f.Params = make([]SettingsParameter, 0, f.Header.Length/6)
	for i := 0; i < len(raw); i += 6 {
		f.Params = append(f.Params, SettingsParameter{
			Identifier: SettingsIdentifier(binary.BigEndian.Uint16(raw[i : i+2])),
			Value:      binary.BigEndian.Uint32(raw[i+2 : i+6]),
		})
	}
	return nil
}

func (f *SettingsFrame) MarshalPayload() ([]byte, error) {
	raw := make([]byte, len(f.Params)*6)
	for i, p := range f.Params {
		binary.BigEndian.PutUint16(raw[i*6:i*6+2], uint16(p.Identifier))
		binary.BigEndian.PutUint32(raw[i*6+2:i*6+6], p.Value)
	}
	return raw, nil
}

func (f *SettingsFrame) MarshalBinary() ([]byte, error) {
	payload, err := f.MarshalPayload()
	if err != nil {
		return nil, err
	}

	f.Header.Length = len(payload)
	f.Header.Type = SettingsFrameType
	header, err := f.Header.MarshalBinary()
	if err != nil {
		return nil, err
	}
	header = append(header, payload...)

	return header, nil
}