-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnginx_conf.go
375 lines (333 loc) · 8.59 KB
/
nginx_conf.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright 2019 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ngconf
import (
"fmt"
"io"
"sort"
"strings"
)
// Pre-defines some errors.
var (
ErrRootDirective = fmt.Errorf("root node must have the directive")
ErrNonRootNoDirective = fmt.Errorf("non-root node have no directive")
)
type nodes []*Node
func (ns nodes) Len() int { return len(ns) }
func (ns nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
func (ns nodes) Less(i, j int) bool { return ns[j] == nil }
type nodeStack struct {
nodes []*Node
}
func (ns *nodeStack) Push(node *Node) { ns.nodes = append(ns.nodes, node) }
func (ns *nodeStack) Pop() *Node {
_len := len(ns.nodes) - 1
node := ns.nodes[_len]
ns.nodes = ns.nodes[:_len]
return node
}
// Node represents a set of the key-value configurations of Nginx.
type Node struct {
Root bool
Directive string
Args []string
Children []*Node
}
func newNode(stmts []string, root ...bool) (*Node, error) {
var isRoot bool
if len(root) > 0 && root[0] {
isRoot = true
}
var directive string
var args []string
switch len(stmts) {
case 0:
case 1:
directive = stmts[0]
default:
directive = stmts[0]
args = stmts[1:]
}
if directive == "" && !isRoot {
return nil, ErrNonRootNoDirective
}
if directive != "" && isRoot {
return nil, ErrRootDirective
}
return &Node{Directive: directive, Args: args, Root: isRoot}, nil
}
func (n *Node) appendChild(node *Node) { n.Children = append(n.Children, node) }
// String is equal to n.Dump(0).
func (n *Node) String() string {
return n.Dump(0)
}
// WriteTo implements the interface WriterTo to write the configuration to file.
func (n *Node) WriteTo(w io.Writer) (int64, error) {
m, err := io.WriteString(w, n.String())
return int64(m), err
}
func (n *Node) getChildren(indent int, ctx *nodeDumpCtx) string {
ss := make([]string, len(n.Children))
for i, node := range n.Children {
if i == 0 {
ctx.LastBlockEnd = false
} else {
ctx.BlockStart = false
}
ss[i] = node.dump(indent, ctx, n)
}
return strings.Join(ss, "\n")
}
// Dump converts the Node to string.
func (n *Node) Dump(indent int) string {
return n.dump(indent, &nodeDumpCtx{FirstBlock: true}, n)
}
type nodeDumpCtx struct {
HasComment bool
FirstBlock bool
BlockStart bool
LastBlockEnd bool
}
func (n *Node) dump(indent int, ctx *nodeDumpCtx, parent *Node) string {
var prefix, spaces string
for i := indent; i > 0; i-- {
spaces += " "
}
lastComment := ctx.HasComment
ctx.HasComment = strings.HasPrefix(n.Directive, "#")
if ctx.HasComment {
if !lastComment {
if !ctx.BlockStart {
prefix = "\n"
}
} else if !parent.Root && ctx.LastBlockEnd {
prefix = "\n"
}
} else if ctx.LastBlockEnd {
prefix = "\n"
}
if len(n.Children) == 0 {
if ctx.HasComment {
if len(n.Args) == 0 {
return fmt.Sprintf("%s%s%s", prefix, spaces, n.Directive)
}
return fmt.Sprintf("%s%s%s %s", prefix, spaces, n.Directive, strings.Join(n.Args, " "))
}
if len(n.Args) == 0 {
return fmt.Sprintf("%s%s%s;", prefix, spaces, n.Directive)
}
return fmt.Sprintf("%s%s%s %s;", prefix, spaces, n.Directive, strings.Join(n.Args, " "))
} else if n.Root {
return n.getChildren(indent, ctx)
} else if args := strings.Join(n.Args, " "); args != "" {
if ctx.FirstBlock {
ctx.FirstBlock = false
if prefix == "" {
prefix = "\n"
}
}
ctx.BlockStart = true
s := fmt.Sprintf("%s%s%s %s {\n%s\n%s}", prefix, spaces, n.Directive, args,
n.getChildren(indent+1, ctx), spaces)
ctx.LastBlockEnd = true
return s
} else {
if ctx.FirstBlock {
ctx.FirstBlock = false
if prefix == "" {
prefix = "\n"
}
}
ctx.BlockStart = true
s := fmt.Sprintf("%s%s%s {\n%s\n%s}", prefix, spaces, n.Directive,
n.getChildren(indent+1, ctx), spaces)
ctx.LastBlockEnd = true
return s
}
}
// Get returns the child node by the given directive with the args.
func (n *Node) Get(directive string, args ...string) []*Node {
results := make([]*Node, 0, 1)
for _, child := range n.Children {
if child.Directive == directive {
results = append(results, child)
}
}
_len := len(results)
if _len == 0 {
return nil
}
if argslen := len(args); argslen > 0 {
var count int
for i, node := range results {
if len(node.Args) < argslen {
results[i] = nil
count++
}
}
if count == _len {
return nil
} else if count > 0 {
sort.Sort(nodes(results))
results = results[:_len-count]
}
oldResults := results
results = make([]*Node, 0, len(oldResults))
for _, node := range oldResults {
ok := true
for i, arg := range args {
if arg != node.Args[i] {
ok = false
break
}
}
if ok {
results = append(results, node)
}
}
}
return results
}
// Add adds and returns the child node with the directive and the args.
//
// If the child node has existed, it will be ignored and return the first old.
func (n *Node) Add(directive string, args ...string) *Node {
if nodes := n.Get(directive, args...); len(nodes) > 0 {
return nodes[0]
}
node := &Node{Directive: directive, Args: args}
n.appendChild(node)
return node
}
// Del deletes the child node by the directive with the args.
//
// If args is nil, it will delete all the child nodes.
func (n *Node) Del(directive string, args ...string) {
_len := len(n.Children)
if _len == 0 {
return
}
var count int
for i, child := range n.Children {
if child.Directive == directive {
if _len := len(args); _len == 0 {
n.Children[i] = nil
count++
} else if _len <= len(child.Args) {
ok := true
for j, arg := range args {
if arg != child.Args[j] {
ok = false
break
}
}
if ok {
n.Children[i] = nil
count++
}
}
}
}
if count > 0 {
sort.Stable(nodes(n.Children))
n.Children = n.Children[:_len-count]
}
}
// Decode decodes the string s to Node.
func Decode(s string) (*Node, error) {
var err error
var node *Node
var isComment bool
stack := &nodeStack{}
currentWord := []rune{}
currentStmt := []string{}
currentBlock, _ := newNode(nil, true)
for _, char := range s {
if isComment {
switch char {
case '\n':
currentStmt = append(currentStmt, string(currentWord))
if node, err = newNode(currentStmt); err != nil {
return nil, err
}
isComment = false
currentWord = nil
currentStmt = nil
currentBlock.appendChild(node)
case ' ', '\t':
// End the current word.
currentStmt = append(currentStmt, string(currentWord))
currentWord = nil
default:
currentWord = append(currentWord, char)
}
continue
}
switch char {
case '{':
// Put the current block on the stack, start a new block.
// Also, if we are in a word, "finish" that off, and end
// the current statement.
stack.Push(currentBlock)
if len(currentWord) > 0 {
currentStmt = append(currentStmt, string(currentWord))
currentWord = nil
}
if currentBlock, err = newNode(currentStmt); err != nil {
return nil, err
}
currentStmt = nil
case '}':
// Finalize the current block, pull the previous (outer) block off
// of the stack, and add the inner block to the previous block's
// map of blocks.
innerBlock := currentBlock
currentBlock = stack.Pop()
currentBlock.appendChild(innerBlock)
case ';':
// End the current word and statement.
currentStmt = append(currentStmt, string(currentWord))
currentWord = nil
if len(currentStmt) > 0 {
if node, err = newNode(currentStmt); err != nil {
return nil, err
}
currentBlock.appendChild(node)
}
currentStmt = nil
case '\n', ' ', '\t':
// End the current word.
if len(currentWord) > 0 {
currentStmt = append(currentStmt, string(currentWord))
currentWord = nil
}
case '#':
isComment = true
currentWord = append(currentWord, char)
default:
// Add current character onto the current word.
currentWord = append(currentWord, char)
}
}
// Support the last line is the comment
if len(currentWord) > 0 {
currentStmt = append(currentStmt, string(currentWord))
}
if len(currentStmt) > 0 {
if node, err = newNode(currentStmt); err == nil {
currentBlock.appendChild(node)
}
}
return currentBlock, nil
}