-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
68 lines (58 loc) · 1.49 KB
/
request.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
package lungo
import (
"net/http"
"path"
"strings"
)
// Canonical returns a canonical path for p, eliminating . and .. elements.
func Canonical(p string) string {
if p == "" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
cp := path.Clean(p)
// path.Clean removes trailing slash except for root, thus
// we need to put the trailing slash back if necessary.
n := len(p) - 1
if p[n] == '/' && cp != "/" {
// Fast path for common case of p being the string we want
if n == len(cp) && strings.HasPrefix(p, cp) {
cp = p
} else {
cp += "/"
}
}
return cp
}
// IsValidMethod checks if the provided http method is valid
func IsValidMethod(method string) bool {
if method == "" {
return false
}
/*
Method = "OPTIONS" ; RFC 7231 Section 9.2
| "GET" ; RFC 7231 Section 9.3
| "HEAD" ; RFC 7231 Section 9.4
| "POST" ; RFC 7231 Section 9.5
| "PUT" ; RFC 7231 Section 9.6
| "DELETE" ; RFC 7231 Section 9.7
| "TRACE" ; RFC 7231 Section 9.8
| "CONNECT" ; RFC 7231 Section 9.9
| "PATCH" ; RFC 5789
*/
switch method {
case http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace:
return true
}
return false
}