Request logic functions return attributes of the current request, such as the server and client addresses, the client location, the request URI, and request headers. The following sections describe the syntax, parameters, example code, and return value of each function.
Function index
The following table lists the request logic functions by category, the value that each function returns, and the parameters that each function accepts.
| Category | Function | Returns | Parameters |
| Server attributes | server_addr | Address of the server that received the current request. | None |
| Server attributes | server_port | Port of the server that received the current request. | None |
| Client attributes | client_addr | Client IP address. | None |
| Client attributes | client_port | Client port. | None |
| Client geolocation | client_country | Country or region code of the client IP address. | None |
| Client geolocation | client_region | Administrative division (province or city) code of the client IP address. | None |
| Client geolocation | client_city | City code of the client IP address. | None |
| Client geolocation | client_isp | ISP code of the client IP address. | None |
| Geolocation of a specified IP address | ip_country | Country or region code of the specified IP address. | ipaddr |
| Geolocation of a specified IP address | ip_region | Administrative division (province or city) code of the specified IP address. | ipaddr |
| Geolocation of a specified IP address | ip_city | City code of the specified IP address. | ipaddr |
| Geolocation of a specified IP address | ip_isp | ISP code of the specified IP address. | ipaddr |
| URI parsing | req_uri | Request URI, excluding the query parameters. | [pattern] |
| URI parsing | req_uri_basename | Filename in the request URI. | [pattern] |
| URI parsing | req_uri_ext | File extension in the request URI. | [pattern] |
| URI parsing | req_uri_seg | Segments of the request URI, split by forward slashes (/). | [idx] |
| URI parsing | req_uri_arg | Value of a specified URI parameter. | name, [pattern] |
| URI parsing | req_uri_query_string | Query parameters of the request, excluding the question mark (?). | [pattern] |
| Request line and headers | req_scheme | Request scheme. | [pattern] |
| Request line and headers | req_method | Request method. | [pattern] |
| Request line and headers | req_host | Value of the Host request header. | [pattern] |
| Request line and headers | req_user_agent | Value of the User-Agent request header. | [pattern] |
| Request line and headers | req_referer | Value of the Referer request header. | [pattern] |
| Request line and headers | req_cookie | Value of a specified cookie. | name, [pattern] |
| Request line and headers | req_first_x_forwarded | First address in the X-Forwarded-For request header. | [pattern] |
| Request line and headers | req_header | Value of a specified request header. | name, [pattern] |
| Request identifier | req_id | Request ID of the current request. | None |
Considerations
The following limitation applies to nine functions in this topic: client_addr, client_country, client_region, client_city, client_isp, ip_country, ip_region, ip_city, and ip_isp.
If the client address is modified by Internet service provider (ISP) network NAT policies, or if the IP address is not in the CDN IP address library, the actual return value of these functions is affected. Use these functions with caution.
Pattern matching
Functions whose syntax includes [pattern] accept an optional pattern parameter. When you specify pattern, the function matches the value that it reads against pattern and returns a boolean result instead of the value itself.
pattern supports two matching modes:
Exact match — Compares the value with
patternfor equality. This is the default mode. Use it to check for a single fixed value.Regular expression match — Uses
patternas a regular expression, prefixed withre:. Use it to match a set of values.
Server attributes
server_addr and server_port read attributes of the server that received the current request.
server_addr
server_addr reads the address of the server that received the current request.
Syntax —
server_addr()Parameters — None.
Return value — Returns the server address. Data type: string.
The following example shows how to use server_addr:
s_addr = server_addr()
say(concat('s_addr:', s_addr))server_port
server_port reads the port of the server that received the current request.
Syntax —
server_port()Parameters — None.
Return value — Returns the server port. Data type: numeric.
The following example shows how to use server_port:
s_port = server_port()
say(concat('s_port:', tostring(s_port)))Client attributes
client_addr and client_port read the address and port of the client that sent the current request.
client_addr
client_addr reads the client IP address. To read the first address in the X-Forwarded-For request header instead, use req_first_x_forwarded.
Syntax —
client_addr()Parameters — None.
Return value — Returns the client IP address. Data type: string.
For an accuracy limitation that applies toclient_addr, see Considerations.
The following example shows how to use client_addr:
c_addr = client_addr()
say(concat('c_addr:', c_addr))client_port
client_port reads the client port.
Syntax —
client_port()Parameters — None.
Return value — Returns the client port. Data type: numeric.
The following example shows how to use client_port:
c_port = client_port()
say(concat('c_port:', tostring(c_port)))Client geolocation
The client_* geolocation functions read the location codes of the client that sent the current request. To resolve an IP address that you provide yourself, use the ip_* functions instead.
For an accuracy limitation that applies to these functions, see Considerations.
client_country
client_country reads the country or region code of the client IP address.
Syntax —
client_country()Parameters — None.
Return value — Returns the country or region code of the client IP address. Data type: string. For country and region codes, see Country codes.
The following example shows how to use client_country:
c_country = client_country()
if c_country {
say(concat('client_country:', c_country))
}client_region
client_region reads the administrative division (province or city) code of the client IP address.
Syntax —
client_region()Parameters — None.
Return value — Returns the administrative division (province or city) code of the client IP address. Data type: string. For administrative division codes, see Administrative division codes.
The following example shows how to use client_region:
c_region = client_region()
if c_region {
say(concat('client_region:', c_region))
}client_isp
client_isp reads the ISP code of the client IP address.
Syntax —
client_isp()Parameters — None.
Return value — Returns the ISP code of the client IP address. Data type: string. For ISP codes, see ISP codes.
The following example shows how to use client_isp:
c_isp = client_isp()
if c_isp {
say(concat('client_isp:', c_isp))
}Geolocation of a specified IP address
The ip_* functions read the location codes of the IP address that you pass in ipaddr. To resolve the client that sent the current request, use the client_* geolocation functions instead.
For an accuracy limitation that applies to these functions, see Considerations.
ip_region
ip_region reads the administrative division (province or city) code of a specified IP address.
Syntax —
ip_region(ipaddr)Parameters —
ipaddr: an IP address string in dotted decimal notation.Return value — Returns the administrative division (province or city) code of the specified IP address. Data type: string. For administrative division codes, see Administrative division codes.
The following example shows how to use ip_region:
c_region = ip_region('192.168.0.1')
if c_region {
say(concat('ip_region:', c_region))
}ip_isp
ip_isp reads the ISP code of a specified IP address.
Syntax —
ip_isp(ipaddr)Parameters —
ipaddr: an IP address string in dotted decimal notation.Return value — Returns the ISP code of the specified IP address. Data type: string. For ISP codes, see ISP codes.
The following example shows how to use ip_isp:
c_isp = ip_isp('192.168.0.1')
if c_isp {
say(concat('ip_isp:', c_isp))
}URI parsing
The following functions read individual parts of the request URI, such as the path, the filename, the file extension, the path segments, a URI parameter, and the query string.
req_uri
req_uri reads the request URI, excluding the query parameters.
Syntax —
req_uri([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the request URI. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_uri:
# 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')
}This example returns the following result:
Request: /path1/path2?mode=ip
Response:
req_uri: /path1/path2
req_uri: plain match
req_uri: regex matchreq_uri_basename
req_uri_basename reads the filename in the request URI.
Syntax —
req_uri_basename([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the filename in the request URI. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following examples show which part of the request URI is the filename:
For
/document_detail/30360.html, the filename is30360.For
/M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp4, the filename is2-1_g.For
/tarball/foo.tar.bz2, the filename isfoo.
The following example shows how to use req_uri_basename:
# 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')
}This example returns the following result:
Request: /path1/path2/foo.tar.bz2
Response:
req_uri_basename: foo 3
req_uri_basename: plain match
req_uri_basename: regex matchreq_uri_ext
req_uri_ext reads the file extension in the request URI.
Syntax —
req_uri_ext([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the file extension in the request URI. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following examples show which part of the request URI is the file extension:
For
/document_detail/30360.html, the file extension is.html.For
/M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp4, the file extension is.mp4.For
/tarball/foo.tar.bz2, the file extension is.tar.bz2.
The following example shows how to use req_uri_ext:
# 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')
}This example returns the following result:
Request: /path1/path2/foo.tar.bz2
Response:
req_uri_ext: .tar.bz2 8
req_uri_ext: plain match
req_uri_ext: regex matchreq_uri_seg
req_uri_seg splits the request URI by forward slashes (/) into segments.
A segment can contain up to 128 characters. Content that exceeds this limit is discarded.
Syntax —
req_uri_seg([idx])Parameters —
idx(optional): specifies the start index. Segment indexes start at 1 and increase from the beginning of the request URI, from left to right.Return value
Without
idx: returns a dictionary that contains all segments. Data type: dictionary.With
idx: returns a dictionary that contains the segments starting from the specified index, including the segment at that index. Data type: dictionary.
When you get a segment at a specified index from the returned dictionary, you must check whether the segment is empty.
The following example shows how to use req_uri_seg:
# 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)))
}This example returns the following result:
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]=path4req_uri_arg
req_uri_arg reads the value of a specified URI parameter.
Syntax —
req_uri_arg(name, [pattern])Parameters
name: the parameter name.pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.
Return value
Without
pattern:The parameter exists: returns the string value of the parameter specified by
name. Data type: string.The parameter does not exist: returns
false.
With
pattern:The parameter exists: performs the match and returns
trueif the match succeeds, orfalseif the match fails. Data type: boolean.The parameter does not exist: returns
false.
The following example shows how to use req_uri_arg:
# 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')
}This example returns the following results:
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 modereq_uri_query_string
req_uri_query_string reads the query parameters of the request, excluding the question mark (?).
Syntax —
req_uri_query_string([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the query parameters of the request. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_uri_query_string:
# 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')
}This example returns the following result:
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 modeRequest line and headers
The following functions read the request line attributes, request header values, and cookie values of the current request.
req_scheme
req_scheme reads the request scheme.
Syntax —
req_scheme([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the request scheme. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_scheme:
# 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')
}This example returns the following result:
Request: http://xx..
Response:
req_scheme: http
check scheme fail. plain mode
check scheme ok. regex modereq_method
req_method reads the request method.
Syntax —
req_method([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the request method. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_method:
# 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')
}This example returns the following result:
Request: POST /xxxx/xxx
Response:
req_method: POST
check method fail. plain mode
check method ok. regex modereq_host
req_host reads the value of the Host request header.
Syntax —
req_host([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the value of theHostrequest header. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_host:
# 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')
}This example returns the following result:
Request: Host: image.developer.aliyundoc.com
Response:
req_host: image.developer.aliyundoc.com
check host fail. plain mode
check host ok. regex modereq_user_agent
req_user_agent reads the value of the User-Agent request header.
Syntax —
req_user_agent([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the value of theUser-Agentrequest header. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_user_agent:
# 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')
}This example returns the following result:
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 modereq_referer
req_referer reads the value of the Referer request header.
Syntax —
req_referer([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the value of theRefererrequest header. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_referer:
# 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')
}This example returns the following result:
Request: Referer: https://example.aliyundoc.com/******00003
Response:
req_referer: https://example.aliyundoc.com/******00003
check referer ok. plain mode
check referer fail. regex modereq_first_x_forwarded
req_first_x_forwarded reads the first address in the X-Forwarded-For request header. To read the client IP address instead, use client_addr.
Syntax —
req_first_x_forwarded([pattern])Parameters —
pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.Return value
Without
pattern: returns the first address in theX-Forwarded-Forrequest header. Data type: string.With
pattern: returnstrueif the match succeeds, orfalseif the match fails. Data type: boolean.
The following example shows how to use req_first_x_forwarded:
# req_first_x_forwarded
say(concat('req_first_x_forwarded: ', req_first_x_forwarded()))
if req_first_x_forwarded('203.0.113.1') {
say('check first_x_forwarded ok. plain mode')
} else {
say('check first_x_forwarded fail. plain mode')
}
if req_first_x_forwarded('re:203.0.113.[0-9]') {
say('check first_x_forwarded ok. regex mode')
} else {
say('check first_x_forwarded fail. regex mode')
}This example returns the following result:
Request: X-Forwarded-For: 203.0.113.1, 10.10.10.10, 172.16.0.1
Response:
req_first_x_forwarded: 203.0.113.1
check first_x_forwarded ok. plain mode
check first_x_forwarded ok. regex modereq_header
req_header reads the value of a specified request header.
Syntax —
req_header(name, [pattern])Parameters
name: the request header name. Replace each hyphen (-) in the request header name with an underscore (_). For example,X-USER-IDbecomesx_user_id.pattern: the pattern used for matching. For the supported matching modes, see Pattern matching.
Return value
Without
pattern:The request header exists: returns the string value of the request header specified by
name. Data type: string.The request header does not exist: returns
false.
With
pattern:The request header exists: performs the match and returns
trueif the match succeeds, orfalseif the match fails. Data type: boolean.The request header does not exist: returns
false.
The following example shows how to use req_header:
# 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')
}This example returns the following result:
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 modeRequest identifier
The following function reads the request ID of the current request.
req_id
req_id reads the request ID, which is the unique identifier of each request.
Syntax —
req_id()Parameters — None.
Return value — Returns the request ID. Data type: string.
The following example shows how to use req_id:
# req_id
say(concat('req_id: ', req_id()))This example returns req_id: 6451c43d15815890089411000e.