-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttprequestargs.go
117 lines (104 loc) · 3.16 KB
/
httprequestargs.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
package function
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
type HTTPRequestArgsGetter func(*http.Request) (map[string]string, error)
func HTTPRequestArgs(args map[string]string) HTTPRequestArgsGetter {
return func(*http.Request) (map[string]string, error) {
return args, nil
}
}
func HTTPRequestArg(name, value string) HTTPRequestArgsGetter {
return HTTPRequestArgs(map[string]string{name: value})
}
func HTTPRequestBodyAsArg(name string) HTTPRequestArgsGetter {
return func(request *http.Request) (map[string]string, error) {
defer request.Body.Close()
body, err := io.ReadAll(request.Body)
if err != nil {
return nil, err
}
return map[string]string{name: string(body)}, nil
}
}
func MergeHTTPRequestArgs(getters ...HTTPRequestArgsGetter) HTTPRequestArgsGetter {
return func(request *http.Request) (map[string]string, error) {
args := make(map[string]string)
for _, getArgs := range getters {
a, err := getArgs(request)
if err != nil {
return nil, err
}
for name, value := range a {
args[name] = value
}
}
return args, nil
}
}
func HTTPRequestQueryArg(name string) HTTPRequestArgsGetter {
return func(request *http.Request) (map[string]string, error) {
return map[string]string{name: request.URL.Query().Get(name)}, nil
}
}
func HTTPRequestQueryAsArg(queryKey, name string) HTTPRequestArgsGetter {
return func(request *http.Request) (map[string]string, error) {
return map[string]string{name: request.URL.Query().Get(queryKey)}, nil
}
}
// HTTPRequestQueryArgs returns the query params of the request as string map.
// If a query param has multiple values, they are joined with ";".
func HTTPRequestQueryArgs(request *http.Request) (map[string]string, error) {
args := make(map[string]string)
for name, values := range request.URL.Query() {
args[name] = strings.Join(values, ";")
}
return args, nil
}
// HTTPRequestMultipartFormArgs returns the multipart form values of the request as string map.
// If a form field has multiple values, they are joined with ";".
func HTTPRequestMultipartFormArgs(request *http.Request) (map[string]string, error) {
err := request.ParseMultipartForm(1 << 20)
if err != nil {
return nil, err
}
args := make(map[string]string)
for name, values := range request.MultipartForm.Value {
args[name] = strings.Join(values, ";")
}
return args, nil
}
func HTTPRequestBodyJSONFieldsAsArgs(request *http.Request) (map[string]string, error) {
defer request.Body.Close()
body, err := io.ReadAll(request.Body)
if err != nil {
return nil, err
}
return namedStringsFromJSON(body)
}
func namedStringsFromJSON(jsonObject []byte) (map[string]string, error) {
fields := make(map[string]json.RawMessage)
err := json.Unmarshal(jsonObject, &fields)
if err != nil {
return nil, err
}
args := make(map[string]string)
for name, rawJSON := range fields {
if len(rawJSON) > 0 && rawJSON[0] == '"' {
// Unescape JSON string
var str string
err = json.Unmarshal(rawJSON, &str)
if err != nil {
return nil, fmt.Errorf("can't unmarshal JSON object value %q as string because of: %w", name, err)
}
args[name] = str
continue
}
args[name] = string(rawJSON)
}
return args, nil
}