Core

/app/controller/cutil/rc.go (3.5 KB)

  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
package cutil

import (
"net/url"
"strconv"

"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/util"
)

func RequestCtxToMap(rc *fasthttp.RequestCtx, as *app.State, ps *PageState) util.ValueMap {
req := util.ValueMap{
"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": RequestHeadersMap(rc),
"bodySize": len(rc.Request.Body()),
}
rsp := util.ValueMap{"code": rc.Response.StatusCode(), "bodySize": len(rc.Response.Body()), "headers": ResponseHeadersMap(rc)}{{{ if .HasModule "help" }}}
hasHelp := as.Services != nil && as.Services.Help != nil && as.Services.Help.Contains(ps.Action){{{ end }}}
action := util.ValueMap{
"action": ps.Action, "admin": ps.Admin, "authed": ps.Authed,
"redirect": ps.ForceRedirect, "flashes": ps.Flashes, "breadcrumbs": ps.Breadcrumbs,
"browser": ps.Browser, "browserVersion": ps.BrowserVersion, "os": ps.OS, "osVersion": ps.OSVersion, "platform": ps.Platform,
"description": ps.Description, "title": ps.Title, "started": ps.Started,{{{ if .HasModule "help" }}} "help": hasHelp,{{{ end }}}
}
ret := util.ValueMap{"action": action, "data": ps.Data, "request": req, "response": rsp}
// $PF_SECTION_START(debugstuff)$
// $PF_SECTION_END(debugstuff)$
return ret
}

func RCRequiredString(rc *fasthttp.RequestCtx, key string, allowEmpty bool) (string, error) {
v, ok := rc.UserValue(key).(string)
if !ok || ((!allowEmpty) && v == "") {
return v, errors.Errorf("must provide [%s] in path", key)
}
v, err := url.QueryUnescape(v)
if err != nil {
return "", err
}
return v, nil
}

func RCRequiredBool(rc *fasthttp.RequestCtx, key string) (bool, error) {
ret, err := RCRequiredString(rc, key, true)
if err != nil {
return false, err
}
return ret == util.BoolTrue, nil
}

func RCRequiredInt(rc *fasthttp.RequestCtx, key string) (int, error) {
s, err := RCRequiredString(rc, key, true)
if err != nil {
return 0, err
}
ret, err := strconv.ParseInt(s, 10, 32)
return int(ret), err
}

func RCRequiredUUID(rc *fasthttp.RequestCtx, key string) (*uuid.UUID, error) {
ret, err := RCRequiredString(rc, key, true)
if err != nil {
return nil, err
}
return util.UUIDFromString(ret), nil
}

func RCRequiredArray(rc *fasthttp.RequestCtx, key string) ([]string, error) {
ret, err := RCRequiredString(rc, key, true)
if err != nil {
return nil, err
}
return util.StringSplitAndTrim(ret, ","), nil
}

func QueryStringBool(rc *fasthttp.RequestCtx, key string) bool {
x := string(rc.URI().QueryArgs().Peek(key))
return x == util.BoolTrue || x == "t" || x == "True" || x == "TRUE"
}

func QueryArgsMap(rc *fasthttp.RequestCtx) util.ValueMap {
ret := make(util.ValueMap, rc.QueryArgs().Len())
rc.QueryArgs().VisitAll(func(k []byte, v []byte) {
curr, _ := ret.GetStringArray(string(k), true)
ret[string(k)] = append(curr, string(v))
})
return ret
}

func RequestHeadersMap(rc *fasthttp.RequestCtx) map[string]string {
ret := make(map[string]string, rc.Request.Header.Len())
rc.Request.Header.VisitAll(func(k, v []byte) {
ret[string(k)] = string(v)
})
return ret
}

func ResponseHeadersMap(rc *fasthttp.RequestCtx) map[string]string {
ret := make(map[string]string, rc.Request.Header.Len())
rc.Response.Header.VisitAll(func(k, v []byte) {
ret[string(k)] = string(v)
})
return ret
}