removed checks/extracts
This commit is contained in:
parent
3159ffe3eb
commit
0e20c4cba6
|
@ -1,8 +1,6 @@
|
|||
package request
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
|
||||
)
|
||||
|
@ -41,7 +39,10 @@ func (r *T) parseHeader(b []byte) error {
|
|||
switch head.Name {
|
||||
|
||||
case header.HOST: fmt.Printf("[host] ")
|
||||
err = r.extractHostPort(head.Values[0])
|
||||
err = r.extractHostPort(head.Values)
|
||||
|
||||
case header.ORIGIN: fmt.Printf("[origin] ")
|
||||
err = r.extractOrigin(head.Values)
|
||||
|
||||
case header.UPGRADE: fmt.Printf("[upgrade] ")
|
||||
err = r.checkUpgrade(head.Values)
|
||||
|
@ -49,83 +50,27 @@ func (r *T) parseHeader(b []byte) error {
|
|||
case header.CONNECTION: fmt.Printf("[connection] ")
|
||||
err = r.checkConnection(head.Values)
|
||||
|
||||
case header.WSKEY: fmt.Printf("[sec-websocket-key] ")
|
||||
case header.ORIGIN: fmt.Printf("[origin] ")
|
||||
case header.WSPROTOCOL: fmt.Printf("[sec-websocket-protocol] ")
|
||||
case header.WSVERSION: fmt.Printf("[sec-websocket-version] ")
|
||||
err = r.checkVersion(head.Values)
|
||||
|
||||
case header.WSKEY: fmt.Printf("[sec-websocket-key] ")
|
||||
err = r.extractKey(head.Values)
|
||||
|
||||
case header.WSPROTOCOL: fmt.Printf("[sec-websocket-protocol] ")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
if err != nil { return err }
|
||||
|
||||
|
||||
for i, v := range head.Values {
|
||||
if i == 0 {
|
||||
fmt.Printf("[ '%s'", v)
|
||||
} else {
|
||||
fmt.Printf(", '%s'", v)
|
||||
}
|
||||
if i == 0 { fmt.Printf("[ '%s'", v)
|
||||
} else { fmt.Printf(", '%s'", v) }
|
||||
}
|
||||
fmt.Printf(" ]\n");
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// checkHost checks and extracts the Host header
|
||||
func (r *T) extractHostPort(b []byte) error {
|
||||
|
||||
split := strings.Split(string(b), ":")
|
||||
|
||||
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 {
|
||||
return fmt.Errorf("Cannot read port number '%s'", split[1])
|
||||
}
|
||||
|
||||
r.port = uint16(readPort)
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
// checkConnection checks the 'Connection' header
|
||||
// it MUST contain 'Upgrade'
|
||||
func (r T) checkConnection(bb [][]byte) error {
|
||||
|
||||
for _, b := range bb {
|
||||
|
||||
if strings.ToLower( string(b) ) == "upgrade" {
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return fmt.Errorf("Connection header must be 'Upgrade'")
|
||||
|
||||
}
|
||||
|
||||
// checkUpgrade checks the 'Upgrade' header
|
||||
// it MUST be 'websocket'
|
||||
func (r T) checkUpgrade(bb [][]byte) error {
|
||||
|
||||
if len(bb) != 1 {
|
||||
return fmt.Errorf("Upgrade header must have only 1 element")
|
||||
}
|
||||
|
||||
if strings.ToLower( string(bb[0]) ) == "websocket" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("Upgrade header must be 'websocket', got '%s'", bb[0])
|
||||
|
||||
}
|
Loading…
Reference in New Issue