-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiago.go
88 lines (73 loc) · 1.87 KB
/
diago.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 diago
import (
"github.com/gouef/utils"
"strings"
)
type ContentType struct {
Types []string
Charsets []string
}
const (
ContentType_HTML = "text/html"
ContentType_PLAIN = "text/plain"
Charset_UTF8 = "utf-8"
Charset_ALL = "*"
)
type Diago struct {
Extensions []Extension
TemplateProvider TemplateProvider
PanelGenerator PanelGenerator
AllowedContentTypes ContentType
}
func NewDiago() *Diago {
return &Diago{
TemplateProvider: NewDefaultTemplateProvider(),
PanelGenerator: NewDefaultPanelGenerator(),
AllowedContentTypes: ContentType{
Types: []string{
ContentType_HTML,
ContentType_PLAIN,
},
Charsets: []string{
Charset_ALL,
},
},
}
}
func (d *Diago) SetAllowedContentTypes(contentType ContentType) *Diago {
d.AllowedContentTypes = contentType
return d
}
func (d *Diago) AddContentType(typeString string) *Diago {
d.AllowedContentTypes.Types = append(d.AllowedContentTypes.Types, typeString)
return d
}
func (d *Diago) AddContentCharset(charset string) *Diago {
d.AllowedContentTypes.Types = append(d.AllowedContentTypes.Charsets, charset)
return d
}
func (d *Diago) ContainsMIME(header string) bool {
parts := strings.Split(header, ";")
if parts[0] == "" {
return false
}
contentType := strings.TrimSpace(parts[0])
charset := ""
if len(parts) > 1 {
for _, part := range parts[1:] {
if strings.HasPrefix(strings.TrimSpace(part), "charset=") {
part = strings.TrimSpace(part)
charset = strings.TrimSpace(strings.TrimPrefix(part, "charset="))
break
}
}
}
return utils.InListArray([]string{"*", charset}, d.AllowedContentTypes.Charsets) && utils.InArray(contentType, d.AllowedContentTypes.Types)
}
func (d *Diago) GetExtensions() []Extension {
return d.Extensions
}
func (d *Diago) AddExtension(extension Extension) *Diago {
d.Extensions = append(d.Extensions, extension)
return d
}