-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
htransformation.go
80 lines (66 loc) · 2.15 KB
/
htransformation.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
package htransformation
import (
"context"
"fmt"
"net/http"
"github.com/tomMoulard/htransformation/pkg/handler/deleter"
"github.com/tomMoulard/htransformation/pkg/handler/join"
"github.com/tomMoulard/htransformation/pkg/handler/rename"
"github.com/tomMoulard/htransformation/pkg/handler/rewrite"
"github.com/tomMoulard/htransformation/pkg/handler/set"
"github.com/tomMoulard/htransformation/pkg/types"
)
// HeadersTransformation holds the necessary components of a Traefik plugin.
type HeadersTransformation struct {
name string
next http.Handler
handlers []types.Handler
}
// Config holds configuration to be passed to the plugin.
type Config struct {
Rules []types.Rule
}
// CreateConfig populates the Config data object.
func CreateConfig() *Config {
return &Config{
Rules: []types.Rule{},
}
}
// New instantiates and returns the required components used to handle an HTTP request.
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
handlerBuilder := map[types.RuleType]func(types.Rule) (types.Handler, error){
types.Delete: deleter.New,
types.Join: join.New,
types.Rename: rename.New,
types.RewriteValueRule: rewrite.New,
types.Set: set.New,
}
handlers := make([]types.Handler, 0, len(config.Rules))
for _, rule := range config.Rules {
newHandler, ok := handlerBuilder[rule.Type]
if !ok {
return nil, fmt.Errorf("%w: %s", types.ErrInvalidRuleType, rule.Name)
}
h, err := newHandler(rule)
if err != nil {
return nil, fmt.Errorf("%w: %s", err, rule.Name)
}
if err := h.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", err, rule.Name)
}
handlers = append(handlers, h)
}
return &HeadersTransformation{
name: name,
next: next,
handlers: handlers,
}, nil
}
// Iterate over every header to match the ones specified in the config and
// return nothing if regexp failed.
func (u *HeadersTransformation) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
for _, handler := range u.handlers {
handler.Handle(responseWriter, request)
}
u.next.ServeHTTP(responseWriter, request)
}