All Products
Search
Document Center

Server Load Balancer:Miscellaneous functions

Last Updated:Apr 01, 2026

EdgeScript provides utility functions for encoding, random number generation, checksum calculation, and type conversion.

Encoding functions: base64_enc | base64_dec | base64_enc_safe | base64_dec_safe | url_escape | url_unescape

Random number functions: randomseed | rand | rand_hit

Checksum and conversion functions: crc | tonumber

Encoding functions

base64_enc

Encodes a string in Base64.

Syntax

base64_enc(s [, no_padding])

Parameters

ParameterTypeRequiredDescription
sStringYesThe string to encode.
no_paddingBooleanNoSet to true to omit padding characters (=). Default: false.

Return value

A Base64-encoded string.

Example

if $http_data {
   decdata = base64_dec($http_data)
   say(concat('base64_decdata=', decdata))
   say(concat('base64_encdata=', base64_enc('hello, dsl')))
}

Request header: data: aGVsbG8sIGRzbA==

Output:

base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==

base64_dec

Decodes a Base64-encoded string.

Syntax

base64_dec(s)

Parameters

ParameterTypeRequiredDescription
sStringYesThe Base64-encoded string to decode.

Return value

The decoded raw string.

Example

if $http_data {
   decdata = base64_dec($http_data)
   say(concat('base64_decdata=', decdata))
   say(concat('base64_encdata=', base64_enc('hello, dsl')))
}

Request header: data: aGVsbG8sIGRzbA==

Output:

base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==

base64_enc_safe

Encodes a string in URL-safe Base64. Compared to base64_enc, this function replaces + with -, replaces / with _, and removes = padding.

Syntax

base64_enc_safe(str)

Parameters

ParameterTypeRequiredDescription
strStringYesThe string to encode.

Return value

A URL-safe Base64-encoded string (no padding).

Example

add_rsp_header('X-RESPOND-OUTPUT', concat('base64_enc_safe=', base64_enc_safe('hello, dsl')), true)

Response header:

X-RESPOND-OUTPUT: base64_enc_safe=aGVsbG8sIGRzbA

base64_dec_safe

Decodes a URL-safe Base64-encoded string. Before decoding, this function replaces - with + and _ with /, then appends = characters to pad the string to a multiple of four.

Syntax

base64_dec_safe(str)

Parameters

ParameterTypeRequiredDescription
strStringYesThe URL-safe Base64-encoded string to decode.

Return value

The decoded raw string.

Example

add_rsp_header('X-RESPOND-OUTPUT', concat('base64_dec_safe=', base64_dec_safe(base64_enc_safe('hello, dsl'))), true)

Response header:

X-RESPOND-OUTPUT: base64_dec_safe=hello, dsl

url_escape

Encodes a string using URL encoding (percent-encoding).

Syntax

url_escape(s)

Parameters

ParameterTypeRequiredDescription
sStringYesThe string to encode.

Return value

A URL-encoded string.

Example

raw = '/abc/123/ dd/file.m3u8'
esdata = url_escape(raw)
dsdata = url_unescape(esdata)
if eq(raw, dsdata) {
    say(concat('raw=', raw))
    say(concat('esdata=', esdata))
    say(concat('dsdata=', dsdata))
}

Output:

raw=/abc/123/ dd/file.m3u8
esdata=%2Fabc%2F123%2F%20dd%2Ffile.m3u8
dsdata=/abc/123/ dd/file.m3u8

url_unescape

Decodes a URL-encoded string.

Syntax

url_unescape(s)

Parameters

ParameterTypeRequiredDescription
sStringYesThe URL-encoded string to decode.

Return value

The decoded raw string.

Example

See the url_escape example, which demonstrates encoding and decoding a string in the same script.

Random number functions

Call randomseed() before using rand() or rand_hit(). Without seeding, the random number generator may produce the same sequence across requests.

randomseed

Seeds the random number generator. Call this function before rand() or rand_hit().

Syntax

randomseed()

Parameters

None.

Return value

None.

Example

randomseed()
r = rand(1, 100)

rand

Generates a random number in the range [n1, n2] (inclusive).

Syntax

rand(n1, n2)

Parameters

ParameterTypeRequiredDescription
n1NumberYesThe lower bound (inclusive).
n2NumberYesThe upper bound (inclusive).

Return value

A random number between n1 and n2, inclusive.

Example

randomseed()
r = rand(1, 100)   -- returns a random integer such as 42

rand_hit

Returns true or false based on the specified probability. Use this function to implement probabilistic traffic sampling or feature rollouts.

Syntax

rand_hit(ratio)

Parameters

ParameterTypeRequiredDescription
ratioNumberYesThe probability of returning true. Valid values: 0–100. A value of 100 always returns true; a value of 0 always returns false.

Return value

true or false.

Example

randomseed()
if rand_hit(80) {
    -- executes with approximately 80% probability
}

Checksum and conversion functions

crc

Calculates a Cyclic Redundancy Check (CRC) digest for a string.

Syntax

crc(s)

Parameters

ParameterTypeRequiredDescription
sStringYesThe string for which to calculate a CRC digest.

Return value

The CRC value of the input string.

Example

crc('hello edgescript')

tonumber

Converts a string to a number.

Syntax

tonumber(s [, base])

Parameters

ParameterTypeRequiredDescription
sStringYesThe string to convert.
baseNumberNoThe numeric base for conversion. Valid values: 10 (decimal) and 16 (hexadecimal). Default: 10.

Return value

The numeric value of the input string.

Example

n = tonumber('100')
say(concat('tonumber()=', n))

Output:

tonumber()=100