Prev: Next: Up: Simple Proxy[Contents][Index]


4.2 Request modifications

A service can modify requests before forwarding them to backends. Several statements are provided for that purpose:

SetHeader

Set a HTTP header.

DeleteHeader

Delete a HTTP header.

SetURL

Rewrite the request URL.

SetPath

Rewrite the path part of the URL.

SetQuery

Rewrite the query part of the URL.

SetQueryParam

Set a single query parameter.

For example, the following service declaration will add the header ‘X-Resent-By: pound’ to each request:

Service
    SetHeader "X-Resent-By: pound"
    Backend
       ...
    End
End

Arguments to request modification statements are expanded before actual use. During expansion, references to parenthesized subexpressions in matching rules are replaced with their actual values. Parenthesized subexpression is a part of a regular expression enclosed in parentheses. It can be referred to in string arguments as ‘$n’, where n is its ordinal number. Numbers start at one, ‘$0’ referring to the entire string that matched.

The process of expanding parenthesized subexpressions is called backreference expansion.

For example, the following condition:

Header "Content-Type: ([^/]+)/(.+)$"

has two subexpressions: ‘$1’ and ‘$2’. The following fragment uses these values to add two query parameters to the URL:

SetQueryParam "type" "$1"
SetQueryParam "subtype" "$2"

As a more practical example, the following service rewrites the path to JPEG and GIF images:

Service
    Path "/([^/]+\\.(jpg|gif))$"
    SetPath "/images/$1"
    ...
End

When several matching statements are used, these forms refer to the last matched one. Subexpressions in prior statements can be referred to using the ‘$i(j)’ construct. Here, j is either the tag of the expression to refer to (if the statement was tagged, see tag), or the 0-based number of the statement, counted from the last one upwards. For example, consider the following statements:

Host -tag "host" -re "(www\\.)?(.+)"
Header -tag "type" -icase "^Content-Type:[[:space:]]*(([^/]+)/([^;]+)).*"
Path -tag "dir" "^/static(/.*)?"

If the incoming request was ‘GET /static/index.html’ with the two headers

Host: example.org
Content-Type: text/html

then, the backreferences and the corresponding strings will be:

$1 ⇒ /index.html
$1(dir) ⇒ /index.html
$1(1) ⇒ text/html
$2(1) ⇒ text
$3(1) ⇒ html
$1(type) ⇒ text/html
$2(type) ⇒ text
$3(type) ⇒ html
$1(2) ⇒ www.
$2(2) ⇒ example.org
$1(host) ⇒ www.
$2(host) ⇒ example.org

String arguments to Set statements can also contain request accessors – special constructs that are expanded to particular values from the request. Syntactically, a request accessor is ‘%[name]’, where name denotes the request part to access. For example, %[url] expands to entire URL, %[path] to the path part of the URL, etc.

Using request accessors, the above example of path modification can be rewritten as:

Path "\\.(jpg|gif)$"
SetPath "/images%[path]"

See Request Accessor Interpretation, for a detailed discussions of available accessors.


Prev: Next: Up: Simple Proxy[Contents][Index]