-
Notifications
You must be signed in to change notification settings - Fork 52
/
utils.go
158 lines (145 loc) · 3.58 KB
/
utils.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package znet
import (
"html/template"
"net/http"
"strings"
"time"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zstring"
)
func completionPath(path, prefix string) string {
if prefix != "" {
if path != "" {
if strings.HasPrefix(path, "/") && strings.HasSuffix(prefix, "/") {
prefix = strings.TrimSuffix(prefix, "/")
}
tmp := zstring.Buffer()
tmp.WriteString(prefix)
tmp.WriteString("/")
tmp.WriteString(strings.TrimPrefix(path, "/"))
path = tmp.String()
} else {
path = prefix
}
}
return path
}
func resolveAddr(addrString string, tlsConfig ...TlsCfg) addrSt {
cfg := addrSt{
addr: addrString,
}
if len(tlsConfig) > 0 {
cfg.Cert = tlsConfig[0].Cert
cfg.HTTPAddr = tlsConfig[0].HTTPAddr
cfg.HTTPProcessing = tlsConfig[0].HTTPProcessing
cfg.Key = tlsConfig[0].Key
cfg.Config = tlsConfig[0].Config
}
return cfg
}
func resolveHostname(addrString string) string {
if strings.Index(addrString, ":") == 0 {
return "127.0.0.1" + addrString
}
return addrString
}
func templateParse(file string, funcMap template.FuncMap) (t *template.Template, err error) {
if zfile.FileExist(file) {
t, err = template.ParseFiles(file)
if err == nil && funcMap != nil {
t.Funcs(funcMap)
}
} else {
t = template.New(file)
if funcMap != nil {
t.Funcs(funcMap)
}
t, err = t.Parse(file)
}
return
}
func parsPattern(res []string) (string, []string) {
var (
matchName []string
pattern = zstring.Buffer()
)
for _, str := range res {
if str == "" {
continue
}
strLen := len(str)
firstChar := string(str[0])
lastChar := string(str[strLen-1])
// todo Need to optimize
if firstChar == "{" && lastChar == "}" {
matchStr := string(str[1 : strLen-1])
res := strings.Split(matchStr, ":")
matchName = append(matchName, res[0])
pattern.WriteString("/(")
pattern.WriteString(res[1])
pattern.WriteString(")")
} else if firstChar == ":" {
matchStr := str
res := strings.Split(matchStr, ":")
matchName = append(matchName, res[1])
if res[1] == idKey {
pattern.WriteString("/(")
pattern.WriteString(idPattern)
pattern.WriteString(")")
} else if res[1] == allKey {
pattern.WriteString("/(")
pattern.WriteString(allPattern)
pattern.WriteString(")")
} else {
pattern.WriteString("/(")
pattern.WriteString(defaultPattern)
pattern.WriteString(")")
}
} else if firstChar == "*" {
pattern.WriteString("/(")
pattern.WriteString(allPattern)
pattern.WriteString(")")
matchName = append(matchName, allKey)
} else {
pattern.WriteString("/")
pattern.WriteString(str)
}
}
return pattern.String(), matchName
}
type tlsRedirectHandler struct {
Domain string
}
func (t *tlsRedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, t.Domain+r.URL.String(), 301)
}
func (e *Engine) NewContext(w http.ResponseWriter, req *http.Request) *Context {
return &Context{
Writer: w,
Request: req,
Engine: e,
Log: e.Log,
Cache: Cache,
Code: http.StatusOK,
StartTime: time.Time{},
heades: map[string]string{},
customizeData: map[string]interface{}{},
}
}
func (c *Context) Reset(w http.ResponseWriter, r *http.Request) {
c.Request = r
c.Writer = w
c.Code = http.StatusOK
c.StartTime = time.Now()
c.StopHandle = false
c.middleware = []HandlerFunc{}[:0]
c.customizeData = map[string]interface{}{}
c.heades = map[string]string{}
c.render = nil
}
func (e *Engine) acquireContext() *Context {
return e.pool.Get().(*Context)
}
func (e *Engine) releaseContext(c *Context) {
e.pool.Put(c)
}