ESA的规则语言提供了用于操作和验证表达式中值的函数,其中包含了转换函数,转换函数用于操作从 HTTP 请求中提取的值。
规则语言支持的转换函数
concat
字符串拼接。
格式:
concat(String | Integer | Bytes | Array elements):String
。处理逻辑:函数的操作对象是一个采用逗号分隔的值列表,作用是将所有的参数值连接到一个字符串中。
示例:
concat("String1"," ","String",2)
将会返回"String1 String2"
。concat("/archive",http.request.uri.path)
,在URI的路径前面插入一段新的路径"/archive"
。concat("userid=123&",http.request.uri.query)
,在URI的查询字符串前面插入一个新的键值对"userid=123"
。concat("https://www.example.com",http.request.uri.path)
,将新的主机名与原始请求中的路径拼接在一起,生成一个新的URL。
ends_with
字符串结束于。
格式:
ends_with(source String,substring String):Boolean
。处理逻辑:
当
source String
以substring String
结尾时返回true
。否则返回false
。source String
必须是字段,不能直接使用固定字符串。例如:不能直接写成ends_with("foo.html",".html")
。
示例:如果
http.request.uri.path
是"/welcome.html"
,则ends_with(http.request.uri.path,".html")
返回true
。
lower
字符串小写转换。
格式:
lower(String):String
处理逻辑:将字段输出的字符串转换为小写,仅转换大写ASCII字符,所有其他字符不受影响。
示例:如果
http.host
是"WWW.example.com"
,则lower(http.host)=="www.example.com"
返回true
。
regex_replace
正则替换。
格式:
regex_replace(source String,regular_expression String,replacement String):String
。处理逻辑:将与正则表达式匹配的源字符串的一部分替换为替换字符串,并返回字符串结果。替换字符串可以包含对正则表达式捕获组的引用,例如:
${1}
和${2}
,最多八个替换引用。示例:
字符匹配替换:
regex_replace("/animal/cat","/cat$","/dog")=="/animal/dog"
。假如没有匹配上,源字符串不会有变化:
regex_replace("/x","^/y$","/z")=="/x"
。默认情况下,匹配区分大小写:
regex_replace("/cat","^/CAT$","/dog")=="/cat"
。当有多个匹配时,只会发生一个替换(第一个):
regex_replace("/a/a","/a","/b")=="/b/a"
。可以在替换字符串中的$符号前加上另一个
$
符号来进行转义:regex_replace("/b","^/b$","/b$$")=="/b$"
。替换为捕获组:
regex_replace("/foo/a/path","^/foo/([^/]*)/(.*)$","/bar/${2}/${1}")=="/bar/path/a"
。通过将正则表达式的一部分放在括号中来创建捕获组。然后在替换字符串中使用
${<NUMBER>}
引用一个捕获组,其中<NUMBER>
是捕获组的编号。
starts_with
字符串开始于。
格式:
starts_with(source String,substrina String):Boolean
处理逻辑:
当
source String
以substring String
开头时返回true
。否则返回false
。source String
必须是字段,不能直接使用固定字符串。例如:不能直接写成starts_with("foo.html",".html")
。
示例:如果
http.request.uri.path
是"/welcome.html"
,则starts_with(http.request.uri.path,".html")
返回true
。
to_string
字符串输出。
格式:
to_string(Integer | Boolean | IP address):String
。处理逻辑:返回
Integer
、Boolean
或IP
地址值的字符串表示形式。示例:
假如
ip.src.asnum
的结果是15169
,那么to_string(ip.src.asnum)
将会返回"15169"
。假如
ssl
的结果是true
,那么to_string(ssl)
将会返回"true"
。假如
ip.src
的结果是192.168.0.1
,那么to_string(ip.src)
将会范围"192.168.0.1"
。
upper
字符串大写转换。
格式:
upper(String):String
。处理逻辑:将字段输出的字符串转换为大写,仅转换小写ASCII字符,所有其他字节不受影响。
示例:如果
http.host
是"www.example.com"
,则upper(http.host)
返回"WWW.EXAMPLE.COM"
。
wildcard_replace
字符串通配符匹配。
格式:
wildcard_replace(source Bytes,wildcard_pattern Bytes,replacement Bytes,flags Bytes optional):String
处理逻辑:
将零个或多个
*
通配符的源字符文字与source字符串匹配替换为替换字符串并返回结果。替换字符串可以包含对通配符捕获组的引用,例如:${1}
和${2}
,最多八个替换引用。如果不匹配,函数将返回source字符串的原始内容。
source参数必须是字段(不能是文字字符串)。此外,整个source值必须与
wildcard_pattern
参数匹配(它不能只匹配字段值的一部分)。要在
wildcard_pattern
参数中输入文字*
字符,必须使用\*
转义它。此外,您还必须使用\\
对\
进行转义。此参数中一行**
中的两个未转义的*
字符被视为无效,无法使用。如果需要执行字符转义,建议对wildcard_pattern
参数使用原始字符串语法。如果要在
replacement
参数中输入文字$
字符,必须使用$$
转义它。如果要执行区分大小写的通配符匹配,请将
flags
参数设置为s
。此函数使用延迟匹配,即它试图将每个*元字符与尽可能短的字符串进行匹配。
示例:
如果完整URI为
https://apps.example.com/calendar/admin?expand=true
,表达式wildcard_replace(http.request.full_uri,"https://*.example.com/*/*","https://example.com/${1}/${2}/${3}")
将返回https://example.com/apps/calendar/admin?expand=true
。如果完整URI为
https://example.com/applications/app1
,表达式wildcard_replace(http.request.full_uri,"/applications/*","/apps/${1}")
将返回https://example.com/applications/app1
(值不变,因为通配符表达式没有与完整URI匹配,您应该使用http.request.uri.path
字段进行URI路径匹配)。如果URI路径为
/calendar
,表达式wildcard_replace(http.request.uri.path,"/*","/apps/${1}")
将返回/apps/calendar
。如果URI路径为
/Apps/calendar
,表达式wildcard_replace(http.request.uri.path,"/apps/*","/${1}")
将返回/calendar
(默认情况下不区分大小写匹配)。如果URI路径为
/Apps/calendar
,表达式wildcard_replace(http.request.uri.path,"/apps/*","/${1}","s")
将返回/Apps/calendar
(因为表达式包含了值为s
的flags
参数,表明启用了区分大小写的匹配模式,在这个模式下,通配符表达式没有匹配到原始URI路径,所以返回结果不变)。如果URI路径为
/apps/calendar/login
,表达式wildcard_replace(http.request.uri.path,"/apps/*/login","/${1}/login")
将返回/calendar/login
。