-
Notifications
You must be signed in to change notification settings - Fork 4
/
mime.go
190 lines (166 loc) · 4.91 KB
/
mime.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cutil
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/util"
)
const (
mimeCSV = "text/csv"
mimeDebug = "text/plain"
mimeHTML = "text/html"
mimeJSON = "application/json"
mimeTOML = "application/toml"
mimeXML = "text/xml"
mimeYAML = "application/x-yaml"
HeaderAccept = "Accept"
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
HeaderCacheControl = "Cache-Control"
HeaderContentType = "Content-Type"
HeaderReferer = "Referer"
)
var (
AllowedRequestHeaders = "*"
AllowedResponseHeaders = "*"
)
func WriteCORS(w http.ResponseWriter) {
h := w.Header()
setIfEmpty := func(k string, v string) {
if x := h.Get(k); x == "" {
h.Set(k, v)
}
}
setIfEmpty(HeaderAccessControlAllowHeaders, AllowedRequestHeaders)
setIfEmpty(HeaderAccessControlAllowMethods, "GET,POST,DELETE,PUT,PATCH,OPTIONS,HEAD")
if x := h.Get(HeaderReferer); x == "" {
setIfEmpty(HeaderAccessControlAllowOrigin, "*")
} else {
u, err := url.Parse(x)
if err == nil {
o := u.Hostname()
if u.Port() != "" {
o += ":" + u.Port()
}
sch := u.Scheme
if strings.Contains(o, ".network") {
sch = "https"
}
setIfEmpty(HeaderAccessControlAllowOrigin, sch+"://"+o)
} else {
setIfEmpty(HeaderAccessControlAllowOrigin, "*")
}
}
setIfEmpty(HeaderAccessControlAllowCredentials, util.BoolTrue)
setIfEmpty(HeaderAccessControlExposeHeaders, AllowedResponseHeaders)
}
func RespondDebug(w *WriteCounter, r *http.Request, as *app.State, filename string, ps *PageState) (string, error) {
return RespondJSON(w, filename, RequestCtxToMap(r, as, ps))
}
func RespondCSV(w *WriteCounter, filename string, body any) (string, error) {
b, err := util.ToCSVBytes(body)
if err != nil {
return "", err
}
return RespondMIME(filename, mimeCSV, b, w)
}
func RespondJSON(w *WriteCounter, filename string, body any) (string, error) {
b := util.ToJSONBytes(body, true)
return RespondMIME(filename, mimeJSON, b, w)
}
func RespondTOML(w *WriteCounter, filename string, body any) (string, error) {
b := util.ToTOMLBytes(body)
return RespondMIME(filename, mimeTOML, b, w)
}
type XMLResponse struct {
Result any `xml:"result"`
}
func RespondXML(w *WriteCounter, filename string, body any) (string, error) {
b, err := util.ToXMLBytes(body, true)
if err != nil {
return "", errors.Wrapf(err, "can't serialize response of type [%T] to XML", body)
}
return RespondMIME(filename, mimeXML, b, w)
}
func RespondYAML(w *WriteCounter, filename string, body any) (string, error) {
b, err := yaml.Marshal(body)
if err != nil {
return "", err
}
return RespondMIME(filename, mimeYAML, b, w)
}
func RespondMIME(filename string, mime string, ba []byte, w *WriteCounter) (string, error) {
h := w.Header()
switch mime {
case mimeCSV, mimeDebug, mimeHTML, mimeJSON, mimeTOML, mimeXML, mimeYAML:
if !strings.Contains(mime, "; charset") {
mime += "; charset=UTF-8"
}
}
h.Set(HeaderContentType, mime)
if filename != "" {
h.Set("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, filename))
}
WriteCORS(w)
if len(ba) == 0 {
return "", errors.New("no bytes available to write")
}
if _, err := w.Write(ba); err != nil {
return "", errors.Wrap(err, "cannot write to response")
}
return "", nil
}
func RespondDownload(filename string, ba []byte, w *WriteCounter) (string, error) {
return RespondMIME(filename, "application/octet-stream", ba, w)
}
func GetContentType(r *http.Request) string {
ret := r.Header.Get(HeaderAccept)
if ret == "" {
ret = r.Header.Get(HeaderContentType)
}
if idx := strings.Index(ret, ";"); idx > -1 {
ret = ret[0:idx]
}
t := r.URL.Query().Get("t")
switch t {
case util.KeyDebug:
return mimeDebug
case util.KeyCSV:
return mimeCSV
case util.KeyJSON:
return mimeJSON
case util.KeyTOML:
return mimeTOML
case util.KeyXML:
return mimeXML
case util.KeyYAML, "yml":
return mimeYAML
default:
return strings.TrimSpace(ret)
}
}
func IsContentTypeCSV(c string) bool {
return c == mimeCSV || c == util.KeyCSV
}
func IsContentTypeDebug(c string) bool {
return c == mimeDebug || c == util.KeyDebug
}
func IsContentTypeJSON(c string) bool {
return c == mimeJSON || c == "text/json" || c == util.KeyJSON
}
func IsContentTypeTOML(c string) bool {
return c == mimeTOML || c == "text/toml" || c == util.KeyTOML
}
func IsContentTypeXML(c string) bool {
return c == "application/xml" || c == mimeXML || c == util.KeyXML
}
func IsContentTypeYAML(c string) bool {
return c == mimeYAML || c == "text/yaml" || c == util.KeyYAML
}