extractHostPort() from the 'Host' header

This commit is contained in:
xdrm-brackets 2018-04-25 16:25:58 +02:00
parent 0792cfaa06
commit 4bcfd9c6ab
1 changed files with 42 additions and 16 deletions

View File

@ -1,6 +1,8 @@
package request package request
import ( import (
"strings"
"strconv"
"fmt" "fmt"
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header" "git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
) )
@ -37,32 +39,56 @@ func (r *T) parseHeader(b []byte) error {
/* (2) Manage header */ /* (2) Manage header */
switch head.Name { switch head.Name {
case header.HOST: fmt.Printf("[host] "); printValues(head.Values)
case header.UPGRADE: fmt.Printf("[upgrade] "); printValues(head.Values) case header.HOST: fmt.Printf("[host] ")
case header.CONNECTION: fmt.Printf("[connection] "); printValues(head.Values) err = r.extractHostPort(head.Values[0])
case header.WSKEY: fmt.Printf("[sec-websocket-key] "); printValues(head.Values)
case header.ORIGIN: fmt.Printf("[origin] "); printValues(head.Values) case header.UPGRADE: fmt.Printf("[upgrade] ")
case header.WSPROTOCOL: fmt.Printf("[sec-websocket-protocol] "); printValues(head.Values) case header.CONNECTION: fmt.Printf("[connection] ")
case header.WSVERSION: fmt.Printf("[sec-websocket-version] "); printValues(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] ")
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)
}
}
fmt.Printf(" ]\n");
return nil return nil
} }
// checkHost checks and extracts the Host header
func (r *T) extractHostPort(b []byte) error {
func printValues(bb [][]byte){ split := strings.Split(string(b), ":")
for i, b := range bb { r.host = split[0]
if i == 0 { // no port
fmt.Printf("[ '%s'", b) if len(split) < 2 {
} else { return nil
fmt.Printf(", '%s'", b)
} }
// extract port
readPort, err := strconv.ParseUint(split[1], 10, 16)
if err != nil {
return fmt.Errorf("Cannot read port number '%s'", split[1])
} }
fmt.Printf(" ]\n"); r.port = uint16(readPort)
return nil
} }