forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_context_vars.go
51 lines (39 loc) · 1.34 KB
/
mw_context_vars.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
package main
import (
"net/http"
"strings"
"github.com/satori/go.uuid"
"github.com/TykTechnologies/tyk/request"
)
type MiddlewareContextVars struct {
BaseMiddleware
}
func (m *MiddlewareContextVars) Name() string {
return "MiddlewareContextVars"
}
func (m *MiddlewareContextVars) EnabledForSpec() bool {
return m.Spec.EnableContextVars
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (m *MiddlewareContextVars) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
r.ParseForm()
contextDataObject := map[string]interface{}{
"request_data": r.Form, // Form params (map[string][]string)
"headers": map[string][]string(r.Header),
"headers_Host": r.Host,
"path_parts": strings.Split(r.URL.Path, "/"), // Path parts
"path": r.URL.Path, // path data
"remote_addr": request.RealIP(r), // IP
"request_id": uuid.NewV4().String(), //Correlation ID
}
for hname, vals := range r.Header {
n := "headers_" + strings.Replace(hname, "-", "_", -1)
contextDataObject[n] = vals[0]
}
for _, c := range r.Cookies() {
name := "cookies_" + strings.Replace(c.Name, "-", "_", -1)
contextDataObject[name] = c.Value
}
ctxSetData(r, contextDataObject)
return nil, http.StatusOK
}