-
Notifications
You must be signed in to change notification settings - Fork 2k
/
location.go
240 lines (220 loc) · 6.17 KB
/
location.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package components
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type Location struct {
Modifier string
Match string
Cache bool
ProxyPass string
Host string
CacheTime int
CacheUint string
Comment string
Directives []IDirective
Line int
Parameters []string
Replaces map[string]string
}
func NewLocation(directive IDirective) *Location {
location := &Location{
Modifier: "",
Match: "",
}
directives := make([]IDirective, 0)
if len(directive.GetParameters()) == 0 {
panic("no enough parameter for location")
}
for _, dir := range directive.GetBlock().GetDirectives() {
directives = append(directives, dir)
params := dir.GetParameters()
switch dir.GetName() {
case "proxy_pass":
location.ProxyPass = params[0]
case "proxy_set_header":
if params[0] == "Host" {
location.Host = params[1]
}
case "proxy_cache":
location.Cache = true
case "if":
if params[0] == "(" && params[1] == "$uri" && params[2] == "~*" {
dirs := dir.GetBlock().GetDirectives()
for _, di := range dirs {
if di.GetName() == "expires" {
re := regexp.MustCompile(`^(\d+)(\w+)$`)
matches := re.FindStringSubmatch(di.GetParameters()[0])
if matches == nil {
continue
}
cacheTime, err := strconv.Atoi(matches[1])
if err != nil {
continue
}
unit := matches[2]
location.CacheUint = unit
location.CacheTime = cacheTime
}
}
}
case "sub_filter":
if location.Replaces == nil {
location.Replaces = make(map[string]string, 0)
}
location.Replaces[strings.Trim(params[0], "\"")] = strings.Trim(params[1], "\"")
}
}
params := directive.GetParameters()
if len(params) == 1 {
location.Match = params[0]
} else if len(params) == 2 {
location.Match = params[1]
location.Modifier = params[0]
}
location.Parameters = directive.GetParameters()
location.Line = directive.GetLine()
location.Comment = directive.GetComment()
location.Directives = directives
return location
}
func (l *Location) GetName() string {
return "location"
}
func (l *Location) GetParameters() []string {
return l.Parameters
}
func (l *Location) GetBlock() IBlock {
return l
}
func (l *Location) GetComment() string {
return l.Comment
}
func (l *Location) GetLine() int {
return l.Line
}
func (l *Location) GetDirectives() []IDirective {
return l.Directives
}
func (l *Location) FindDirectives(directiveName string) []IDirective {
directives := make([]IDirective, 0)
for _, directive := range l.Directives {
if directive.GetName() == directiveName {
directives = append(directives, directive)
}
if directive.GetBlock() != nil {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
return directives
}
func (l *Location) UpdateDirective(key string, params []string) {
if key == "" || len(params) == 0 {
return
}
directives := l.Directives
index := -1
for i, dir := range directives {
if dir.GetName() == key {
if IsRepeatKey(key) {
oldParams := dir.GetParameters()
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
continue
}
}
index = i
break
}
}
newDirective := &Directive{
Name: key,
Parameters: params,
}
if index > -1 {
directives[index] = newDirective
} else {
directives = append(directives, newDirective)
}
l.Directives = directives
}
func (l *Location) RemoveDirective(key string, params []string) {
directives := l.Directives
var newDirectives []IDirective
for _, dir := range directives {
if dir.GetName() == key {
if len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
continue
}
} else {
continue
}
}
newDirectives = append(newDirectives, dir)
}
l.Directives = newDirectives
}
func (l *Location) ChangePath(Modifier string, Match string) {
if Match != "" && Modifier != "" {
l.Parameters = []string{Modifier, Match}
}
if Match != "" && Modifier == "" {
l.Parameters = []string{Match}
}
l.Modifier = Modifier
l.Match = Match
}
func (l *Location) AddCache(cacheTime int, cacheUint string) {
l.RemoveDirective("add_header", []string{"Cache-Control", "no-cache"})
l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"})
directives := l.GetDirectives()
newDir := &Directive{
Name: "if",
Parameters: []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"},
Block: &Block{},
}
block := &Block{}
block.Directives = append(block.Directives, &Directive{
Name: "expires",
Parameters: []string{strconv.Itoa(cacheTime) + cacheUint},
})
newDir.Block = block
directives = append(directives, newDir)
l.Directives = directives
l.UpdateDirective("proxy_ignore_headers", []string{"Set-Cookie", "Cache-Control", "expires"})
l.UpdateDirective("proxy_cache", []string{"proxy_cache_panel"})
l.UpdateDirective("proxy_cache_key", []string{"$host$uri$is_args$args"})
l.UpdateDirective("proxy_cache_valid", []string{"200", "304", "301", "302", "10m"})
l.Cache = true
l.CacheTime = cacheTime
l.CacheUint = cacheUint
}
func (l *Location) RemoveCache() {
l.RemoveDirective("if", []string{"(", "$uri", "~*", `"\.(gif|png|jpg|css|js|woff|woff2)$"`, ")"})
l.RemoveDirective("proxy_ignore_headers", []string{"Set-Cookie"})
l.RemoveDirective("proxy_cache", []string{"proxy_cache_panel"})
l.RemoveDirective("proxy_cache_key", []string{"$host$uri$is_args$args"})
l.RemoveDirective("proxy_cache_valid", []string{"200"})
l.UpdateDirective("add_header", []string{"Cache-Control", "no-cache"})
l.CacheTime = 0
l.CacheUint = ""
l.Cache = false
}
func (l *Location) AddSubFilter(subFilters map[string]string) {
l.RemoveDirective("sub_filter", []string{})
l.Replaces = subFilters
for k, v := range subFilters {
l.UpdateDirective("sub_filter", []string{fmt.Sprintf(`"%s"`, k), fmt.Sprintf(`"%s"`, v)})
}
l.UpdateDirective("proxy_set_header", []string{"Accept-Encoding", `""`})
l.UpdateDirective("sub_filter_once", []string{"off"})
}
func (l *Location) RemoveSubFilter() {
l.RemoveDirective("sub_filter", []string{})
l.RemoveDirective("proxy_set_header", []string{"Accept-Encoding", `""`})
l.RemoveDirective("sub_filter_once", []string{"off"})
l.Replaces = nil
}