[resending as the original reply bounced]




At Feb 19, 2015, 1:07:14 AM, Sudheer Vinukonda wrote:
Yeah, there's really no hook named "READ_REQUEST_HOOK", so, it probably becomes a no-op (adding the correct hook name READ_REQUEST_HDR_HOOK will cause it not to work, since the read request state is not available on a remap mode). To avoid confusion, I would simply remove that line.

As for the logic, yes you are absolutely correct. The rules basically are two fold. One to check for the necessary Query param and add a header in the client request and the second check for that specific client request header to add the required header in the outgoing client response. One little neat thing to note here is that the header added to the client request is an @ header, which means it never goes out to the origin. It is only added internally within ATS (Without the @ prefix the header QueryPresent in the client request will actually go out to the origin as well, which is unnecessary).

Thanks,

Sudheer



At Feb 19, 2015, 12:42:44 AM, Ezequiel Foncubierta wrote:Hi,

It works even with the READ_REQUEST_HOOK! Kudos to you sir. Just to understand what is going on as there is not much documentation about this.

1. We are adding a header in the client request header if the query parameter is present.
cond %{QUERY} /(^|&)a=true(&|$)/
add-header @QueryPresent TRUE [L]

2. Check wether the flag (@QueryPresent) exists in the client request header.
cond %{SEND_RESPONSE_HDR_HOOK} [AND]
cond %{CLIENT-HEADER:@QueryPresent} =TRUE
add-header TestHeader 1234 [L]

is that correct?

Thank you so much!

Best regards.

On 18 Feb 2015, at 20:17, Sudheer Vinukonda sudheerv@yahoo-inc.com> wrote:

Sorry, the hook name READ_REQUEST_HOOK is incorrect - the below config is sufficient (as this is being added on a remap hook anyway).

% cat rules.config
cond %{QUERY} /(^|&)a=true(&|$)/
add-header @QueryPresent TRUE [L]
cond %{SEND_RESPONSE_HDR_HOOK} [AND]
cond %{CLIENT-HEADER:@QueryPresent} =TRUE
add-header TestHeader 1234 [L]

%cat remap.config
map http://cache.server:8080 http://web.server:8080 @plugin=header_rewrite.so @pparam=rules.config

From: Sudheer Vinukonda sudheerv@yahoo-inc.com>
To: "users@trafficserver.apache.org" users@trafficserver.apache.org>
Sent: Wednesday, February 18, 2015 10:50 AM
Subject: Re: Header rewrite rule based on query string

The below config seems to do what you need:

% cat rules.config
cond %{READ_REQUEST_HOOK} [AND]
cond %{QUERY} /(^|&)a=true(&|$)/
add-header @QueryPresent TRUE [L]
cond %{SEND_RESPONSE_HDR_HOOK} [AND]
cond %{CLIENT-HEADER:@QueryPresent} =TRUE
add-header TestHeader 1234 [L]

%cat remap.config
map http://cache.server:8080 http://web.server:8080 @plugin=header_rewrite.so @pparam=rules.config


Thanks,

Sudheer





From: Ezequiel Foncubierta ezequiel.foncubierta@gmail.com>
To: users@trafficserver.apache.org
Sent: Wednesday, February 18, 2015 6:35 AM
Subject: Header rewrite rule based on query string

Hi,

I am trying to rewrite the response header using the header_rewrite.so plugin.

My remap.config file:
map http://cache.server:8080 http://web.server:8080 @plugin=header_rewrite.so @pparam=rules.conf
reverse_map http://web.server:8080 http://cache.server:8080

My rules.conf file:
cond %{SEND_RESPONSE_HDR_HOOK}
cond %{QUERY} /(^|&)a=true(&|$)/
add-header TestHeader 1234

What I am expecting with this configuration so far is that if the request URL has a query parameter "a=true”, then the TestHeader 1234 will be added to the response header. However, I get the following message in the debug traces.

