2018-11-14 09:18:03 +00:00
|
|
|
package stripprefixregex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2019-09-03 18:32:03 +00:00
|
|
|
"regexp"
|
2018-11-14 09:18:03 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/middlewares"
|
|
|
|
"github.com/containous/traefik/v2/pkg/middlewares/stripprefix"
|
|
|
|
"github.com/containous/traefik/v2/pkg/tracing"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeName = "StripPrefixRegex"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StripPrefixRegex is a middleware used to strip prefix from an URL request.
|
|
|
|
type stripPrefixRegex struct {
|
2019-09-03 18:32:03 +00:00
|
|
|
next http.Handler
|
|
|
|
expressions []*regexp.Regexp
|
|
|
|
name string
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New builds a new StripPrefixRegex middleware.
|
2019-07-10 07:26:04 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, config dynamic.StripPrefixRegex, name string) (http.Handler, error) {
|
2018-11-14 09:18:03 +00:00
|
|
|
middlewares.GetLogger(ctx, name, typeName).Debug("Creating middleware")
|
|
|
|
|
|
|
|
stripPrefix := stripPrefixRegex{
|
2019-09-03 18:32:03 +00:00
|
|
|
next: next,
|
|
|
|
name: name,
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:32:03 +00:00
|
|
|
for _, exp := range config.Regex {
|
|
|
|
reg, err := regexp.Compile(strings.TrimSpace(exp))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stripPrefix.expressions = append(stripPrefix.expressions, reg)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &stripPrefix, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stripPrefixRegex) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return s.name, tracing.SpanKindNoneEnum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stripPrefixRegex) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2019-09-03 18:32:03 +00:00
|
|
|
for _, exp := range s.expressions {
|
|
|
|
parts := exp.FindStringSubmatch(req.URL.Path)
|
|
|
|
if len(parts) > 0 && len(parts[0]) > 0 {
|
|
|
|
prefix := parts[0]
|
|
|
|
if !strings.HasPrefix(req.URL.Path, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-09-03 18:32:03 +00:00
|
|
|
req.Header.Add(stripprefix.ForwardedPrefixHeader, prefix)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-09-03 18:32:03 +00:00
|
|
|
req.URL.Path = strings.Replace(req.URL.Path, prefix, "", 1)
|
|
|
|
if req.URL.RawPath != "" {
|
|
|
|
req.URL.RawPath = req.URL.RawPath[len(prefix):]
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-09-03 18:32:03 +00:00
|
|
|
req.RequestURI = ensureLeadingSlash(req.URL.RequestURI())
|
|
|
|
s.next.ServeHTTP(rw, req)
|
|
|
|
return
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2019-09-03 18:32:03 +00:00
|
|
|
|
|
|
|
s.next.ServeHTTP(rw, req)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ensureLeadingSlash(str string) string {
|
|
|
|
return "/" + strings.TrimPrefix(str, "/")
|
|
|
|
}
|