forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_context_vars.go
68 lines (48 loc) · 1.76 KB
/
middleware_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"net/http"
"strings"
"github.com/gorilla/context"
)
type MiddlewareContextVars struct {
*TykMiddleware
}
type MiddlewareContextVarsConfig struct{}
// New lets you do any initialisations for the object can be done here
func (m *MiddlewareContextVars) New() {}
func (m *MiddlewareContextVars) GetName() string {
return "MiddlewareContextVars"
}
// GetConfig retrieves the configuration from the API config - we user mapstructure for this for simplicity
func (m *MiddlewareContextVars) GetConfig() (interface{}, error) {
var moduleConfig MiddlewareContextVarsConfig
return moduleConfig, nil
}
func (m *MiddlewareContextVars) IsEnabledForSpec() 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, configuration interface{}) (error, int) {
if !m.Spec.EnableContextVars {
return nil, 200
}
copiedRequest := CopyHttpRequest(r)
contextDataObject := make(map[string]interface{})
copiedRequest.ParseForm()
// Form params (map[string][]string)
contextDataObject["request_data"] = copiedRequest.Form
contextDataObject["headers"] = map[string][]string(copiedRequest.Header)
for hname, vals := range copiedRequest.Header {
n := "headers_" + strings.Replace(hname, "-", "_", -1)
contextDataObject[n] = vals[0]
}
// Path parts
segmentedPathArray := strings.Split(copiedRequest.URL.Path, "/")
contextDataObject["path_parts"] = segmentedPathArray
// path data
contextDataObject["path"] = copiedRequest.URL.Path
// IP:Port
contextDataObject["remote_addr"] = copiedRequest.RemoteAddr
context.Set(r, ContextData, contextDataObject)
return nil, 200
}