-
Notifications
You must be signed in to change notification settings - Fork 77
/
websocket.go
292 lines (257 loc) · 8.44 KB
/
websocket.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package gotalk
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"golang.org/x/net/websocket"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
// WebSocketConnection is an alias for the websocket connection type, to spare the use
// from having to import golang.org/x/net/websocket
type WebSocketConnection = websocket.Conn
// WebSocket is a type of gotalk.Sock used for web socket connections,
// managed by a WebSocketServer.
type WebSocket struct {
Sock
// A function to be called when the socket closes. See Socket.CloseHandler for details.
CloseHandler func(s *WebSocket, code int)
}
// Conn returns the underlying web socket connection.
//
// Accessing the web socket connection inside a handler function:
// Handler functions can opt in to receive a pointer to a Sock but not a WebSocket.
// This makes handlers more portable, testable and the implementation becomes simpler.
// However, sometimes you might need to access the web socket connection anyhow:
//
// gotalk.Handle("foo", func(s *gotalk.Sock, m FooMessage) error {
// ws := s.Conn().(*gotalk.WebSocketConnection)
// // do something with ws
// return nil
// })
//
func (s *WebSocket) Conn() *WebSocketConnection { return s.Sock.conn.(*WebSocketConnection) }
// Request returns the http request upgraded to the WebSocket
func (s *WebSocket) Request() *http.Request { return s.Conn().Request() }
// Context returns the http request's context. The returned context is never nil.
func (s *WebSocket) Context() context.Context { return s.Request().Context() }
// ---------------------------------------------------------------------------------
// WebSocketServer conforms to http.HandlerFunc and is used to serve Gotalk over HTTP or HTTPS
type WebSocketServer struct {
// Handlers describe what this server is capable of responding to.
// Initially set to gotalk.DefaultHandlers by NewWebSocketServer().
//
// Handler can be assigned a new set of handlers at any time.
// Whenever a new socket is connected, it references the current value of Handlers, therefore
// changes to Handlers has an effect for newly connected sockets only.
*Handlers
// Limits control resource limits.
// Initially set to gotalk.DefaultLimits by NewWebSocketServer().
*Limits
// OnConnect is an optional handler to be invoked when a new socket is connected.
// This handler is only called for sockets which passed the protocol handshake.
// If you want to deny a connection, simply call s.Close() on the socket in this handler.
//
// Gotalk checks if Origin header is a valid URL by default but does nothing else in terms of
// origin validation. You might want to verify s.Conn().Config().Origin in OnConnect.
OnConnect func(s *WebSocket)
// HeartbeatInterval is not used directly by WebSocketServer but assigned to every new socket
// that is connected. The default initial value (0) means "no automatic heartbeats" (disabled.)
//
// Note that automatic heartbeats are usually not a good idea for web sockets for two reasons:
//
// a) You usually want to keep as few connections open as possible; letting them time out is
// often desired (heartbeats prevent connection timeout.)
//
// b) Automatic timeout uses more resources
//
HeartbeatInterval time.Duration
// OnHeartbeat is an optional callback for heartbeat confirmation messages.
// Not used directly by WebSocketServer but assigned to every new socket that is connected.
OnHeartbeat func(load int, t time.Time)
// Underlying websocket server (will become a function in gotalk 2)
Server *websocket.Server
// DEPRECATED use OnConnect instead
OnAccept SockHandler
// storage for underlying web socket server.
// Server is a pointer to this for legacy reasons.
// Gotalk <=1.1.5 allocated websocket.Server separately on the heap and assigned it to Server.
server websocket.Server
}
// NewWebSocketServer creates a web socket server which is a http.Handler
func NewWebSocketServer() *WebSocketServer {
s := &WebSocketServer{
Handlers: DefaultHandlers,
Limits: DefaultLimits,
server: websocket.Server{
Handshake: checkOrigin,
},
}
s.Server = &s.server // legacy API
s.server.Handler = s.onAccept
return s
}
// DEPREACTED use NewWebSocketServer
func WebSocketHandler() *WebSocketServer {
return NewWebSocketServer()
}
func (s *WebSocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, gotalkJSSuffix) {
if s.maybeReplyNotModified(w, r) {
return
}
contentLength := jslibLen
body := jslibBody
header := w.Header()
acceptEncoding := parseCommaStrSet(r.Header.Get("Accept-Encoding"))
if _, ok := acceptEncoding["gzip"]; ok {
header["Content-Encoding"] = []string{"gzip"}
contentLength = jslibLenGzip
body = jslibBodyGzip
}
header["Content-Length"] = contentLength
for k, v := range jslibHeader {
header[k] = v
}
w.WriteHeader(http.StatusOK)
w.Write(body)
} else {
// upgrade request connection to web socket protocol
s.server.ServeHTTP(w, r)
}
}
// ---------------------------------------------------------------------------------
// internal
// onAccept is called for new web socket connections
func (server *WebSocketServer) onAccept(ws *WebSocketConnection) {
// Set the frame payload type of the web socket
ws.PayloadType = websocket.BinaryFrame
// Create a new gotalk socket of the WebSocket flavor
sock := &WebSocket{
Sock: Sock{
Handlers: server.Handlers,
HeartbeatInterval: server.HeartbeatInterval,
OnHeartbeat: server.OnHeartbeat,
conn: ws,
},
}
sock.Sock.CloseHandler = func(_ *Sock, code int) {
if sock.CloseHandler != nil {
sock.CloseHandler(sock, code)
}
}
// Adopt the web socket
sock.Adopt(ws)
// perform protocol handshake
if err := sock.Handshake(); err != nil {
sock.Close()
return
}
// Call optional OnConnect handler
if server.OnConnect != nil {
server.OnConnect(sock)
} else if server.OnAccept != nil {
// legacy deprecated callback
server.OnAccept(&sock.Sock)
}
// If the OnConnect handler closed the connection, stop here
if sock.conn == nil {
return
}
// enter read loop
sock.Read(server.Limits)
}
func checkOrigin(config *websocket.Config, req *http.Request) (err error) {
config.Origin, err = websocket.Origin(config, req)
if err == nil && config.Origin == nil {
return fmt.Errorf("null origin")
}
return err
}
const gotalkJSSuffix = "/gotalk.js"
const gotalkJSMapSuffix = "/gotalk.js.map"
// js lib cache
var (
jslibInitOnce sync.Once
jslibETag string
jslibHeader http.Header // alias map[string][]string
jslibLen []string
jslibBody []byte
jslibLenGzip []string
jslibBodyGzip []byte
)
func init() {
jslibETag = "\"" + JSLibSHA1Base64 + "\""
jslibHeader = map[string][]string{
"Content-Type": {"application/javascript; charset=utf-8"},
"Cache-Control": {"public,max-age=300"}, // 5min
"ETag": {jslibETag},
}
// Note on Cache-Control:
// max-age is the max time a browser can hold on to a copy of gotalk.js without
// checking back with an etag. I.e. it does not limit the age of the cached resource
// but limits how often the browser revalidates. max-age should thus be relatively low
// as a revalidation request will in most cases end early with a HTTP 304 Not Modified
// response. 5min seems like a decent value.
// TODO: Consider making max-age configurable.
// uncompressed
jslibBody = []byte(JSLibString)
jslibLen = []string{strconv.FormatInt(int64(len(jslibBody)), 10)}
// gzip
var zbuf bytes.Buffer
zw, _ := gzip.NewWriterLevel(&zbuf, gzip.BestCompression)
_, err := zw.Write(jslibBody)
if err2 := zw.Close(); err2 != nil && err == nil {
err = err2
}
if err != nil {
panic(err)
}
jslibBodyGzip = zbuf.Bytes()
jslibLenGzip = []string{strconv.FormatInt(int64(len(jslibBodyGzip)), 10)}
}
func (s *WebSocketServer) maybeReplyNotModified(w http.ResponseWriter, r *http.Request) bool {
reqETag := r.Header["If-None-Match"]
if len(reqETag) != 0 && reqETag[0] == jslibETag {
w.WriteHeader(http.StatusNotModified)
return true
}
return false
}
func parseCommaStrSet(s string) map[string]struct{} {
m := make(map[string]struct{})
if len(s) == 0 {
return m
}
var start int = 0
var end int = -1
for i, b := range s {
if start == -1 {
if b != ' ' {
start = i
}
} else {
switch b {
case ' ':
end = i
case ',':
if end == -1 {
end = i
}
m[s[start:end]] = struct{}{}
end = -1
start = -1
break
}
}
}
if start != -1 {
end = len(s)
m[s[start:end]] = struct{}{}
}
return m
}