/
parsereq.go
99 lines (87 loc) · 2.15 KB
/
parsereq.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
/**
* Created by IDEA.
* User: godsoul
*/
//解析request到结构体
package middleware
import (
"io/ioutil"
"github.com/guobin8205/api_demo/utils/helper/convert"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
"log"
"strings"
)
type Req struct {
Accept string
Ver string
Area string
OS int
UID int
Token string
ReqBody
}
func ParseRequest() gin.HandlerFunc {
return func(c *gin.Context) {
req := &Req{
Ver: c.Request.Header.Get("Ver"),
Area: c.Request.Header.Get("Area"),
OS: convert.ToInt(c.Request.Header.Get("OS")),
UID: convert.ToInt(c.Request.Header.Get("UID")),
Token: c.Request.Header.Get("Token"),
}
log.Println(c.Request.Header)
body, _ := ioutil.ReadAll(c.Request.Body)
defer c.Request.Body.Close()
//sbody, _ := base64.StdEncoding.DecodeString(string(body))
log.Println(c.Request.RequestURI)
if strings.Contains(c.Request.RequestURI,"debug/pprof"){
//if c.Request.RequestURI == "/debug/pprof/" || c.Request.RequestURI == "/debug/pprof/heap" || c.Request.RequestURI == "/debug/pprof/goroutine" ||
// c.Request.RequestURI == "/debug/pprof/block" || c.Request.RequestURI == "/debug/pprof/threadcreate"{
req.b = string(body)
c.Set("req", req)
}else{
//dst := make([]byte, len(body))
//dst,_ = crypto.NewAesCrypto().Decrypt(body)
//req.b = string(dst)
//log.Println(string(dst))
////log.Println("======================")
//c.Set("req", req)
req.b = string(body)
c.Set("req", req)
}
}
}
type ReqBody struct {
b string
}
func (req *ReqBody) String(key string) string {
s := gjson.Get(req.b, key).String()
if s == "null" {
return ""
}
return s
}
func (req *ReqBody) DefaultString(key string, def string) string {
v := gjson.Get(req.b, key).String()
if v == "null" {
return def
}
return v
}
func (req *ReqBody) Int(key string) int {
return int(gjson.Get(req.b, key).Int())
}
func (req *ReqBody) Int64(key string) int64 {
return gjson.Get(req.b, key).Int()
}
func (req *ReqBody) DefaultInt(key string, def int) int {
v := gjson.Get(req.b, key).Int()
if v == 0 {
return def
}
return int(v)
}
func (req *ReqBody) GetBody() string {
return req.b
}