-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
60 lines (50 loc) · 1.69 KB
/
http.go
File metadata and controls
60 lines (50 loc) · 1.69 KB
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
package rweb
import (
"strings"
"github.com/rohanthewiz/rweb/consts"
)
// isValidRequestMethod returns true if the given string is a valid HTTP request method.
func isValidRequestMethod(method string) bool {
switch method {
case consts.MethodGet, consts.MethodHead, consts.MethodPost, consts.MethodPut,
consts.MethodDelete, consts.MethodConnect, consts.MethodOptions, consts.MethodTrace, consts.MethodPatch:
return true
default:
return false
}
}
// parseURL parses a URL and returns the scheme, host, path and query.
// The URL is expected to be in the format "scheme://host/path?query"
// Though we could have used the standard URL package we wanted to maintain fine control.
func parseURL(url string, urlOpts URLOptions) (scheme string, host string, path string, query string) {
schemeEndPos := strings.Index(url, consts.SchemeDelimiter)
if schemeEndPos != -1 {
scheme = url[:schemeEndPos]
url = url[schemeEndPos+len(consts.SchemeDelimiter):]
}
pathStartPos := strings.IndexByte(url, consts.RuneFwdSlash)
if pathStartPos != -1 {
host = url[:pathStartPos]
url = url[pathStartPos:]
}
queryPos := strings.IndexByte(url, consts.RuneQuestion)
if queryPos != -1 && queryPos < len(url)+1 /* we will go one past the question sign below */ {
path = url[:queryPos]
query = url[queryPos+1:] // check above ensures we don't go past the end of the string
} else {
path = url
}
// FIXUPS
if lnPath := len(path); lnPath == 0 {
path = "/"
} else { // Trailing slash removal
if !urlOpts.KeepTrailingSlashes && lnPath > 1 && strings.HasSuffix(path, "/") {
path = path[:lnPath-1]
}
}
// If the host is empty, set it to "localhost"
if host == "" {
host = consts.Localhost
}
return
}