From 8cda08731f24a2d0cb5166acb178fda6f0a609e0 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 25 Apr 2018 15:01:12 +0200 Subject: [PATCH] [http.upgrade.request.private] clean 'parseHeader()' with using [internal.http.request.parser.header] --- http/upgrade/request/private.go | 45 ++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/http/upgrade/request/private.go b/http/upgrade/request/private.go index 4209513..956993e 100644 --- a/http/upgrade/request/private.go +++ b/http/upgrade/request/private.go @@ -2,7 +2,7 @@ package request import ( "fmt" - "strings" + "git.xdrm.io/gws/internal/http/request/parser/header" ) @@ -28,24 +28,39 @@ func (r *T) parseHeader(b []byte) { /* (2) Other lines -> Header-Name: Header-Value ---------------------------------------------------------*/ - /* (1) Split by ': ' */ - parts := strings.Split(string(b), ": ") - - /* (2) Abort on failure */ - if len(parts) != 2 { + /* (1) Try to parse header */ + head, err := header.Parse(b) + if err != nil { + r.Err = fmt.Errorf("Error parsing header: %s", err) return } - /* (3) Match header */ - switch strings.ToLower(parts[0]) { - case "host": fmt.Printf("[host] '%s'\n", parts[1]) - case "upgrade": fmt.Printf("[upgrade] '%s'\n", parts[1]) - case "connection": fmt.Printf("[connection] '%s'\n", parts[1]) - case "sec-websocket-key": fmt.Printf("[sec-websocket-key] '%s'\n", parts[1]) - case "origin": fmt.Printf("[origin] '%s'\n", parts[1]) - case "sec-websocket-protocol": fmt.Printf("[sec-websocket-protocol] '%s'\n", parts[1]) - case "sec-websocket-version": fmt.Printf("[sec-websocket-version] '%s'\n", parts[1]) + /* (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) + } + +} + + +func printValues(bb [][]byte){ + + for i, b := range bb { + + if i == 0 { + fmt.Printf("[ '%s'", b) + } else { + fmt.Printf(", '%s'", b) + } } + fmt.Printf(" ]\n"); + } \ No newline at end of file