-
Notifications
You must be signed in to change notification settings - Fork 12
/
jar.go
88 lines (76 loc) · 1.98 KB
/
jar.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
package aqua
import (
_ "fmt"
"io/ioutil"
"net/http"
"strings"
)
var separator string = ","
// Jar allows access to 1/ the reqeust, and 2/ variables (post, get and body)
// However, to load these variable values, LoadVars() method needs to be invoked
// in prior.
type Jar struct {
// request handle
Request *http.Request
// variables
PostVars map[string]string
QueryVars map[string]string
Body string
}
func NewJar(r *http.Request) Jar {
return Jar{
Request: r,
}
}
func (j *Jar) LoadVars() {
if j.PostVars != nil {
panic("Jar.Load can be called only once")
} else {
j.PostVars = make(map[string]string)
j.QueryVars = make(map[string]string)
}
if j.Request.Method == "POST" || j.Request.Method == "PUT" {
ctype := j.Request.Header.Get("Content-Type")
switch {
case ctype == "application/x-www-form-urlencoded":
j.Request.ParseForm()
j.loadPostVars(j.Request)
j.loadQueryVars(j.Request, true)
case strings.HasPrefix(ctype, "multipart/form-data;"):
// ParseMultiPart form should ideally populate
// r.PostForm, but instead it fills r.Form
// https://github.com/golang/go/issues/9305
j.Request.ParseMultipartForm(1024 * 1024)
j.loadPostVars(j.Request)
j.loadQueryVars(j.Request, true)
default:
j.Body = getBody(j.Request)
}
} else if j.Request.Method == "GET" {
j.Request.ParseForm()
j.loadQueryVars(j.Request, false)
}
}
func getBody(r *http.Request) string {
b, err := ioutil.ReadAll(r.Body)
panicIf(err)
defer r.Body.Close()
return string(b)
}
func (j *Jar) loadPostVars(r *http.Request) {
for k, _ := range r.PostForm {
j.PostVars[k] = strings.Join(r.PostForm[k], separator)
}
}
func (j *Jar) loadQueryVars(r *http.Request, skipPostVars bool) {
for k, _ := range r.Form {
if skipPostVars {
// only add to query-vars if it is NOT a post var
if _, found := j.PostVars[k]; !found {
j.QueryVars[k] = strings.Join(r.Form[k], separator)
}
} else {
j.QueryVars[k] = strings.Join(r.Form[k], separator)
}
}
}