2018-04-24 21:10:01 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
2018-04-25 14:25:58 +00:00
|
|
|
"strings"
|
|
|
|
"strconv"
|
2018-04-24 21:10:01 +00:00
|
|
|
"fmt"
|
2018-04-25 13:54:15 +00:00
|
|
|
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
|
2018-04-24 21:10:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-04-25 13:41:39 +00:00
|
|
|
// parseHeader parses any http request line
|
|
|
|
// (header and request-line)
|
|
|
|
func (r *T) parseHeader(b []byte) error {
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
/* (1) First line -> GET {uri} HTTP/{version}
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if !r.first {
|
|
|
|
|
2018-04-25 07:58:50 +00:00
|
|
|
err := r.request.Parse(b)
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-04-25 13:41:39 +00:00
|
|
|
return fmt.Errorf("Error while parsing first line: %s", err)
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.first = true
|
2018-04-25 13:41:39 +00:00
|
|
|
return nil
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-25 07:58:50 +00:00
|
|
|
|
2018-04-24 21:10:01 +00:00
|
|
|
/* (2) Other lines -> Header-Name: Header-Value
|
|
|
|
---------------------------------------------------------*/
|
2018-04-25 13:01:12 +00:00
|
|
|
/* (1) Try to parse header */
|
|
|
|
head, err := header.Parse(b)
|
|
|
|
if err != nil {
|
2018-04-25 13:41:39 +00:00
|
|
|
return fmt.Errorf("Error parsing header: %s", err)
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 13:01:12 +00:00
|
|
|
/* (2) Manage header */
|
|
|
|
switch head.Name {
|
2018-04-25 14:25:58 +00:00
|
|
|
|
|
|
|
case header.HOST: fmt.Printf("[host] ")
|
|
|
|
err = r.extractHostPort(head.Values[0])
|
|
|
|
|
|
|
|
case header.UPGRADE: fmt.Printf("[upgrade] ")
|
2018-04-25 14:40:43 +00:00
|
|
|
err = r.checkUpgrade(head.Values)
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
case header.CONNECTION: fmt.Printf("[connection] ")
|
2018-04-25 14:40:43 +00:00
|
|
|
err = r.checkConnection(head.Values)
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
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] ")
|
|
|
|
default:
|
|
|
|
return nil
|
2018-04-25 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
|
|
|
|
for i, v := range head.Values {
|
|
|
|
if i == 0 {
|
|
|
|
fmt.Printf("[ '%s'", v)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(", '%s'", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf(" ]\n");
|
|
|
|
|
2018-04-25 13:41:39 +00:00
|
|
|
return nil
|
|
|
|
|
2018-04-25 13:01:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
// checkHost checks and extracts the Host header
|
|
|
|
func (r *T) extractHostPort(b []byte) error {
|
2018-04-25 13:01:12 +00:00
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
split := strings.Split(string(b), ":")
|
2018-04-25 13:01:12 +00:00
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
r.host = split[0]
|
2018-04-25 13:01:12 +00:00
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
// no port
|
|
|
|
if len(split) < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-24 21:10:01 +00:00
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
// extract port
|
|
|
|
readPort, err := strconv.ParseUint(split[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot read port number '%s'", split[1])
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 14:25:58 +00:00
|
|
|
r.port = uint16(readPort)
|
|
|
|
|
|
|
|
return nil
|
2018-04-25 13:01:12 +00:00
|
|
|
|
2018-04-25 14:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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])
|
|
|
|
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|