-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.go
More file actions
602 lines (545 loc) · 18.3 KB
/
websocket.go
File metadata and controls
602 lines (545 loc) · 18.3 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Package websocket implements a basic websocket server.
package websocket
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"slices"
"strings"
"sync"
"time"
"unicode/utf8"
)
// Default options.
const (
DefaultMaxFrameSize int = 16 << 10 // 16KiB
DefaultMaxMessageSize int = 256 << 10 // 256KiB
)
// Mode enalbes server or client behavior
type Mode bool
// Valid modes
const (
ServerMode Mode = false
ClientMode = true
)
// Options define the limits imposed on a websocket connection.
type Options struct {
Hooks Hooks
ReadTimeout time.Duration
WriteTimeout time.Duration
CloseTimeout time.Duration // defaults to ReadTimeout if set
MaxFrameSize int
MaxMessageSize int
}
type deadliner interface {
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
}
// Connections can be in one of three states.
type connState string
const (
connStateOpen connState = "open"
connStateClosing connState = "closing"
connStateClosed connState = "closed"
)
// ErrConnectionClosed is returned on any attempt to read from or write to
// a closed or closing connection.
var ErrConnectionClosed = errors.New("websocket: connection not open for reading or writing")
// Websocket is a websocket connection.
type Websocket struct {
conn net.Conn
mode Mode
// connection state, protected by mutex
mu sync.Mutex
state connState
// observability
clientKey ClientKey
hooks Hooks
// limits
closeTimeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
maxFrameSize int
maxMessageSize int
}
// Accept handles the initial HTTP-based handshake and upgrades the TCP
// connection to a websocket connection.
func Accept(w http.ResponseWriter, r *http.Request, opts Options) (*Websocket, error) {
clientKey, err := Handshake(w, r)
if err != nil {
return nil, fmt.Errorf("websocket: accept: handshake failed: %w", err)
}
conn, err := HijackConn(w)
if err != nil {
return nil, fmt.Errorf("websocket: accept: hijack failed: %w", err)
}
return New(conn, clientKey, ServerMode, opts), nil
}
// HijackConn takes over the [net.Conn] underlying an [http.ResponseWriter]
// for use as a websocket.
func HijackConn(w http.ResponseWriter) (net.Conn, error) {
hj, ok := w.(http.Hijacker)
if !ok {
return nil, errors.New("server does not support hijacking")
}
conn, rw, err := hj.Hijack()
if err != nil {
return nil, err
}
// per the Hijack docs, the returned read buffer may contain unprocessed
// data, so we return a net.Conn implementation that will read from that
// buffer first.
return &bufferedConn{
Conn: conn,
Reader: rw.Reader,
}, nil
}
// New is a low-level API that manually creates a new websocket connection.
// Caller is responsible for completing initial handshake before creating a
// websocket connection.
//
// Prefer the higher-level [Accept] API when possible. See also [Handshake] if
// using New directly.
func New(conn net.Conn, clientKey ClientKey, mode Mode, opts Options) *Websocket {
setDefaults(&opts)
return &Websocket{
conn: conn,
state: connStateOpen,
mode: mode,
clientKey: clientKey,
hooks: opts.Hooks,
closeTimeout: opts.CloseTimeout,
readTimeout: opts.ReadTimeout,
writeTimeout: opts.WriteTimeout,
maxFrameSize: opts.MaxFrameSize,
maxMessageSize: opts.MaxMessageSize,
}
}
// setDefaults sets the default values for any unset options.
func setDefaults(opts *Options) {
if opts.MaxFrameSize <= 0 {
opts.MaxFrameSize = DefaultMaxFrameSize
}
if opts.MaxMessageSize <= 0 {
opts.MaxMessageSize = DefaultMaxMessageSize
}
if opts.CloseTimeout == 0 && opts.ReadTimeout != 0 {
opts.CloseTimeout = opts.ReadTimeout
}
setupHooks(&opts.Hooks)
}
// Handshake is a low-level helper that validates the request and performs
// the WebSocket Handshake, after which only websocket frames should be
// written to the underlying connection.
//
// Prefer the higher-level [Accept] API when possible.
func Handshake(w http.ResponseWriter, r *http.Request) (ClientKey, error) {
if strings.ToLower(r.Header.Get("Upgrade")) != "websocket" {
return "", fmt.Errorf("missing required `Upgrade: websocket` header")
}
if v := r.Header.Get("Sec-Websocket-Version"); v != requiredVersion {
return "", fmt.Errorf("only websocket version %q is supported, got %q", requiredVersion, v)
}
clientKey := r.Header.Get("Sec-Websocket-Key")
if clientKey == "" {
return "", fmt.Errorf("missing required `Sec-Websocket-Key` header")
}
w.Header().Set("Connection", "upgrade")
w.Header().Set("Upgrade", "websocket")
w.Header().Set("Sec-Websocket-Accept", acceptKey(clientKey))
w.WriteHeader(http.StatusSwitchingProtocols)
return ClientKey(clientKey), nil
}
// ReadMessage reads a single [Message] from the connection, handling
// fragments and control frames automatically. Callers are responsible for
// closing the connection on error.
func (ws *Websocket) ReadMessage(ctx context.Context) (*Message, error) {
ws.mu.Lock()
defer ws.mu.Unlock()
if ws.state != connStateOpen {
return nil, ErrConnectionClosed
}
var msg *Message
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("websocket: read: %w", ctx.Err())
default:
}
frame, err := ws.readFrame()
if err != nil {
// If we failed to read a frame for any reason (generally a read
// timeout or a validation error), send a close frame to inform
// the peer of the reason and close connection immediately without
// waiting for a reply.
//
// This approach seems a) expected by the Autobahn test suite and
// b) supported by the RFC:
// https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.7
//
// (Search for "Fail the" to see all of the scenarios where
// immediately closing the connection is the correct behavior.)
return nil, err
}
opcode := frame.Opcode()
switch opcode {
case OpcodeBinary, OpcodeText:
if msg != nil {
return nil, ErrContinuationExpected
}
msg = &Message{
Binary: opcode == OpcodeBinary,
Payload: frame.Payload,
}
case OpcodeContinuation:
if msg == nil {
return nil, ErrContinuationUnexpected
}
if len(msg.Payload)+len(frame.Payload) > ws.maxMessageSize {
return nil, ErrMessageTooLarge
}
msg.Payload = append(msg.Payload, frame.Payload...)
case OpcodeClose:
if err := ws.doCloseHandshakeReply(closeReplyFrame(frame)); err != nil {
return nil, err
}
return nil, io.EOF
case OpcodePing:
frame = NewFrame(OpcodePong, true, frame.Payload)
if err := ws.writeFrame(frame); err != nil {
return nil, err
}
continue
case OpcodePong:
continue // no-op, assume reply to a ping
default:
return nil, ErrOpcodeUnknown
}
if frame.Fin() {
if !msg.Binary && !utf8.Valid(msg.Payload) {
return nil, ErrInvalidFramePayload
}
ws.hooks.OnReadMessage(ws.clientKey, msg)
return msg, nil
}
}
}
// WriteMessage writes a single [Message] to the connection, after splitting
// it into fragments (if necessary). Callers are responsible for closing the
// connection on error.
func (ws *Websocket) WriteMessage(ctx context.Context, msg *Message) error {
ws.mu.Lock()
defer ws.mu.Unlock()
if ws.state != connStateOpen {
return ErrConnectionClosed
}
ws.hooks.OnWriteMessage(ws.clientKey, msg)
for _, frame := range FrameMessage(msg, ws.maxFrameSize) {
select {
case <-ctx.Done():
return fmt.Errorf("websocket: write: %w", ctx.Err())
default:
if err := ws.writeFrame(frame); err != nil {
return fmt.Errorf("websocket: write: %w", err)
}
}
}
return nil
}
func (ws *Websocket) readFrame() (*Frame, error) {
ws.resetReadDeadline()
frame, err := ReadFrame(ws.conn, ws.mode, ws.maxFrameSize)
if err != nil {
ws.hooks.OnReadError(ws.clientKey, err)
return nil, fmt.Errorf("websocket: read: %w", err)
}
if err := validateFrame(frame); err != nil {
ws.hooks.OnReadError(ws.clientKey, err)
return nil, fmt.Errorf("websocket: read: %w", err)
}
ws.hooks.OnReadFrame(ws.clientKey, frame)
return frame, nil
}
func (ws *Websocket) writeFrame(frame *Frame) error {
ws.resetWriteDeadline()
ws.hooks.OnWriteFrame(ws.clientKey, frame)
if err := WriteFrame(ws.conn, ws.mask(), frame); err != nil {
ws.hooks.OnWriteError(ws.clientKey, err)
return err
}
return nil
}
// Handle is a high-level convienience method for request-response style
// websocket connections, where the given [Handler] is called for each
// incoming message read from the peer.
//
// If the handler returns a non-nil message, that message is written back
// to the peer. A nil message indicates that there is no reply to send.
//
// If the handler returns a non-nil error, its message will be sent to the
// client as the start of the two-way closing handshake with a status of
// Internal Error. NOTE: This means errors returned by the handler should not
// contain sensitive information that should not be exposed to peers.
//
// If there is an error reading from or writing to the underlying connection,
// the connection is assumed to be unrecoverable and is closed immediately,
// without waiting for the full two-way handshake. For more control over
// error handling and closing behavior, use [ReadMessage], [WriteMessage], and
// [CloseWithStatus] directly.
//
// See also [EchoHandler], a minimal handler that echoes each incoming message
// verbatim.
func (ws *Websocket) Handle(ctx context.Context, handler Handler) error {
for {
msg, err := ws.ReadMessage(ctx)
if err != nil {
// Special case: connection is closed, there's no need to write
// closing handshake. Consider this a normal, non-error end to the
// connection.
if errors.Is(err, io.EOF) {
return nil
}
// If we failed to read a message for any other reason (generally
// a read timeout or a validation error), send a close frame to
// inform the peer of the reason and close connection immediately
// without waiting for a reply.
//
// This approach seems a) expected by the Autobahn test suite and
// b) supported by the RFC:
// https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.7
//
// (Search for "Fail the" to see all of the scenarios where
// immediately closing the connection is the correct behavior.)
return ws.closeImmediately(err)
}
resp, err := handler(ctx, msg)
if err != nil {
// on error from handler function, start full closing handshake
// so that clients may be properly informed of application-level
// errors.
if closeErr := ws.CloseWithStatus(statusCodeForError(err)); closeErr != nil {
return fmt.Errorf("websocket: serve: error closing connection after application error: %w: %w", err, closeErr)
}
return err
}
if resp != nil {
if err := ws.WriteMessage(ctx, resp); err != nil {
// As with reads above, an error writing a message for any
// reason is considered fatal, so we try writing a close frame
// (which will probably also fail) and close the connection
// immediately instead of waiting for full two way handshake.
return ws.closeImmediately(err)
}
}
}
}
// mask returns an appropriate masking key for use when writing a message's
// frames.
func (ws *Websocket) mask() MaskingKey {
if ws.mode == ServerMode {
return Unmasked
}
return NewMaskingKey()
}
// Close starts a normal closing handshake. See [CloseWithStatus] for control
// over status code and reason.
func (ws *Websocket) Close() error {
return ws.CloseWithStatus(StatusNormalClosure, "")
}
// CloseWithStatus starts the closing handshake with the given status code and
// optional reason.
func (ws *Websocket) CloseWithStatus(status StatusCode, reason string) error {
ws.mu.Lock()
defer ws.mu.Unlock()
return ws.doCloseHandshake(NewCloseFrame(status, reason), nil)
}
// doCloseHandshake initiates the closing handshake and blocks until the
// handshake is completed by the peer or until the configured close timeout
// elapses.
//
// Note: caller must hold the mutex.
func (ws *Websocket) doCloseHandshake(closeFrame *Frame, cause error) error {
// enter closing state and ensure no one read or write can exceed our
// close timeout, since we may do multiple reads below while waiting for a
// reply to our close frame
ws.setState(connStateClosing)
ws.readTimeout = ws.closeTimeout
ws.writeTimeout = ws.closeTimeout
ws.hooks.OnCloseHandshakeStart(ws.clientKey, 0, cause)
if err := ws.writeFrame(closeFrame); err != nil {
ws.finishClose()
return fmt.Errorf("websocket: close: failed to write close frame %w", err)
}
// Once we've written the frame to start the closing handshake, we
// need to read frames until a) we get an ack close frame in return or
// b) the close deadline elapses. Any non-close frames read are
// discarded.
for {
frame, err := ws.readFrame()
if err != nil {
if !errors.Is(err, io.EOF) {
cause = fmt.Errorf("websocket: close: read failed while waiting for reply: %w", err)
}
ws.finishClose()
return cause
}
if frame.Opcode() != OpcodeClose {
// drop any non-close frames recevied after closing handshake
// is started
continue
}
ws.hooks.OnCloseHandshakeDone(ws.clientKey, 0, nil)
ws.finishClose()
return cause
}
}
// doCloseHandshakeReply sends a close frame in response to a closing
// handshake initiated by the other end and then closes our end of the
// connection.
func (ws *Websocket) doCloseHandshakeReply(closeFrame *Frame) error {
err := ws.writeFrame(closeFrame)
ws.hooks.OnCloseHandshakeDone(ws.clientKey, 0, err)
ws.finishClose()
return err
}
func (ws *Websocket) closeImmediately(cause error) error {
code, reason := statusCodeForError(cause)
frame := NewCloseFrame(code, reason)
_ = ws.writeFrame(frame) // connection is closed on our end, error not actionable
ws.hooks.OnCloseHandshakeDone(ws.clientKey, code, cause)
ws.finishClose()
return cause
}
func (ws *Websocket) finishClose() {
ws.setState(connStateClosed)
_ = ws.conn.Close() // nothing we can do about an error here
}
func (ws *Websocket) resetReadDeadline() {
if ws.readTimeout == 0 {
return
}
err := ws.conn.(deadliner).SetReadDeadline(time.Now().Add(ws.readTimeout))
if err != nil && !errors.Is(err, net.ErrClosed) {
panic(fmt.Sprintf("websocket: failed to set read deadline: %s", err))
}
}
func (ws *Websocket) resetWriteDeadline() {
if ws.writeTimeout == 0 {
return
}
err := ws.conn.(deadliner).SetWriteDeadline(time.Now().Add(ws.writeTimeout))
if err != nil && !errors.Is(err, net.ErrClosed) {
panic(fmt.Sprintf("websocket: failed to set write deadline: %s", err))
}
}
var validTransitions = map[connState][]connState{
connStateOpen: {
// proper closing handshake has started, waiting on reply from peer
connStateClosing,
// for some errors (e.g. network-level issues) we transition directly
// from open -> closed without doing a full closing handshake
connStateClosed,
},
connStateClosing: {connStateClosed},
connStateClosed: {connStateClosed}, // we treat closing a connection twice as a no-op
}
// setState updates the connection's state, ensuring that the transition is
// valid.
//
// Note: caller must hold the mutex.
func (ws *Websocket) setState(newState connState) {
isValid := slices.Contains(validTransitions[ws.state], newState)
if !isValid {
panic(fmt.Errorf("websocket: setState: invalid transition from %q to %q", ws.state, newState))
}
ws.state = newState
}
// ClientKey returns the [ClientKey] for a connection.
func (ws *Websocket) ClientKey() ClientKey {
return ws.clientKey
}
// Handler handles a single websocket [Message] as part of the high level
// [Handle] request-response API.
//
// If the returned message is non-nil, it will be sent to the client. If an
// error is returned, the connection will be closed.
type Handler func(ctx context.Context, msg *Message) (*Message, error)
// EchoHandler is a [Handler] that echoes each incoming [Message] back to the
// client.
var EchoHandler Handler = func(_ context.Context, msg *Message) (*Message, error) {
return msg, nil
}
// statusCodeForError returns an appropriate close frame status code and reason
// for the given error. If error is nil, returns a normal closure status code.
func statusCodeForError(err error) (StatusCode, string) {
if err == nil || errors.Is(err, io.EOF) {
return StatusNormalClosure, ""
}
var protoErr *Error
if errors.As(err, &protoErr) {
return protoErr.code, protoErr.Error()
}
return StatusInternalError, err.Error()
}
// bufferedConn ties a [net.Conn] to a [bufio.Reader] wrapping that conn, such
// as those returned by [http.Hijacker.Hijack], so that all reads go through
// the buffered reader but writes go directly to the underlying conn.
type bufferedConn struct {
net.Conn
Reader *bufio.Reader
}
func (bc *bufferedConn) Read(p []byte) (int, error) {
return bc.Reader.Read(p)
}
var _ net.Conn = &bufferedConn{}
// Hooks define the callbacks that are called during the lifecycle of a
// websocket connection.
type Hooks struct {
// OnCloseStart is called when the a close handshake is initiated.
OnCloseHandshakeStart func(ClientKey, StatusCode, error)
// OnCloseHandshakeDone is called when the close handshake is complete.
OnCloseHandshakeDone func(ClientKey, StatusCode, error)
// OnReadError is called when a read error occurs.
OnReadError func(ClientKey, error)
// OnReadFrame is called when a frame is read.
OnReadFrame func(ClientKey, *Frame)
// OnReadMessage is called when a complete message is read.
OnReadMessage func(ClientKey, *Message)
// OnWriteError is called when a write error occurs.
OnWriteError func(ClientKey, error)
// OnWriteFrame is called when a frame is written.
OnWriteFrame func(ClientKey, *Frame)
// OnWriteMessage is called when a complete message is written.
OnWriteMessage func(ClientKey, *Message)
}
// setupHooks ensures that all hooks have a default no-op function if unset.
func setupHooks(hooks *Hooks) {
if hooks.OnCloseHandshakeStart == nil {
hooks.OnCloseHandshakeStart = func(ClientKey, StatusCode, error) {}
}
if hooks.OnCloseHandshakeDone == nil {
hooks.OnCloseHandshakeDone = func(ClientKey, StatusCode, error) {}
}
if hooks.OnReadError == nil {
hooks.OnReadError = func(ClientKey, error) {}
}
if hooks.OnReadFrame == nil {
hooks.OnReadFrame = func(ClientKey, *Frame) {}
}
if hooks.OnReadMessage == nil {
hooks.OnReadMessage = func(ClientKey, *Message) {}
}
if hooks.OnWriteError == nil {
hooks.OnWriteError = func(ClientKey, error) {}
}
if hooks.OnWriteFrame == nil {
hooks.OnWriteFrame = func(ClientKey, *Frame) {}
}
if hooks.OnWriteMessage == nil {
hooks.OnWriteMessage = func(ClientKey, *Message) {}
}
}