All Products
Search
Document Center

CDN:Miscellaneous functions

Last Updated:Oct 14, 2024

This topic describes the syntax, description, parameters, and return values of miscellaneous functions. This topic also provides examples of these functions.

base64_enc

The following table describes the details about this function.
ItemDescription
Syntaxbase64_enc(s [, no_padding])
DescriptionEncodes a string in Base64.
Parameter
  • s: the string that you want to encode.
  • no_padding: specifies whether to pad the string. A value of true specifies that the string is not padded. Default value: false.
Return valueReturns 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=="
Response: base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==

base64_dec

The following table describes the details about this function.
ItemDescription
Syntaxbase64_dec(s)
DescriptionDecodes a Base64-encoded string.
Parameters: the string that you want to decode.
Return valueReturns a 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=="
Response: base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==

url_escape

The following table describes the details about this function.
ItemDescription
Syntaxurl_escape(s)
DescriptionUses URL encoding to encode a string.
Parameters: the string to be decoded.
Return valueReturns 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('dsdata=', dsdata))
}
Output: raw=/abc/123/ dd/file.m3u8
esdata=%2Fabc%2F123%2F%20dd%2Ffile.m3u8
dsdata=/abc/123/ dd/file.m3u8

url_unescape

The following table describes the details about this function.
ItemDescription
Syntaxurl_unescape(s)
DescriptionDecodes a URL-encoded string.
Parameters: the string that you want to decode.
Return valueReturns a decoded raw 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('dsdata=', dsdata))
}
Output:
raw=/abc/123/ dd/file.m3u8
esdata=%2Fabc%2F123%2F%20dd%2Ffile.m3u8
dsdata=/abc/123/ dd/file.m3u8

rand

The following table describes the details about this function.
ItemDescription
Syntaxrand(n1, n2)
DescriptionGenerates and returns a random number. Value values: n1 <= returned number <= n2. The n1 parameter specifies the smallest number. The n2 parameter specifies the greatest number.
Parameter
  • n1: the smallest number.
  • n2: the greatest number.
Return valueReturns a random number.
Example
r = rand(1,100)

rand_hit

The following table describes the details about this function.
ItemDescription
Syntaxrand_hit(ratio)
DescriptionRetrieves a value of true or false based on the specified probability.
Parameterratio: the probability. Valid values: 0 to 100.
Return valueReturns true or false based on the specified probability. If you set ratio to 100, true is returned. If you set ratio to 0, false is returned.
Example
rand_hit(80)

crc

The following table describes the details about this function.
ItemDescription
Syntaxcrc(s)
DescriptionCalculates a Cyclic Redundancy Check (CRC) value.
Parameters: the string for which you want to calculate a CRC digest.
Return valueReturns the CRC value of the string specified by the s parameter.
Example
crc('hello edgescript')

tonumber

The following table describes the details about this function.
ItemDescription
Syntaxtonumber(s [, base])
DescriptionConverts a string to the numeric type.
Parameter
  • s: the string that you want to convert.
  • base: the positional notation that you want to use to convert the string. Valid values: 10 and 16. Default value: 10.
Example
n = tonumber('100')
say(concat('tonumber()=', n))

Output: tonumber()=100

base64_enc_safe

The following table describes the details about this function.
ItemDescription
Syntaxbase64_enc_safe(str)
DescriptionEncodes a string in Base64. In the encoded string, plus signs (+) are replaced by minus signs (-), forward slashes (/) are replaced by underscores (_), and equal signs (=) are removed.
Parameterstr: the string that you want to encode.
Return valueReturns a decoded raw string.
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

The following table describes the details about this function.
ItemDescription
Syntaxbase64_dec_safe(str)
DescriptionDecodes a Base64-encoded string. In the decoded string, minus signs (-) are replaced by plus signs (+) and underscores (_) are replaced by forward slashes (/). Equal signs (=) are added to the end of the string to ensure that the string is padded to a multiple of four characters.
Parameterstr: the Base64-encoded string that you want to decode.
Return valueReturns a 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

randomseed

The following table describes the details about this function.
ItemDescription
Syntaxrandomseed()
DescriptionGenerates a random seed.
ParameterN/A
Return valueN/A
Example
randomseed()
r = rand(1,100)

rand_bytes

The following table describes the details about this function.
ItemDescription
Syntaxrand_bytes(len)
DescriptionGenerates a random numeric string.
Parameterlen: the length of the generated random numeric string.
Return valueReturns the generated random numeric string.
Example
rand_bytes(16)

uuid

The following table describes the details about this function.
ItemDescription
Syntaxuuid().
DescriptionReturns a string in the UUID format.
ParameterNone
Return valueReturns a UUID. Example: 16903a86-4173-4 dea-842c-926c5860fe05.
Example
rand_bytes(say(uuid()))
Output: 16903a86-4173-4 dea-842c-926c5860fe05.