EdgeScript (ES) provides six logical functions for building conditional expressions in CDN rules: and, or, not, eq, ne, and null. The three Boolean operators (and, or, not) support short-circuit evaluation, so the engine stops evaluating arguments as soon as the result is determined.
Function summary
| Function | Signature | Short-circuit | Returns |
|---|---|---|---|
and | and(arg, ...) : Boolean | Stops on first false | true if all args are true |
or | or(arg, ...) : Boolean | Stops on first true | true if any arg is true |
not | not(arg) : Boolean | — | Inverted Boolean |
eq | eq(arg1, arg2) : Boolean | — | true if equal |
ne | ne(arg1, arg2) : Boolean | — | true if not equal |
null | null(v) : Boolean | — | true if v is an empty string, empty array, or empty dictionary |
and
Signature: and(arg, ...) : Boolean
Executes the AND logical operator. When any argument evaluates to false, short-circuit evaluation stops and the remaining arguments are skipped.
Parameters: One or more values of any type.
Return value: true if all values evaluate to true; false if any value evaluates to false.
Example: Set a response header only when a specific query parameter is present and matches a given value.
if and($arg_mode, eq($arg_mode, 'set_header')) {
add_rsp_header('USER-DEFINED-1','path1')
}If the request includes
mode=set_header,and()evaluates both conditions and returnstrue. TheUSER-DEFINED-1response header is set.If the request has no
modeparameter,$arg_modeevaluates tofalse. Short-circuit evaluation stops —eq()is never called — and the response header is not set.
or
Signature: or(arg, ...) : Boolean
Executes the OR logical operator. When any argument evaluates to true, short-circuit evaluation stops and the remaining arguments are skipped.
Parameters: One or more values of any type.
Return value: true if any value evaluates to true; false if all values evaluate to false.
Example: Redirect requests based on the value of the From header.
if and($http_from, or(eq($http_from, 'wap'), eq($http_from, 'comos'))) {
rewrite(concat('http://example.com.cn/zt_d/we2015/', $http_from), 'enhance_redirect')
}If the
Fromheader iswaporcomos, the request is redirected (HTTP 302) tohttp://example.com.cn/zt_d/we2015/[wap|comos].If the
Fromheader iswap,or()returnstrueimmediately — thecomoscheck is skipped.
not
Signature: not(arg) : Boolean
Executes the NOT logical operator. The values of undef and false evaluate to false, and other values evaluate to true.
Parameter: Exactly one value of any type.
Return value: true or false.
Example: Block requests that are missing required parameters or cookies.
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 has nokeyquery parameter,$arg_keyisundef.not(undef)returnstrue, and the request exits with HTTP 403.not($cookie_user)— if the request has nocookie_usercookie,$cookie_userisundef.not(undef)returnstrue, and the request exits with HTTP 403 with the bodynot cookie user.not(0)— returnsfalse. The block does not execute.not(false)—falseis falsy, sonot(false)returnstrue. The block executes and exits with HTTP 403.
eq
Signature: eq(arg1: any, arg2: same type as arg1) : Boolean
Compares whether two values are equal.
Parameters:
arg1: The first value. Data type: any.arg2: The second value. Data type: same asarg1.
Return value: true if the two values are equal; false otherwise.
Example: The following example combines eq and ne inside an and() call. See the ne section for the full explanation.
key1 = 'value1'
key2 = 'value2'
if and($arg_k1, $arg_k2, eq(key1, $arg_k1), ne(key2, $arg_k2)) {
say('match condition')
}ne
Signature: ne(arg1: any, arg2: same type as arg1) : Boolean
Compares whether two values are not equal.
Parameters:
arg1: The first value. Data type: any.arg2: The second value. Data type: same asarg1.
Return value: true if the two values are not equal; false otherwise.
Example: Match a request only when both query parameters are present and meet specific value conditions.
key1 = 'value1'
key2 = 'value2'
if and($arg_k1, $arg_k2, eq(key1, $arg_k1), ne(key2, $arg_k2)) {
say('match condition')
}All four conditions must be true for the body to execute:
The request includes the
k1parameter.The request includes the
k2parameter.The value of
k1equalsvalue1(checked byeq).The value of
k2does not equalvalue2(checked byne).
If the request is missing k1 or k2, short-circuit evaluation stops at step 1 or 2, and neither eq() nor ne() is called.
null
Signature: null(v: array | dictionary | string) : Boolean
Checks whether a data type is specified for EdgeScript (ES).
Parameter: v — an array, dictionary, or string. Passing any other data type returns false.
Return value: Boolean.
true— ifvis an empty string, or an array or dictionary with no elements.false— ifvis a non-empty string, a non-empty array or dictionary, or any other data type.
Example:
d = []
say(null(d))
set(d, 1, 'v1')
say(null(d))
say(tostring(null('x')))
say(tostring(null('')))Output:
true
false
false
truenull(d)on an empty array[]returnstrue.After
set(d, 1, 'v1'), the array is non-empty, sonull(d)returnsfalse.null('x')— non-empty string — returnsfalse.null('')— empty string — returnstrue.