-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Add PathStripRegex rule #1339
Add PathStripRegex rule #1339
Conversation
@seguins thanks for your contribution. I think we should avoid adding a dependency to |
I think we can create an interface for the matching, and move the type PrefixMatcher interface {
Match(r *http.Request) (bool, string)
} The package middlewares
import (
"net/http"
)
type PrefixMatcher interface {
Match(r *http.Request) (bool, string)
}
// StripPrefix is a middleware used to strip prefix from an URL request
type StripPrefix struct {
Handler http.Handler
matcher PrefixMatcher
}
// NewStripPrefix builds a new StripPrefix given a handler and prefixes
func NewStripPrefix(handler http.Handler, matcher PrefixMatcher) *StripPrefix {
stripPrefix := StripPrefix{Handler: handler, matcher: matcher}
return &stripPrefix
}
func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if match, prefix := s.matcher.Match(r); match == true {
r.URL.Path = r.URL.Path[len(prefix):]
r.RequestURI = r.URL.RequestURI()
s.Handler.ServeHTTP(w, r)
return
}
http.NotFound(w, r)
}
// SetHandler sets handler
func (s *StripPrefix) SetHandler(Handler http.Handler) {
s.Handler = Handler
} For you information, I have checked other files in middlewares, and I found that handlerSwitcher.go and routes.go already use @emilevauge do you think we should do this change ? |
@seguins |
966f3c3
to
f513f9b
Compare
@emilevauge, I've pushed a version with the regex matching separated. |
Great job @seguins! Could you fix this https://travis-ci.org/containous/traefik/jobs/226814163#L827 ? Thanks. |
f513f9b
to
7ed4d8e
Compare
Fixed 😃 |
7ed4d8e
to
bf36738
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @seguins, awesome job :)
LGTM
Fixes #928