/
query.go
141 lines (122 loc) · 3.61 KB
/
query.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
package builtin
import (
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
)
type modQueryBehavior int
const (
set modQueryBehavior = 1 + iota
drop
)
type modQuery struct {
behavior modQueryBehavior
name *eskip.Template
value *eskip.Template
}
// Returns a new dropQuery filter Spec, whose instances drop a corresponding
// query parameter.
//
// As an EXPERIMENTAL feature: the dropQuery filter provides the possiblity
// to apply template operations. The current solution supports templates
// with placeholders of the format: ${param1}, and the placeholders will
// be replaced with the values of the same name from the wildcards in the
// Path() predicate.
// The templating feature will stay in Skipper, but the syntax of the
// templating may change.
//
// See also: https://github.com/zalando/skipper/issues/182
//
// Name: "dropQuery".
func NewDropQuery() filters.Spec { return &modQuery{behavior: drop} }
// Returns a new setQuery filter Spec, whose instances replace
// the query parameters.
//
// As an EXPERIMENTAL feature: the setPath filter provides the possiblity
// to apply template operations. The current solution supports templates
// with placeholders of the format: ${param1}, and the placeholders will
// be replaced with the values of the same name from the wildcards in the
// Path() predicate.
//
// See: https://godoc.org/github.com/zalando/skipper/routing#hdr-Wildcards
//
// The templating feature will stay in Skipper, but the syntax of the
// templating may change.
//
// See also: https://github.com/zalando/skipper/issues/182
//
// Instances expect two parameters: the name and the value to be set, either
// strings or templates are valid.
//
// Name: "setQuery".
func NewSetQuery() filters.Spec { return &modQuery{behavior: set} }
// "setQuery" or "dropQuery"
func (spec *modQuery) Name() string {
switch spec.behavior {
case drop:
return DropQueryName
case set:
return SetQueryName
default:
panic("unspecified behavior")
}
}
func createDropQuery(config []interface{}) (filters.Filter, error) {
if len(config) != 1 {
return nil, filters.ErrInvalidFilterParameters
}
tpl, ok := config[0].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
return &modQuery{behavior: drop, name: eskip.NewTemplate(tpl)}, nil
}
func createSetQuery(config []interface{}) (filters.Filter, error) {
l := len(config)
if l < 1 || l > 2 {
return nil, filters.ErrInvalidFilterParameters
}
name, ok := config[0].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
if l == 1 {
return &modQuery{behavior: set, name: eskip.NewTemplate(name)}, nil
}
value, ok := config[1].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
return &modQuery{behavior: set, name: eskip.NewTemplate(name), value: eskip.NewTemplate(value)}, nil
}
// Creates instances of the modQuery filter.
func (spec *modQuery) CreateFilter(config []interface{}) (filters.Filter, error) {
switch spec.behavior {
case drop:
return createDropQuery(config)
case set:
return createSetQuery(config)
default:
panic("unspecified behavior")
}
}
// Modifies the query of a request.
func (f *modQuery) Request(ctx filters.FilterContext) {
req := ctx.Request()
params := req.URL.Query()
switch f.behavior {
case drop:
params.Del(f.name.Apply(ctx.PathParam))
case set:
if f.value == nil {
req.URL.RawQuery = f.name.Apply(ctx.PathParam)
return
} else {
params.Set(f.name.Apply(ctx.PathParam), f.value.Apply(ctx.PathParam))
}
default:
panic("unspecified behavior")
}
req.URL.RawQuery = params.Encode()
}
// Noop.
func (*modQuery) Response(filters.FilterContext) {}