From fc500b28498987f2f75a5a06327bb5748d62eef3 Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Wed, 14 Sep 2016 06:51:23 -0400 Subject: [PATCH 1/5] updates to allow for building with go1.5, testing with travis --- .travis.yml | 2 +- common.go | 13 ------------- methods.go | 22 ++++++++++++++++++++++ methods_legacy.go | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 methods.go create mode 100644 methods_legacy.go diff --git a/.travis.yml b/.travis.yml index 8bc89b8..a359f6f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.6.3 + - 1.5.3 env: - "PATH=/home/travis/gopath/bin:$PATH" before_install: diff --git a/common.go b/common.go index e4c729d..86e98bf 100644 --- a/common.go +++ b/common.go @@ -10,19 +10,6 @@ import ( "strings" ) -// methods - a list of methods that are allowed -var methods = map[string]bool{ - http.MethodConnect: true, - http.MethodDelete: true, - http.MethodGet: true, - http.MethodHead: true, - http.MethodOptions: true, - http.MethodPatch: true, - http.MethodPost: true, - http.MethodPut: true, - http.MethodTrace: true, -} - // AllowTrace - Globally allow the TRACE method handling within vestigo url router. This // generally not a good idea to have true in production settings, but excellent for testing. var AllowTrace = false diff --git a/methods.go b/methods.go new file mode 100644 index 0000000..7c2607c --- /dev/null +++ b/methods.go @@ -0,0 +1,22 @@ +// +build !go1.5 + +// Copyright 2015 Husobee Associates, LLC. All rights reserved. +// Use of this source code is governed by The MIT License, which +// can be found in the LICENSE file included. + +package vestigo + +import "net/http" + +// methods - a list of methods that are allowed +var methods = map[string]bool{ + http.MethodConnect: true, + http.MethodDelete: true, + http.MethodGet: true, + http.MethodHead: true, + http.MethodOptions: true, + http.MethodPatch: true, + http.MethodPost: true, + http.MethodPut: true, + http.MethodTrace: true, +} diff --git a/methods_legacy.go b/methods_legacy.go new file mode 100644 index 0000000..4d7355b --- /dev/null +++ b/methods_legacy.go @@ -0,0 +1,20 @@ +// +build go1.5 + +// Copyright 2015 Husobee Associates, LLC. All rights reserved. +// Use of this source code is governed by The MIT License, which +// can be found in the LICENSE file included. + +package vestigo + +// methods - a list of methods that are allowed +var methods = map[string]bool{ + "CONNECT": true, + "DELETE": true, + "GET": true, + "HEAD": true, + "OPTIONS": true, + "PATCH": true, + "POST": true, + "PUT": true, + "TRACE": true, +} From 1fb5795d6695a2fc210f7d735231d360497174a2 Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Wed, 14 Sep 2016 07:00:05 -0400 Subject: [PATCH 2/5] more corrections needed to support pre 1.5 builds --- methods.go | 14 +++++++++++++- methods_legacy.go | 14 +++++++++++++- resource.go | 20 ++++++++++---------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/methods.go b/methods.go index 7c2607c..3fdc736 100644 --- a/methods.go +++ b/methods.go @@ -1,4 +1,4 @@ -// +build !go1.5 +// +build !go1.5,!go1.4,!go1.3,!go1.2,!go1.1,!go1.0 // Copyright 2015 Husobee Associates, LLC. All rights reserved. // Use of this source code is governed by The MIT License, which @@ -20,3 +20,15 @@ var methods = map[string]bool{ http.MethodPut: true, http.MethodTrace: true, } + +var ( + httpConnect = http.MethodConnect + httpDelete = http.MethodDelete + httpGet = http.MethodGet + httpHead = http.MethodHead + httpOptions = http.MethodOptions + httpPatch = http.MethodPatch + httpPost = http.MethodPost + httpPut = http.MethodPut + httpTrace = http.MethodTrace +) diff --git a/methods_legacy.go b/methods_legacy.go index 4d7355b..d1c91fb 100644 --- a/methods_legacy.go +++ b/methods_legacy.go @@ -1,4 +1,4 @@ -// +build go1.5 +// +build go1.5,go1.4,go1.3,go1.2,go1.1,go1.0 // Copyright 2015 Husobee Associates, LLC. All rights reserved. // Use of this source code is governed by The MIT License, which @@ -18,3 +18,15 @@ var methods = map[string]bool{ "PUT": true, "TRACE": true, } + +var ( + httpConnect = "CONNECT" + httpDelete = "DELETE" + httpGet = "GET" + httpHead = "HEAD" + httpOptions = "OPTIONS" + httpPatch = "PATCH" + httpPost = "POST" + httpPut = "PUT" + httpTrace = "TRACE" +) diff --git a/resource.go b/resource.go index 7b2e310..7e6ef86 100644 --- a/resource.go +++ b/resource.go @@ -56,33 +56,33 @@ func (h *resource) Clean() { h.allowedMethods = "" hasOneMethod := false if h.Get != nil { - h.addToAllowedMethods(http.MethodGet) - h.addToAllowedMethods(http.MethodHead) + h.addToAllowedMethods(httpGet) + h.addToAllowedMethods(httpHead) h.Head = headHandler(h.Get) hasOneMethod = true } if h.Put != nil { - h.addToAllowedMethods(http.MethodPut) + h.addToAllowedMethods(httpPut) hasOneMethod = true } if h.Post != nil { - h.addToAllowedMethods(http.MethodPost) + h.addToAllowedMethods(httpPost) hasOneMethod = true } if h.Patch != nil { - h.addToAllowedMethods(http.MethodPatch) + h.addToAllowedMethods(httpPatch) hasOneMethod = true } if h.Delete != nil { - h.addToAllowedMethods(http.MethodDelete) + h.addToAllowedMethods(httpDelete) hasOneMethod = true } if h.Connect != nil { - h.addToAllowedMethods(http.MethodConnect) + h.addToAllowedMethods(httpConnect) hasOneMethod = true } if hasOneMethod && AllowTrace { - h.addToAllowedMethods(http.MethodTrace) + h.addToAllowedMethods(httpTrace) h.Trace = traceHandler } } @@ -94,13 +94,13 @@ func (h *resource) AddMethodHandler(method string, handler http.HandlerFunc) { secondChar := method[1] if h != nil { if AllowTrace { - h.addToAllowedMethods(http.MethodTrace) + h.addToAllowedMethods(httpTrace) h.Trace = traceHandler } if l == 3 { if uint16(firstChar)<<8|uint16(secondChar) == 0x4745 { h.addToAllowedMethods(method) - h.addToAllowedMethods(http.MethodHead) + h.addToAllowedMethods(httpHead) h.Get = handler h.Head = headHandler(handler) } From 3949836bf1da2f2777d09a41abd54d7e5f060a8e Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Wed, 14 Sep 2016 07:05:45 -0400 Subject: [PATCH 3/5] fixing build tag --- methods_legacy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/methods_legacy.go b/methods_legacy.go index d1c91fb..18e315d 100644 --- a/methods_legacy.go +++ b/methods_legacy.go @@ -1,4 +1,4 @@ -// +build go1.5,go1.4,go1.3,go1.2,go1.1,go1.0 +// +build go1.5 go1.4 go1.3 go1.2 go1.1 go1.0 // Copyright 2015 Husobee Associates, LLC. All rights reserved. // Use of this source code is governed by The MIT License, which From 98cd793aa8b7e9b9826715d46f2cc1b1566ad866 Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Wed, 14 Sep 2016 07:08:19 -0400 Subject: [PATCH 4/5] updates to router to support 1.5 --- router.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/router.go b/router.go index 92a3494..1132158 100644 --- a/router.go +++ b/router.go @@ -56,43 +56,43 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { // Get - Helper method to add HTTP GET Method to router func (r *Router) Get(path string, handler http.HandlerFunc) { - r.Add(http.MethodGet, path, handler) + r.Add(httpGet, path, handler) } // Post - Helper method to add HTTP POST Method to router func (r *Router) Post(path string, handler http.HandlerFunc) { - r.Add(http.MethodPost, path, handler) + r.Add(httpPost, path, handler) } // Connect - Helper method to add HTTP CONNECT Method to router func (r *Router) Connect(path string, handler http.HandlerFunc) { - r.Add(http.MethodConnect, path, handler) + r.Add(httpConnect, path, handler) } // Delete - Helper method to add HTTP DELETE Method to router func (r *Router) Delete(path string, handler http.HandlerFunc) { - r.Add(http.MethodDelete, path, handler) + r.Add(httpDelete, path, handler) } // Patch - Helper method to add HTTP PATCH Method to router func (r *Router) Patch(path string, handler http.HandlerFunc) { - r.Add(http.MethodPatch, path, handler) + r.Add(httpPatch, path, handler) } // Put - Helper method to add HTTP PUT Method to router func (r *Router) Put(path string, handler http.HandlerFunc) { - r.Add(http.MethodPut, path, handler) + r.Add(httpPut, path, handler) } // Trace - Helper method to add HTTP TRACE Method to router func (r *Router) Trace(path string, handler http.HandlerFunc) { - r.Add(http.MethodTrace, path, handler) + r.Add(httpTrace, path, handler) } // Handle - Helper method to add all HTTP Methods to router func (r *Router) Handle(path string, handler http.Handler) { for k := range methods { - if k == http.MethodHead || k == http.MethodOptions || k == http.MethodTrace { + if k == httpHead || k == httpOptions || k == httpTrace { continue } r.Add(k, path, handler.ServeHTTP) @@ -102,7 +102,7 @@ func (r *Router) Handle(path string, handler http.Handler) { // HandleFunc - Helper method to add all HTTP Methods to router func (r *Router) HandleFunc(path string, handler http.HandlerFunc) { for k := range methods { - if k == http.MethodHead || k == http.MethodOptions || k == http.MethodTrace { + if k == httpHead || k == httpOptions || k == httpTrace { continue } r.Add(k, path, handler.ServeHTTP) From 762ee41a7048e366dd9ff5fda41f55b77b0ae699 Mon Sep 17 00:00:00 2001 From: Ben Huson Date: Wed, 14 Sep 2016 07:10:26 -0400 Subject: [PATCH 5/5] fixing router tests to work with go1.5 --- router_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/router_test.go b/router_test.go index f652c85..77ecc7d 100644 --- a/router_test.go +++ b/router_test.go @@ -700,7 +700,7 @@ func TestHandleAddRoute(t *testing.T) { path := "/test" router.Handle(path, http.HandlerFunc(f)) for k := range methods { - if k == http.MethodHead || k == http.MethodOptions || k == http.MethodTrace { + if k == httpHead || k == httpOptions || k == httpTrace { continue } w := httptest.NewRecorder() @@ -724,7 +724,7 @@ func TestHandleFuncAddRoute(t *testing.T) { path := "/test" router.HandleFunc(path, f) for k := range methods { - if k == http.MethodHead || k == http.MethodOptions || k == http.MethodTrace { + if k == httpHead || k == httpOptions || k == httpTrace { continue } w := httptest.NewRecorder()