|
cwist
Modern C web framework with libttak runtime
|
Content-hashed in-memory assets: registration, URL lookup and serving. More...
#include "assets_internal.h"#include <cwist/sys/app/assets.h>#include <cwist/core/crypto/sha256.h>#include <cwist/core/mem/alloc.h>#include <stdatomic.h>#include <stdio.h>#include <string.h>
Go to the source code of this file.
Macros | |
| #define | ASSET_HASH_HEX 16 |
Typedefs | |
| typedef struct cwist_asset_registry | cwist_asset_registry |
Functions | |
| cwist_error_t | cwist_app_asset_prefix (cwist_app *app, const char *url_prefix) |
| Set the URL prefix under which assets are served. | |
| cwist_error_t | cwist_app_asset_add (cwist_app *app, const char *name, const void *data, size_t len, const char *content_type) |
| Publish an in-memory buffer as an asset. | |
| cwist_error_t | cwist_app_asset_add_file (cwist_app *app, const char *name, const char *path, const char *content_type) |
| Read a file from disk and publish it as an asset. | |
| const char * | cwist_app_asset_url (cwist_app *app, const char *name) |
Return the URL of the newest asset registered under name. | |
| bool | cwist_assets_match (cwist_app *app, const cwist_http_request *req, cwist_asset_match *out) |
| Match an HTTP request to a registered asset. | |
| void | cwist_assets_respond (cwist_http_request *req, cwist_http_response *res, const cwist_asset_match *match) |
| Fill a response for a matched asset. | |
| int | cwist_assets_clone (void **dst, const void *src) |
| Deep-copy a registry (used when an app is cloned). | |
| void | cwist_assets_destroy (void *registry) |
Content-hashed in-memory assets: registration, URL lookup and serving.
Assets are immutable once published. A registry is a newest-first singly linked list whose head is swapped in atomically, and nodes are only freed with the app, so a request that resolved an asset can keep using it while another thread registers more.
Definition in file assets.c.
| cwist_error_t cwist_app_asset_add | ( | cwist_app * | app, |
| const char * | name, | ||
| const void * | data, | ||
| size_t | len, | ||
| const char * | content_type | ||
| ) |
Publish an in-memory buffer as an asset.
Register an asset from memory. The data is copied.
Copies data; on success the asset is immutable and served under its content-hashed name. Duplicate content is a no-op.
| app | App instance; must not be NULL. |
| name | Logical asset name; must be a valid relative path. |
| data | Asset bytes; may be NULL only when len is 0. |
| len | Length of data in bytes. |
| content_type | Optional Content-Type override; if NULL a type is guessed from name. |
Definition at line 306 of file assets.c.
References cap, and cwist_alloc().
| cwist_error_t cwist_app_asset_add_file | ( | cwist_app * | app, |
| const char * | name, | ||
| const char * | path, | ||
| const char * | content_type | ||
| ) |
Read a file from disk and publish it as an asset.
Register an asset from a file, read once now.
The file is read fully into memory; on success its content is served like an asset added with cwist_app_asset_add().
| app | App instance; must not be NULL. |
| name | Logical asset name; must be a valid relative path. |
| path | Filesystem path to read; must not be NULL. |
| content_type | Optional Content-Type override; if NULL a type is guessed from name. |
Definition at line 336 of file assets.c.
References cap, cwist_alloc(), cwist_free(), and cwist_realloc().
| cwist_error_t cwist_app_asset_prefix | ( | cwist_app * | app, |
| const char * | url_prefix | ||
| ) |
Set the URL prefix under which assets are served.
Choose the URL prefix assets are served under.
Must be called before any asset is registered; fails once the registry is non-empty.
| app | App instance; must not be NULL. |
| url_prefix | Absolute path such as "/assets" (trailing '/' stripped), or "/" for the root. |
Definition at line 197 of file assets.c.
References cap, and cwist_free().
Return the URL of the newest asset registered under name.
Content-hashed URL of the newest asset registered under name.
The returned string is owned by the asset registry and stays valid until the app is destroyed.
| app | App instance; must not be NULL. |
| name | Logical asset name. |
Definition at line 386 of file assets.c.
References cap.
Deep-copy a registry (used when an app is cloned).
Copies the prefix and every asset, preserving newest-first order. On failure *dst stays NULL and no partial registry is left behind.
| dst | Receives the new registry; set to NULL first. |
| src | Registry to copy; may be NULL, which is treated as empty. |
Definition at line 514 of file assets.c.
References ASSET_HASH_HEX, cap, cwist_alloc(), cwist_assets_destroy(), cwist_free(), cwist_strdup(), and head.
Free a registry and every asset in it. No-op for NULL.
Definition at line 570 of file assets.c.
References cap, and cwist_free().
Referenced by cwist_app_destroy(), and cwist_assets_clone().
| bool cwist_assets_match | ( | cwist_app * | app, |
| const cwist_http_request * | req, | ||
| cwist_asset_match * | out | ||
| ) |
Match an HTTP request to a registered asset.
Only GET and HEAD requests under the registry prefix are considered. A request for a hashed (immutable) URL takes precedence over a logical name.
| app | App instance; must not be NULL. |
| req | Request to match; path must be present. |
| out | Filled with the matched asset and its immutability flag on match. |
Definition at line 410 of file assets.c.
References cwist_asset_match::asset, cwist_app::assets, cap, CWIST_HTTP_GET, CWIST_HTTP_HEAD, cwist_sstring::data, cwist_asset_match::immutable, cwist_http_request::method, and cwist_http_request::path.
| void cwist_assets_respond | ( | cwist_http_request * | req, |
| cwist_http_response * | res, | ||
| const cwist_asset_match * | match | ||
| ) |
Fill a response for a matched asset.
Always sets ETag and Cache-Control; honors If-None-Match with 304 and an empty body. The body points directly at the asset data, which stays alive until the app is destroyed.
| req | Request being answered. |
| res | Response to fill. |
| match | Match from cwist_assets_match(); if NULL or without an asset, the response status is set to 500. |
Definition at line 475 of file assets.c.
References cwist_http_response::body, cap, CWIST_ASSET_IMMUTABLE_CACHE, cwist_error_dispose(), cwist_http_header_get(), CWIST_HTTP_INTERNAL_ERROR, CWIST_HTTP_NOT_MODIFIED, CWIST_HTTP_OK, cwist_http_response_set_body_ptr(), cwist_sstring_assign(), cwist_http_request::headers, and cwist_http_response::status_code.