-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacceptor.go
More file actions
186 lines (151 loc) · 4.57 KB
/
Copy pathacceptor.go
File metadata and controls
186 lines (151 loc) · 4.57 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
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
package websockets
import (
"crypto/sha1"
"encoding/base64"
"errors"
"net"
"net/http"
"strings"
"lowbit.dev/cooper"
)
var (
ErrUpgradeFailed = errors.New("websocket upgrade failed")
ErrMissingKey = errors.New("missing Sec-WebSocket-Key header")
ErrUnsupportedVersion = errors.New("unsupported websocket version")
)
// Upgrader defines the server-side configuration for accepting WebSocket connections.
type Acceptor struct {
MaxReadLimit int64
MaxFrameSize int64
Subprotocols []string
connectionPool *ConnPool
deflatePool *DeflateConnPool
}
func NewAcceptor() *Acceptor {
return &Acceptor{
MaxReadLimit: ReadLimitStandard,
MaxFrameSize: FrameSizeLowMemory,
}
}
func NewAcceptorWithConnPool(p *ConnPool) *Acceptor {
u := NewAcceptor()
u.connectionPool = p
return u
}
func NewAcceptorWithDeflatePool(p *DeflateConnPool) *Acceptor {
u := NewAcceptor()
u.deflatePool = p
return u
}
func NewAcceptorWithConnAndDeflatePools(p *ConnPool, dp *DeflateConnPool) *Acceptor {
u := NewAcceptor()
u.connectionPool = p
u.deflatePool = dp
return u
}
// Accept inspects the HTTP request, performs the websocket handshake
// and returns a fully initialized WebSocket Connection interface.
func (a *Acceptor) Accept(w http.ResponseWriter, r *http.Request) (Connection, error) {
return a.AcceptwWithConnFactory(w, r, func(c net.Conn) *Conn {
if a.connectionPool != nil {
return a.connectionPool.Acquire(c, 0, 0)
}
return NewConn(c, a.MaxReadLimit, a.MaxFrameSize)
})
}
func (a *Acceptor) AcceptwWithConnFactory(w http.ResponseWriter, r *http.Request, connectionFactory func(net.Conn) *Conn) (Connection, error) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return nil, ErrUpgradeFailed
}
if r.Header.Get("Sec-WebSocket-Version") != "13" {
w.Header().Set("Sec-WebSocket-Version", "13")
http.Error(w, "Upgrade Required", http.StatusUpgradeRequired)
return nil, ErrUnsupportedVersion
}
clientKey := r.Header.Get("Sec-WebSocket-Key")
if clientKey == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return nil, ErrMissingKey
}
clientProtocols := r.Header.Get("Sec-WebSocket-Protocol")
var selectedProtocol string
if clientProtocols != "" && len(a.Subprotocols) > 0 {
selectedProtocol = negotiateSubprotocol(clientProtocols, a.Subprotocols)
}
useDeflate := a.deflatePool != nil && strings.Contains(r.Header.Get("Sec-WebSocket-Extensions"), "permessage-deflate")
rawConn, _, err := cooper.HijackAndReturn(w, r, cooper.BuildHijackConfig(
cooper.Protocols("websocket"),
cooper.ResponseHeaders(func(_ *http.Request, _ string) http.Header {
h := http.Header{}
h.Set("Sec-WebSocket-Accept", deriveAccept(clientKey))
if selectedProtocol != "" {
h.Set("Sec-WebSocket-Protocol", selectedProtocol)
}
if useDeflate {
// Enforce strict no_context_takeover policies
h.Set("Sec-WebSocket-Extensions", "permessage-deflate; server_no_context_takeover; client_no_context_takeover")
}
return h
}),
))
if err != nil {
var uerr *cooper.UpgradeError
if errors.As(err, &uerr) {
uerr.WriteTo(w)
}
return nil, err
}
c := connectionFactory(rawConn)
c.isServer = true
c.subprotocol = selectedProtocol
if !useDeflate {
return c, nil
}
return a.deflatePool.Acquire(c), nil
}
// negotiateSubprotocol matches the client's requested protocols against the server's
// supported list, returning the first server-preferred match.
func negotiateSubprotocol(clientHeader string, serverSupported []string) string {
for _, supported := range serverSupported {
for p := clientHeader; p != ""; {
token, rest := nextToken(p)
p = rest
if strings.TrimSpace(token) == supported {
return supported
}
}
}
return ""
}
func nextToken(s string) (token, rest string) {
if idx := strings.IndexByte(s, ','); idx >= 0 {
return s[:idx], s[idx+1:]
}
return s, ""
}
// Release safely returns a Connection and its underlying buffers to their
// respective pools. It should be explicitly deferred by the caller after Close.
func (a *Acceptor) Release(c Connection) {
if c == nil {
return
}
if dc, ok := c.(*PerMessageDeflateConn); ok {
if a.deflatePool != nil {
a.deflatePool.Release(dc)
}
c = dc.Conn
}
if base, ok := c.(*Conn); ok {
if a.connectionPool != nil {
a.connectionPool.Release(base)
}
}
}
// deriveAccept computes the response hash for a given client key.
func deriveAccept(clientKey string) string {
h := sha1.New()
h.Write([]byte(clientKey))
h.Write([]byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}