From 0d56a98836582b7e9265b6bf462bed4c66cabb83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Thu, 24 Aug 2017 19:28:03 +0100 Subject: [PATCH] Add support for Query String filtering --- docs/basics.md | 1 + server/rules.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/docs/basics.md b/docs/basics.md index 3f8bdbbb9..0094065f9 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -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. 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]+}`. diff --git a/server/rules.go b/server/rules.go index 04468ff12..18444ad74 100644 --- a/server/rules.go +++ b/server/rules.go @@ -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, @@ -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 {