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

Add support for Query String filtering #1934

Merged
merged 2 commits into from
Aug 24, 2017
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
1 change: 1 addition & 0 deletions docs/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Following is the list of existing matcher rules along with examples:
- `PathPrefix: /products/, /articles/{category}/{id:[0-9]+}`: Match request prefix path. It accepts a sequence of literal and regular expression prefix paths.
- `PathPrefixStrip: /products/`: Match request prefix path and strip off the path prefix prior to forwarding the request to the backend. It accepts a sequence of literal prefix paths. Starting with Traefik 1.3, the stripped prefix path will be available in the `X-Forwarded-Prefix` header.
- `PathPrefixStripRegex: /articles/{category}/{id:[0-9]+}`: Match request prefix path and strip off the path prefix prior to forwarding the request to the backend. It accepts a sequence of literal and regular expression prefix paths. Starting with Traefik 1.3, the stripped prefix path will be available in the `X-Forwarded-Prefix` header.
- `Query: foo=bar, bar=baz`: Match Query String parameters. It accepts a sequence of key=value pairs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now it doesn't. Currently it splits the = sign and analyzes each part. I can do a PR in the future supporting Regex but i need to figure out how.

Copy link

@ztl8702 ztl8702 Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it does.
I just tested: Query: a={a:[0-1]+} will match http://foobar/?a=1 but not http://foobar/?a=2 for example. Exactly as Juliens pointed out: such Regex syntax is implemented in the underlying mux package, by just splitting the = sign driverpt's commit actually "unintentionally" enables this feature. (But I am saying this is a good thing. It worked for my use case. ) https://github.com/containous/mux/blob/06ccd3e75091eb659b1d720cda0e16bc7057954c/route.go#L387

Should update the docs I guess?


In order to use regular expressions with Host and Path matchers, you must declare an arbitrarily named variable followed by the colon-separated regular expression, all enclosed in curly braces. Any pattern supported by [Go's regexp package](https://golang.org/pkg/regexp/) may be used. Example: `/posts/{id:[0-9]+}`.

Expand Down
10 changes: 10 additions & 0 deletions server/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ func (r *Rules) headersRegexp(headers ...string) *mux.Route {
return r.route.route.HeadersRegexp(headers...)
}

func (r *Rules) query(query ...string) *mux.Route {
var queries []string
for _, elem := range query {
queries = append(queries, strings.Split(elem, "=")...)
}

return r.route.route.Queries(queries...)
}

func (r *Rules) parseRules(expression string, onRule func(functionName string, function interface{}, arguments []string) error) error {
functions := map[string]interface{}{
"Host": r.host,
Expand All @@ -146,6 +155,7 @@ func (r *Rules) parseRules(expression string, onRule func(functionName string, f
"HeadersRegexp": r.headersRegexp,
"AddPrefix": r.addPrefix,
"ReplacePath": r.replacePath,
"Query": r.query,
}

if len(expression) == 0 {
Expand Down