全部產品
Search
文件中心

Edge Security Acceleration:函數

更新時間:May 14, 2025

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 Stringsubstring 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

  • 處理邏輯:將與Regex匹配的源字串的一部分替換為替換字串,並返回字串結果。替換字串可以包含對Regex擷取的群組的引用,例如:${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"

    • 通過將Regex的一部分放在括弧中來建立擷取的群組。然後在替換字串中使用${<NUMBER>}引用一個擷取的群組,其中<NUMBER>是擷取的群組的編號。

starts_with

字串開始於。

  • 格式:starts_with(source String,substrina String):Boolean

  • 處理邏輯:

    • source Stringsubstring 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

  • 處理邏輯:返回IntegerBooleanIP地址值的字串表示形式。

  • 樣本:

    • 假如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(因為運算式包含了值為sflags參數,表明啟用了區分大小寫匹配模式,在這個模式下,萬用字元運算式沒有匹配到原始URI路徑,所以返回結果不變)。

    • 如果URI路徑為/apps/calendar/login,運算式wildcard_replace(http.request.uri.path,"/apps/*/login","/${1}/login")將返回/calendar/login