From a85e16e96a509c2abd3742757529ecfc6669b1df Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 25 Apr 2018 21:51:53 +0200 Subject: [PATCH] moved http.* into /internal | created upgrader interface in /upgrader --- cmd/gwstester/tester.go | 23 ++++++---------- .../http}/upgrade/request/header_check.go | 2 +- .../http}/upgrade/request/private.go | 2 +- .../http}/upgrade/request/public.go | 2 +- .../http}/upgrade/request/types.go | 2 +- .../http}/upgrade/response/public.go | 1 - .../http}/upgrade/response/status_code.go | 0 .../http}/upgrade/response/types.go | 0 upgrader/public.go | 26 +++++++++++++++++++ upgrader/types.go | 1 + 10 files changed, 39 insertions(+), 20 deletions(-) rename {http => internal/http}/upgrade/request/header_check.go (98%) rename {http => internal/http}/upgrade/request/private.go (97%) rename {http => internal/http}/upgrade/request/public.go (97%) rename {http => internal/http}/upgrade/request/types.go (91%) rename {http => internal/http}/upgrade/response/public.go (94%) rename {http => internal/http}/upgrade/response/status_code.go (100%) rename {http => internal/http}/upgrade/response/types.go (100%) create mode 100644 upgrader/public.go create mode 100644 upgrader/types.go diff --git a/cmd/gwstester/tester.go b/cmd/gwstester/tester.go index 3f30860..1f8f292 100644 --- a/cmd/gwstester/tester.go +++ b/cmd/gwstester/tester.go @@ -1,11 +1,11 @@ package main import ( + "git.xdrm.io/gws/upgrader" "fmt" "net" "os" "time" - "git.xdrm.io/gws/http/upgrade/request" ) func main() { @@ -27,8 +27,6 @@ func main() { return } - fmt.Printf("+ new client\n") - go manageClient(sock) } @@ -43,26 +41,21 @@ func manageClient(sock net.Conn) { for { - /* (1) Parse request */ - req, errRq := request.Parse(sock) - if errRq != nil { fmt.Printf("Request parsing error: %s\n", errRq) } - fmt.Printf("%v\n", req) + fmt.Printf("+ new client\n") - /* (2) Build response */ - res := req.BuildResponse() + fmt.Printf(" + upgrade\n") + err := upgrader.Upgrade(sock) - /* (3) Write into socket */ - written, err := res.Send(sock) if err != nil { - fmt.Printf("WRITE ERROR: %s\n", err); + fmt.Printf(" + error: %s\n", err) return } - - fmt.Printf("Written %d bytes\n", written) + fmt.Printf(" + 2-way exchange\n") break; } - fmt.Printf("Elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3) + fmt.Printf("+ elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3) + } diff --git a/http/upgrade/request/header_check.go b/internal/http/upgrade/request/header_check.go similarity index 98% rename from http/upgrade/request/header_check.go rename to internal/http/upgrade/request/header_check.go index 73f5212..b6deaec 100644 --- a/http/upgrade/request/header_check.go +++ b/internal/http/upgrade/request/header_check.go @@ -1,7 +1,7 @@ package request import ( - "git.xdrm.io/gws/http/upgrade/response" + "git.xdrm.io/gws/internal/http/upgrade/response" "git.xdrm.io/gws/internal/http/upgrade/request/parser/header" "fmt" "strconv" diff --git a/http/upgrade/request/private.go b/internal/http/upgrade/request/private.go similarity index 97% rename from http/upgrade/request/private.go rename to internal/http/upgrade/request/private.go index 83c721b..2a47343 100644 --- a/http/upgrade/request/private.go +++ b/internal/http/upgrade/request/private.go @@ -1,7 +1,7 @@ package request import ( - "git.xdrm.io/gws/http/upgrade/response" + "git.xdrm.io/gws/internal/http/upgrade/response" "fmt" "git.xdrm.io/gws/internal/http/upgrade/request/parser/header" ) diff --git a/http/upgrade/request/public.go b/internal/http/upgrade/request/public.go similarity index 97% rename from http/upgrade/request/public.go rename to internal/http/upgrade/request/public.go index 89a8654..cd57f17 100644 --- a/http/upgrade/request/public.go +++ b/internal/http/upgrade/request/public.go @@ -1,7 +1,7 @@ package request import ( - "git.xdrm.io/gws/http/upgrade/response" + "git.xdrm.io/gws/internal/http/upgrade/response" "git.xdrm.io/gws/internal/http/reader" "fmt" "io" diff --git a/http/upgrade/request/types.go b/internal/http/upgrade/request/types.go similarity index 91% rename from http/upgrade/request/types.go rename to internal/http/upgrade/request/types.go index f149332..388ba42 100644 --- a/http/upgrade/request/types.go +++ b/internal/http/upgrade/request/types.go @@ -1,7 +1,7 @@ package request import "git.xdrm.io/gws/internal/http/upgrade/request/parser/reqline" -import "git.xdrm.io/gws/http/upgrade/response" +import "git.xdrm.io/gws/internal/http/upgrade/response" // T represents an HTTP Upgrade request type T struct { diff --git a/http/upgrade/response/public.go b/internal/http/upgrade/response/public.go similarity index 94% rename from http/upgrade/response/public.go rename to internal/http/upgrade/response/public.go index e64518d..bd46d61 100644 --- a/http/upgrade/response/public.go +++ b/internal/http/upgrade/response/public.go @@ -54,7 +54,6 @@ func (r T) Send(w io.Writer) (int, error) { raw := []byte(fmt.Sprintf("%s%s\r\n", responseLine, headers)) /* (4) Write */ - fmt.Printf("Upgrade Response\n----------------\n%s----------------\n", raw) written, err := w.Write(raw) return written, err diff --git a/http/upgrade/response/status_code.go b/internal/http/upgrade/response/status_code.go similarity index 100% rename from http/upgrade/response/status_code.go rename to internal/http/upgrade/response/status_code.go diff --git a/http/upgrade/response/types.go b/internal/http/upgrade/response/types.go similarity index 100% rename from http/upgrade/response/types.go rename to internal/http/upgrade/response/types.go diff --git a/upgrader/public.go b/upgrader/public.go new file mode 100644 index 0000000..c6c978a --- /dev/null +++ b/upgrader/public.go @@ -0,0 +1,26 @@ +package upgrader + +import ( + "fmt" + "git.xdrm.io/gws/internal/http/upgrade/request" + "net" +) + + +func Upgrade(s net.Conn) error { + + /* (1) Parse request */ + req, _ := request.Parse(s) + + /* (2) Build response */ + res := req.BuildResponse() + + /* (3) Write into socket */ + _, err := res.Send(s) + if err != nil { + return fmt.Errorf("Socket write: %s", err) + } + + return nil + +} \ No newline at end of file diff --git a/upgrader/types.go b/upgrader/types.go new file mode 100644 index 0000000..2df626f --- /dev/null +++ b/upgrader/types.go @@ -0,0 +1 @@ +package upgrader \ No newline at end of file