Lua API
Page not available in that version
The current page Lua API doesn't exist in version 1.6.2 of the documentation for this product.
Overview
The Lua API manages the custom Lua scripts that routing loads beside the CDN Director’s Lua standard library. It lists, reads, stores and deletes scripts, and evaluates a Lua expression against them without side effects.
Scripts are sent and served as application/x-lua, in UTF-8. A script is stored byte for byte as sent.
Base URL
https://<manager-host>/api/v1/lua
Script Paths
{path} is where a script is stored, relative and with no leading slash, such as lib/helpers.lua.
- It has one or more segments separated by
/. No segment may be empty,.or.., or longer than 255 bytes. - The last segment must end in
.luaand must not start with.. - No other segment may contain
*,?,[or\. - The whole path has no NUL byte and is at most 3968 bytes.
- Percent-encoded bytes are decoded first, and must decode to UTF-8.
//is read as/, and a trailing/is dropped. - Paths are compared byte for byte, so case matters.
- A path cannot name both a script and a folder of scripts:
helper.luaandhelper.lua/child.luacannot both be stored.
A path that breaks a rule is answered with 400, and message says which rule.
Endpoints
GET /api/v1/lua
List every custom script.
Success Response (200): an array of the scripts’ paths and checksums, ordered by the bytes of the path. With no scripts it is an empty array. file_checksum is the lowercase hex MD5 of the script’s bytes.
[
{ "path": "lib/helpers.lua", "file_checksum": "3bc8e173a68e60bfbbf5c803364978c9" }
]
Errors:
503- Service unavailable
Caching: the response carries an ETag. A request whose If-None-Match is * or lists that tag gets an empty 304.
Example:
curl -s "https://cdn-manager/api/v1/lua" | jq '.'
GET /api/v1/lua/{path}
Read one script, byte for byte as stored.
Success Response (200): the script, as application/x-lua. The ETag is the script’s strong entity tag: the script’s MD5 in quotes, as in "3bc8e173a68e60bfbbf5c803364978c9".
Errors:
400- The path breaks a path rule404- No script is stored at the path503- Service unavailable
Caching: a request whose If-None-Match is * or lists the script’s tag, compared weakly, gets an empty 304.
Example:
curl -s "https://cdn-manager/api/v1/lua/lib/helpers.lua"
HEAD /api/v1/lua/{path}
Check one script. It returns the headers a GET returns, with the script’s length as Content-Length, and no body. The errors and caching are those of GET.
Example:
curl -I "https://cdn-manager/api/v1/lua/lib/helpers.lua"
PUT /api/v1/lua/{path}
Store a script at the path, replacing any script stored there.
Request: the script, as application/x-lua. Media-type parameters such as charset are ignored. The body must be UTF-8 and hold no NUL byte. An empty body is an empty script. Storing the same bytes again changes nothing. The body is limited by tuning.max_body_bytes, 20 MiB (20971520 bytes) by default.
Conditions: with If-Match or Match-If, the script is stored only if the condition holds. See Conditional Writes.
The script is first loaded beside the Lua standard library and the other stored scripts, in path order, as routing loads them. It is refused if it fails to load itself. Another stored script that fails to load does not refuse it.
Success: 204 No Content
Errors:
400- The path breaks a path rule, the body is not UTF-8 or holds a NUL byte, it was not sent asapplication/x-lua, orIf-MatchorMatch-Ifis malformed.messagesays which.409- A stored script’s path is a folder of this path, or this path is a folder of a stored script’s.messagenames both scripts. Nothing is stored.412- The condition did not hold. Nothing is changed.413- The body is larger than the limit.messagenames the limit in bytes.422- The script fails to load.messagenames the script and quotes the Lua error. Nothing is stored.503- The storage backend is unavailable, the Lua standard library cannot be read, or the custom scripts stayed locked for the whole wait budget. Only the last carriesRetry-After.
Example:
curl -s -X PUT "https://cdn-manager/api/v1/lua/lib/helpers.lua" \
-H "Content-Type: application/x-lua" \
--data-binary @helpers.lua
DELETE /api/v1/lua/{path}
Delete a script. With If-Match or Match-If, it is deleted only if the condition holds. See Conditional Writes.
Success: 204 No Content
Errors:
400- The path breaks a path rule, orIf-MatchorMatch-Ifis malformed404- No script is stored at the path, and no condition was sent412- The condition did not hold. Nothing is changed.503- The storage backend is unavailable, or the custom scripts stayed locked for the whole wait budget. Only the latter carriesRetry-After.
Example:
curl -s -X DELETE "https://cdn-manager/api/v1/lua/lib/helpers.lua"
POST /api/v1/lua/debug
Evaluate a Lua expression once, with the Director’s Lua standard library and every stored script loaded, reading the current selection input.
Request: the expression, as application/x-lua in UTF-8. The manager evaluates return <body>, so the body is an expression: a statement fails to compile. An empty body is an empty expression. Only the first returned value is used.
The evaluation has no side effects: send_to_data_stream and the metrics counters do nothing, secrets.get_secret returns nil, and nothing is logged.
Limits: the evaluation runs under an instruction limit. What it prints is capped by tuning.custom_lua.debug.max_stdout_bytes, and its value, as JSON, by tuning.custom_lua.debug.max_value_bytes, 1 MiB each by default. Passing a limit answers success: false. At most tuning.custom_lua.debug.max_concurrent evaluations, 4 by default, run at once.
Success Response (200): the evaluation, whether it succeeded or not.
{
"success": true,
"return": { "lua_type_name": "number", "value": 42 },
"stdout": "",
"error_msg": ""
}
| Field | Meaning |
|---|---|
success | Whether the expression ran and its value converted within the limits |
return.lua_type_name | Lua’s type() name for the value. Empty when the expression failed to compile or run, or its stdout passed its limit |
return.value | The value as JSON, or null on failure |
stdout | What the expression printed, one line per print, joined by newlines. Output from loading the scripts is not included |
error_msg | Why the evaluation failed, or empty on success |
The value is converted to JSON as follows:
nil,NaNand the infinities arenull.- A table whose keys are exactly
1..nis an array, an empty table included. - Any other table is an object sorted by the bytes of its keys, with a number or boolean key written in brackets, as
[3]or[true]. - A function, thread or userdata, a table that contains itself, more than 64 nested tables, or a string that is not UTF-8 fails the evaluation.
Errors:
400- The body is not UTF-8, or it was not sent asapplication/x-lua.messagesays which.413- The body is larger thantuning.max_body_bytes.messagenames the limit in bytes.503- The custom scripts or the Lua standard library cannot be loaded yet, or the debug concurrency limit was reached. Only the latter carriesRetry-After.
Example:
curl -s -X POST "https://cdn-manager/api/v1/lua/debug" \
-H "Content-Type: application/x-lua" \
--data-binary '40 + 2' | jq '.'
Conditional Writes
PUT and DELETE accept a condition, so that a script changed since you read it is not overwritten.
If-Match: write only if the script stored at the path has one of these entity tags, the quotedETaga read returned, or*for any stored script. Tags are compared strongly, so a weakW/tag never matches. It fails when no script is stored.If-None-Matchis ignored on writes.Match-If: the same condition, under the name the CDN Director reads. A tag may also be sent unquoted. When both headers are sent, both must hold.
A condition that does not hold is answered with 412, and message says whether the script has changed since the tag was read or does not exist.
Example:
ETAG=$(curl -s -I "https://cdn-manager/api/v1/lua/lib/helpers.lua" \
| tr -d '\r' | awk -F': ' 'tolower($1)=="etag" {print $2}')
curl -s -X PUT "https://cdn-manager/api/v1/lua/lib/helpers.lua" \
-H "Content-Type: application/x-lua" \
-H "If-Match: $ETAG" \
--data-binary @helpers.lua
Replication Headers
A read of the script list or of one script carries a generation header and, when the scripts are replicated, a bootstrap-offset header. A client that follows the scripts’ replication topic uses them to order what it reads against the notifications it receives: start the consumer at bootstrap-offset, and apply only messages whose generation is greater than generation. A 404 for a script that is not stored carries generation too.
Next Steps
- Routing API - GeoIP lookups, IP validation and routing evaluation
- Discovery API - Host and namespace discovery
- OpenAPI Specification - Complete API specification