ws/internal/http/upgrade/response.go

95 lines
2.2 KiB
Go
Raw Normal View History

2021-05-14 15:19:02 +00:00
package upgrade
import (
2018-09-29 12:39:12 +00:00
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
)
2021-05-14 15:19:02 +00:00
// HTTPVersion constant
const HTTPVersion = "1.1"
// UsedWSVersion constant websocket version
const UsedWSVersion = 13
// WSSalt constant websocket salt
const WSSalt = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
// Response represents an HTTP Upgrade Response
type Response struct {
code StatusCode // status code
accept []byte // processed from Sec-WebSocket-Key
protocol []byte // set from Sec-WebSocket-Protocol or none if not received
}
// SetStatusCode sets the status code
2021-05-14 15:19:02 +00:00
func (r *Response) SetStatusCode(sc StatusCode) {
r.code = sc
}
2021-05-14 14:47:02 +00:00
// SetProtocol sets the protocols
2021-05-14 15:19:02 +00:00
func (r *Response) SetProtocol(p []byte) {
r.protocol = p
}
// ProcessKey processes the accept token according
// to the rfc from the Sec-WebSocket-Key
2021-05-14 15:19:02 +00:00
func (r *Response) ProcessKey(k []byte) {
// do nothing for empty key
if k == nil || len(k) == 0 {
r.accept = nil
return
}
2021-05-14 15:19:02 +00:00
// 1. Concat with constant salt
mix := append(k, []byte(WSSalt)...)
2021-05-14 15:19:02 +00:00
// 2. Hash with sha1 algorithm
digest := sha1.Sum(mix)
2021-05-14 15:19:02 +00:00
// 3. Base64 encode it
2018-09-29 12:39:12 +00:00
r.accept = []byte(base64.StdEncoding.EncodeToString(digest[:sha1.Size]))
}
// Send sends the response through an io.Writer
// typically a socket
2021-05-14 15:19:02 +00:00
func (r Response) Send(w io.Writer) (int, error) {
2021-05-14 15:19:02 +00:00
// 1. Build response line
responseLine := fmt.Sprintf("HTTP/%s %d %s\r\n", HTTPVersion, r.code, r.code)
2021-05-14 15:19:02 +00:00
// 2. Build headers
optionalProtocol := ""
if len(r.protocol) > 0 {
optionalProtocol = fmt.Sprintf("Sec-WebSocket-Protocol: %s\r\n", r.protocol)
}
2021-05-14 15:19:02 +00:00
headers := fmt.Sprintf("Upgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: %d\r\n%s", UsedWSVersion, optionalProtocol)
if r.accept != nil {
headers = fmt.Sprintf("%sSec-WebSocket-Accept: %s\r\n", headers, r.accept)
}
headers = fmt.Sprintf("%s\r\n", headers)
2021-05-14 15:19:02 +00:00
// 3. Build all
2018-05-02 20:36:59 +00:00
raw := []byte(fmt.Sprintf("%s%s", responseLine, headers))
2021-05-14 15:19:02 +00:00
// 4. Write
written, err := w.Write(raw)
return written, err
}
// GetProtocol returns the choosen protocol if set, else nil
2021-05-14 15:19:02 +00:00
func (r Response) GetProtocol() []byte {
return r.protocol
}
// GetStatusCode returns the response status code
2021-05-14 15:19:02 +00:00
func (r Response) GetStatusCode() StatusCode {
return r.code
2018-09-29 12:39:12 +00:00
}