/
mapping.go
176 lines (145 loc) · 4.38 KB
/
mapping.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
package mapping
import (
"errors"
"fmt"
"net/http"
"path"
"regexp"
"strings"
"github.com/visola/variables/variables"
)
// Mapping represents a mapping that can be active or not
type Mapping struct {
Active bool `json:"active"`
Before string `json:"before"`
From string `json:"from"`
Inject Injection `json:"injection"`
MappingID string `json:"mappingID"`
Origin string `json:"origin"`
Proxy bool `json:"proxy"`
Regexp string `json:"regexp"`
Tags []string `json:"tags"`
To string `json:"to"`
}
// Injection represents parameters that can be injected into proxied requests
type Injection struct {
Headers map[string]string `json:"headers"`
}
// MatchResult stores the result for a mapping that matched a request
type MatchResult struct {
Mapping Mapping
NewPath string
Parts []string
}
// GetVariables returns all the variables set in a mapping
func (mapping *Mapping) GetVariables() []variables.Variable {
result := make([]variables.Variable, 0)
result = append(result, variables.FindVariables(mapping.From)...)
result = append(result, variables.FindVariables(mapping.To)...)
for _, v := range mapping.Inject.Headers {
result = append(result, variables.FindVariables(v)...)
}
return result
}
// Match tests if the mapping matches the specific request. If it does, it will
// return a match result with the new path where to go. Otherwise it will return nil.
func (mapping *Mapping) Match(req *http.Request) *MatchResult {
if mapping.From != "" {
return mapping.matchWithFrom(req)
}
if mapping.Regexp != "" {
return mapping.matchWithRegexp(req)
}
return nil
}
// Validate makes sure that the a mapping is correctly setup
func (mapping *Mapping) Validate() error {
if mapping.From == "" && mapping.Regexp == "" {
return errors.New("Either `from` or `regexp` need to be present")
}
if mapping.To == "" {
return errors.New("Missing value for `to`")
}
if mapping.Regexp != "" {
_, err := regexp.Compile(mapping.Regexp)
if err != nil {
return fmt.Errorf("Error compiling regexp: '%s'\n%s", mapping.Regexp, err)
}
}
if len(mapping.Inject.Headers) > 0 && !mapping.Proxy {
return errors.New("Inject is only available for proxy mappings")
}
return nil
}
// WithReplacedVariables creates a new mapping with all its variables replaced by the
// values passed in the context
func (mapping Mapping) WithReplacedVariables(context map[string]string) Mapping {
result := Mapping{
Active: mapping.Active,
Before: mapping.Before,
From: mapping.From,
MappingID: mapping.MappingID,
Origin: mapping.Origin,
Proxy: mapping.Proxy,
Regexp: mapping.Regexp,
Tags: mapping.Tags,
To: mapping.To,
}
result.From = variables.ReplaceVariables(result.From, context)
result.To = variables.ReplaceVariables(result.To, context)
inject := Injection{Headers: make(map[string]string)}
result.Inject = inject
for k, v := range mapping.Inject.Headers {
inject.Headers[k] = variables.ReplaceVariables(v, context)
}
return result
}
func (mapping *Mapping) matchForProxyWithFrom(req *http.Request) *MatchResult {
newPath := req.URL.Path[len(mapping.From):]
if strings.HasPrefix(newPath, "/") {
newPath = mapping.To + newPath
} else {
newPath = mapping.To + "/" + newPath
}
return &MatchResult{
Mapping: *mapping,
NewPath: newPath,
Parts: []string{req.URL.Path},
}
}
func (mapping *Mapping) matchForStaticWithFrom(req *http.Request) *MatchResult {
return &MatchResult{
Mapping: *mapping,
NewPath: path.Join(mapping.To, req.URL.Path[len(mapping.From):]),
Parts: []string{req.URL.Path},
}
}
func (mapping *Mapping) matchWithFrom(req *http.Request) *MatchResult {
if !strings.HasPrefix(req.URL.Path, mapping.From) {
return nil
}
if mapping.Proxy {
return mapping.matchForProxyWithFrom(req)
}
return mapping.matchForStaticWithFrom(req)
}
func (mapping *Mapping) matchWithRegexp(req *http.Request) *MatchResult {
r, err := regexp.Compile(mapping.Regexp)
if err != nil {
// Assume validation errors will be exposed some other way
return nil
}
matched := r.FindStringSubmatch(req.URL.Path)
if len(matched) > 0 {
newPath := mapping.To
for index, part := range matched[1:] {
newPath = strings.Replace(newPath, fmt.Sprintf("$%d", index+1), part, -1)
}
return &MatchResult{
Mapping: *mapping,
NewPath: newPath,
Parts: matched,
}
}
return nil
}