vagrant@cache:~/trafficserver$ sudo traffic_server -T"header"
traffic_server: using root directory '/usr/local'
traffic_server: using root directory '/usr/local'
[Feb 18 14:05:04.450] Server {0x7fd4f5454740} DIAG: (header_rewrite) Remap plugin is successfully initialized
[Feb 18 14:05:04.451] Server {0x7fd4f5454740} DIAG: (header_rewrite) Instantiating a new remap.config plugin rule
[Feb 18 14:05:04.451] Server {0x7fd4f5454740} DIAG: (header_rewrite) Loading remap configuration file rules.conf
[Feb 18 14:05:04.451] Server {0x7fd4f5454740} DIAG: (header_rewrite) Adding condition: %{QUERY} with arg: /(^|&)a=true(&|$)/
[Feb 18 14:05:04.452] Server {0x7fd4f5454740} DIAG: (header_rewrite) Regex precompiled successfully
[Feb 18 14:05:04.452] Server {0x7fd4f5454740} DIAG: (header_rewrite) Adding operator: add-header(TestHeader)
[Feb 18 14:05:04.452] Server {0x7fd4f5454740} DIAG: (header_rewrite) Succesfully loaded remap config file rules.conf
[Feb 18 14:05:04.453] Server {0x7fd4f5454740} DIAG: (header_rewrite) Adding remap ruleset to hook=TS_HTTP_SEND_RESPONSE_HDR_HOOK
[Feb 18 14:05:23.841] Server {0x7fd4f1819700} DIAG: (header_rewrite) Added remapped TXN hook=TS_HTTP_SEND_RESPONSE_HDR_HOOK
[Feb 18 14:05:23.841] Server {0x7fd4f1819700} DIAG: (header_rewrite) Building resources, hook=(null)
[Feb 18 14:05:23.841] Server {0x7fd4f1819700} DIAG: (header_rewrite) Adding TXN client request header buffers
[Feb 18 14:05:23.841] Server {0x7fd4f1819700} DIAG: (header_rewrite) Adding TXN client request header buffers for remap instance
[Feb 18 14:05:23.896] Server {0x7fd4f1819700} DIAG: (header_rewrite) Building resources, hook=TS_HTTP_SEND_RESPONSE_HDR_HOOK
[Feb 18 14:05:23.896] Server {0x7fd4f1819700} DIAG: (header_rewrite) Adding TXN client request header buffers
[Feb 18 14:05:23.896] Server {0x7fd4f1819700} DIAG: (header_rewrite) Adding TXN client response header buffers
[Feb 18 14:05:23.896] Server {0x7fd4f1819700} DIAG: (header_rewrite) QUERY requires remap initialization! Evaluating to false!

If I get rid of the SEND_RESPONSE_HDR_HOOK condition in rules.conf, I get the following traces:

vagrant@cache:~/trafficserver$ sudo traffic_server -T"header"
traffic_server: using root directory '/usr/local'
traffic_server: using root directory '/usr/local'
[Feb 18 14:09:49.061] Server {0x7f7d6d42f740} DIAG: (header_rewrite) Remap plugin is successfully initialized
[Feb 18 14:09:49.061] Server {0x7f7d6d42f740} DIAG: (header_rewrite) Instantiating a new remap.config plugin rule
[Feb 18 14:09:49.061] Server {0x7f7d6d42f740} DIAG: (header_rewrite) Loading remap configuration file rules.conf
[Feb 18 14:09:49.061] Server {0x7f7d6d42f740} DIAG: (header_rewrite) Adding condition: %{QUERY} with arg: /(^|&)a=true(&|$)/
[Feb 18 14:09:49.061] Server {0x7f7d6d42f740} DIAG: (header_rewrite) Regex precompiled successfully
[Feb 18 14:09:49.062] Server {0x7f7d6d42f740} DIAG: (header_rewrite) Adding operator: add-header(TestHeader)
[Feb 18 14:09:49.062] Server {0x7f7d6d42f740} DIAG: (header_rewrite) Succesfully loaded remap config file rules.conf
[Feb 18 14:09:55.967] Server {0x7f7d697f4700} DIAG: (header_rewrite) Building resources, hook=(null)
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) Adding TXN client request header buffers
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) Adding TXN client request header buffers for remap instance
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) Appending QUERY to evaluation value: a=true
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) Evaluating QUERY - a=true
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) Test regular expression (^|&)a=true(&|$) : a=true
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) Successfully found regular expression match
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) OperatorAddHeader::exec() invoked on header TestHeader: 1234
[Feb 18 14:09:55.968] Server {0x7f7d697f4700} DIAG: (header_rewrite) Adding header TestHeader

The regular expression is working as it is expected for the QUERY condition but, obviously, the TestHeader is added to the HTTP request from cache.server to web.server instead of the HTTP response from cache.server to the client. On the other hand, the PATH condition works nicely with the SEND_RESPONSE_HDR_HOOK condition.

I have tried many different configuration with no luck (i.e. setting the plugin in plugin.config). Any help would be really appreciated.

Thank you!

Best regards.