Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.6.3
- 1.5.3
env:
- "PATH=/home/travis/gopath/bin:$PATH"
before_install:
Expand Down
13 changes: 0 additions & 13 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +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
// 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,
}

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
)
32 changes: 32 additions & 0 deletions methods_legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +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
// 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,
}

var (
httpConnect = "CONNECT"
httpDelete = "DELETE"
httpGet = "GET"
httpHead = "HEAD"
httpOptions = "OPTIONS"
httpPatch = "PATCH"
httpPost = "POST"
httpPut = "PUT"
httpTrace = "TRACE"
)
20 changes: 10 additions & 10 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)
}
Expand Down
18 changes: 9 additions & 9 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down