This topic describes the syntax, features, parameters, and return values of request logic functions. This topic also provides examples of these functions.

server_addr

This function queries the IP addresses of servers that receive the current request. The following table describes the details about this function.
Item Description
Syntax server_addr()
Parameters None
Examples
s_addr = server_addr()
say(concat('s_addr:', s_addr))
Return value Returns the IP addresses of the servers in a string.

server_port

This function queries the server port that receives the current request. The following table describes the details about this function.
Item Description
Syntax server_port()
Parameters None
Examples
s_addr = server_addr()
say(concat('s_addr:', s_addr))
Return value Returns the server port that receives the current request. Data type: numeric.

client_addr

Notice Caution: The return value may not be accurate because the client address may be changed by the NAT rules of the Internet service provider (ISP) or the IP address is not in the address pool of Alibaba Cloud CDN. Proceed with caution.
This function queries the IP address of a client. The following table describes the details about this function.
Item Description
Syntax client_addr()
Parameters None
Examples
c_addr = client_addr()
c_port = client_port()
say(concat('c_addr:', c_addr))
say(concat('c_port:', tostring(c_port)))
Return value Returns the IP address of the specified client in a string.

client_port

This function queries the port of a client. The following table describes the details about this function.
Item Description
Syntax client_port()
Parameters None
Examples
c_addr = client_addr()
c_port = client_port()
say(concat('c_addr:', c_addr))
say(concat('c_port:', tostring(c_port)))
Return value Returns the port of the specified client. Data type: numeric.

client_region

Notice Caution: The return value may not be accurate because the client address may be changed by the NAT rules of the Internet service provider (ISP) or the IP address is not in the address pool of Alibaba Cloud CDN. Proceed with caution.
This function queries the administrative division code of a client. The following table describes the details about this function.
Item Description
Syntax client_region()
Parameters None
Examples
Return value Returns the administrative division code of the specified client in a string. For more information about administrative division codes, see Administrative division codes.

client_city

Notice Caution: The return value may not be accurate because the client address may be changed by the NAT rules of the Internet service provider (ISP) or the IP address is not in the address pool of Alibaba Cloud CDN. Proceed with caution.

This function queries the city code of a client. The following table describes the details about this function.

Item Description
Syntax client_city()
Parameters None
Examples
Return value Returns the city code of the specified client in a string. For more information about city codes, see Administrative division codes of prefectural-level subdivisions of China.

client_isp

Notice Caution: The return value may not be accurate because the client address may be changed by the NAT rules of the Internet service provider (ISP) or the IP address is not in the address pool of Alibaba Cloud CDN. Proceed with caution.
This function queries the ISP code of a client. The following table describes the details about this function.
Item Description
Syntax client_isp()
Parameters None
Examples
Return value Returns the ISP code of the specified client in a string. For more information about ISP codes, see ISP codes.

ip_region

Notice Caution: The return value may not be accurate because the client address may be changed by the NAT rules of the Internet service provider (ISP) or the IP address is not in the address pool of Alibaba Cloud CDN. Proceed with caution.
This function queries the administrative division code of the city or province to which a specified IP address belongs. The following table describes the details about this function.
Item Description
Syntax ip_region(ipaddr)
Parameters
Examples
Return value Returns the administrative division code of the city or province to which the specified IP address belongs in a string. For more information about administrative division codes, see Administrative division codes..

ip_city

Notice Caution: The return value may not be accurate because the client address may be changed by the NAT rules of the Internet service provider (ISP) or the IP address is not in the address pool of Alibaba Cloud CDN. Proceed with caution.

Queries the country code of a specified IP address. The following table describes the details about this function.

Item Description
Syntax ip_city(ipaddr)
Parameters
Examples
Return value Returns the country code of the specified IP address in a string. For more information about city codes, see Administrative division codes of prefectural-level subdivisions of China.

ip_isp

