-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrader-helper.go
44 lines (36 loc) · 967 Bytes
/
upgrader-helper.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
package wstf
import (
"net/http"
"strings"
"github.com/gorilla/websocket"
)
var WebSocketHeaderRequirements = map[string]string{
"Connection": "upgrade",
"Upgrade": "websocket",
"Sec-Websocket-Version": "13",
//"Pragma": "no-cache",
//"Cache-Control": "no-cache",
}
// The helper function for the upgrader to check whether a request can be upgraded.
// @see https://tools.ietf.org/html/rfc6455#page-60
// @see https://github.com/gorilla/websocket/blob/master/server.go#L112
func AbleToUpgrade(upgrader *websocket.Upgrader, w http.ResponseWriter, r *http.Request) bool {
if r.Method != "GET" {
return false
}
// Check headers.
for k, v := range WebSocketHeaderRequirements {
if strings.ToLower(r.Header.Get(k)) != v {
return false
}
}
if r.Header.Get("Sec-Websocket-Key") == "" {
return false
}
// Check origin.
if upgrader.CheckOrigin == nil || !upgrader.CheckOrigin(r) {
return false
}
_, ok := w.(http.Hijacker)
return ok
}