forked from tufanbarisyildirim/gonginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
88 lines (76 loc) · 2.15 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
package gonginx
import (
"errors"
)
// HTTP represents http block
type HTTP struct {
Servers []*Server
Directives []IDirective
Comment []string
}
// GetComment comment of the HTTP directive
func (h *HTTP) GetComment() []string {
return h.Comment
}
// SetComment set the comment of the HTTP directive
func (h *HTTP) SetComment(comment []string) {
h.Comment = comment
}
// NewHTTP create an http block from a directive which has a block
func NewHTTP(directive IDirective) (*HTTP, error) {
if block := directive.GetBlock(); block != nil {
http := &HTTP{
Servers: []*Server{},
Directives: []IDirective{},
}
for _, directive := range block.GetDirectives() {
if server, ok := directive.(*Server); ok {
http.Servers = append(http.Servers, server)
continue
}
http.Directives = append(http.Directives, directive)
}
http.Comment = directive.GetComment()
return http, nil
}
return nil, errors.New("http directive must have a block")
}
// GetName get directive name to construct the statment string
func (h *HTTP) GetName() string { //the directive name.
return "http"
}
// GetParameters get directive parameters if any
func (h *HTTP) GetParameters() []string {
return []string{}
}
// GetDirectives get all directives in http
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
}
// FindDirectives find 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 include, ok := directive.(*Include); ok {
for _, c := range include.Configs {
directives = append(directives, c.FindDirectives(directiveName)...)
}
}
if directive.GetBlock() != nil {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
return directives
}
// GetBlock get block if any
func (h *HTTP) GetBlock() IBlock {
return h
}