Repository navigation
Expand file tree
/
Copy pathhost.go
More file actions
130 lines (116 loc) · 2.55 KB
/
Copy pathhost.go
File metadata and controls
130 lines (116 loc) · 2.55 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package echo
import (
"fmt"
"regexp"
)
type Hoster interface {
Name() string
Alias() string
Format(args ...any) string
FormatMap(params H) string
RegExp() *regexp.Regexp
Match(host string) (r []string, hasExpr bool)
HasParam(name string) bool
}
type host struct {
name string
alias string
format string
regExp *regexp.Regexp
names []string
namek map[string]struct{}
}
func (h *host) Name() string {
return h.name
}
func (h *host) HasParam(name string) bool {
_, ok := h.namek[name]
return ok
}
func (h *host) Alias() string {
return h.alias
}
func (h *host) Format(args ...any) string {
if len(args) > 0 {
return fmt.Sprintf(h.format, args...)
}
return h.format
}
func (h *host) FormatMap(params H) string {
if len(params) > 0 {
args := make([]any, len(h.names))
for index, name := range h.names {
v, y := params[name]
if y {
args[index] = v
} else {
args[index] = ``
}
}
return fmt.Sprintf(h.format, args...)
}
return h.format
}
func (h *host) RegExp() *regexp.Regexp {
return h.regExp
}
func (h *host) Match(host string) (r []string, hasExpr bool) {
if h.regExp != nil {
match := h.regExp.FindStringSubmatch(host)
if len(match) > 0 {
return match[1:], true
}
return nil, true
}
return nil, false
}
func NewHost(name string) *host {
return &host{name: name, namek: map[string]struct{}{}}
}
var hostRegExp = regexp.MustCompile(`<([^:]+)(?:\:(.+?))?>`)
func ParseURIRegExp(uriRegexp string, dflRegexp string) (names []string, format string, regExp *regexp.Regexp) {
matches := hostRegExp.FindAllStringSubmatchIndex(uriRegexp, -1)
if len(matches) == 0 {
return
}
if len(dflRegexp) == 0 {
dflRegexp = `[^.]+`
}
var regExpr string
var lastPosition int
for _, matchIndex := range matches {
if matchIndex[0] > 0 {
v := uriRegexp[lastPosition:matchIndex[0]]
format += v
regExpr += regexp.QuoteMeta(v)
}
lastPosition = matchIndex[1]
format += `%v`
name := uriRegexp[matchIndex[2]:matchIndex[3]]
if matchIndex[4] > 0 {
regExpr += `(` + uriRegexp[matchIndex[4]:matchIndex[5]] + `)`
} else {
regExpr += `(` + dflRegexp + `)`
}
names = append(names, name)
}
if lastPosition > 0 {
if lastPosition < len(uriRegexp) {
v := uriRegexp[lastPosition:]
format += v
regExpr += regexp.QuoteMeta(v)
}
}
regExp = regexp.MustCompile(`^` + regExpr + `$`)
return
}
func (h *host) Parse() *host {
h.names, h.format, h.regExp = ParseURIRegExp(h.name, ``)
if h.namek == nil {
h.namek = map[string]struct{}{}
}
for _, name := range h.names {
h.namek[name] = struct{}{}
}
return h
}