-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
89 lines (76 loc) · 1.97 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package myhttp
import (
"net/http"
"strings"
)
type HTTPMethod string
const (
MethodAny = HTTPMethod("*")
MethodOptions = HTTPMethod(http.MethodOptions)
MethodGet = HTTPMethod(http.MethodGet)
MethodHead = HTTPMethod(http.MethodHead)
MethodPost = HTTPMethod(http.MethodPost)
MethodPut = HTTPMethod(http.MethodPut)
MethodDelete = HTTPMethod(http.MethodDelete)
MethodTrace = HTTPMethod(http.MethodTrace)
MethodConnect = HTTPMethod(http.MethodConnect)
MethodPatch = HTTPMethod(http.MethodPatch)
)
type StatusCode int
const (
StatusOK = StatusCode(http.StatusOK)
StatusFound = StatusCode(http.StatusFound)
StatusBadRequest = StatusCode(http.StatusBadRequest)
StatusMethodNotAllowed = StatusCode(http.StatusMethodNotAllowed)
StatusNotFound = StatusCode(http.StatusNotFound)
StatusInternalServerError = StatusCode(http.StatusInternalServerError)
StatusBadGateway = StatusCode(http.StatusBadGateway)
)
func ToHTTPMethod(method string) HTTPMethod {
upper := strings.ToUpper(method)
switch upper {
case "*":
return MethodAny
case http.MethodOptions:
return MethodOptions
case http.MethodGet:
return MethodGet
case http.MethodHead:
return MethodHead
case http.MethodPost:
return MethodPost
case http.MethodPut:
return MethodPut
case http.MethodDelete:
return MethodDelete
case http.MethodTrace:
return MethodTrace
case http.MethodConnect:
return MethodConnect
case http.MethodPatch:
return MethodPatch
default:
return HTTPMethod(upper)
}
}
func (m HTTPMethod) Matches(method HTTPMethod) bool {
return m == MethodAny || m == method
}
func MethodsAnyMatches(methods []HTTPMethod, method HTTPMethod) bool {
for _, m := range methods {
if method.Matches(m) {
return true
}
}
return false
}
func (m HTTPMethod) String() string {
return string(m)
}
func MethodsToStringSlice(methods []HTTPMethod) []string {
r := make([]string, len(methods))
for i, e := range methods {
r[i] = e.String()
}
return r
}