-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
446 lines (396 loc) · 11.8 KB
/
handler.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package ensweb
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/EnsurityTechnologies/helper/jsonutil"
)
// bufferedReader can be used to replace a request body with a buffered
// version. The Close method invokes the original Closer.
type bufferedReader struct {
*bufio.Reader
rOrig io.ReadCloser
}
func newBufferedReader(r io.ReadCloser) *bufferedReader {
return &bufferedReader{
Reader: bufio.NewReader(r),
rOrig: r,
}
}
func (b *bufferedReader) Close() error {
return b.rOrig.Close()
}
func parseQuery(values url.Values) map[string]interface{} {
data := map[string]interface{}{}
for k, v := range values {
// Skip the help key as this is a reserved parameter
if k == "help" {
continue
}
switch {
case len(v) == 0:
case len(v) == 1:
data[k] = v[0]
default:
data[k] = v
}
}
if len(data) > 0 {
return data
}
return nil
}
// isForm tries to determine whether the request should be
// processed as a form or as JSON.
//
// Virtually all existing use cases have assumed processing as JSON,
// and there has not been a Content-Type requirement in the API. In order to
// maintain backwards compatibility, this will err on the side of JSON.
// The request will be considered a form only if:
//
// 1. The content type is "application/x-www-form-urlencoded"
// 2. The start of the request doesn't look like JSON. For this test we
// we expect the body to begin with { or [, ignoring leading whitespace.
func isForm(head []byte, contentType string) bool {
contentType, _, err := mime.ParseMediaType(contentType)
if err != nil || contentType != "application/x-www-form-urlencoded" {
return false
}
// Look for the start of JSON or not-JSON, skipping any insignificant
// whitespace (per https://tools.ietf.org/html/rfc7159#section-2).
for _, c := range head {
switch c {
case ' ', '\t', '\n', '\r':
continue
case '[', '{': // JSON
return false
default: // not JSON
return true
}
}
return true
}
func parseJSONRequest(secondary bool, r *http.Request, w http.ResponseWriter, out interface{}) (io.ReadCloser, error) {
reader := r.Body
ctx := r.Context()
maxRequestSize := ctx.Value("max_request_size")
if maxRequestSize != nil {
max, ok := maxRequestSize.(int64)
if !ok {
return nil, errors.New("could not parse max_request_size from request context")
}
if max > 0 {
reader = http.MaxBytesReader(w, r.Body, max)
}
}
var origBody io.ReadWriter
if secondary {
// Since we're checking PerfStandby here we key on origBody being nil
// or not later, so we need to always allocate so it's non-nil
origBody = new(bytes.Buffer)
reader = ioutil.NopCloser(io.TeeReader(reader, origBody))
}
err := jsonutil.DecodeJSONFromReader(reader, out)
if err != nil && err != io.EOF {
return nil, err
}
if origBody != nil {
return ioutil.NopCloser(origBody), err
} else {
reader.Close()
}
return nil, err
}
// parseFormRequest parses values from a form POST.
//
// A nil map will be returned if the format is empty or invalid.
func parseFormRequest(r *http.Request) (map[string]interface{}, error) {
maxRequestSize := r.Context().Value("max_request_size")
if maxRequestSize != nil {
max, ok := maxRequestSize.(int64)
if !ok {
return nil, errors.New("could not parse max_request_size from request context")
}
if max > 0 {
r.Body = ioutil.NopCloser(io.LimitReader(r.Body, max))
}
}
if err := r.ParseForm(); err != nil {
return nil, err
}
var data map[string]interface{}
if len(r.PostForm) != 0 {
data = make(map[string]interface{}, len(r.PostForm))
for k, v := range r.PostForm {
switch len(v) {
case 0:
case 1:
data[k] = v[0]
default:
// Almost anywhere taking in a string list can take in comma
// separated values, and really this is super niche anyways
data[k] = strings.Join(v, ",")
}
}
}
return data, nil
}
func basicPreflightHandleFunc(s *Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := basicRequestFunc(s, w, r)
if s.debugMode {
s.enableCors(&req.w)
}
req.w.Header().Set("Content-Type", "application/json")
res := &Result{
Status: http.StatusOK,
Done: true,
}
req.w.WriteHeader(http.StatusOK)
if res != nil && s.auditLog != nil {
timeDuration := time.Now().Nanosecond() - req.TimeIn.Nanosecond()
userAgent := r.Header.Get("User-Agent")
if res.Done {
s.auditLog.Info("HTTP request processed", "Path", req.Path, "IP Address", req.Connection.RemoteAddr, "Status", res.Status, "Duration", timeDuration, "User-Agent", userAgent)
} else {
s.auditLog.Error("HTTP request failed", "Path", req.Path, "IP Address", req.Connection.RemoteAddr, "Duration", timeDuration, "User-Agent", userAgent)
}
}
})
}
func basicHandleFunc(s *Server, hf HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := basicRequestFunc(s, w, r)
var res *Result
errAuth := false
if s.secureAPI && req.Path != GetPublicKeyAPI {
licenkey := s.GetReqHeader(req, LicenseKeyHdr)
if licenkey != s.licenseKey {
errAuth = true
s.log.Error("invaid license", "exp", s.licenseKey, "recv", licenkey)
res = s.RenderJSONError(req, http.StatusUnauthorized, "invalid license key", "invalid license key")
}
if !errAuth {
var rid RequestID
err := decryptModel(req.ss, req.redID, &rid)
if err != nil {
errAuth = true
res = s.RenderJSONError(req, http.StatusUnauthorized, "failed to decrypt the request ID", "failed to decrypt the request ID", "err", err)
}
// ::TODO:: validate request time
}
}
if !errAuth {
res = hf(req)
}
if res != nil && s.auditLog != nil {
timeDuration := time.Now().Nanosecond() - req.TimeIn.Nanosecond()
userAgent := r.Header.Get("User-Agent")
if res.Done {
s.auditLog.Info("HTTP request processed", "Path", req.Path, "IP Address", req.Connection.RemoteAddr, "Status", res.Status, "Duration", timeDuration, "User-Agent", userAgent)
} else {
s.auditLog.Error("HTTP request failed", "Path", req.Path, "IP Address", req.Connection.RemoteAddr, "Duration", timeDuration, "User-Agent", userAgent)
}
}
})
}
func indexRoute(s *Server, dirPath string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs := http.FileServer(http.Dir(dirPath))
// If the requested file exists then return if; otherwise return index.html (fileserver default page)
if r.URL.Path != "/" {
r.URL.Path = strings.TrimPrefix(r.URL.Path, s.prefixPath)
fullPath := dirPath + r.URL.Path
_, err := os.Stat(fullPath)
if err != nil {
if !os.IsNotExist(err) {
panic(err)
}
fmt.Printf("Not Found : %s\n", r.URL.Path)
// Requested file does not exist so we return the default (resolves to index.html)
r.URL.Path = "/"
}
}
if s.debugMode {
s.enableCors(&w)
}
fs.ServeHTTP(w, r)
})
}
func (s *Server) AddExtension(fileExtension string, fileType string) {
mime.AddExtensionType(fileExtension, fileType)
}
func (s *Server) IsFORM(req *Request) (bool, error) {
bufferedBody := newBufferedReader(req.r.Body)
req.r.Body = bufferedBody
head, err := bufferedBody.Peek(512)
if err != nil && err != bufio.ErrBufferFull && err != io.EOF {
return false, fmt.Errorf("error reading data")
}
if isForm(head, req.r.Header.Get("Content-Type")) {
return true, nil
}
return false, nil
}
func (s *Server) ParseJSON(req *Request, model interface{}) error {
var err error
if s.secureAPI {
var sd SecureData
_, err = parseJSONRequest(false, req.r, req.w, &sd)
if err != nil {
return err
}
req.sd = sd.Data
return decryptModel(req.ss, sd.Data, model)
} else {
_, err = parseJSONRequest(false, req.r, req.w, model)
}
return err
}
func (s *Server) ParseFORM(req *Request) (map[string]interface{}, error) {
formData, err := parseFormRequest(req.r)
if err != nil {
return nil, fmt.Errorf("error parsing form data")
}
return formData, nil
}
func (s *Server) GetQuerry(req *Request, key string) string {
return req.r.URL.Query().Get(key)
}
func (s *Server) ParseMultiPartForm(req *Request, dirPath string) ([]string, map[string][]string, error) {
mediatype, _, err := mime.ParseMediaType(req.r.Header.Get("Content-Type"))
if err != nil {
return nil, nil, err
}
if mediatype != "multipart/form-data" {
return nil, nil, fmt.Errorf("invalid content type")
}
defer req.r.Body.Close()
req.r.ParseMultipartForm(52428800)
paramFiles := make([]string, 0)
paramTexts := make(map[string][]string)
for k, v := range req.r.MultipartForm.Value {
paramTexts[k] = append(paramTexts[k], v...)
}
for k, _ := range req.r.MultipartForm.File {
file, fileHeader, err := req.r.FormFile(k)
if err != nil {
return nil, nil, fmt.Errorf("invalid form file")
}
localFileName := dirPath + fileHeader.Filename
out, err := os.OpenFile(localFileName, os.O_CREATE|os.O_RDWR, 0777)
if err != nil {
file.Close()
return nil, nil, fmt.Errorf("faile to open file")
}
_, err = io.Copy(out, file)
if err != nil {
file.Close()
out.Close()
return nil, nil, fmt.Errorf("faile to copy file")
}
out.Close()
file.Close()
paramFiles = append(paramFiles, localFileName)
}
return paramFiles, paramTexts, nil
}
func (s *Server) Redirect(req *Request, url string) *Result {
r := req.r
w := req.w
http.Redirect(w, r, url, http.StatusSeeOther)
return &Result{
Status: http.StatusSeeOther,
Done: true,
}
}
func (s *Server) ServerStatic(req *Request) *Result {
r := req.r
w := req.w
fs := http.FileServer(http.Dir(s.rootDir))
// If the requested file exists then return if; otherwise return index.html (fileserver default page)
if r.URL.Path != "/" {
r.URL.Path = strings.TrimPrefix(r.URL.Path, s.prefixPath)
fullPath := s.rootDir + r.URL.Path
_, err := os.Stat(fullPath)
if err != nil {
if !os.IsNotExist(err) {
panic(err)
}
fmt.Println("Not Found : ", r.URL.Path)
// Requested file does not exist so we return the default (resolves to index.html)
r.URL.Path = "/"
}
}
fs.ServeHTTP(w, r)
res := &Result{
Status: http.StatusOK,
Done: true,
}
return res
}
// func (s *Server) ParseMultiPartForm(req *Request, dirPath string) ([]string, map[string]string, error) {
// mediatype, params, err := mime.ParseMediaType(req.r.Header.Get("Content-Type"))
// if err != nil {
// return nil, nil, err
// }
// if mediatype != "multipart/form-data" {
// return nil, nil, fmt.Errorf("invalid content type")
// }
// defer req.r.Body.Close()
// mr := multipart.NewReader(req.r.Body, params["boundary"])
// paramFiles := make([]string, 0)
// paramTexts := make(map[string]string)
// for {
// part, err := mr.NextPart()
// if err != nil {
// if err != io.EOF { //io.EOF error means reading is complete
// return paramFiles, paramTexts, fmt.Errorf(" error reading multipart request: %+v", err)
// }
// break
// }
// if part.FileName() != "" {
// chunk := make([]byte, 4096)
// f, err := os.OpenFile(dirPath+part.FileName(), os.O_WRONLY|os.O_CREATE, 0666)
// if err != nil {
// return paramFiles, paramTexts, fmt.Errorf("error in creating file %+v", err)
// }
// for {
// n, err := part.Read(chunk)
// if err != nil {
// if err != io.EOF {
// return paramFiles, paramTexts, fmt.Errorf(" error reading multipart file %+v", err)
// }
// if n > 0 {
// f.Write(chunk[:n])
// }
// break
// } else {
// if n > 0 {
// f.Write(chunk[:n])
// }
// }
// }
// f.Close()
// if err != nil {
// return paramFiles, paramTexts, fmt.Errorf("error reading file param %+v", err)
// }
// paramFiles = append(paramFiles, dirPath+part.FileName())
// } else {
// name := part.FormName()
// buf := new(bytes.Buffer)
// buf.ReadFrom(part)
// paramTexts[name] = buf.String()
// }
// }
// return paramFiles, paramTexts, nil
// }