/
util.go
219 lines (174 loc) · 4.83 KB
/
util.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package digest
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/spikeekips/mitum-currency/currency"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/state"
quicnetwork "github.com/spikeekips/mitum/network/quic"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/encoder"
jsonenc "github.com/spikeekips/mitum/util/encoder/json"
"github.com/spikeekips/mitum/util/valuehash"
)
func IsAccountState(st state.State) (currency.Account, bool, error) {
if !currency.IsStateAccountKey(st.Key()) {
return currency.Account{}, false, nil
}
ac, err := currency.LoadStateAccountValue(st)
if err != nil {
return currency.Account{}, false, err
}
return ac, true, nil
}
func IsBalanceState(st state.State) (currency.Amount, bool, error) {
if !currency.IsStateBalanceKey(st.Key()) {
return currency.Amount{}, false, nil
}
am, err := currency.StateBalanceValue(st)
if err != nil {
return currency.Amount{}, false, err
}
return am, true, nil
}
func parseHeightFromPath(s string) (base.Height, error) {
s = strings.TrimSpace(s)
if len(s) < 1 {
return base.NilHeight, errors.Errorf("empty height")
} else if len(s) > 1 && strings.HasPrefix(s, "0") {
return base.NilHeight, errors.Errorf("invalid height, %q", s)
}
return base.NewHeightFromString(s)
}
func parseHashFromPath(s string) (valuehash.Hash, error) {
s = strings.TrimSpace(s)
if len(s) < 1 {
return nil, errors.Errorf("empty hash")
}
h := valuehash.NewBytesFromString(s)
if err := h.IsValid(nil); err != nil {
return nil, err
}
return h, nil
}
func parseLimitQuery(s string) int64 {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return int64(-1)
}
return n
}
func parseStringQuery(s string) string {
return strings.TrimSpace(s)
}
func parseOffsetQuery(s string) string {
return strings.TrimSpace(s)
}
func stringOffsetQuery(offset string) string {
return fmt.Sprintf("offset=%s", offset)
}
func stringCollectionQuery(collection string) string {
return fmt.Sprintf("collection=%s", collection)
}
func parseBoolQuery(s string) bool {
return s == "1"
}
func stringBoolQuery(key string, v bool) string { // nolint:unparam
if v {
return fmt.Sprintf("%s=1", key)
}
return ""
}
func addQueryValue(b, s string) string {
if len(s) < 1 {
return b
}
if !strings.Contains(b, "?") {
return b + "?" + s
}
return b + "&" + s
}
func HTTP2Stream(enc encoder.Encoder, w http.ResponseWriter, bufsize int, status int) (*jsoniter.Stream, func()) {
w.Header().Set(HTTP2EncoderHintHeader, enc.Hint().String())
w.Header().Set("Content-Type", HALMimetype)
if status != http.StatusOK {
w.WriteHeader(status)
}
stream := jsoniter.NewStream(HALJSONConfigDefault, w, bufsize)
return stream, func() {
_ = stream.Flush()
}
}
func HTTP2NotSupported(w http.ResponseWriter, err error) {
if err == nil {
err = quicnetwork.NotSupportedErorr
}
HTTP2ProblemWithError(w, err, http.StatusInternalServerError)
}
func HTTP2ProblemWithError(w http.ResponseWriter, err error, status int) {
HTTP2WritePoblem(w, NewProblemFromError(err), status)
}
func HTTP2WritePoblem(w http.ResponseWriter, pr Problem, status int) {
if status == 0 {
status = http.StatusInternalServerError
}
w.Header().Set("Content-Type", ProblemMimetype)
w.Header().Set("X-Content-Type-Options", "nosniff")
var output []byte
if b, err := jsonenc.Marshal(pr); err != nil {
output = unknownProblemJSON
} else {
output = b
}
w.WriteHeader(status)
_, _ = w.Write(output)
}
func HTTP2WriteHal(enc encoder.Encoder, w http.ResponseWriter, hal Hal, status int) { // nolint:unparam
stream, flush := HTTP2Stream(enc, w, 1, status)
defer flush()
stream.WriteVal(hal)
}
func HTTP2WriteHalBytes(enc encoder.Encoder, w http.ResponseWriter, b []byte, status int) { // nolint:unparam
w.Header().Set(HTTP2EncoderHintHeader, enc.Hint().String())
w.Header().Set("Content-Type", HALMimetype)
if status != http.StatusOK {
w.WriteHeader(status)
}
_, _ = w.Write(b)
}
func HTTP2WriteCache(w http.ResponseWriter, key string, expire time.Duration) {
if expire < 1 {
return
}
if cw, ok := w.(*CacheResponseWriter); ok {
_ = cw.SetKey(key).SetExpire(expire)
}
}
func HTTP2HandleError(w http.ResponseWriter, err error) {
status := http.StatusInternalServerError
switch {
case errors.Is(err, util.NotFoundError):
status = http.StatusNotFound
case errors.Is(err, quicnetwork.BadRequestError):
status = http.StatusBadRequest
case errors.Is(err, quicnetwork.NotSupportedErorr):
status = http.StatusInternalServerError
}
HTTP2ProblemWithError(w, err, status)
}
func parseContractIDFromPath(s string) (string, error) {
s = strings.TrimSpace(s)
if len(s) < 1 {
return "", errors.Errorf("empty id")
}
// h, err := document.ParseDocID(s)
// if err != nil {
// return "", err
// }
return s, nil
}