2021-05-14 15:19:02 +00:00
|
|
|
package upgrade
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// checkHost checks and extracts the Host header
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) extractHostPort(bb HeaderValue) error {
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
if len(bb) != 1 {
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Host", fmt.Sprintf("expected single value, got %d", len(bb))}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 06:53:15 +00:00
|
|
|
if len(bb[0]) <= 3 {
|
|
|
|
return &InvalidRequest{"Host", fmt.Sprintf("expected non-empty value (got %d bytes)", len(bb[0]))}
|
|
|
|
}
|
|
|
|
|
2018-04-25 15:01:39 +00:00
|
|
|
split := strings.Split(string(bb[0]), ":")
|
|
|
|
|
|
|
|
r.host = split[0]
|
|
|
|
|
|
|
|
// no port
|
|
|
|
if len(split) < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract port
|
|
|
|
readPort, err := strconv.ParseUint(split[1], 10, 16)
|
|
|
|
if err != nil {
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = BadRequest
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Host", "cannot read port"}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.port = uint16(readPort)
|
|
|
|
|
|
|
|
// if 'Origin' header is already read, check it
|
|
|
|
if len(r.origin) > 0 {
|
|
|
|
if err != nil {
|
2018-09-29 12:36:47 +00:00
|
|
|
err = r.checkOriginPolicy()
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = Forbidden
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidOriginPolicy{r.host, r.origin, err}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkOrigin checks the Origin Header
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) extractOrigin(bb HeaderValue) error {
|
2018-04-25 15:01:39 +00:00
|
|
|
|
2018-05-01 20:14:35 +00:00
|
|
|
// bypass
|
2018-09-29 12:36:47 +00:00
|
|
|
if bypassOriginPolicy {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-01 20:14:35 +00:00
|
|
|
|
2018-04-25 15:01:39 +00:00
|
|
|
if len(bb) != 1 {
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = Forbidden
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Origin", fmt.Sprintf("expected single value, got %d", len(bb))}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.origin = string(bb[0])
|
|
|
|
|
|
|
|
// if host already stored, check origin policy
|
|
|
|
if len(r.host) > 0 {
|
|
|
|
err := r.checkOriginPolicy()
|
|
|
|
if err != nil {
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = Forbidden
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidOriginPolicy{r.host, r.origin, err}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkOriginPolicy origin policy based on 'host' value
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) checkOriginPolicy() error {
|
2018-04-25 15:01:39 +00:00
|
|
|
// TODO: Origin policy, for now BYPASS
|
2018-04-25 15:21:56 +00:00
|
|
|
r.validPolicy = true
|
2018-04-25 15:01:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkConnection checks the 'Connection' header
|
|
|
|
// it MUST contain 'Upgrade'
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) checkConnection(bb HeaderValue) error {
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
for _, b := range bb {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
if strings.ToLower(string(b)) == "upgrade" {
|
2018-04-25 15:21:56 +00:00
|
|
|
r.hasConnection = true
|
2018-04-25 15:01:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = BadRequest
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Upgrade", "expected 'Upgrade' (case insensitive)"}
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkUpgrade checks the 'Upgrade' header
|
|
|
|
// it MUST be 'websocket'
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) checkUpgrade(bb HeaderValue) error {
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
if len(bb) != 1 {
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = BadRequest
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Upgrade", fmt.Sprintf("expected single value, got %d", len(bb))}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
if strings.ToLower(string(bb[0])) == "websocket" {
|
2018-04-25 15:21:56 +00:00
|
|
|
r.hasUpgrade = true
|
2018-04-25 15:01:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = BadRequest
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Upgrade", fmt.Sprintf("expected 'websocket' (case insensitive), got '%s'", bb[0])}
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkVersion checks the 'Sec-WebSocket-Version' header
|
|
|
|
// it MUST be '13'
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) checkVersion(bb HeaderValue) error {
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
if len(bb) != 1 || string(bb[0]) != "13" {
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = UpgradeRequired
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Sec-WebSocket-Version", fmt.Sprintf("expected '13', got '%s'", bb[0])}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 15:21:56 +00:00
|
|
|
r.hasVersion = true
|
2018-04-25 15:01:39 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractKey extracts the 'Sec-WebSocket-Key' header
|
|
|
|
// it MUST be 24 bytes (base64)
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) extractKey(bb HeaderValue) error {
|
2018-04-25 15:01:39 +00:00
|
|
|
|
|
|
|
if len(bb) != 1 || len(bb[0]) != 24 {
|
2021-05-14 15:19:02 +00:00
|
|
|
r.code = BadRequest
|
2018-05-04 06:41:40 +00:00
|
|
|
return &InvalidRequest{"Sec-WebSocket-Key", fmt.Sprintf("expected 24 bytes base64, got %d bytes", len(bb[0]))}
|
2018-04-25 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 15:21:56 +00:00
|
|
|
r.key = bb[0]
|
|
|
|
|
2018-04-25 15:01:39 +00:00
|
|
|
return nil
|
|
|
|
|
2018-04-25 15:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extractProtocols extracts the 'Sec-WebSocket-Protocol' header
|
|
|
|
// it can contain multiple values
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Request) extractProtocols(bb HeaderValue) error {
|
2018-04-25 15:21:56 +00:00
|
|
|
|
|
|
|
r.protocols = bb
|
|
|
|
|
|
|
|
return nil
|
2018-09-29 12:36:47 +00:00
|
|
|
}
|