Lua API

Store, read and try out custom Lua scripts
You're viewing a development version of manager, the latest released version is 1.6.2

The current page Lua API doesn't exist in version 1.6.2 of the documentation for this product.
We can take you to the closest parent section instead: /docs/acd/components/manager/1.6.2/api_guide/

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 .lua and 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.lua and helper.lua/child.lua cannot 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 rule
  • 404 - No script is stored at the path
  • 503 - 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 as application/x-lua, or If-Match or Match-If is malformed. message says which.
  • 409 - A stored script’s path is a folder of this path, or this path is a folder of a stored script’s. message names both scripts. Nothing is stored.
  • 412 - The condition did not hold. Nothing is changed.
  • 413 - The body is larger than the limit. message names the limit in bytes.
  • 422 - The script fails to load. message names 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 carries Retry-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, or If-Match or Match-If is malformed
  • 404 - No script is stored at the path, and no condition was sent
  • 412 - 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 carries Retry-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": ""
}
FieldMeaning
successWhether the expression ran and its value converted within the limits
return.lua_type_nameLua’s type() name for the value. Empty when the expression failed to compile or run, or its stdout passed its limit
return.valueThe value as JSON, or null on failure
stdoutWhat the expression printed, one line per print, joined by newlines. Output from loading the scripts is not included
error_msgWhy the evaluation failed, or empty on success

The value is converted to JSON as follows:

  • nil, NaN and the infinities are null.
  • A table whose keys are exactly 1..n is 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 as application/x-lua. message says which.
  • 413 - The body is larger than tuning.max_body_bytes. message names 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 carries Retry-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 quoted ETag a read returned, or * for any stored script. Tags are compared strongly, so a weak W/ tag never matches. It fails when no script is stored. If-None-Match is 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