Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stripPrefix and stripPrefixRegex. #5291

Merged
merged 5 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 6 additions & 13 deletions docs/content/middlewares/stripprefixregex.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,50 @@
Removing Prefixes From the Path Before Forwarding the Request (Using a Regex)
{: .subtitle }

`TODO: add schema`

Remove the matching prefixes from the URL path.

## Configuration Examples

```yaml tab="Docker"
# Replace the path by /foo
labels:
- "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=^/foo/(.*)",
- "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=/foo/[a-z0-9]+/[0-9]+/",
```

```yaml tab="Kubernetes"
# Replace the path by /foo
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: test-stripprefixregex
spec:
stripPrefixRegex:
regex:
- "^/foo/(.*)"
- "/foo/[a-z0-9]+/[0-9]+/"
```

```json tab="Marathon"
"labels": {
"traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex": "^/foo/(.*)"
"traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex": "/foo/[a-z0-9]+/[0-9]+/"
}
```

```yaml tab="Rancher"
# Replace the path by /foo
labels:
- "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=^/foo/(.*)",
- "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=/foo/[a-z0-9]+/[0-9]+/",
```

```toml tab="File (TOML)"
# Replace the path by /foo
[http.middlewares]
[http.middlewares.test-stripprefixregex.stripPrefixRegex]
regex = ["^/foo/(.*)"]
regex = ["/foo/[a-z0-9]+/[0-9]+/"]
```

```yaml tab="File (YAML)"
# Replace the path by /foo
http:
middlewares:
test-stripprefixregex:
stripPrefixRegex:
regex:
- "^/foo/(.*)"
- "/foo/[a-z0-9]+/[0-9]+/"
```

## Configuration Options
Expand Down
4 changes: 2 additions & 2 deletions integration/fixtures/https/https_redirect.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@
[http.middlewares.foo-slash-add-prefix.addPrefix]
prefix = "/foo/"
[http.middlewares.id-strip-regex-prefix.stripPrefixRegex]
regex = ["/{id:[a-z]+}"]
regex = ["/[a-z]+"]
[http.middlewares.id-slash-strip-regex-prefix.stripPrefixRegex]
regex = ["/{id:[a-z]+}/"]
regex = ["/[a-z]+/"]
[http.middlewares.api-regex-replace.replacePathRegex]
regex = "/api"
replacement = "/"
Expand Down
2 changes: 1 addition & 1 deletion pkg/middlewares/stripprefix/strip_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *stripPrefix) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
}
http.NotFound(rw, req)
s.next.ServeHTTP(rw, req)
}

