Repository navigation
Expand file tree
/
Copy pathloader.go
More file actions
88 lines (85 loc) · 2.84 KB
/
Copy pathloader.go
File metadata and controls
88 lines (85 loc) · 2.84 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
package goa
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/url"
"strings"
)
// LoadRequestBody decodes the request body. It returns the decoded content or a slice of decoded
// contents in the case of a multipart body. The decoded content is an interface value corresponding
// to one of bool, float64, string, []interface{}, map[string]interface{} or nil.
// The following content types are supported:
// application/json, text/json, <anything>+json: body is decoded with the JSON decoder.
// application/x-www-form-urlencoded: body is read as a url encoded form. Result is stored in a
// map[string]interface{}.
// multipart/<anything>: each part is decoded using the decoder returned by applying this same
// algorithm to the part content-type header.
// Returns an error if the content type is not supported or decoding fails.
func LoadRequestBody(r *http.Request) (interface{}, error) {
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return nil, fmt.Errorf("invalid request media type: %s", err)
}
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(r.Body, params["boundary"])
var contents []interface{}
for {
p, err := mr.NextPart()
if err == io.EOF {
return contents, nil
}
if err != nil {
return nil, fmt.Errorf("fail to read part enveloppe: %s", err)
}
mediaType, _, err := mime.ParseMediaType(p.Header.Get("Content-Type"))
if err != nil {
return nil, fmt.Errorf("invalid part media type: %s", err)
}
c, err := loadSingleBody(mediaType, p)
if err != nil {
return nil, fmt.Errorf("fail to decode part body: %s", err)
}
contents = append(contents, c)
}
}
return loadSingleBody(mediaType, r.Body)
}
// loadSingleBody is a helper function used by LoadRequestBody to decode the content of a single
// HTTP request body encoded using the media type identified by mt. See LoadRequestBody for more
// details.
func loadSingleBody(mt string, body io.Reader) (interface{}, error) {
if strings.Contains(mt, "form-urlencoded") {
maxFormSize := int64(1<<63 - 1)
b, err := ioutil.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("fail to read form body: %s", err)
}
if int64(len(b)) > maxFormSize {
return nil, fmt.Errorf("request body too large")
}
vs, err := url.ParseQuery(string(b))
if err != nil {
return nil, fmt.Errorf("fail to decode form body: %s", err)
}
values := make(map[string]interface{})
for n, v := range vs {
values[n] = v
}
return values, nil
} else if strings.HasSuffix(mt, "json") {
decoder := json.NewDecoder(body)
var decoded interface{}
err := decoder.Decode(&decoded)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON: %s", err)
}
return decoded, nil
} else {
return nil, fmt.Errorf("unsupported content type '%s'", mt)
}
}