From fbde0f7d267224fcd74abf8132678b387d47fa70 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Mon, 22 Feb 2021 11:01:37 +0100 Subject: [PATCH] Adds request method and host template placeholders (#1716) Signed-off-by: Alexander Yastrebov --- docs/reference/filters.md | 4 +++- eskip/template.go | 4 ++++ eskip/template_test.go | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/reference/filters.md b/docs/reference/filters.md index 424b4ce9bd..42e8ac2863 100644 --- a/docs/reference/filters.md +++ b/docs/reference/filters.md @@ -22,11 +22,13 @@ Several filters support template placeholders (`${var}`) in string parameters. Template placeholder is replaced by the value that is looked up in the following sources: +* request method (`${request.method}`) +* request host (`${request.host}`) * request url path (`${request.path}`) * request url query (if starts with `request.query.` prefix, e.g `${request.query.q}` is replaced by `q` query parameter value) * request headers (if starts with `request.header.` prefix, e.g `${request.header.Content-Type}` is replaced by `Content-Type` request header value) * request cookies (if starts with `request.cookie.` prefix, e.g `${request.cookie.PHPSESSID}` is replaced by `PHPSESSID` request cookie value) -* request IP address +* request IP address - `${request.source}` - first IP address from `X-Forwarded-For` header or request remote IP address if header is absent, similar to [Source](predicates.md#source) predicate - `${request.sourceFromLast}` - last IP address from `X-Forwarded-For` header or request remote IP address if header is absent, similar to [SourceFromLast](predicates.md#sourcefromlast) predicate - `${request.clientIP}` - request remote IP address similar to [ClientIP](predicates.md#clientip) predicate diff --git a/eskip/template.go b/eskip/template.go index 0f375a24de..209d61f056 100644 --- a/eskip/template.go +++ b/eskip/template.go @@ -75,6 +75,10 @@ func (t *Template) ApplyContext(ctx TemplateContext) (string, bool) { return "" } switch key { + case "request.method": + return ctx.Request().Method + case "request.host": + return ctx.Request().Host case "request.path": return ctx.Request().URL.Path case "request.source": diff --git a/eskip/template_test.go b/eskip/template_test.go index f19c344cc2..02de07262b 100644 --- a/eskip/template_test.go +++ b/eskip/template_test.go @@ -72,6 +72,13 @@ func TestTemplateApplyContext(t *testing.T) { } return u } + request := func(method, url string) *http.Request { + r, err := http.NewRequest(method, url, nil) + if err != nil { + t.Fatal(err) + } + return r + } for _, ti := range []struct { name string @@ -327,6 +334,14 @@ func TestTemplateApplyContext(t *testing.T) { }, "192.168.0.1", true, + }, { + "request method host", + "${request.method} ${request.host}", + &filtertest.Context{ + FRequest: request("GET", "https://example.com/test/1"), + }, + "GET example.com", + true, }, } { t.Run(ti.name, func(t *testing.T) {