Notice Caution: The return value may not be accurate because the client address may be changed by the NAT rules of the Internet service provider (ISP) or the IP address is not in the address pool of Alibaba Cloud CDN. Proceed with caution.
This function queries the ISP code of a specified IP address. The following table describes the details about this function.
Item Description
Syntax ip_isp(ipaddr)
Parameters
Examples
Return value Returns the ISP code of the specified IP address in a string. For more information about ISP codes, see ISP codes.

req_uri

Usage notes on this function:
  • If the pattern parameter is not included in the request, the URI of the request is returned, excluding the parameters.
  • If the pattern parameter is included in the request, the URI of the request is compared with the match conditions.
The following table describes the details about this function.
Item Description
Syntax req_uri([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_uri
say(concat('req_uri: ', req_uri()))
if req_uri('/path1/path2') {
    say('req_uri: plain match')
}
if req_uri('re:/path[0-9]/path[0-9]') {
    say('req_uri: regex match')
}
Return value
  • If the pattern parameter is not included in the request, the request URI is returned. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: /path1/path2?mode=ip   
Response:
req_uri: /path1/path2
req_uri: plain match
req_uri: regex match

req_uri_basename

Usage notes on this function:
  • Returns the file name in the request URI if the pattern parameter is not included in the request.
  • Compares the file name in the request URI with the match conditions if the pattern parameter is included in the request.
Sample file names:
  • Example 1: For /document_detail/30360.html, the file name is 30360.
  • Example 2: For /M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp4, the file name is 2-1_g.
  • Example 3: For /tarball/foo.tar.bz2, the file name is foo.
The following table describes the details about this function.
Item Description
Syntax req_uri_basename([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_uri_basename
basename = req_uri_basename()
say(concat('req_uri_basename: ', basename, ' ', len(basename)))
if req_uri_basename('foo') {
    say('req_uri_basename: plain match')
}
if req_uri_basename('re:^f.*') {
    say('req_uri_basename: regex match')
}
Return value
  • Returns the file name in the request URI if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: /path1/path2/foo.tar.bz2
Response:
req_uri_basename: foo 3
req_uri_basename: plain match
req_uri_basename: regex match

req_uri_ext

Usage notes on this function:
  • Returns the extension in the request URI if the pattern parameter is not included in the request.
  • Compares the extension in the request URI with the match conditions if the pattern parameter is included in the request.
Sample extensions:
  • Example 1: For /document_detail/30360.html, the extension is .html.
  • Example 2: For /M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp4, the extension is .mp4.
  • Example 3: For /tarball/foo.tar.bz2, the extension is .tar.bz2.
The following table describes the details about this function.
Item Description
Syntax req_uri_ext([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_uri_ext
ext = req_uri_ext()
say(concat('req_uri_ext: ', ext, ' ', len(ext)))
if req_uri_ext('.tar.bz2') {
    say('req_uri_ext: plain match')
}
if req_uri_ext('re:\.tar\.bz[0-2]') {
    say('req_uri_ext: regex match')
}
Return value
  • Returns the extension in the request URI if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: /path1/path2/foo.tar.bz2
Response:
req_uri_ext: .tar.bz2 8
req_uri_ext: plain match
req_uri_ext: regex match

req_uri_seg

Notes on this function:
  • In response parameters, the segments are separated by forward slashes (/).
    • Returns all segments if the idx parameter is not included in the request.
    • Returns the segments (including the index) that follow the specified index if the idx parameter is included in the request.
  • Indexes for paragraphs: the index starts from 1 and increases from the leftmost index when more paragraphs are added.
  • Paragraph limit: A paragraph can contain up to 128 characters. Characters that exceed this limit are dropped.
The following table describes the details about this function.
Item Description
Syntax req_uri_seg([idx])
Parameters idx: specifies the start index. This parameter is optional.
Examples
# req_uri_seg
def echo_each(k, v, u) {
    say(concat(get(u, 'msg'), ' : segs[', k, ']=', v))
}
# fetch all segments
segs = req_uri_seg()
foreach(segs, echo_each, ['msg'='req_uri_seg()'])
# fetch segments from idx 3
segs = req_uri_seg(3)
if get(segs, 3) {
    say(concat('req_uri_seg(3): segs[3]=', get(segs, 3)))
}
if get(segs, 4) {
    say(concat('req_uri_seg(3): segs[4]=', get(segs, 4)))
}
if get(segs, 5) {
    say(concat('req_uri_seg(3): segs[5]=', get(segs, 5)))
}
Return value Data type: dictionary. The relevant paragraphs are included.
Note When the function retrieves the paragraph from the returned dictionary based on the specified index, the function checks whether the paragraph is empty.
In this example, the following values are returned:
Request: /path1/path2/path3/path4?mode=req2
Response:
req_uri_seg() : segs[1]=path1
req_uri_seg() : segs[2]=path2
req_uri_seg() : segs[3]=path3
req_uri_seg() : segs[4]=path4
req_uri_seg(3): segs[3]=path3
req_uri_seg(3): segs[4]=path4

req_uri_arg

This function queries the value of a specified parameter. If the pattern parameter is included in the request, the value of the specified parameter is compared with the match conditions. The following table describes the details about this function.
Item Description
Syntax req_uri_arg(name, [pattern])
Parameters
  • name: the name of the parameter.
  • pattern: compared with the match conditions. The following match types are supported:
    • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
    • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_uri_arg
uid = req_uri_arg('uid')
if uid {
    say(concat('found uid ', uid))
} else {
    say('not found uid')
}
uid_chk = req_uri_arg('uid', '058334')
if uid_chk {
    say('check uid ok. plain mode')
} else {
    say('check uid fail. plain mode')
}
uid_chk = req_uri_arg('uid', 're:[0-9]+')
if uid_chk {
    say('check uid ok. regex mode')
} else {
    say('check uid fail. regex mode')
}
Return value
  • If the pattern parameter is not included in the request
    • The specified parameter exists: returns the value of the parameter specified by the name parameter in a string.
    • The specified parameter does not exist: returns a value of false.
  • If the pattern parameter is included in the request
    • The specified parameter exists: returns a value of true if the value matches the match conditions. Otherwise, a value of false is returned.
    • The specified parameter does not exist: returns a value of false.
In this example, the following values are returned:
Request: /path1/path2/path3/path4?mode=req4&uid
Response:
not found uid
check uid fail. plain mode
check uid fail. regex mode

Request: /path1/path2/path3/path4?mode=req4&uid=
Response:
found uid
check uid fail. plain mode
check uid fail. regex mode

Request: /path1/path2/path3/path4?mode=req4&uid=12345
Response:
found uid 12345
check uid fail. plain mode
check uid ok. regex mode

req_uri_query_string

Usage notes on this function:
  • If the pattern parameter is not included in the request, the parameters in the request are returned, excluding the question mark (?).
  • If the pattern parameter is included in the request, the parameters in the requests are compared with the match conditions.
The following table describes the details about this function.
Item Description
Syntax req_uri_query_string([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_uri_query_string
say(concat('req_uri_query_string: ', req_uri_query_string()))
if req_uri_query_string('mode=') {
    say('check uri query string ok. plain mode')
} else {
    say('check uri query string fail. plain mode')
}
if req_uri_query_string('re:mode=[0-9a-z]+') {
    say('check uri query string ok. regex mode')
} else {
    say('check uri query string fail. regex mode')
}
Return value
  • Returns the parameters in the request if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: /path1/path2/path3/path4?mode=req5&token=34Deasd#243
Response:
req_uri_query_string: mode=req5&token=34Deasd
check uri query string fail. plain mode
check uri query string ok. regex mode

req_scheme

Usage notes on this function:
  • Returns the request scheme if the pattern parameter is not included in the request.
  • Compares the request scheme with the match conditions if the pattern parameter is included in the request.
The following table describes the details about this function.
Item Description
Syntax req_scheme([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_scheme
say(concat('req_scheme: ', req_scheme()))
if req_scheme('https') {
    say('check scheme ok. plain mode')
} else {
    say('check scheme fail. plain mode')
}
if req_scheme('re:https?') {
    say('check scheme ok. regex mode')
} else {
    say('check scheme fail. regex mode')
}
Return value
  • Returns the request scheme if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: http://xx..
req_scheme: http
check scheme fail. plain mode
check scheme ok. regex mode

req_method

Notes on this function:
  • Returns the request method if the pattern parameter is not included in the request.
  • Compares the request method with the match conditions if the pattern parameter is included in the request.
The following table describes the details about this function.
Item Description
Syntax req_method([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_method
say(concat('req_method: ', req_method()))
if req_method('GET') {
    say('check method ok. plain mode')
} else {
    say('check method fail. plain mode')
}
if req_method('re:(GET|POST)') {
    say('check method ok. regex mode')
} else {
    say('check method fail. regex mode')
}
Return value
  • Returns the request method if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: POST /xxxx/xxx
Response:
req_method: POST
check method fail. plain mode
check method ok. regex mode

req_host

Notes on this function:
  • Returns the value of the Host request header if the pattern parameter is not included in the request.
  • Compares the value of the Host request header with the match conditions if the pattern parameter is included in the request.
The following table describes the details about this function.
Item Description
Syntax req_host([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_host
say(concat('req_host: ', req_host()))
if req_host('image.developer.aliyundoc.com') {
    say('check host ok. plain mode')
} else {
    say('check host fail. plain mode')
}
if req_host('re:.+\.y\.z\.com') {
    say('check host ok. regex mode')
} else {
    say('check host fail. regex mode')
}
Return value
  • Returns the value of the Host request header if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: Host: image.developer.aliyundoc.com
Response:
req_host: image.developer.aliyundoc.com
check host fail. plain mode
check host ok. regex mode

req_user_agent

Usage notes on this function:
  • Returns the value of the User-Agent request header if the pattern parameter is not included in the request.
  • Compares the value of the User-Agent request header with the match conditions if the pattern parameter is included in the request.
The following table describes the details about this function.
Item Description
Syntax req_user_agent([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_user_agent
say(concat('req_user_agent: ', req_user_agent()))
if req_user_agent('Mozilla') {
    say('check user_agent ok. plain mode')
} else {
    say('check user_agent fail. plain mode')
}
if req_user_agent('re:^Mozilla') {
    say('check user_agent ok. regex mode')
} else {
    say('check user_agent fail. regex mode')
}
Return value
  • Returns the value of the User-Agent request header if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Response:
req_user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
check user_agent fail. plain mode
check user_agent ok. regex mode

req_referer

Notes on this function:
  • Returns the value of the Referer request header if the pattern parameter is not included in the request.
  • Compares the value of the Referer request header with the match conditions if the pattern parameter is included in the request.
The following table describes the details about this function.
Item Description
Syntax req_referer([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_referer
say(concat('req_referer: ', req_referer()))
if req_referer('https://example.aliyundoc.com/******00003') {
    say('check referer ok. plain mode')
} else {
    say('check referer fail. plain mode')
}
if req_referer('re:https://foo\.bar\.cn/\*+[0-9]+') {
    say('check referer ok. regex mode')
} else {
    say('check referer fail. regex mode')
}
Return value
  • Returns the value of the Referer request header if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: Referer: https://example.aliyundoc.com/******00003
Response:
req_referer: https://example.aliyundoc.com/******00003
check referer ok. plain mode
check referer ok. regex mode

req_cookie

This function queries the value of a specified cookie. If the pattern parameter is included in the request, the value of the specified cookie is compared with the match conditions. The following table describes the details about this function.
Item Description
Syntax req_cookie(name, [pattern])
Parameters
  • name: specifies the name of the cookie.
  • pattern: compared with the match conditions. The following match types are supported:
    • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
    • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_cookie
uid = req_cookie('uid')
if uid {
    say(concat('found cookie uid ', uid))
} else {
    say('not found cookie uid')
}
uid_chk = req_cookie('uid', '058334')
if uid_chk {
    say('check cookie uid ok. plain mode')
} else {
    say('check cookie uid fail. plain mode')
}
uid_chk = req_cookie('uid', 're:^[0-9]+')
if uid_chk {
    say('check cookie uid ok. regex mode')
} else {
    say('check cookie uid fail. regex mode')
}
Return value
  • If the pattern parameter is not included in the request
    • The specified cookie exists: returns the value of the cookie specified by the name parameter in a string.
    • The specified parameter does not exist: returns a value of false.
  • If the pattern parameter is included in the request
    • The specified parameter exists: returns a value of true if the value matches the match conditions. Otherwise, a value of false is returned.
    • The specified parameter does not exist: returns a value of false.
In this example, the following values are returned:
Request: Cookie: uid=123456; token=value2
Response:
found cookie uid 123456
check cookie uid fail. plain mode
check cookie uid ok. regex mode

req_first_x_forwarded

Notes on this function:
  • Returns the first address in the X-Forwarded-For request header if the pattern parameter is not included in the request.
  • Compares the first address in the X-Forwarded-For request header with the match conditions if the pattern parameter is included in the request.
The following table describes the details about this function.
Item Description
Syntax req_first_x_forwarded_addr([pattern])
Parameters pattern: compared with the match conditions. The following match types are supported:
  • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
  • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_first_x_forwarded
say(concat('req_first_x_forwarded: ', req_first_x_forwarded()))
if req_first_x_forwarded('1.1.1.1') {
    say('check first_x_forwarded ok. plain mode')
} else {
    say('check first_x_forwarded fail. plain mode')
}
if req_first_x_forwarded('re:1.1.1.[0-9]') {
    say('check first_x_forwarded ok. regex mode')
} else {
    say('check first_x_forwarded fail. regex mode')
}
Return value
  • Returns the first address in the X-Forwarded-For request header if the pattern parameter is not included in the request. Data type: string.
  • If the pattern parameter is included in the request and the request matches the match conditions, a value of true is returned. If no condition is matched, a value of false is returned.
In this example, the following values are returned:
Request: X-Forwarded-For: 1.1.1.1, 10.10.10.10, 172.16.0.1
Response:
req_first_x_forwarded: 1.1.1.1
check first_x_forwarded ok. plain mode
check first_x_forwarded ok. regex mode

req_header

This function queries the value of a specified request header. If the pattern parameter is included in the request, the value of the specified request header is compared with the match conditions. The following table describes the details about this function.
Item Description
Syntax req_header(name, [pattern])
Parameters
  • name: specifies the name of the request header.

    Replace hyphens (-) in the request header with underscores (_). For example, X-USER-ID must be changed to x_user_id.

  • pattern: compared with the match conditions. The following match types are supported:
    • Brute-force algorithm: compares the values based on a brute-force algorithm. This is the default match type.
    • Regular expression: compares the values based on a leading regular expression specified by re:.
Examples
# req_header
uid = req_header('x_uid')
if uid {
    say(concat('found header x-uid ', uid))
} else {
    say('not found header x-uid')
}
uid_chk = req_header('x_uid', 'es developer')
if uid_chk {
    say('check header x-uid ok. plain mode')
} else {
    say('check header x-uid fail. plain mode')
}
uid_chk = req_header('x_uid', 're:es [a-z]+')
if uid_chk {
    say('check header x-uid ok. regex mode')
} else {
    say('check header x-uid fail. regex mode')
}
Return value
  • If the pattern parameter is not included in the request
    • The specified request header exists: returns the value of the specified request header specified by the name parameter in a string.
    • The specified parameter does not exist: returns a value of false.
  • If the pattern parameter is included in the request
    • The specified parameter exists: returns a value of true if the value matches the match conditions. Otherwise, a value of false is returned.
    • The specified parameter does not exist: returns a value of false.
In this example, the following values are returned:
Request: X-UID: es developer
Response:
found header x-uid es developer
check header x-uid ok. plain mode
check header x-uid ok. regex mode

req_id

This function queries the Eagle Eye ID of a request. Each Eagle Eye ID uniquely identifies a request. The following table describes the details about this function.
Item Description
Syntax req_id()
Parameters None
Examples
# req_id
say(concat('req_id: ', req_id()))
Return value Returns the request ID in a string. In this example, the ID of the request is req_id: 6451c43d15815890089411000e.