AScript provides six logical functions for building conditions in forwarding rules: and(), or(), not(), eq(), ne(), and null().
Quick reference
| Function | Signature | Returns | Short-circuit |
|---|---|---|---|
and | and(arg, ...) → Boolean | true if all args are true | Stops at first false |
or | or(arg, ...) → Boolean | true if any arg is true | Stops at first true |
not | not(arg) → Boolean | Inverts truthiness of arg | N/A |
eq | eq(arg1, arg2) → Boolean | true if arg1 equals arg2 | N/A |
ne | ne(arg1, arg2) → Boolean | true if arg1 does not equal arg2 | N/A |
null | null(v) → Boolean | true if v is empty (array, dict, or empty string) | N/A |
and
| Item | Description |
|---|---|
| Syntax | and(arg, ...) → Boolean |
| Description | Executes the AND logical operator. Supports short-circuit evaluation: when any argument evaluates to false, the remaining arguments are not evaluated. |
| Parameter | One or more values to evaluate. Data type: any type. |
| Return value | true if all arguments evaluate to true. false if any argument evaluates to false. |
| Example | See below. |
if and($arg_mode, eq($arg_mode, 'set_header')) {
add_rsp_header('USER-DEFINED-1','path1')
}If the request includes the
modeparameter and its value isset_header, theUSER-DEFINED-1response header is set.If the request does not include the
modeparameter, short-circuit evaluation stops execution. Theeqcomparison is not run, andUSER-DEFINED-1is not set becauseand()returnsfalse.
or
| Item | Description |
|---|---|
| Syntax | or(arg, ...) → Boolean |
| Description | Executes the OR logical operator. Supports short-circuit evaluation: when any argument evaluates to true, the remaining arguments are not evaluated. |
| Parameter | One or more values to evaluate. Data type: any type. |
| Return value | true if any argument evaluates to true. false if all arguments evaluate to false. |
| Example | See below. |
if and($http_from, or(eq($http_from, 'wap'), eq($http_from, 'comos'))) {
rewrite(concat('http://tech.com.cn/zt_d/we2015/', $http_from), 'enhance_redirect')
}If the request includes the
fromheader and its value iswaporcomos, the HTTP 302 redirect returns and the request is redirected tohttp://tech.com.cn/zt_d/we2015/[wap|comos].If the
fromheader value iswap, short-circuit evaluation stops theeq('comos')check.or()returnstrueimmediately.
not
| Item | Description |
|---|---|
| Syntax | not(arg) → Boolean |
| Description | Executes the NOT logical operator. undef and false evaluate to false; all other values evaluate to true. |
| Parameter | Exactly one value to evaluate. Data type: any type. |
| Return value | true if the argument is falsy (undef or false). false if the argument is truthy. |
| Example | See below. |
Boundary behavior:
| Input | not() result |
|---|---|
undef (variable not present) | true |
false | true |
0 | false |
if not($arg_key) {
exit(403)
}
if not($cookie_user) {
exit(403, 'not cookie user')
}
if not(0) {
exit(403)
}
if not(false) {
exit(403)
}not($arg_key): if the request does not include thekeyparameter, the request is denied and HTTP 403 is returned.not($cookie_user): if the request does not includecookie_user, the request is denied. HTTP 403 and the response bodynot cookie userare returned.not(0)returnsfalse.not(false)returnstrue.
eq
| Item | Description |
|---|---|
| Syntax | eq(arg1, arg2) → Boolean |
| Description | Checks whether two values are equal. |
| Parameters | arg1: the first value. Data type: any type. arg2: the second value. Data type: same as arg1. |
| Return value | true if the two values are equal. false if they are not equal. |
| Example | See below. |
key1 = 'value1'
key2 = 'value2'
if and($arg_k1, $arg_k2, eq(key1, $arg_k1), ne(key2, $arg_k2)) {
say('match condition')
}If the request includes both the
k1andk2parameters, the comparison operations run.If the request does not include
k1ork2, short-circuit evaluation stops the comparisons.eq(key1, $arg_k1): checks whether the value ofk1equalsvalue1.ne(key2, $arg_k2): checks whether the value ofk2does not equalvalue2.The response body
match conditionis returned only when the request includes bothk1andk2, the value ofk1equalsvalue1, and the value ofk2does not equalvalue2.
ne
| Item | Description |
|---|---|
| Syntax | ne(arg1, arg2) → Boolean |
| Description | Checks whether two values are not equal. |
| Parameters | arg1: the first value. Data type: any type. arg2: the second value. Data type: same as arg1. |
| Return value | true if the two values are not equal. false if they are equal. |
| Example | See below. |
key1 = 'value1'
key2 = 'value2'
if and($arg_k1, $arg_k2, eq(key1, $arg_k1), ne(key2, $arg_k2)) {
say('match condition')
}If the request includes both the
k1andk2parameters, the comparison operations run.If the request does not include
k1ork2, short-circuit evaluation stops the comparisons.eq(key1, $arg_k1): checks whether the value ofk1equalsvalue1.ne(key2, $arg_k2): checks whether the value ofk2does not equalvalue2.The response body
match conditionis returned only when the request includes bothk1andk2, the value ofk1equalsvalue1, and the value ofk2does not equalvalue2.
null
| Item | Description |
|---|---|
| Syntax | null(v) → Boolean |
| Description | Checks whether a value is empty. Returns true for an empty array, empty dictionary, or empty string. Returns false for all other types and non-empty values. |
| Parameter | v: the value to check. Data type: array, dictionary, or string. Returns false for other data types. |
| Return value | Data type: Boolean. See the boundary behavior table below. |
| Example | See below. |
Boundary behavior:
| Input | null() result |
|---|---|
Empty array ([]) | true |
| Non-empty array | false |
Empty dictionary ({}) | true |
| Non-empty dictionary | false |
Empty string ('') | true |
| Non-empty string | false |
| Other data types | false |
d = []
say(null(d))
set(d, 1, 'v1')
say(null(d))
say(tostring(null('x')))
say(tostring(null('')))Output:
true
false
false
true