用途
该插件允许你通过预先定义的规则来修改不同的headers。改造自Apache HTTPD的mod_rewrite模块。
源码目录
trafficserver-5.3.2/plugins/header_rewrite
用法(Using the plugin)
既可以用作全局插件(作用于全局请求),也可以用作remap插件(作用于匹配的特定请求)
全局插件用法:
在plugin.config中添加
header_rewrite.so config_file_1.conf config_file_2.conf ...
如果没有绝对路径,将会默认在配置文件目录下查找,比如/usr/local/etc/trafficserver
remap插件用法:
在remap.config中添加
map http://a http://b @plugin=header_rewrite.so @pparam=rules1.conf ...
在READ_RESPONSE_HDR_HOOK阶段会读取配置文件信息,
操作(Operators)
下面的操作(增删改,计数)是可以使用的
rm-header header-name [operator_flags]
add-header header <value> [operator_flags]
set-header header <value> [operator_flags]
set-status <status-code> [operator_flags]
set-destination [qual] <value> [operator_flags]
set-redirect <status-code> <destination> [operator_flags]
set-timeout-out <value> [operator_flags]
set-status-reason <value> [operator_flags]
set-config overridable-config <value> [operator_flags]
set-conn-dscp <value> [operator_flags]
counter counter-name [operator_flags]
no-op [operator_flags]
这里qual是支持的URL修饰符之一:
HOSTPORTPATHQUERYSCHEMEURL
比如(作为一条remap规则)
cond %{HEADER:X-Mobile} = "foo"
set-destination HOST foo.mobile.bar.com [L]
操作符标志(Operator flags)
操作符标志是可选的,方括号内一定不能包含空格,当前,只支持一种标志
[L] Last rule, do not continue
[QSA] Append query string
变长表达式(variable expansion)
在add-header中当前只支持有限个数的变长表达式,支持的替换(substitutions)包括:
%<proto> Protocol
%<port> Port
%<chi> Client IP
%<cqhl> Client request length
%<cqhm> Client HTTP method
%<cquup> Client unmapped URI
条件(Condition)
这些条件被用作修饰词:只有条件满足时,指定的操作符才会被评估(evaluated)
cond %{RANDOM:nn} operand [condition_flags]
cond %{ACCESS:file} [condition_flags]
cond %{TRUE} [condition_flags]
cond %{FALSE} [condition_flags]
cond %{HEADER:header-name} operand [condition_flags]
cond %{COOKIE:cookie-name} operand [condition_flags]
cond %{CLIENT-HEADER:header-name} operand [condition_flags]
cond %{PROTOCOL} operand [condition_flags]
cond %{HOST} operand [condition_flags]
cond %{TOHOST} operand [condition_flags]
cond %{FROMHOST} operand [condition_flags]
cond %{PATH} operand [condition_flags]
cond %{QUERY} operand [condition_flags]
cond %{INTERNAL-TRANSACTION} [condition_flags]
cond %{CLIENT-IP} [condition_flags]
cond %{INCOMING-PORT} [condition_flags]
cond %{METHOD} [condition_flags]
这里,HEADER和CLIENT-HEADER的区别是,HEADER会遵从它运行的hook,但是CLIENT-HEADER总是应用于client request header。假如没有其它条件指定,%{TRUE}条件也是默认的条件。
这些条件必须首先在规则集ruleset中,每条rule必须只有一个condition,这意味着,一个新hook condition也是从一个新rule开始
cond %{STATUS} operand [condition_flags]
cond %{READ_RESPONSE_HDR_HOOK} (this is the default hook)
cond %{READ_REQUEST_HDR_HOOK}
cond %{READ_REQUEST_PRE_REMAP_HOOK}
cond %{SEND_REQUEST_HDR_HOOK}
cond %{SEND_RESPONSE_HDR_HOOK}
对remap.config插件的例子,默认的hook名为REMAP_PSEUDO_HOOK,假如你在一个配置文件中将其它钩子也混进来,这将会很有用,但它默认是可选的。
条件标志(Condition flags)
条件标志是可选的,你能够将多个合并为一个逗号分隔的flags,注意方括号内部不允许有空格:
[NC] Not case sensitive condition (when applicable) [NOT IMPLEMENTED!]
[AND] AND with next condition (default)
[OR] OR with next condition
[NOT] Invert this condition
分别是,条件大小写不敏感,两条件做和,或,取反运算。
条件操作(Operands to conditions)
/string/ # regular expression
<string # lexically lower
>string # lexically greater
=string # lexically equal
缺少matcher意味着有值存在
值(Values)
比如给一个header设置一个value可以采用下面的格式:
任何一条从请求中提取值的条件定义
正则表达式的分组,$N 0 <= N <= 9
上面取得的字符串
空值null
示例(Example)
cond %{HEADER:X-Y-Foobar}
cond %{COOKIE:X-DC} =DC1
add-header X-Y-Fiefum %{HEADER:X-Y-Foobar}
add-header X-Forwarded-For %<chi>
rm-header X-Y-Foobar
rm-header Set-Cookie
counter plugin.header_rewrite.x-y-foobar-dc1
cond %{HEADER:X-Y-Foobar} "Some string" [AND,NC]
源码解读
该插件文件很多,但是核心文件是header_rewrite.cc,结构比较清楚,其它文件基本是在处理大部分的业务逻辑。这里并没有按照每个请求的整个处理流程来添加钩子,而是打乱了,集中设置,具体是设置哪些钩子,需要由配置来限定,比较有新意。
RulesConfig类是综合所有业务逻辑文件,同时又是作为变换的私有数据,非常重要。
参考文献