-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpattern.go
57 lines (48 loc) · 1 KB
/
pattern.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
package xun
import (
"strings"
)
func splitPattern(s string) (string, string, string) {
if len(s) == 0 {
return "", "", ""
}
method, rest, found := s, "", false
if i := strings.IndexAny(s, " \t"); i >= 0 {
method, rest, found = s[:i], strings.TrimLeft(s[i+1:], " \t"), true
}
if !found {
rest = method
method = ""
}
i := strings.IndexByte(rest, '/')
if i < 0 {
return method, "", rest
}
return method, rest[:i], rest[i+1:]
}
// host, path, pattern
func splitFile(s string) (host string, path string, pattern string) {
defer func() {
if pattern[len(pattern)-1] == '/' {
pattern = pattern + "{$}"
}
}()
if len(s) == 0 {
pattern = "GET /"
return
}
i := strings.IndexByte(s, '@')
// xxx
if i < 0 { // no host
path = "/" + s // /xxx
pattern = "GET /" + s // GET /xxx
return
}
// @abc.com/xxx
e := strings.IndexByte(s, '/')
// has host
host = s[i+1 : e] // abc.com
path = "/" + s[e+1:] // /xxx
pattern = "GET " + s[i+1:] // GET abc.com/xxx
return
}