Fortinet white logo
Fortinet white logo

HTTP Header manipulate

HTTP Header manipulate

HTTP:set_path(“new-path”)

Change the path in HTTP request header.

Return true for success and false for failure.

Syntax
HTTP:set_path(path)
Arguments

Name

Description

path

String type path name.

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_path("/new_path")
}

HTTP:set_query(“new-query”)

Change the query in HTTP request header.

Return true for success and false for failure.

Syntax
HTTP:set_query(query)
Arguments

Name

Description

query

String type query name.

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_query("test=1")
}

HTTP:set_url(“new-url”)

Change the whole URL, including the path and query.

Return true for success and false for failure.

Syntax
HTTP:set_url("new-url")
Arguments

Name

Description

query

String type query name.

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_url("/new_path?test=1")
}

HTTP:set_method(“new-method”)

Change the method in HTTP request header.

Return true for success and false for failure.

Syntax
HTTP:set_method(method)
Arguments

Name

Description

method

String type http method

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_method("POST")
}

HTTP:set_status(status-code) \ HTTP:set_status("status-code", “reason”)

Change the status code and reason in HTTP response header. If reason does not exist, use default reason.

Return true for success and false for failure.

Syntax
HTTP:set_status(status_code) \ HTTP:set_status(status_code, reason)
Arguments

Name

Description

status_code

Integer status code

reason

Optional, string type reason.

Events

Applicable in HTTP_RESPONSE and HTTP_DATA_RESPONSE

Example
when HTTP_RESPONSE {
    HTTP:set_status(200, "Other Reason")
}

HTTP:add_header(“header-name”, “header-value”)

Add a header line to HTTP request or response header.

Return true for success and false for failure.

Syntax
HTTP:add_header(header_name, header_value)
Arguments

Name

Description

header_name

String type header_name

header_value

String type header_value

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:add_header("X-COUNTRY-FMF", ip.geo(client_ip) or "unknown") -- add a new header line
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
         rewrite_request(HTTP, IP, HTTP:args())
    end
}

HTTP:del_header(“header-name”)

Remove the header with name “header-name” from HTTP request or response.

Return true for success and false for failure.

Syntax
HTTP:del_header(header_name)
Arguments

Name

Description

header_name

String type header_name

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE events.

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:del_header("test")
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
        rewrite_request(HTTP, IP, HTTP:args())
    end
}

HTTP:set_header(“header-name”, "header-value-array")

Remove the header with name “header-name” from HTTP request or response, and add this header with new value header-value-array. The argument header-value-array is a Lua array which is the value got from HTTP:header().

Return true for success and false for failure.

Syntax
HTTP:set_header(header_name, header_value_array)
Arguments

Name

Description

header_name

String type header_name

header_value_array

Lua array

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE events.

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:set_header("test", { "line1", "line2", "line3" })
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
        rewrite_request(HTTP, IP, HTTP:args())
    end
}

HTTP:replace_header(“header-name”, “regex”, “replace”)

Match the regular expression in all occurrences of header field “header-name” according to “regex”, and replaces them with the “replace” argument. The replacement value can contain back references like 1,2, …

Return true for success and false for failure.

Syntax
HTTP:replace_header(header_name, regex, replace)
Arguments

Name

Description

header_name

String type header_name

regex

Match what to be replaced.

replace

Content to replace argument with.

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE events.

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:replace_header("Set-Cookie", [[(.*)(Path=\/)(.*)]], [[\1\2api\3]])
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
        rewrite_request(HTTP, IP, HTTP:args())
    end
}

HTTP Header manipulate

HTTP Header manipulate

HTTP:set_path(“new-path”)

Change the path in HTTP request header.

Return true for success and false for failure.

Syntax
HTTP:set_path(path)
Arguments

Name

Description

path

String type path name.

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_path("/new_path")
}

HTTP:set_query(“new-query”)

Change the query in HTTP request header.

Return true for success and false for failure.

Syntax
HTTP:set_query(query)
Arguments

Name

Description

query

String type query name.

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_query("test=1")
}

HTTP:set_url(“new-url”)

Change the whole URL, including the path and query.

Return true for success and false for failure.

Syntax
HTTP:set_url("new-url")
Arguments

Name

Description

query

String type query name.

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_url("/new_path?test=1")
}

HTTP:set_method(“new-method”)

Change the method in HTTP request header.

Return true for success and false for failure.

Syntax
HTTP:set_method(method)
Arguments

Name

Description

method

String type http method

Events

Applicable in HTTP_REQUEST and HTTP_DATA_REQUEST

Example
when HTTP_REQUEST {
    HTTP:set_method("POST")
}

HTTP:set_status(status-code) \ HTTP:set_status("status-code", “reason”)

Change the status code and reason in HTTP response header. If reason does not exist, use default reason.

Return true for success and false for failure.

Syntax
HTTP:set_status(status_code) \ HTTP:set_status(status_code, reason)
Arguments

Name

Description

status_code

Integer status code

reason

Optional, string type reason.

Events

Applicable in HTTP_RESPONSE and HTTP_DATA_RESPONSE

Example
when HTTP_RESPONSE {
    HTTP:set_status(200, "Other Reason")
}

HTTP:add_header(“header-name”, “header-value”)

Add a header line to HTTP request or response header.

Return true for success and false for failure.

Syntax
HTTP:add_header(header_name, header_value)
Arguments

Name

Description

header_name

String type header_name

header_value

String type header_value

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:add_header("X-COUNTRY-FMF", ip.geo(client_ip) or "unknown") -- add a new header line
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
         rewrite_request(HTTP, IP, HTTP:args())
    end
}

HTTP:del_header(“header-name”)

Remove the header with name “header-name” from HTTP request or response.

Return true for success and false for failure.

Syntax
HTTP:del_header(header_name)
Arguments

Name

Description

header_name

String type header_name

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE events.

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:del_header("test")
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
        rewrite_request(HTTP, IP, HTTP:args())
    end
}

HTTP:set_header(“header-name”, "header-value-array")

Remove the header with name “header-name” from HTTP request or response, and add this header with new value header-value-array. The argument header-value-array is a Lua array which is the value got from HTTP:header().

Return true for success and false for failure.

Syntax
HTTP:set_header(header_name, header_value_array)
Arguments

Name

Description

header_name

String type header_name

header_value_array

Lua array

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE events.

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:set_header("test", { "line1", "line2", "line3" })
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
        rewrite_request(HTTP, IP, HTTP:args())
    end
}

HTTP:replace_header(“header-name”, “regex”, “replace”)

Match the regular expression in all occurrences of header field “header-name” according to “regex”, and replaces them with the “replace” argument. The replacement value can contain back references like 1,2, …

Return true for success and false for failure.

Syntax
HTTP:replace_header(header_name, regex, replace)
Arguments

Name

Description

header_name

String type header_name

regex

Match what to be replaced.

replace

Content to replace argument with.

Events

Applicable in HTTP_REQUEST and HTTP_RESPONSE events.

Example
function rewrite_request(HTTP, IP, args)
    debug("%s", IP:client_addr())
    client_ip = IP:client_addr()
    -- add/del/set header
    HTTP:replace_header("Set-Cookie", [[(.*)(Path=\/)(.*)]], [[\1\2api\3]])
end
when HTTP_REQUEST{
    local path = HTTP:path()
    if path == "/rewrite_request" then
        rewrite_request(HTTP, IP, HTTP:args())
    end
}