Fortinet black logo

Handbook

Scripting application

Scripting application

HTTP scripts are composed of several functional components that define the trigger events, commands, operators, and more. The following example demonstrates how HTTP scripting is applied to rewrite the HTTP Host header and path in an HTTP request.

The HTTP script:
when RULE_INIT {
debug("rewrite the HTTP Host header and path in a HTTP request \n")
}
 
when HTTP_REQUEST{
host = HTTP:header_get_value("Host")
path = HTTP:path_get() 
if host:lower():find("myold.hostname.com") then
debug("found myold.hostname.com in Host %s \n", host)
HTTP:header_replace("Host", "mynew.hostname.com")
HTTP:path_set("/other.html")
end
}
Script component breakdown:

Parameter

Example

Description

Events — for details, see Events and actions.

RULE_INIT

The event is used to initialize global or static variables used within a script. It is triggered when a script is added or modified, or when the device starts up, or when the software is restarted.

HTTP_REQUEST

The virtual server receives a complete HTTP request header.

Commands — for details, see Predefined commands.

Debug(str)

Prints the debug information when VS using scripting.

HTTP:header_get_values(header_name)

Returns a list of value(s) of the HTTP header named <header_name>, with a count for each value. Note that the command returns all the values in the headers as a list if there are multiple headers with the same name.

HTTP:path_get()

Returns the string of the HTTP request path.

HTTP:header_replace(header_name, value)

Replaces the occurrence value of header <header_name> with value <value>.

HTTP:path_set(value)

Sets HTTP request path to the string <value>.

Operators — for details, see Operators. (Not applicable in this example).

Strings — for details, see String library.

host:lower():find("myold.hostname.com")

The string library includes the string-manipulation functions, such as:

string.lower

string.find(s, pattern)

This example combines the above string manipulation functions, using lower() to convert the Host strings to lowercase and then find() to search for the Host header for a match.

Control structures — for details, see Control structures.

if...then

end

if condition1 then

else if condition2 then

… break

else

… go to location1

end

::location1::

Functions — for details, see Functions. (Not applicable in this example).

Scripting application

HTTP scripts are composed of several functional components that define the trigger events, commands, operators, and more. The following example demonstrates how HTTP scripting is applied to rewrite the HTTP Host header and path in an HTTP request.

The HTTP script:
when RULE_INIT {
debug("rewrite the HTTP Host header and path in a HTTP request \n")
}
 
when HTTP_REQUEST{
host = HTTP:header_get_value("Host")
path = HTTP:path_get() 
if host:lower():find("myold.hostname.com") then
debug("found myold.hostname.com in Host %s \n", host)
HTTP:header_replace("Host", "mynew.hostname.com")
HTTP:path_set("/other.html")
end
}
Script component breakdown:

Parameter

Example

Description

Events — for details, see Events and actions.

RULE_INIT

The event is used to initialize global or static variables used within a script. It is triggered when a script is added or modified, or when the device starts up, or when the software is restarted.

HTTP_REQUEST

The virtual server receives a complete HTTP request header.

Commands — for details, see Predefined commands.

Debug(str)

Prints the debug information when VS using scripting.

HTTP:header_get_values(header_name)

Returns a list of value(s) of the HTTP header named <header_name>, with a count for each value. Note that the command returns all the values in the headers as a list if there are multiple headers with the same name.

HTTP:path_get()

Returns the string of the HTTP request path.

HTTP:header_replace(header_name, value)

Replaces the occurrence value of header <header_name> with value <value>.

HTTP:path_set(value)

Sets HTTP request path to the string <value>.

Operators — for details, see Operators. (Not applicable in this example).

Strings — for details, see String library.

host:lower():find("myold.hostname.com")

The string library includes the string-manipulation functions, such as:

string.lower

string.find(s, pattern)

This example combines the above string manipulation functions, using lower() to convert the Host strings to lowercase and then find() to search for the Host header for a match.

Control structures — for details, see Control structures.

if...then

end

if condition1 then

else if condition2 then

… break

else

… go to location1

end

::location1::

Functions — for details, see Functions. (Not applicable in this example).