/
options.go
52 lines (46 loc) · 1.72 KB
/
options.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
package ws
import (
"net/http"
"time"
)
// ProxyOption provides functional-style configuration settings for a proxy instance.
type ProxyOption func(p *Proxy) error
// EnableCompression specify if the server should attempt to negotiate per
// message compression (RFC 7692). Setting this value to true does not guarantee
// that compression will be supported. Currently, only "no context takeover"
// modes are supported.
func EnableCompression() ProxyOption {
return func(p *Proxy) error {
p.wsConf.EnableCompression = true
return nil
}
}
// CheckOrigin should return true if the request Origin header is acceptable.
// If no setting is provided a safe default is used: return false if the Origin
// request header is present and the origin host is not equal to request Host
// header. A CheckOrigin function should carefully validate the request origin
// to prevent cross-site request forgery.
func CheckOrigin(f func(*http.Request) bool) ProxyOption {
return func(p *Proxy) error {
p.wsConf.CheckOrigin = f
return nil
}
}
// SubProtocols specifies the server's supported protocols in order of preference.
// If no value is provided, the server negotiates a sub-protocol by selecting
// the first match in this list with a protocol requested by the client. If there's
// no match, then no protocol is negotiated (the Sec-Websocket-Protocol header
// is not included in the handshake response).
func SubProtocols(list []string) ProxyOption {
return func(p *Proxy) error {
p.wsConf.Subprotocols = list
return nil
}
}
// HandshakeTimeout specifies the duration for the handshake to complete.
func HandshakeTimeout(timeout time.Duration) ProxyOption {
return func(p *Proxy) error {
p.wsConf.HandshakeTimeout = timeout
return nil
}
}