livery_static (livery v0.13.0)

View Source

Static-directory file handler.

Returns a handler that serves files from a root directory, inferring Content-Type from the extension, emitting a weak ETag for conditional GET, honoring Range, and confining every path under the root so a request can never traverse out of it.

Mount it on a router wildcard route and read the captured sub-path:

Router = livery_router:add(
    '_', <<"/assets/*path">>, livery_static:handler("priv/assets"), #{}, Router0
).

Without a router binding it falls back to stripping a configured prefix from the request path. Options (all optional): binding (default <<"path">>), prefix, index (default <<"index.html">>, or false), cache_control (binary | directive list | undefined), etag (default true), range (default true).

Security: the sub-path is percent-decoded and then confined - any .. segment, control byte, or escape is rejected - and only regular files are served. A symlink anywhere below the root, and a directory without an index, yield 404. The checks run before the file is opened, so the root must not be writable by anyone you do not trust. See AGENTS.md, Known deviations.

Summary

Functions

Static handler serving regular files under Root.

Types

opts()

-type opts() ::
          #{binding => binary(),
            prefix => binary(),
            index => binary() | false,
            cache_control => binary() | [livery_resp:cache_directive()] | undefined,
            etag => boolean(),
            range => boolean()}.

Functions

handler(Root)

-spec handler(file:name_all()) -> livery_middleware:handler().

Static handler serving regular files under Root.

handler(Root, Opts)

-spec handler(file:name_all(), opts()) -> livery_middleware:handler().

handler/1 with options.