This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
websocket.go
307 lines (267 loc) · 10.2 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://fiber.wiki
// 📝 Github Repository: https://github.com/gofiber/fiber
package websocket
import (
"errors"
"io"
"sync"
"time"
"github.com/fasthttp/websocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)
// Config ...
type Config struct {
// Filter defines a function to skip middleware.
// Optional. Default: nil
Filter func(*fiber.Ctx) bool
// HandshakeTimeout specifies the duration for the handshake to complete.
HandshakeTimeout time.Duration
// Subprotocols specifies the client's requested subprotocols.
Subprotocols []string
// Allowed Origin's based on the Origin header, this validate the request origin to
// prevent cross-site request forgery. Everything is allowed if left empty.
Origins []string
// ReadBufferSize and WriteBufferSize specify I/O buffer sizes in bytes. If a buffer
// size is zero, then a useful default size is used. The I/O buffer sizes
// do not limit the size of the messages that can be sent or received.
ReadBufferSize, WriteBufferSize int
// WriteBufferPool is a pool of buffers for write operations. If the value
// is not set, then write buffers are allocated to the connection for the
// lifetime of the connection.
//
// A pool is most useful when the application has a modest volume of writes
// across a large number of connections.
//
// Applications should use a single pool for each unique value of
// WriteBufferSize.
WriteBufferPool websocket.BufferPool
// EnableCompression specifies if the client 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.
EnableCompression bool
}
// New returns a new `handler func(*Conn)` that upgrades a client to the
// websocket protocol, you can pass an optional config.
func New(handler func(*Conn), config ...Config) fiber.Handler {
// Init config
var cfg Config
if len(config) > 0 {
cfg = config[0]
}
if len(cfg.Origins) == 0 {
cfg.Origins = []string{"*"}
}
if cfg.ReadBufferSize == 0 {
cfg.ReadBufferSize = 1024
}
if cfg.WriteBufferSize == 0 {
cfg.WriteBufferSize = 1024
}
var upgrader = websocket.FastHTTPUpgrader{
HandshakeTimeout: cfg.HandshakeTimeout,
Subprotocols: cfg.Subprotocols,
ReadBufferSize: cfg.ReadBufferSize,
WriteBufferSize: cfg.WriteBufferSize,
EnableCompression: cfg.EnableCompression,
WriteBufferPool: cfg.WriteBufferPool,
CheckOrigin: func(fctx *fasthttp.RequestCtx) bool {
if cfg.Origins[0] == "*" {
return true
}
origin := utils.UnsafeString(fctx.Request.Header.Peek("Origin"))
for i := range cfg.Origins {
if cfg.Origins[i] == origin {
return true
}
}
return false
},
}
return func(c *fiber.Ctx) error {
if cfg.Filter != nil && !cfg.Filter(c) {
return c.Next()
}
conn := acquireConn()
// locals
c.Context().VisitUserValues(func(key []byte, value interface{}) {
conn.locals[string(key)] = value
})
// params
params := c.Route().Params
for i := 0; i < len(params); i++ {
conn.params[utils.CopyString(params[i])] = utils.ImmutableString(c.Params(params[i]))
}
// queries
c.Context().QueryArgs().VisitAll(func(key, value []byte) {
conn.queries[string(key)] = string(value)
})
// cookies
c.Context().Request.Header.VisitAllCookie(func(key, value []byte) {
conn.cookies[string(key)] = string(value)
})
// headers
c.Context().Request.Header.VisitAll(func(key, value []byte) {
conn.headers[string(key)] = string(value)
})
if err := upgrader.Upgrade(c.Context(), func(fconn *websocket.Conn) {
conn.Conn = fconn
defer releaseConn(conn)
handler(conn)
}); err != nil { // Upgrading required
return fiber.ErrUpgradeRequired
}
return nil
}
}
// Conn https://godoc.org/github.com/gorilla/websocket#pkg-index
type Conn struct {
*websocket.Conn
locals map[string]interface{}
params map[string]string
cookies map[string]string
headers map[string]string
queries map[string]string
}
// Conn pool
var poolConn = sync.Pool{
New: func() interface{} {
return new(Conn)
},
}
// Acquire Conn from pool
func acquireConn() *Conn {
conn := poolConn.Get().(*Conn)
conn.locals = make(map[string]interface{})
conn.params = make(map[string]string)
conn.queries = make(map[string]string)
conn.cookies = make(map[string]string)
conn.headers = make(map[string]string)
return conn
}
// Return Conn to pool
func releaseConn(conn *Conn) {
conn.Conn = nil
poolConn.Put(conn)
}
// Locals makes it possible to pass interface{} values under string keys scoped to the request
// and therefore available to all following routes that match the request.
func (conn *Conn) Locals(key string) interface{} {
return conn.locals[key]
}
// Params is used to get the route parameters.
// Defaults to empty string "" if the param doesn't exist.
// If a default value is given, it will return that value if the param doesn't exist.
func (conn *Conn) Params(key string, defaultValue ...string) string {
v, ok := conn.params[key]
if !ok && len(defaultValue) > 0 {
return defaultValue[0]
}
return v
}
// Query returns the query string parameter in the url.
// Defaults to empty string "" if the query doesn't exist.
// If a default value is given, it will return that value if the query doesn't exist.
func (conn *Conn) Query(key string, defaultValue ...string) string {
v, ok := conn.queries[key]
if !ok && len(defaultValue) > 0 {
return defaultValue[0]
}
return v
}
// Cookies is used for getting a cookie value by key
// Defaults to empty string "" if the cookie doesn't exist.
// If a default value is given, it will return that value if the cookie doesn't exist.
func (conn *Conn) Cookies(key string, defaultValue ...string) string {
v, ok := conn.cookies[key]
if !ok && len(defaultValue) > 0 {
return defaultValue[0]
}
return v
}
// Headers is used for getting a header value by key
// Defaults to empty string "" if the header doesn't exist.
// If a default value is given, it will return that value if the header doesn't exist.
func (conn *Conn) Headers(key string, defaultValue ...string) string {
v, ok := conn.headers[key]
if !ok && len(defaultValue) > 0 {
return defaultValue[0]
}
return v
}
// Constants are taken from https://github.com/fasthttp/websocket/blob/master/conn.go#L43
// Close codes defined in RFC 6455, section 11.7.
const (
CloseNormalClosure = 1000
CloseGoingAway = 1001
CloseProtocolError = 1002
CloseUnsupportedData = 1003
CloseNoStatusReceived = 1005
CloseAbnormalClosure = 1006
CloseInvalidFramePayloadData = 1007
ClosePolicyViolation = 1008
CloseMessageTooBig = 1009
CloseMandatoryExtension = 1010
CloseInternalServerErr = 1011
CloseServiceRestart = 1012
CloseTryAgainLater = 1013
CloseTLSHandshake = 1015
)
// The message types are defined in RFC 6455, section 11.8.
const (
// TextMessage denotes a text data message. The text message payload is
// interpreted as UTF-8 encoded text data.
TextMessage = 1
// BinaryMessage denotes a binary data message.
BinaryMessage = 2
// CloseMessage denotes a close control message. The optional message
// payload contains a numeric code and text. Use the FormatCloseMessage
// function to format a close message payload.
CloseMessage = 8
// PingMessage denotes a ping control message. The optional message payload
// is UTF-8 encoded text.
PingMessage = 9
// PongMessage denotes a pong control message. The optional message payload
// is UTF-8 encoded text.
PongMessage = 10
)
var (
// ErrBadHandshake is returned when the server response to opening handshake is
// invalid.
ErrBadHandshake = errors.New("websocket: bad handshake")
// ErrCloseSent is returned when the application writes a message to the
// connection after sending a close message.
ErrCloseSent = errors.New("websocket: close sent")
// ErrReadLimit is returned when reading a message that is larger than the
// read limit set for the connection.
ErrReadLimit = errors.New("websocket: read limit exceeded")
)
// FormatCloseMessage formats closeCode and text as a WebSocket close message.
// An empty message is returned for code CloseNoStatusReceived.
func FormatCloseMessage(closeCode int, text string) []byte {
return websocket.FormatCloseMessage(closeCode, text)
}
// IsCloseError returns boolean indicating whether the error is a *CloseError
// with one of the specified codes.
func IsCloseError(err error, codes ...int) bool {
return websocket.IsCloseError(err, codes...)
}
// IsUnexpectedCloseError returns boolean indicating whether the error is a
// *CloseError with a code not in the list of expected codes.
func IsUnexpectedCloseError(err error, expectedCodes ...int) bool {
return websocket.IsUnexpectedCloseError(err, expectedCodes...)
}
// IsWebSocketUpgrade returns true if the client requested upgrade to the
// WebSocket protocol.
func IsWebSocketUpgrade(c *fiber.Ctx) bool {
return websocket.FastHTTPIsWebSocketUpgrade(c.Context())
}
// JoinMessages concatenates received messages to create a single io.Reader.
// The string term is appended to each message. The returned reader does not
// support concurrent calls to the Read method.
func JoinMessages(c *websocket.Conn, term string) io.Reader {
return websocket.JoinMessages(c, term)
}