2022-10-24 23:06:49 +08:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Http struct {
|
|
|
|
Comment string
|
|
|
|
Servers []*Server
|
|
|
|
Directives []IDirective
|
2022-11-30 17:33:30 +08:00
|
|
|
Line int
|
2022-10-24 23:06:49 +08:00
|
|
|
}
|
|
|
|
|
2024-02-06 14:02:12 +08:00
|
|
|
func (h *Http) GetCodeBlock() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:06:49 +08:00
|
|
|
func (h *Http) GetComment() string {
|
|
|
|
return h.Comment
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttp(directive IDirective) (*Http, error) {
|
|
|
|
if block := directive.GetBlock(); block != nil {
|
|
|
|
http := &Http{
|
2022-11-30 17:33:30 +08:00
|
|
|
Line: directive.GetBlock().GetLine(),
|
2022-10-24 23:06:49 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
func (h *Http) UpdateDirective(key string, params []string) {
|
|
|
|
if key == "" || len(params) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
directives := h.GetDirectives()
|
2022-10-24 23:06:49 +08:00
|
|
|
index := -1
|
2022-11-24 10:28:39 +08:00
|
|
|
for i, dir := range directives {
|
2022-11-30 17:33:30 +08:00
|
|
|
if dir.GetName() == key {
|
|
|
|
if IsRepeatKey(key) {
|
|
|
|
oldParams := dir.GetParameters()
|
|
|
|
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 23:06:49 +08:00
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 17:33:30 +08:00
|
|
|
newDirective := &Directive{
|
|
|
|
Name: key,
|
|
|
|
Parameters: params,
|
|
|
|
}
|
2022-10-24 23:06:49 +08:00
|
|
|
if index > -1 {
|
2022-11-30 17:33:30 +08:00
|
|
|
directives[index] = newDirective
|
2022-10-24 23:06:49 +08:00
|
|
|
} else {
|
2022-11-30 17:33:30 +08:00
|
|
|
directives = append(directives, newDirective)
|
2022-10-24 23:06:49 +08:00
|
|
|
}
|
|
|
|
h.Directives = directives
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
func (h *Http) RemoveDirective(key string, params []string) {
|
2022-10-28 17:04:57 +08:00
|
|
|
directives := h.GetDirectives()
|
|
|
|
var newDirectives []IDirective
|
|
|
|
for _, dir := range directives {
|
2022-11-30 17:33:30 +08:00
|
|
|
if dir.GetName() == key {
|
2022-11-30 21:40:05 +08:00
|
|
|
if IsRepeatKey(key) && len(params) > 0 {
|
2022-11-30 17:33:30 +08:00
|
|
|
oldParams := dir.GetParameters()
|
2022-11-30 21:40:05 +08:00
|
|
|
if oldParams[0] == params[0] {
|
2022-11-30 17:33:30 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-28 17:04:57 +08:00
|
|
|
}
|
|
|
|
newDirectives = append(newDirectives, dir)
|
|
|
|
}
|
|
|
|
h.Directives = newDirectives
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:06:49 +08:00
|
|
|
func (h *Http) GetBlock() IBlock {
|
|
|
|
return h
|
|
|
|
}
|
2022-11-24 10:28:39 +08:00
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
func (h *Http) GetLine() int {
|
|
|
|
return h.Line
|
2022-11-24 10:28:39 +08:00
|
|
|
}
|