/
reqs.go
310 lines (259 loc) · 6.66 KB
/
reqs.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package hsrv
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"github.com/NightmareZero/nzgoutil/util"
)
type RequestHandler func(Ctx)
type ErrorHandler func(Ctx, error)
type Ctx struct {
RW http.ResponseWriter
R *http.Request
*reqCtx
}
// Response=========================================================
func (r *Ctx) doResponse(code int, body []byte) (err error) {
if r.after != nil {
for _, pp := range r.after {
pp.After(*r)
}
}
r.RW.WriteHeader(code)
_, err = r.RW.Write(body)
return
}
func (r *Ctx) doResponse1(code int, body io.Reader) (wirtten int64, err error) {
r.RW.WriteHeader(http.StatusOK)
return io.Copy(r.RW, body)
}
func (r *Ctx) Text(statusCode int, txt string) (err error) {
r.RW.Header().Add("Content-Type", "text/plain; charset=utf-8")
r.doResponse(statusCode, util.String2Bytes(txt))
return
}
func (r *Ctx) Html(statusCode int, html string) (err error) {
r.RW.Header().Add("Content-Type", "text/html; charset=utf-8")
r.doResponse(statusCode, util.String2Bytes(html))
return
}
func (r *Ctx) Sse(fun func(hf http.Flusher, r *Ctx)) (err error) {
flusher, ok := r.RW.(http.Flusher)
if !ok {
http.Error(r.RW, "Streaming unsupported!", http.StatusInternalServerError)
return
}
r.RW.Header().Add("Content-Type", "text/event-stream; charset=utf-8")
r.RW.Header().Set("Cache-Control", "no-cache")
r.RW.Header().Set("Connection", "keep-alive")
r.RW.Header().Set("Access-Control-Allow-Origin", "*")
// fun 方法说明:
// 返回数据包含id、event(非必须,如果包含本项,则必须使用eventlistener接收,否则为onMessage)、data,结尾必须使用\n\n
// fmt.Fprintf(rw, "id: %d\nevent: ping \ndata: %d\n\n", time.Now().Unix(), time.Now().Unix())
// flusher.Flush()
fun(flusher, r)
return
}
func (r *Ctx) Json(statusCode int, target any) error {
b, err := json.Marshal(target)
if err != nil {
return fmt.Errorf("error on unmarshal json, %w", err)
}
r.RW.Header().Add("Content-Type", "application/json; charset=utf-8")
r.doResponse(statusCode, b)
return err
}
func (r *Ctx) File(input io.Reader, size int64, name string) (int64, error) {
ct, down := getContentTypeByFilename(name)
cd := ""
if down {
cd = "attachment; filename=" + name
} else {
cd = " filename=" + name
}
r.RW.Header().Set("Content-Disposition", cd)
r.RW.Header().Set("Content-Type", ct)
if size > 0 {
r.RW.Header().Set("Content-Length", strconv.FormatInt(size, 10))
}
w, err := r.doResponse1(http.StatusOK, input)
if err != nil {
return w, fmt.Errorf("fail on response file, %w", err)
}
return w, nil
}
func getContentTypeByFilename(name string) (ct string, down bool) {
lName := strings.ToLower(name)
s := path.Ext(lName)
switch s {
case ".jpg":
fallthrough
case ".jpeg":
return "image/jpeg", false
case ".png":
return "image/png", false
case ".svg":
return "image/svg+xml", false
case ".ico":
return "image/x-icon", false
case ".tif":
return "image/tiff", false
case ".gif":
return "image/gif", false
case ".bmp":
return "image/bmp", false
case ".webp":
return "image/webp", false
case ".mp4":
return "video/mp4", false
case ".avi":
return "video/x-msvideo", false
case ".mov":
return "video/quicktime", false
case ".mp3":
return "audio/mpeg", false
case ".wav":
return "audio/wav", false
case ".ogg":
return "audio/ogg", false
case ".pdf":
return "application/pdf", false
}
return "application/octet-stream", true
}
// Request=============================================================
func (r *Ctx) IP() (string, error) {
ip := r.R.Header.Get("X-Real-IP")
if net.ParseIP(ip) != nil {
return ip, nil
}
ip = r.R.Header.Get("X-Forward-For")
for _, i := range strings.Split(ip, ",") {
if net.ParseIP(i) != nil {
return i, nil
}
}
ip, _, err := net.SplitHostPort(r.R.RemoteAddr)
if err != nil {
return "", err
}
if net.ParseIP(ip) != nil {
return ip, nil
}
return "", errors.New("no valid ip found")
}
func (r *Ctx) WebContext() any {
return r.R.Context().Value(WebContextVName)
}
func (r *Ctx) ParseText() (string, error) {
defer r.R.Body.Close()
b, err := io.ReadAll(r.R.Body)
if err != nil {
return "", fmt.Errorf("error on read request body, %w", err)
}
return string(b), nil
}
func (r *Ctx) ParseJson(target any) error {
defer r.R.Body.Close()
b, err := io.ReadAll(r.R.Body)
if err != nil {
return fmt.Errorf("error on read request body, %w", err)
}
err = json.Unmarshal(b, target)
if err != nil {
return fmt.Errorf("error on unmarshal json, %w", err)
}
return nil
}
func (r *Ctx) UrlParam() (map[string]string, error) {
buffer := bytes.NewBuffer(make([]byte, 0, r.R.ContentLength))
buffer.ReadFrom(r.R.Body)
fmt.Println(buffer.String())
params, err := url.ParseQuery(buffer.String())
if err != nil {
return nil, fmt.Errorf("error on parse url param, %w", err)
}
values := map[string]string{}
for key, value := range params {
if len(value) > 0 {
values[key] = value[0]
} else {
values[key] = ""
}
}
return values, nil
}
func (r *Ctx) Stream(wr io.Writer) (err error) {
defer r.R.Body.Close()
_, err = io.Copy(wr, r.R.Body)
return
}
func (r *Ctx) FormData() (map[string][]string, error) {
defer r.R.Body.Close()
contentType := r.R.Header.Get("content-type")
var formValue map[string][]string = make(map[string][]string)
if !strings.Contains(contentType, "application/x-www-form-urlencoded") {
return formValue, fmt.Errorf("content-type must be application/x-www-form-urlencoded")
}
err := r.R.ParseForm()
if err != nil {
return formValue, fmt.Errorf("error on pares form data, %w", err)
}
return r.R.Form, nil
}
type MultiForm struct {
close bool
*multipart.Form
r *Ctx
}
func (m *MultiForm) Close() {
if m.close {
m.r.R.Body.Close()
m.close = true
}
}
func (m *MultiForm) ParseFile(writer func(string, io.Reader, error)) {
for _, content := range m.File {
for _, fh := range content {
f, err2 := fh.Open()
if err2 != nil {
writer("", nil, err2)
}
writer(fh.Filename, f, nil)
f.Close()
}
}
m.Close()
}
func (r *Ctx) MultipartForm() (f *MultiForm, err error) {
return r.MultipartFormLarge(16)
}
// 处理更大的formdata
// size: 单位为 Mib
func (r *Ctx) MultipartFormLarge(size int64) (f *MultiForm, err error) {
contentType := r.R.Header.Get("content-type")
if !strings.Contains(contentType, "multipart/form-data") {
err = fmt.Errorf("content-type must be multipart/form-data")
return
}
var err2 error = r.R.ParseMultipartForm(2 * size * 1024 * 1024)
if err2 != nil {
err = fmt.Errorf("failure on parse files, %w", err2)
return
}
f = &MultiForm{
close: false,
Form: r.R.MultipartForm,
r: r,
}
return
}