/
h2settings.go
147 lines (143 loc) · 3.95 KB
/
h2settings.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
package transport
import (
"github.com/wangluozhe/chttp/http2"
)
var settings = map[string]http2.SettingID{
"HEADER_TABLE_SIZE": http2.SettingHeaderTableSize,
"ENABLE_PUSH": http2.SettingEnablePush,
"MAX_CONCURRENT_STREAMS": http2.SettingMaxConcurrentStreams,
"INITIAL_WINDOW_SIZE": http2.SettingInitialWindowSize,
"MAX_FRAME_SIZE": http2.SettingMaxFrameSize,
"MAX_HEADER_LIST_SIZE": http2.SettingMaxHeaderListSize,
}
type H2Settings struct {
//HEADER_TABLE_SIZE
//ENABLE_PUSH
//MAX_CONCURRENT_STREAMS
//INITIAL_WINDOW_SIZE
//MAX_FRAME_SIZE
//MAX_HEADER_LIST_SIZE
Settings map[string]int `json:"Settings"`
//HEADER_TABLE_SIZE
//ENABLE_PUSH
//MAX_CONCURRENT_STREAMS
//INITIAL_WINDOW_SIZE
//MAX_FRAME_SIZE
//MAX_HEADER_LIST_SIZE
SettingsOrder []string `json:"SettingsOrder"`
ConnectionFlow int `json:"ConnectionFlow"`
HeaderPriority map[string]interface{} `json:"HeaderPriority"`
PriorityFrames []map[string]interface{} `json:"PriorityFrames"`
}
func ToHTTP2Settings(h2Settings *H2Settings) (http2Settings *http2.HTTP2Settings) {
http2Settings = &http2.HTTP2Settings{
Settings: nil,
ConnectionFlow: 0,
HeaderPriority: &http2.PriorityParam{},
PriorityFrames: nil,
}
if h2Settings.Settings != nil {
if h2Settings.SettingsOrder != nil {
for _, orderKey := range h2Settings.SettingsOrder {
val := h2Settings.Settings[orderKey]
if val != 0 || orderKey == "ENABLE_PUSH" {
http2Settings.Settings = append(http2Settings.Settings, http2.Setting{
ID: settings[orderKey],
Val: uint32(val),
})
}
}
} else {
for id, val := range h2Settings.Settings {
http2Settings.Settings = append(http2Settings.Settings, http2.Setting{
ID: settings[id],
Val: uint32(val),
})
}
}
}
if h2Settings.ConnectionFlow != 0 {
http2Settings.ConnectionFlow = h2Settings.ConnectionFlow
}
if h2Settings.HeaderPriority != nil {
var weight int
var streamDep int
w := h2Settings.HeaderPriority["weight"]
switch w.(type) {
case int:
weight = w.(int)
case float64:
weight = int(w.(float64))
}
s := h2Settings.HeaderPriority["streamDep"]
switch s.(type) {
case int:
streamDep = s.(int)
case float64:
streamDep = int(s.(float64))
}
var priorityParam *http2.PriorityParam
if w == nil {
priorityParam = &http2.PriorityParam{
StreamDep: uint32(streamDep),
Exclusive: h2Settings.HeaderPriority["exclusive"].(bool),
}
} else {
priorityParam = &http2.PriorityParam{
StreamDep: uint32(streamDep),
Exclusive: h2Settings.HeaderPriority["exclusive"].(bool),
Weight: uint8(weight - 1),
}
}
http2Settings.HeaderPriority = priorityParam
}
if h2Settings.PriorityFrames != nil {
for _, frame := range h2Settings.PriorityFrames {
var weight int
var streamDep int
var streamID int
priorityParamSource := frame["priorityParam"].(map[string]interface{})
w := priorityParamSource["weight"]
switch w.(type) {
case int:
weight = w.(int)
case float64:
weight = int(w.(float64))
}
s := priorityParamSource["streamDep"]
switch s.(type) {
case int:
streamDep = s.(int)
case float64:
streamDep = int(s.(float64))
}
sid := frame["streamID"]
switch sid.(type) {
case int:
streamID = sid.(int)
case float64:
streamID = int(sid.(float64))
}
var priorityParam http2.PriorityParam
if w == nil {
priorityParam = http2.PriorityParam{
StreamDep: uint32(streamDep),
Exclusive: priorityParamSource["exclusive"].(bool),
}
} else {
priorityParam = http2.PriorityParam{
StreamDep: uint32(streamDep),
Exclusive: priorityParamSource["exclusive"].(bool),
Weight: uint8(weight - 1),
}
}
http2Settings.PriorityFrames = append(http2Settings.PriorityFrames, http2.PriorityFrame{
FrameHeader: http2.FrameHeader{
StreamID: uint32(streamID),
},
PriorityParam: priorityParam,
})
}
}
return http2Settings
}