-
Notifications
You must be signed in to change notification settings - Fork 2k
/
http.go
125 lines (111 loc) · 2.62 KB
/
http.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
package components
import (
"errors"
)
type Http struct {
Comment string
Servers []*Server
Directives []IDirective
Line int
}
func (h *Http) GetComment() string {
return h.Comment
}
func NewHttp(directive IDirective) (*Http, error) {
if block := directive.GetBlock(); block != nil {
http := &Http{
Line: directive.GetBlock().GetLine(),
Servers: []*Server{},
Directives: []IDirective{},
Comment: block.GetComment(),
}
for _, directive := range block.GetDirectives() {
if server, ok := directive.(*Server); ok {
http.Servers = append(http.Servers, server)
continue
}
http.Directives = append(http.Directives, directive)
}
return http, nil
}
return nil, errors.New("http directive must have a block")
}
func (h *Http) GetName() string {
return "http"
}
func (h *Http) GetParameters() []string {
return []string{}
}
func (h *Http) GetDirectives() []IDirective {
directives := make([]IDirective, 0)
directives = append(directives, h.Directives...)
for _, directive := range h.Servers {
directives = append(directives, directive)
}
return directives
}
func (h *Http) FindDirectives(directiveName string) []IDirective {
directives := make([]IDirective, 0)
for _, directive := range h.GetDirectives() {
if directive.GetName() == directiveName {
directives = append(directives, directive)
}
if directive.GetBlock() != nil {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
return directives
}
func (h *Http) UpdateDirective(key string, params []string) {
if key == "" || len(params) == 0 {
return
}
directives := h.GetDirectives()
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)
}
h.Directives = directives
}
func (h *Http) RemoveDirective(key string, params []string) {
directives := h.GetDirectives()
var newDirectives []IDirective
for _, dir := range directives {
if dir.GetName() == key {
if IsRepeatKey(key) && len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
continue
}
} else {
continue
}
}
newDirectives = append(newDirectives, dir)
}
h.Directives = newDirectives
}
func (h *Http) GetBlock() IBlock {
return h
}
func (h *Http) GetLine() int {
return h.Line
}