forked from meson10/gottp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
253 lines (218 loc) · 4.79 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package gottp
import (
"compress/gzip"
"compress/zlib"
"log"
"net/http"
"strconv"
"strings"
"gopkg.in/simversity/gottp.v3/utils"
)
//*http.Request, rw ResponseWriter
const PAGE_SIZE = 30
const SKIP = 0
const serverUA = "Gottp Server"
const ERROR = `Oops! An Internal Error occured while performing that action.
Please try again later`
type WireSender interface {
SendOverWire() utils.Q
}
type Paginator struct {
Skip int
Limit int
Wlt string
Wgt string
Wkey string
Ids []string
}
func makeInt(val interface{}, fallback int) int {
switch val.(type) {
case int:
return val.(int)
case string:
_val := val.(string)
ret, err := strconv.Atoi(_val)
if err == nil {
return ret
}
case float64:
return int(val.(float64))
}
return fallback
}
func makeString(val interface{}) (ret string) {
switch val.(type) {
case string:
ret = val.(string)
case int:
val := val.(int)
ret = strconv.Itoa(val)
}
return
}
type Request struct {
Request *http.Request
Writer http.ResponseWriter
UrlArgs *map[string]string
pipeOutput chan<- *utils.Q
pipeIndex int
params *utils.Q
}
func (r *Request) makeUrlArgs(params utils.Q) {
if r.UrlArgs != nil {
for key, value := range *r.UrlArgs {
params[key] = value
}
}
}
func (r *Request) GetPaginator() *Paginator {
p := Paginator{Limit: -1}
qp := r.GetArguments()
for key, value := range *qp {
switch key {
case "skip":
p.Skip = makeInt(value, SKIP)
case "limit":
p.Limit = makeInt(value, PAGE_SIZE)
case "wlt":
p.Wlt = makeString(value)
case "wgt":
p.Wgt = makeString(value)
case "wkey":
p.Wkey = makeString(value)
case "ids":
ids, ok := value.([]string)
if !ok {
id := value.(string)
p.Ids = []string{id}
} else {
p.Ids = ids
}
}
}
if p.Limit < 0 {
p.Limit = PAGE_SIZE
}
return &p
}
func (r *Request) makeUrlParams(params utils.Q) {
r.Request.ParseForm()
for key, value := range r.Request.Form {
length := len(value)
if length == 1 {
params[key] = value[0]
} else {
params[key] = value
}
}
}
func (r *Request) makeBodyParams(params utils.Q) {
//if (r.Request.Method == "PUT" || r.Request.Method == "POST") {
if r.Request.ContentLength != 0 {
utils.DecodeStream(r.Request.Body, ¶ms)
}
}
func (r *Request) GetArguments() *utils.Q {
if r.params == nil {
params := utils.Q{}
r.makeUrlArgs(params)
r.makeBodyParams(params)
r.makeUrlParams(params)
r.params = ¶ms
}
return r.params
}
func (r *Request) ConvertArguments(f interface{}) {
utils.Convert(r.GetArguments(), f)
}
func (r *Request) GetArgument(key string) interface{} {
args := *r.GetArguments()
return args[key]
}
func (r *Request) ConvertArgument(key string, f interface{}) {
args := *r.GetArguments()
val := args[key]
utils.Convert(val, f)
}
func (r *Request) Finish(data interface{}) []byte {
r.Writer.Header().Set("Server", serverUA)
r.Writer.Header().Set("Access-Control-Allow-Origin", "*")
r.Writer.Header().Set("Content-Type", "application/json")
return utils.Encoder(data)
}
func (r *Request) Redirect(url string, status int) {
log.Println("Redirecting to", url)
http.Redirect(r.Writer, r.Request, url, status)
return
}
func (r *Request) Write(data interface{}) {
var piped utils.Q
if v, ok := data.(WireSender); ok {
piped = v.SendOverWire()
} else {
piped = utils.Q{
"data": data,
"status": http.StatusOK,
"message": "",
}
}
if r.pipeOutput != nil {
piped["index"] = r.pipeIndex
r.pipeOutput <- &piped
} else if strings.Contains(
r.Request.Header.Get("Accept-Encoding"), "deflate",
) {
r.Writer.Header().Set("Content-Encoding", "deflate")
gz := zlib.NewWriter(r.Writer)
defer gz.Close()
gz.Write(r.Finish(piped))
} else if strings.Contains(
r.Request.Header.Get("Accept-Encoding"), "gzip",
) {
r.Writer.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(r.Writer)
defer gz.Close()
gz.Write(r.Finish(piped))
} else {
r.Writer.Write(r.Finish(piped))
}
}
func (r *Request) Raise(e HttpError) {
r.Write(e)
}
func performRequest(handler Handler, p *Request) {
method := (*p).Request.Method
switch method {
case "GET":
handler.Get(p)
case "POST":
handler.Post(p)
case "PUT":
handler.Put(p)
case "DELETE":
handler.Delete(p)
case "HEAD":
handler.Head(p)
case "OPTIONS":
handler.Options(p)
case "PATCH":
handler.Patch(p)
default:
log.Println("Unsupported method", method)
}
}
func doRequest(request *Request, availableUrls *[]*Url) {
requestUrl := request.Request.URL.Path
defer Tracer.Notify(getTracerExtra(request))
for _, url := range *availableUrls {
urlArgs, err := url.MakeUrlArgs(&requestUrl)
if !err {
request.UrlArgs = urlArgs
performRequest(url.handler, request)
return
}
}
e := HttpError{404, requestUrl + " did not match any exposed urls."}
request.Raise(e)
return
}