2018-05-01 23:45:06 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsWebSocketUpgrade checks to see if the request is a WebSocket connection.
|
|
|
|
func IsWebSocketUpgrade(req *http.Request) bool {
|
|
|
|
return websocket.IsWebSocketUpgrade(req)
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
// NewResponseHeader returns headers needed to return to origin for completing handshake
|
|
|
|
func NewResponseHeader(req *http.Request) http.Header {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Add("Connection", "Upgrade")
|
2021-04-02 06:10:43 +00:00
|
|
|
header.Add("Sec-Websocket-Accept", generateAcceptKey(req.Header.Get("Sec-WebSocket-Key")))
|
2020-12-09 21:46:53 +00:00
|
|
|
header.Add("Upgrade", "websocket")
|
|
|
|
return header
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 06:10:43 +00:00
|
|
|
// from RFC-6455
|
|
|
|
var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
|
|
|
|
|
|
|
|
func generateAcceptKey(challengeKey string) string {
|
|
|
|
h := sha1.New()
|
|
|
|
h.Write([]byte(challengeKey))
|
|
|
|
h.Write(keyGUID)
|
|
|
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
|
|
}
|