forked from tufanbarisyildirim/gonginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
73 lines (63 loc) · 1.73 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
package gonginx
import (
"errors"
)
//Http represents http block
type Http struct {
Servers []*Server
Directives []IDirective
}
//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)
}
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)
for _, directive := range h.Directives {
directives = append(directives, directive)
}
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 directive.GetBlock() != nil {
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
}
}
return directives
}
//GetBlock get block if any
func (h *Http) GetBlock() IBlock {
return h
}