2018-11-14 09:18:03 +00:00
|
|
|
package responsemodifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/runtime"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewBuilder creates a builder.
|
2019-07-15 15:04:04 +00:00
|
|
|
func NewBuilder(configs map[string]*runtime.MiddlewareInfo) *Builder {
|
2018-11-14 09:18:03 +00:00
|
|
|
return &Builder{configs: configs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builder holds builder configuration.
|
|
|
|
type Builder struct {
|
2019-07-15 15:04:04 +00:00
|
|
|
configs map[string]*runtime.MiddlewareInfo
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
|
|
|
}
|