-
Notifications
You must be signed in to change notification settings - Fork 4
/
rc.go
41 lines (37 loc) · 1.3 KB
/
rc.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
// Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app/util"
)
func RequestCtxToMap(rc *fasthttp.RequestCtx, data any) map[string]any {
reqHeaders := make(map[string]string, rc.Request.Header.Len())
rc.Request.Header.VisitAll(func(k, v []byte) {
reqHeaders[string(k)] = string(v)
})
req := map[string]any{
"id": rc.ID(),
"url": rc.URI().String(),
"protocol": string(rc.Request.URI().Scheme()),
"host": string(rc.Request.URI().Host()),
"path": string(rc.Request.URI().Path()),
"queryString": string(rc.Request.URI().QueryString()),
"headers": reqHeaders,
"bodySize": len(rc.Request.Body()),
"string": rc.Request.String(),
}
rspHeaders := make(map[string]string, rc.Response.Header.Len())
rc.Response.Header.VisitAll(func(k, v []byte) {
rspHeaders[string(k)] = string(v)
})
rsp := map[string]any{
"code": rc.Response.StatusCode(),
"bodySize": len(rc.Response.Body()),
"headers": rspHeaders,
"string": rc.Response.String(),
}
return map[string]any{"data": data, "request": req, "response": rsp}
}
func RequestCtxBool(rc *fasthttp.RequestCtx, key string) bool {
return string(rc.URI().QueryArgs().Peek(key)) == util.BoolTrue
}