/
headertoquery.go
63 lines (50 loc) · 1.48 KB
/
headertoquery.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
package builtin
import (
"github.com/zalando/skipper/filters"
)
type (
headerToQuerySpec struct {
}
headerToQueryFilter struct {
headerName string
queryParamName string
}
)
// NewHeaderToQuery creates a filter which converts the headers
// from the incoming Request to query params
//
// headerToQuery("X-Foo-Header", "foo-query-param")
//
// The above filter will set the "foo-query-param" query param
// to the value of "X-Foo-Header" header, to the request
// and will override the value if the queryparam exists already
func NewHeaderToQuery() filters.Spec {
return &headerToQuerySpec{}
}
func (*headerToQuerySpec) Name() string {
return filters.HeaderToQueryName
}
// CreateFilter creates a `headerToQuery` filter instance with below signature
// s.CreateFilter("X-Foo-Header", "foo-query-param")
func (*headerToQuerySpec) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) != 2 {
return nil, filters.ErrInvalidFilterParameters
}
h, ok := args[0].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
q, ok := args[1].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
return &headerToQueryFilter{h, q}, nil
}
func (f *headerToQueryFilter) Request(ctx filters.FilterContext) {
req := ctx.Request()
params := req.URL.Query()
headerValue := req.Header.Get(f.headerName)
params.Set(f.queryParamName, headerValue)
req.URL.RawQuery = params.Encode()
}
func (*headerToQueryFilter) Response(ctx filters.FilterContext) {}