-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite-http.go
118 lines (103 loc) · 2.9 KB
/
write-http.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
// Copyright 2022 Teal.Finance/incorruptible contributors
// This file is part of Teal.Finance/incorruptible
// a tiny+secured cookie token licensed under the MIT License.
// SPDX-License-Identifier: MIT
package incorruptible
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
type WriteErr func(w http.ResponseWriter, r *http.Request, statusCode int, messages ...any)
// Write is a fast and pretty JSON marshaler.
func defaultWriteErr(w http.ResponseWriter, r *http.Request, statusCode int, messages ...any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
buf := make([]byte, 0, 1024)
buf = append(buf, '{')
if len(messages) > 0 {
buf = appendMessages(buf, messages)
}
if r != nil {
if len(messages) > 0 {
buf = append(buf, ',', '\n')
}
buf = appendURL(buf, r.URL)
}
buf = append(buf, '}')
//nolint:errcheck // we do not care if write has failed
w.Write(buf)
}
func appendMessages(buf []byte, messages []any) []byte {
buf = append(buf, []byte(`"message":`)...)
buf = appendKey(buf, messages[0])
for i := 1; i < len(messages); i += 2 {
buf = append(buf, ',', '\n')
buf = appendKey(buf, messages[i])
buf = append(buf, ':')
if i+1 < len(messages) {
buf = appendValue(buf, messages[i+1])
} else {
buf = append(buf, '0')
}
}
return buf
}
func appendURL(buf []byte, u *url.URL) []byte {
buf = append(buf, []byte(`"path":`)...)
buf = strconv.AppendQuote(buf, u.Path)
if u.RawQuery != "" {
buf = append(buf, []byte(",\n"+`"query":`)...)
buf = strconv.AppendQuote(buf, u.RawQuery)
}
return buf
}
func appendKey(buf []byte, a any) []byte {
switch val := a.(type) {
case string:
return strconv.AppendQuote(buf, val)
case []byte:
return strconv.AppendQuote(buf, string(val))
default:
return strconv.AppendQuote(buf, fmt.Sprint(val))
}
}
func appendValue(buf []byte, a any) []byte {
switch val := a.(type) {
case bool:
return strconv.AppendBool(buf, val)
case float32:
return strconv.AppendFloat(buf, float64(val), 'f', 9, 32)
case float64:
return strconv.AppendFloat(buf, val, 'f', 9, 64)
case int:
return strconv.AppendInt(buf, int64(val), 10)
case int8:
return strconv.AppendInt(buf, int64(val), 10)
case int16:
return strconv.AppendInt(buf, int64(val), 10)
case int32:
return strconv.AppendInt(buf, int64(val), 10)
case int64:
return strconv.AppendInt(buf, val, 10)
case uint:
return strconv.AppendUint(buf, uint64(val), 10)
case uint8:
return strconv.AppendUint(buf, uint64(val), 10)
case uint16:
return strconv.AppendUint(buf, uint64(val), 10)
case uint32:
return strconv.AppendUint(buf, uint64(val), 10)
case uint64:
return strconv.AppendUint(buf, val, 10)
case uintptr:
return strconv.AppendUint(buf, uint64(val), 10)
case string:
return strconv.AppendQuote(buf, val)
case []byte:
return strconv.AppendQuote(buf, string(val))
default: // complex64 complex128
return strconv.AppendQuote(buf, fmt.Sprint(val))
}
}