-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
85 lines (75 loc) · 1.63 KB
/
utils.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
package summer
import (
"encoding/json"
"errors"
"io"
"mime"
"net/http"
"net/url"
"strconv"
"strings"
)
func respondInternal(rw http.ResponseWriter, s string, code int) {
buf := []byte(s)
rw.Header().Set("Content-Type", ContentTypeTextPlainUTF8)
rw.Header().Set("Content-Length", strconv.Itoa(len(buf)))
rw.Header().Set("X-Content-Type-Options", "nosniff")
rw.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate")
rw.WriteHeader(code)
_, _ = rw.Write(buf)
}
func flattenSingleSlice[T any](s []T) any {
if len(s) == 1 {
return s[0]
}
return s
}
func extractRequest(m map[string]any, req *http.Request) (err error) {
// header
for k, vs := range req.Header {
k = "header_" + strings.ToLower(strings.ReplaceAll(k, "-", "_"))
m[k] = flattenSingleSlice(vs)
}
// query
for k, vs := range req.URL.Query() {
v := flattenSingleSlice(vs)
m[k] = v
m["query_"+k] = v
}
// body
var buf []byte
if buf, err = io.ReadAll(req.Body); err != nil {
return
}
if len(buf) == 0 {
return
}
var contentType string
if contentType, _, err = mime.ParseMediaType(req.Header.Get("Content-Type")); err != nil {
return
}
switch contentType {
case ContentTypeTextPlain:
m["text"] = string(buf)
case ContentTypeApplicationJSON:
var j map[string]any
if err = json.Unmarshal(buf, &j); err != nil {
return
}
for k, v := range j {
m[k] = v
}
case ContentTypeFormURLEncoded:
var q url.Values
if q, err = url.ParseQuery(string(buf)); err != nil {
return
}
for k, vs := range q {
m[k] = flattenSingleSlice(vs)
}
default:
err = errors.New("unsupported request body type")
return
}
return
}