func (s *stripPrefix) serveRequest(rw http.ResponseWriter, req *http.Request, prefix string) {
Expand Down
8 changes: 6 additions & 2 deletions pkg/middlewares/stripprefix/strip_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func TestStripPrefix(t *testing.T) {
Prefixes: []string{},
},
path: "/noprefixes",
expectedStatusCode: http.StatusNotFound,
expectedStatusCode: http.StatusOK,
expectedPath: "/noprefixes",
},
{
desc: "wildcard (.*) requests",
Expand Down Expand Up @@ -76,7 +77,8 @@ func TestStripPrefix(t *testing.T) {
Prefixes: []string{"/stat/"},
},
path: "/status",
expectedStatusCode: http.StatusNotFound,
expectedStatusCode: http.StatusOK,
expectedPath: "/status",
},
{
desc: "general prefix on matching path",
Expand Down Expand Up @@ -149,6 +151,8 @@ func TestStripPrefix(t *testing.T) {
require.NoError(t, err)

req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
req.RequestURI = req.URL.RequestURI()

resp := &httptest.ResponseRecorder{Code: http.StatusOK}

handler.ServeHTTP(resp, req)
Expand Down
61 changes: 30 additions & 31 deletions pkg/middlewares/stripprefixregex/strip_prefix_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package stripprefixregex
import (
"context"
"net/http"
"regexp"
"strings"

"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"
"github.com/gorilla/mux"
"github.com/opentracing/opentracing-go/ext"
)

Expand All @@ -19,23 +19,26 @@ const (

// StripPrefixRegex is a middleware used to strip prefix from an URL request.
type stripPrefixRegex struct {
next http.Handler
router *mux.Router
name string
next http.Handler
expressions []*regexp.Regexp
name string
}

// New builds a new StripPrefixRegex middleware.
func New(ctx context.Context, next http.Handler, config dynamic.StripPrefixRegex, name string) (http.Handler, error) {
middlewares.GetLogger(ctx, name, typeName).Debug("Creating middleware")

stripPrefix := stripPrefixRegex{
next: next,
router: mux.NewRouter(),
name: name,
next: next,
name: name,
}

for _, prefix := range config.Regex {
stripPrefix.router.PathPrefix(prefix)
for _, exp := range config.Regex {
reg, err := regexp.Compile(strings.TrimSpace(exp))
if err != nil {
return nil, err
}
stripPrefix.expressions = append(stripPrefix.expressions, reg)
}

return &stripPrefix, nil
Expand All @@ -46,32 +49,28 @@ func (s *stripPrefixRegex) GetTracingInformation() (string, ext.SpanKindEnum) {
}

func (s *stripPrefixRegex) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
var match mux.RouteMatch
if s.router.Match(req, &match) {
params := make([]string, 0, len(match.Vars)*2)
for key, val := range match.Vars {
params = append(params, key)
params = append(params, val)
}
for _, exp := range s.expressions {
parts := exp.FindStringSubmatch(req.URL.Path)
if len(parts) > 0 && len(parts[0]) > 0 {
prefix := parts[0]
ldez marked this conversation as resolved.
Show resolved Hide resolved
if !strings.HasPrefix(req.URL.Path, prefix) {
continue
}

prefix, err := match.Route.URL(params...)
if err != nil || len(prefix.Path) > len(req.URL.Path) {
logger := middlewares.GetLogger(req.Context(), s.name, typeName)
logger.Error("Error in stripPrefix middleware", err)
return
}
req.Header.Add(stripprefix.ForwardedPrefixHeader, prefix)

req.URL.Path = req.URL.Path[len(prefix.Path):]
if req.URL.RawPath != "" {
req.URL.RawPath = req.URL.RawPath[len(prefix.Path):]
}
req.Header.Add(stripprefix.ForwardedPrefixHeader, prefix.Path)
req.RequestURI = ensureLeadingSlash(req.URL.RequestURI())
req.URL.Path = strings.Replace(req.URL.Path, prefix, "", 1)
if req.URL.RawPath != "" {
req.URL.RawPath = req.URL.RawPath[len(prefix):]
}

s.next.ServeHTTP(rw, req)
return
req.RequestURI = ensureLeadingSlash(req.URL.RequestURI())
s.next.ServeHTTP(rw, req)
return
}
}
http.NotFound(rw, req)

s.next.ServeHTTP(rw, req)
}

func ensureLeadingSlash(str string) string {
Expand Down
13 changes: 10 additions & 3 deletions pkg/middlewares/stripprefixregex/strip_prefix_regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestStripPrefixRegex(t *testing.T) {
testPrefixRegex := dynamic.StripPrefixRegex{
Regex: []string{"/a/api/", "/b/{regex}/", "/c/{category}/{id:[0-9]+}/"},
Regex: []string{"/a/api/", "/b/([a-z0-9]+)/", "/c/[a-z0-9]+/[0-9]+/"},
}

testCases := []struct {
Expand All @@ -27,7 +27,13 @@ func TestStripPrefixRegex(t *testing.T) {
}{
{
path: "/a/test",
expectedStatusCode: http.StatusNotFound,
expectedStatusCode: http.StatusOK,
expectedPath: "/a/test",
},
{
path: "/a/test",
expectedStatusCode: http.StatusOK,
expectedPath: "/a/test",
},
{
path: "/a/api/test",
Expand Down Expand Up @@ -65,7 +71,8 @@ func TestStripPrefixRegex(t *testing.T) {
},
{
path: "/c/api/abc/test4",
expectedStatusCode: http.StatusNotFound,
expectedStatusCode: http.StatusOK,
expectedPath: "/c/api/abc/test4",
},
{
path: "/a/api/a%2Fb",
Expand Down