Core

/app/controller/cutil/mime.go (3.6 KB)

  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
package cutil

import (
"encoding/xml"
"fmt"
"net/url"
"strings"

"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"gopkg.in/yaml.v2"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/util"
)

const (
mimeJSON = "application/json"
mimeXML = "text/xml"
mimeYAML = "application/x-yaml"
)

var (
AllowedRequestHeaders = "*"
AllowedResponseHeaders = "*"
)

func WriteCORS(rc *fasthttp.RequestCtx) {
setIfEmpty := func(k string, v string) {
if x := rc.Response.Header.Peek(k); len(x) == 0 {
rc.Response.Header.Set(k, v)
}
}
setIfEmpty(fasthttp.HeaderAccessControlAllowHeaders, AllowedRequestHeaders)
setIfEmpty(fasthttp.HeaderAccessControlAllowMethods, "GET,POST,DELETE,PUT,PATCH,OPTIONS,HEAD")
if x := string(rc.Request.Header.Peek(fasthttp.HeaderReferer)); x == "" {
setIfEmpty(fasthttp.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(fasthttp.HeaderAccessControlAllowOrigin, sch+"://"+o)
} else {
setIfEmpty(fasthttp.HeaderAccessControlAllowOrigin, "*")
}
}
setIfEmpty(fasthttp.HeaderAccessControlAllowCredentials, util.BoolTrue)
setIfEmpty(fasthttp.HeaderAccessControlExposeHeaders, AllowedResponseHeaders)
}

func RespondDebug(rc *fasthttp.RequestCtx, as *app.State, filename string, ps *PageState) (string, error) {
return RespondJSON(rc, filename, RequestCtxToMap(rc, as, ps))
}

func RespondJSON(rc *fasthttp.RequestCtx, filename string, body any) (string, error) {
b := util.ToJSONBytes(body, true)
return RespondMIME(filename, mimeJSON, "json", b, rc)
}

type XMLResponse struct {
Result any `xml:"result"`
}

func RespondXML(rc *fasthttp.RequestCtx, filename string, body any) (string, error) {
body = XMLResponse{Result: body}
b, err := xml.Marshal(body)
if err != nil {
return "", errors.Wrapf(err, "can't serialize response of type [%T] to XML", body)
}
return RespondMIME(filename, mimeXML, "xml", b, rc)
}

func RespondYAML(rc *fasthttp.RequestCtx, filename string, body any) (string, error) {
b, err := yaml.Marshal(body)
if err != nil {
return "", err
}
return RespondMIME(filename, mimeYAML, "yaml", b, rc)
}

func RespondMIME(filename string, mime string, ext string, ba []byte, rc *fasthttp.RequestCtx) (string, error) {
rc.Response.Header.SetContentType(mime + "; charset=UTF-8")
if len(filename) > 0 {
if !strings.HasSuffix(filename, "."+ext) {
filename = filename + "." + ext
}
rc.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, filename))
}
WriteCORS(rc)
if len(ba) == 0 {
return "", errors.New("no bytes available to write")
}
if _, err := rc.Write(ba); err != nil {
return "", errors.Wrap(err, "cannot write to response")
}

return "", nil
}

func GetContentType(rc *fasthttp.RequestCtx) string {
ret := string(rc.Request.Header.ContentType())
if idx := strings.Index(ret, ";"); idx > -1 {
ret = ret[0:idx]
}
t := string(rc.URI().QueryArgs().Peek("t"))
switch t {
case "debug":
return t
case "json":
return mimeJSON
case "xml":
return mimeXML
case "yaml", "yml":
return mimeYAML
default:
return strings.TrimSpace(ret)
}
}

func IsContentTypeJSON(c string) bool {
return c == "application/json" || c == "text/json"
}

func IsContentTypeXML(c string) bool {
return c == "application/xml" || c == mimeXML
}

func IsContentTypeYAML(c string) bool {
return c == "application/x-yaml" || c == "text/yaml"
}