All Products
Search
Document Center

Server Load Balancer:Request logic functions

Last Updated:Oct 31, 2023

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

server_addr | server_port | client_addr | client_port | req_uri | req_uri_basename | req_uri_ext | req_uri_seg | req_uri_arg | req_uri_query_string | req_scheme | req_method | req_host | req_user_agent | req_referer | req_cookie | req_first_x_forwarded | req_header

server_addr

Item

Description

Syntax

server_addr()

Description

server_addr is used to query the IP address of the server that receives the current request.

Parameter

N/A

Examples

s_addr = server_addr()
say(concat('s_addr:', s_addr))

Return values

The IP address of the server. Data type: string.

server_port

Item

Description

Syntax

server_port()

Description

server_port is used to query the server port that receives the current request.

Parameter

N/A

Examples

s_addr = server_port()
say(concat('s_addr:', s_port))

Return values

The server port that receives the current request. Data type: numeric.

client_addr

Important

If NAT is configured, the IP address returned by client_addr may not be the real client IP address.

Item

Description

Syntax

client_addr()

Description

client_addr is used to query the client IP address.

Parameter

N/A

Examples

c_addr = client_addr()
c_port = client_port()
say(concat('c_addr:', c_addr))
say(concat('c_port:', tostring(c_port)))

Return values

The client IP address. Data type: string.

client_port

Item

Description

Syntax

client_port()

Description

client_port is used to query the client port.

Parameter

N/A

Examples

c_addr = client_addr()
c_port = client_port()
say(concat('c_addr:', c_addr))
say(concat('c_port:', tostring(c_port)))

Return values

The client port. Data type: numeric.

req_uri

Item

Description

Syntax

req_uri([pattern])

Description

  • 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.

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
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 values

  • 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, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_uri_basename([pattern])

Description

  • If the pattern parameter is not included in the request, the file name in the request URI is returned.

  • The file name in the request URI is compared 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.

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_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 values

  • If the pattern parameter is not included in the request, the file name in the request URI is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_uri_ext([pattern])

Description

  • If the pattern parameter is not included in the request, the extension in the request URI is returned.

  • The extension in the request URI is compared 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.

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_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 values

  • If the pattern parameter is not included in the request, the extension in the request URI is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_uri_seg([idx])

Description

  • In response parameters, the segments are separated by forward slashes (/).

    • If the idx parameter is not included in the request, all segments are returned.

    • If the idx parameter is included in the request, the segments that follow the specified index, including the index, are returned.

  • 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.

Parameter

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 values

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(4): segs[4]=path4

req_uri_arg

Item

Description

Syntax

req_uri_arg(name, [pattern])

Description

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 of this function.

Parameter

  • 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 values

  • If the pattern parameter is not included in the request, the value that is returned varies in the following scenarios:

    • 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 value that is returned varies in the following scenarios:

    • The specified parameter exists: returns true if the value matches the match conditions. Otherwise, false is returned.

    • The specified parameter does not exist: returns 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

Item

Description

Syntax

req_uri_query_string([pattern])

Description

  • 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.

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_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 values

  • If the pattern parameter is not included in the request, the parameters in the request are returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_scheme([pattern])

Description

  • If the pattern parameter is not included in the request, the request scheme is returned.

  • The request scheme is compared with the match conditions if the pattern parameter is included in the request.

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_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 values

  • If the pattern parameter is not included in the request, the request scheme is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_method([pattern])

Description

  • If the pattern parameter is not included in the request, the request method is returned.

  • The request method is compared with the match conditions if the pattern parameter is included in the request.

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_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 values

  • If the pattern parameter is not included in the request, the request method is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_host([pattern])

Description

  • If the pattern parameter is not included in the request, the value of the Host request header is returned.

  • The value of the Host request header is compared with the match conditions if the pattern parameter is included in the request.

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_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 values

  • If the pattern parameter is not included in the request, the value of the Host request header is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_user_agent([pattern])

Description

  • If the pattern parameter is not included in the request, the value of the User-Agent request header is returned.

  • The value of the User-Agent request header is compared with the match conditions if the pattern parameter is included in the request.

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_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 values

  • If the pattern parameter is not included in the request, the value of the User-Agent request header is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_referer([pattern])

Description

  • If the pattern parameter is not included in the request, the value of the Referer request header is returned.

  • The value of the Referer request header is compared with the match conditions if the pattern parameter is included in the request.

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_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 values

  • If the pattern parameter is not included in the request, the value of the Referer request header is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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 fail. regex mode

req_cookie

Item

Description

Syntax

req_cookie(name, [pattern])

Description

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 of this function.

Parameter

  • 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 values

  • If the pattern parameter is not included in the request, the value returned varies in the following scenarios:

    • 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 false.

  • If the pattern parameter is included in the request, the value returned varies in the following scenarios:

    • The specified parameter exists: returns true if the value matches the match conditions. Otherwise, false is returned.

    • The specified parameter does not exist: returns 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

Item

Description

Syntax

req_first_x_forwarded

Description

  • If the pattern parameter is not included in the request, the first address in the X-Forwarded-For request header is returned.

  • The first address in the X-Forwarded-For request header is compared with the match conditions if the pattern parameter is included in the request.

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_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 values

  • If the pattern parameter is not included in the request, the first address in the X-Forwarded-For request header is returned. Data type: string.

  • If the pattern parameter is included in the request and the request matches the match conditions, true is returned. If no condition is matched, 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

Item

Description

Syntax

req_header(name, [pattern])

Description

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.

Parameter

  • 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 values

  • If the pattern parameter is not included in the request, the value returned varies in the following scenarios:

    • 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 value that is returned varies in the following scenarios:

    • The specified parameter exists: returns true if the value matches the match conditions. Otherwise, false is returned.

    • The specified parameter does not exist: returns 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