Response Translation Function
Specifies the body of a Lua function that inspects every outgoing HTTP response and overwrites individual fields before being sent to the client.
Returns nil
when nothing is to be changed, or HTTPResponse(t)
where t
is a table with any of the following optional fields:
Code
- Description: Replaces status code in the response being sent.
- Type:
integer
- Example:
200
,404
Text
- Description: Replaces status text in the response being sent.
- Type:
string
- Example:
'OK'
,'Not found'
MajorVersion
- Description: Replaces major HTTP version such as
x
inHTTP/x.1
in the response being sent. - Type:
integer
- Example:
1
- Description: Replaces major HTTP version such as
MinorVersion
- Description: Replaces minor HTTP version such as
x
inHTTP/1.x
in the response being sent. - Type:
integer
- Example:
1
- Description: Replaces minor HTTP version such as
Protocol
- Description: Replaces protocol in the response being sent.
- Type:
string
- Example:
'HTTP'
,'HTTPS'
Body
- Description: Replaces body in the response being sent.
- Type:
string
ornil
- Example:
'{"foo": "bar"}'
Headers
- Description: Adds, removes or replaces individual headers in the response being sent.
- Type: nested
table
(indexed by number) representing an array of response headers as{[1]='Name',[2]='Value'}
pairs that are added to the response being sent, or overwriting existing request headers with colliding names. To remove a header from the response, specifynil
as value, i.e.Headers={..., {[1]='foo',[2]=nil} ...}
. Duplicate names are supported. A multi-value header such asFoo: bar1,bar2
is defined by specifyingHeaders={..., {[1]='foo',[2]='bar1'}, {[1]='foo',[2]='bar2'}, ...}
.
Example of a response_translation_function
body that sets the Location
header to a hardcoded value:
-- Statements go here
print('Setting hardcoded Location')
return HTTPResponse({
Headers = {
{'Location', 'cdn1.com/content.mpd?a=b'}
}
})
Arguments
The following (iterable) arguments will be known by the function:
Headers
Type: nested
table
(indexed by number).Description: Array of response headers as
{[1]='Name',[2]='Value'}
pairs that are present in the response being sent. Format identical to theHTTPResponse.Headers
-field specified for the return value above. A multi-value header such asFoo: bar1,bar2
is seen inresponse_translation_function
asHeaders={..., {[1]='foo',[2]='bar1'}, {[1]='foo',[2]='bar1'}, ...}
.Example usage:
for _, header in pairs(Headers) do print(header[1]..'='..header[2]) end
Additional data
In addition to the arguments above, the following Lua tables, documented in Global Lua Tables, provide additional data that is available when executing the response translation function: