-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathhttp2s.go
More file actions
149 lines (124 loc) · 4.05 KB
/
Copy pathhttp2s.go
File metadata and controls
149 lines (124 loc) · 4.05 KB
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
package surf
import (
"github.com/enetx/http"
"github.com/enetx/http2"
)
// HTTP2Settings represents HTTP/2 settings.
// https://lwthiker.com/networks/2022/06/17/http2-fingerprinting.html
type HTTP2Settings struct {
priorityFrames []http2.PriorityFrame
priorityParam http2.PriorityParam
builder *Builder
headerTableSize uint32
maxConcurrentStreams uint32
initialWindowSize uint32
maxFrameSize uint32
maxHeaderListSize uint32
connectionFlow uint32
initialStreamID uint32
noRFC7540Priorities uint32
enablePush uint32
usePush bool
}
// InitialStreamID sets the initial stream id for HTTP/2 streams.
func (h *HTTP2Settings) InitialStreamID(id uint32) *HTTP2Settings {
h.initialStreamID = id
return h
}
// HeaderTableSize sets the header table size for HTTP/2 settings.
func (h *HTTP2Settings) HeaderTableSize(size uint32) *HTTP2Settings {
h.headerTableSize = size
return h
}
// EnablePush enables HTTP/2 server push functionality.
func (h *HTTP2Settings) EnablePush(size uint32) *HTTP2Settings {
h.usePush = true
h.enablePush = size
return h
}
// MaxConcurrentStreams sets the maximum number of concurrent streams in HTTP/2.
func (h *HTTP2Settings) MaxConcurrentStreams(size uint32) *HTTP2Settings {
h.maxConcurrentStreams = size
return h
}
// InitialWindowSize sets the initial window size for HTTP/2 streams.
func (h *HTTP2Settings) InitialWindowSize(size uint32) *HTTP2Settings {
h.initialWindowSize = size
return h
}
// MaxFrameSize sets the maximum frame size for HTTP/2 frames.
func (h *HTTP2Settings) MaxFrameSize(size uint32) *HTTP2Settings {
h.maxFrameSize = size
return h
}
// MaxHeaderListSize sets the maximum size of the header list in HTTP/2.
func (h *HTTP2Settings) MaxHeaderListSize(size uint32) *HTTP2Settings {
h.maxHeaderListSize = size
return h
}
// NoRFC7540Priorities disables RFC 7540 priority signaling in HTTP/2.
func (h *HTTP2Settings) NoRFC7540Priorities(size uint32) *HTTP2Settings {
h.noRFC7540Priorities = size
return h
}
// ConnectionFlow sets the flow control for the HTTP/2 connection.
func (h *HTTP2Settings) ConnectionFlow(size uint32) *HTTP2Settings {
h.connectionFlow = size
return h
}
// PriorityParam sets the priority parameter for HTTP/2.
func (h *HTTP2Settings) PriorityParam(priorityParam http2.PriorityParam) *HTTP2Settings {
h.priorityParam = priorityParam
return h
}
// PriorityFrames sets the priority frames for HTTP/2.
func (h *HTTP2Settings) PriorityFrames(priorityFrames []http2.PriorityFrame) *HTTP2Settings {
h.priorityFrames = priorityFrames
return h
}
// Set applies the accumulated HTTP/2 settings.
// It configures the HTTP/2 settings for the surf client.
func (h *HTTP2Settings) Set() *Builder {
if h.builder.forceHTTP1 {
return h.builder
}
return h.builder.addCliMW(func(c *Client) error {
t1, ok := c.GetTransport().(*http.Transport)
if !ok {
return nil
}
t1.ForceAttemptHTTP2 = true
t2, err := http2.ConfigureTransports(t1)
if err != nil {
return err
}
t2.Settings = make([]http2.Setting, 0, 7)
appendSetting := func(id http2.SettingID, val uint32) {
if val != 0 || (id == http2.SettingEnablePush && h.usePush) {
t2.Settings = append(t2.Settings, http2.Setting{ID: id, Val: val})
}
}
appendSetting(http2.SettingHeaderTableSize, h.headerTableSize)
appendSetting(http2.SettingEnablePush, h.enablePush)
appendSetting(http2.SettingMaxConcurrentStreams, h.maxConcurrentStreams)
appendSetting(http2.SettingInitialWindowSize, h.initialWindowSize)
appendSetting(http2.SettingMaxFrameSize, h.maxFrameSize)
appendSetting(http2.SettingMaxHeaderListSize, h.maxHeaderListSize)
appendSetting(http2.SettingNoRFC7540Priorities, h.noRFC7540Priorities)
if h.initialStreamID != 0 {
t2.StreamID = h.initialStreamID
}
if h.connectionFlow != 0 {
t2.ConnectionFlow = h.connectionFlow
}
if !h.priorityParam.IsZero() {
t2.PriorityParam = h.priorityParam
}
if h.priorityFrames != nil {
t2.PriorityFrames = h.priorityFrames
}
t1.H2transport = t2
c.transport = t1
return nil
}, 0)
}