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
package wsconn
import (
"cmp"
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
)
const (
HandshakeTimeout = 15 * time.Second
ReadTimeout = 2 * time.Minute
MaxFrameBytes = 8 << 20
PongWriteTimeout = 10 * time.Second
MaxBufferedBytes = 2 * MaxFrameBytes
CloseAbnormal = 1006
RestoredCloseReason = "restored from a memory snapshot"
)
type DialFunc func(ctx context.Context, network, address string) (net.Conn, error)
type Config struct {
Dial DialFunc
TLSConfig *tls.Config
HandshakeTimeout time.Duration
}
type HandshakeError struct {
StatusCode int
}
func (e *HandshakeError) Error() string {
return fmt.Sprintf("wsconn: websocket handshake: %s", cmp.Or(http.StatusText(e.StatusCode), "websocket handshake failed"))
}
type CloseError struct {
Code int
Reason string
Clean bool
}
func (e *CloseError) Error() string {
return fmt.Sprintf("wsconn: websocket closed: code %d reason %q clean %t", e.Code, e.Reason, e.Clean)
}
func Restored(err error) bool {
var closeErr *CloseError
return errors.As(err, &closeErr) && closeErr.Code == CloseAbnormal && closeErr.Reason == RestoredCloseReason
}
func Dial(ctx context.Context, target string, dial DialFunc) (*Conn, error) {
return DialConfig(ctx, target, Config{Dial: dial})
}
func (c Config) handshakeTimeout() time.Duration {
if c.HandshakeTimeout > 0 {
return c.HandshakeTimeout
}
return HandshakeTimeout
}
func (c Config) tlsConfig() *tls.Config {
if c.TLSConfig != nil {
return c.TLSConfig
}
return &tls.Config{MinVersion: tls.VersionTLS12}
}
func Normalize(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" {
return ""
}
if !strings.Contains(raw, "://") {
return "http://" + raw
}
return raw
}
func Parse(raw string) (*url.URL, error) {
parsed, err := url.Parse(Normalize(raw))
if err != nil {
return nil, fmt.Errorf("wsconn: %q: %w", raw, err)
}
switch strings.ToLower(parsed.Scheme) {
case "http", "https", "ws", "wss":
default:
return nil, fmt.Errorf("wsconn: %q is not an http, https, ws or wss URL", raw)
}
if strings.TrimSpace(parsed.Host) == "" {
return nil, fmt.Errorf("wsconn: %q has no host", raw)
}
if parsed.User != nil {
return nil, fmt.Errorf("wsconn: %q must not carry user info", raw)
}
return parsed, nil
}
func WebSocketScheme(u *url.URL) {
switch strings.ToLower(u.Scheme) {
case "https", "wss":
u.Scheme = "wss"
default:
u.Scheme = "ws"
}
}