From 4bcfd9c6abb08bf428b752a90d317fbd347bf6cd Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 25 Apr 2018 16:25:58 +0200 Subject: [PATCH] extractHostPort() from the 'Host' header --- http/upgrade/request/private.go | 58 ++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/http/upgrade/request/private.go b/http/upgrade/request/private.go index 52183eb..5ae960c 100644 --- a/http/upgrade/request/private.go +++ b/http/upgrade/request/private.go @@ -1,6 +1,8 @@ package request import ( + "strings" + "strconv" "fmt" "git.xdrm.io/gws/internal/http/upgrade/request/parser/header" ) @@ -37,32 +39,56 @@ func (r *T) parseHeader(b []byte) error { /* (2) Manage header */ switch head.Name { - case header.HOST: fmt.Printf("[host] "); printValues(head.Values) - case header.UPGRADE: fmt.Printf("[upgrade] "); printValues(head.Values) - case header.CONNECTION: fmt.Printf("[connection] "); printValues(head.Values) - case header.WSKEY: fmt.Printf("[sec-websocket-key] "); printValues(head.Values) - case header.ORIGIN: fmt.Printf("[origin] "); printValues(head.Values) - case header.WSPROTOCOL: fmt.Printf("[sec-websocket-protocol] "); printValues(head.Values) - case header.WSVERSION: fmt.Printf("[sec-websocket-version] "); printValues(head.Values) + + case header.HOST: fmt.Printf("[host] ") + err = r.extractHostPort(head.Values[0]) + + case header.UPGRADE: fmt.Printf("[upgrade] ") + case header.CONNECTION: fmt.Printf("[connection] ") + 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 } +// 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 { - - if i == 0 { - fmt.Printf("[ '%s'", b) - } else { - fmt.Printf(", '%s'", b) - } + r.host = split[0] + // no port + if len(split) < 2 { + return nil } - fmt.Printf(" ]\n"); + // 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 } \ No newline at end of file