Skip to content

Commit

Permalink
Adds request method and host template placeholders (#1716)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Feb 22, 2021
1 parent 3b229ab commit fbde0f7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/reference/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions eskip/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
15 changes: 15 additions & 0 deletions eskip/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fbde0f7

Please sign in to comment.