-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
response_modifier.go
51 lines (41 loc) · 1.25 KB
/
response_modifier.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
package responsemodifiers
import (
"context"
"net/http"
"github.com/containous/traefik/pkg/config/runtime"
)
// NewBuilder creates a builder.
func NewBuilder(configs map[string]*runtime.MiddlewareInfo) *Builder {
return &Builder{configs: configs}
}
// Builder holds builder configuration.
type Builder struct {
configs map[string]*runtime.MiddlewareInfo
}
// Build Builds the response modifier.
func (f *Builder) Build(ctx context.Context, names []string) func(*http.Response) error {
var modifiers []func(*http.Response) error
for _, middleName := range names {
if conf, ok := f.configs[middleName]; ok {
if conf.Headers != nil {
getLogger(ctx, middleName, "Headers").Debug("Creating Middleware (ResponseModifier)")
modifiers = append(modifiers, buildHeaders(conf.Headers))
} else if conf.Chain != nil {
getLogger(ctx, middleName, "Chain").Debug("Creating Middleware (ResponseModifier)")
modifiers = append(modifiers, f.Build(ctx, conf.Chain.Middlewares))
}
}
}
if len(modifiers) > 0 {
return func(resp *http.Response) error {
for i := len(modifiers); i > 0; i-- {
err := modifiers[i-1](resp)
if err != nil {
return err
}
}
return nil
}
}
return func(response *http.Response) error { return nil }
}