-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparsing.go
106 lines (89 loc) · 2.76 KB
/
parsing.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
package gimlet
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
const maxRequestSize = 16 * 1024 * 1024 // 16 MB
// GetVars is a helper method that processes an http.Request and
// returns a map of strings to decoded strings for all arguments
// passed to the method in the URL. Use this helper function when
// writing handler functions.
func GetVars(r *http.Request) map[string]string {
vars := mux.Vars(r)
for k, v := range vars {
unescapedVar, err := url.PathUnescape(v)
if err != nil {
return make(map[string]string)
}
vars[k] = unescapedVar
}
return vars
}
// SetURLVars sets URL variables for testing purposes only.
func SetURLVars(r *http.Request, val map[string]string) *http.Request {
return mux.SetURLVars(r, val)
}
// GetJSON parses JSON from a io.ReadCloser (e.g. http/*Request.Body
// or http/*Response.Body) into an object specified by the
// request. Used in handler functiosn to retreve and parse data
// submitted by the client.
//
// Returns an error if the body is greater than 16 megabytes in size.
func GetJSON(r io.ReadCloser, data interface{}) error {
if r == nil {
return errors.New("no data defined")
}
defer r.Close()
bytes, err := ioutil.ReadAll(&io.LimitedReader{R: r, N: maxRequestSize})
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(json.Unmarshal(bytes, data))
}
// GetJSONUnlimited reads data from a io.ReadCloser, as with GetJSON,
// but does not bound the size of the request.
func GetJSONUnlimited(r io.ReadCloser, data interface{}) error {
if r == nil {
return errors.New("no data defined")
}
defer r.Close()
bytes, err := ioutil.ReadAll(r)
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(json.Unmarshal(bytes, data))
}
// GetYAML parses YAML from a io.ReadCloser (e.g. http/*Request.Body
// or http/*Response.Body) into an object specified by the
// request. Used in handler functiosn to retreve and parse data
// submitted by the client.u
func GetYAML(r io.ReadCloser, data interface{}) error {
if r == nil {
return errors.New("no data defined")
}
defer r.Close()
bytes, err := ioutil.ReadAll(&io.LimitedReader{R: r, N: maxRequestSize})
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(yaml.Unmarshal(bytes, data))
}
// GetYAMLUnlimited reads data from a io.ReadCloser, as with GetYAML,
// but does not bound the size of the request.
func GetYAMLUnlimited(r io.ReadCloser, data interface{}) error {
if r == nil {
return errors.New("no data defined")
}
defer r.Close()
bytes, err := ioutil.ReadAll(&io.LimitedReader{R: r, N: maxRequestSize})
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(yaml.Unmarshal(bytes, data))
}