-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
175 lines (159 loc) · 3.81 KB
/
request.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package essen
import (
"io"
"log"
"net/http"
"net/url"
"strings"
)
type getBody struct {
body *url.URL
}
type postBody struct {
body *http.Request
}
type multiPartBody struct {
body *http.Request
uid string
}
type param interface {
Params(name string) (string, EssenError)
}
func (b getBody) Params(name string) (string, EssenError) {
v := b.body.Query().Get(name)
ee := EssenError{nilval: true}
if v == "" {
ee.nilval = false
ee.errortype = "InvalidParam"
ee.message = `No parameter with key "` + name + `"`
}
return v, ee
}
func (b postBody) Params(name string) (string, EssenError) {
ee := EssenError{nilval: true}
v := b.body.PostFormValue(name)
if v == "" {
ee.nilval = false
ee.errortype = "InvalidParam"
ee.message = `No parameter with key "` + name + `"`
return "", ee
}
return v, ee
}
func (b multiPartBody) Params(name string) (string, EssenError) {
ee := EssenError{nilval: true}
uid := b.uid
v, ok := uploadedPaths[uid][name]
if ok {
return v, ee
}
file, fileHeader, err := b.body.FormFile(name)
if err != nil && err.Error() != "http: no such file" {
ee.nilval = false
ee.errortype = "FormParseError"
ee.message = err.Error()
return "", ee
} else if err == nil {
UploadDir := MultiPartConfig["UploadDir"]
path := UploadDir + "/" + fileHeader.Filename
f, ee := CreateFileIfNotExist(path)
if !ee.IsNil() {
return "", ee
}
n, err := io.Copy(f, file)
log.Println(n, err)
m := uploadedPaths[uid].push(name, path)
if m != nil {
uploadedPaths[uid] = m
}
return path, ee
}
v = b.body.FormValue(name)
if v == "" {
ee.nilval = false
ee.errortype = "InvalidParam"
ee.message = `No parameter with key "` + name + `"`
return "", ee
}
return v, ee
}
//Path functions is used to get URL path
func (r Request) Path() string {
return r.Req.URL.Path
}
//Host is used to get host address from the url
func (r Request) Host() string {
return r.Req.URL.Host
}
//Method is used to check HTTP request method.
func (r Request) Method() string {
return r.Req.Method
}
//HasHeader is used to check if a header is sent for the request.
func (r Request) HasHeader(key string) bool {
v := r.Req.Header.Get(key)
if v == "" {
return false
}
return true
}
//HasCookie is used to check if a cookie exists in a request
func (r Request) HasCookie(key string) bool {
_, err := r.Req.Cookie(key)
if err != nil {
return false
}
return true
}
//CookieVal is used to get value of a cookie
func (r Request) CookieVal(key string) (string, EssenError) {
ee := EssenError{nilval: true}
if !r.HasCookie(key) {
ee.nilval = false
ee.errortype = "CookieError"
ee.message = "Cookie does not exist"
return "", ee
}
cookie, _ := r.Req.Cookie(key)
return cookie.Value, ee
}
//Header function is used to get value of request headers
func (r Request) Header(key string) (string, EssenError) {
if r.HasHeader(key) {
hval := r.Req.Header.Get(key)
ok := strings.HasPrefix(hval, "multipart")
if ok {
hval = strings.Split(hval, ";")[0]
}
return hval, EssenError{nilval: true}
}
return "", EssenError{message: "No Header Found", errortype: "NoHeader", nilval: false}
}
func (r *Request) requestBody() {
contentType, ee := r.Header("Content-Type")
r.App = essenGlobal.Locals
r.Locals = make(map[string]interface{})
if !ee.IsNil() {
contentType = "application/x-www-form-urlencoded"
}
if contentType == "multipart/form-data" {
if !mConfigIsSet() {
setDefaultConfig()
}
r.Body = multiPartBody{body: r.Req, uid: r.Uid}
return
}
if r.Method() == "GET" || r.Method() == "HEAD" {
r.Body = getBody{body: r.Req.URL}
return
}
if r.Method() == "POST" || r.Method() == "PUT" {
err := r.Req.ParseForm()
if err != nil {
ee := EssenError{nilval: false, errortype: "FormParseError", message: err.Error()}
log.Panic(ee.Error())
}
r.Body = postBody{body: r.Req}
return
}
}