-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx.go
287 lines (242 loc) · 5.79 KB
/
ctx.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
package sgin
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/clbanning/mxj/v2"
"github.com/spf13/cast"
"github.com/bytedance/sonic"
"github.com/gin-gonic/gin"
)
const (
FormatXML = "XML"
FormatJSON = "JSON"
FormatString = "String"
FormatUpload = "Upload"
FormatDownload = "Download"
)
type Ctx struct {
Request *http.Request
Writer gin.ResponseWriter
Params gin.Params
Keys map[string]any
ctx *gin.Context
engine *Engine
args any
}
func newCtx(ctx *gin.Context, e *Engine) *Ctx {
return &Ctx{
engine: e,
ctx: ctx,
Request: ctx.Request,
Writer: ctx.Writer,
Params: ctx.Params,
Keys: ctx.Keys,
}
}
// ------ 请求参数 ------
func (c *Ctx) Args() (args map[string]any) {
// 已经解析过请求数据
if args, _ = c.args.(map[string]any); args != nil {
return
}
args = map[string]any{}
ct := c.Header(HeaderContentType)
if ct == "" || c.Request.Method == "GET" || ct == gin.MIMEPOSTForm {
_ = c.Request.ParseForm()
for k, v := range c.Request.Form {
args[k] = v[0]
}
} else if strings.HasPrefix(ct, gin.MIMEMultipartPOSTForm) {
if form, err := c.ctx.MultipartForm(); err == nil {
for k, v := range form.Value {
args[k] = v[0]
}
for k, v := range form.File {
args[k] = v[0]
}
}
}
switch ct {
case gin.MIMEJSON:
r := bytes.NewReader(c.RawBody())
dec := sonic.ConfigDefault.NewDecoder(r)
dec.UseNumber()
_ = dec.Decode(&args)
case gin.MIMEXML:
if m, _ := mxj.NewMapXml(c.RawBody()); m != nil {
args = m
}
}
c.args = args
return args
}
func (c *Ctx) Arg(key string, or ...string) string {
if v, ok := c.Args()[key]; ok {
return fmt.Sprint(v)
}
return append(or, "")[0]
}
func (c *Ctx) ArgInt(key string, or ...int) int {
v, err := cast.ToIntE(c.Arg(key))
if err != nil && or != nil {
return or[0]
}
return v
}
func (c *Ctx) ArgInt64(key string, or ...int64) int64 {
v, err := cast.ToInt64E(c.Arg(key))
if err != nil && or != nil {
return or[0]
}
return v
}
func (c *Ctx) ArgFloat64(key string, or ...float64) float64 {
v, err := cast.ToFloat64E(c.Arg(key))
if err != nil && or != nil {
return or[0]
}
return v
}
func (c *Ctx) ArgBool(key string) bool {
return cast.ToBool(c.Arg(key))
}
// ------ 响应 ------
func (c *Ctx) Send(body any, format ...string) error {
c.autoFormat(body, format...)
return nil
}
func (c *Ctx) SendHTML(name string, data any) error {
c.ctx.Abort()
c.ctx.HTML(c.StatusCode(), name, data)
return nil
}
func (c *Ctx) Next() error {
c.ctx.Next()
return nil
}
// ------ SET 设置 ------
func (c *Ctx) Status(code int) *Ctx {
c.Writer.WriteHeader(code)
return c
}
// Locals 设置或将值存储到上下文
func (c *Ctx) Locals(key string, value ...any) any {
if value != nil {
c.ctx.Set(key, value[0])
return nil
}
v, _ := c.ctx.Get(key)
return v
}
// ------ GET 获取 ------
func (c *Ctx) Method() string {
return c.Request.Method
}
// Header 获取 HTTP 请求头的值,如果不存在则返回可选的默认值。
func (c *Ctx) Header(key string, value ...string) string {
header := c.ctx.GetHeader(key)
if header == "" && value != nil {
return value[0]
}
return header
}
func (c *Ctx) SetHeader(key string, value string) {
c.ctx.Header(key, value)
}
func (c *Ctx) RawBody() (body []byte) {
if body, _ = c.Locals(gin.BodyBytesKey).([]byte); body == nil {
if body, _ = io.ReadAll(c.Request.Body); body != nil {
c.Locals(gin.BodyBytesKey, body)
}
}
return body
}
func (c *Ctx) StatusCode() int {
return c.ctx.Writer.Status()
}
func (c *Ctx) Path(full ...bool) string {
if full != nil {
return c.ctx.FullPath()
}
return c.ctx.Request.URL.Path
}
func (c *Ctx) Param(key string) string {
return c.Params.ByName(key)
}
func (c *Ctx) IP() (ip string) {
if ip = c.ctx.ClientIP(); ip == "::1" {
ip = "127.0.0.1"
}
return ip
}
func (c *Ctx) Cookie(name string) (string, error) {
return c.ctx.Cookie(name)
}
func (c *Ctx) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
c.ctx.SetCookie(name, value, maxAge, path, domain, secure, httpOnly)
}
func (c *Ctx) SaveFile(file *multipart.FileHeader, dst string) error {
return c.ctx.SaveUploadedFile(file, dst)
}
// autoFormat 自动根据 Accept 头返回对应类型的数据
func (c *Ctx) autoFormat(body any, format ...string) {
ginCtx := c.ctx
if ginCtx.Abort(); body == nil { // 停止请求链
return
}
// 上传或下载文件
responseType := append(format, "")[0]
if responseType == FormatUpload || responseType == FormatDownload {
file := fmt.Sprint(body)
filename := filepath.Base(file)
if responseType == FormatDownload {
c.Header(HeaderContentDisposition, `attachment; filename*=UTF-8''`+url.QueryEscape(filename))
}
ginCtx.File(file)
return
}
// 返回状态码、字符串、错误
switch b := body.(type) {
case int:
ginCtx.Status(b)
ginCtx.Writer.WriteHeaderNow()
return
case error:
_ = c.engine.config.ErrorHandler(c, b)
return
}
// 返回响应体
status := c.StatusCode()
accept := c.Header(HeaderAccept)
if responseType == FormatJSON || strings.Contains(accept, gin.MIMEJSON) {
ginCtx.JSON(status, body)
return
}
if responseType == FormatXML || strings.Contains(accept, gin.MIMEXML) {
if b, ok := body.([]byte); ok { // 返回 []byte
body = string(b)
}
if s, ok := body.(string); ok { // 返回 string
ginCtx.String(status, s)
return
}
ginCtx.XML(status, body) // 返回其他
return
}
bodyType := fmt.Sprintf("%T", body)
if responseType == FormatString ||
strings.Contains(accept, gin.MIMEHTML) ||
strings.Contains(accept, gin.MIMEPlain) ||
bodyType == "string" ||
bodyType == "[]uint8" /* []byte */ {
ginCtx.String(status, fmt.Sprintf("%s", body))
return
}
ginCtx.JSON(status, body) // 默认返回 JSON
}