2012年11月30日 星期五

TrafficScript的基本文字運算


官方的教學文章中提到許多文字操作的的範例,相當具有參考價值,這邊擷取精華部分呈現:

使用RE的範例:
$id ="user=Joe, password=Secret";
if(string.regexmatch( $id,"^user=(.*), password=(.*)$")){
   log.info("Got UserID: ".$1.", Password: ".$2 );
}

文字大寫寫轉換:
$string ="AbCdEfG"; $upper =string.toUpper( $string );# returns "ABCDEFG" $lower =string.toLower( $string );# returns "abcdefg" HTML文件標籤與特殊字元encode: $xss ="<script>alert('Hello!');</script>"; 結果:"&lt;script&gt;alert('Hello!');&lt;/script&gt;" $safe =string.htmlencode( $xss );

文字中跳脫字元轉換:
$str =string.escape("Hello World!\r\n"); 結果:"Hello%20World!%0D%0A"
使用HTTP BASIC authentication方式加密帳密: $enc =string.base64encode("user:passwd"); http.setHeader("Authorization","Basic ".$enc );
HEX加解密: string.hexEncode() string.hexDecode(). AES cipher加解密: $encrypted =string.encrypt( $plaintext, $passphrase ); $plaintext =string.decrypt( $encrypted, $passphrase );