cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
flash.h File Reference

One-time flash messages (ala Flask flash/get_flashed_messages). More...

Include dependency graph for flash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void cwist_flash_set (cwist_http_request *req, const char *key, const char *value)
 Set a flash message on the request.
 
const char * cwist_flash_get (cwist_http_request *req, const char *key)
 Get a flash message and consume it (one-time read).
 
const char * cwist_flash_peek (cwist_http_request *req, const char *key)
 Peek at a flash message without consuming it.
 
char * cwist_flash_pop_all_json (cwist_http_request *req)
 Serialize all flash messages to a JSON object string. Caller must free the returned string.
 

Detailed Description

One-time flash messages (ala Flask flash/get_flashed_messages).

Definition in file flash.h.

Function Documentation

◆ cwist_flash_get()

const char * cwist_flash_get ( cwist_http_request *  req,
const char *  key 
)

Get a flash message and consume it (one-time read).

Returns
The message value, or NULL if not found.

Get a flash message and consume it (one-time read).

Destructive lookup: on success the entry's bucket is unlinked and freed, and ownership of the value string is transferred to the caller, who must release it with cwist_free().

Parameters
reqRequest whose flash map is read.
keyEntry key to look up.
Returns
Newly owned copy pointer to the value (caller frees), or NULL if req/key are NULL, the flash map is empty, or the key is missing.

Definition at line 55 of file flash.c.

References cwist_query_map::buckets, cap, cwist_free(), cwist_http_request::flash, cwist_query_bucket::next, cwist_query_map::seed, siphash24(), and cwist_query_map::size.

◆ cwist_flash_peek()

const char * cwist_flash_peek ( cwist_http_request *  req,
const char *  key 
)

Peek at a flash message without consuming it.

Returns
The message value, or NULL if not found.

Peek at a flash message without consuming it.

Non-destructive lookup; the entry remains in the flash map.

Parameters
reqRequest whose flash map is read.
keyEntry key to look up.
Returns
Pointer to the stored value, or NULL if req/key are NULL or the key is not present. The pointer stays owned by the flash map.

Definition at line 39 of file flash.c.

References cap, cwist_query_map_get(), and cwist_http_request::flash.

◆ cwist_flash_pop_all_json()

char * cwist_flash_pop_all_json ( cwist_http_request *  req)

Serialize all flash messages to a JSON object string. Caller must free the returned string.

Returns
JSON string like {"key":"value",...} or NULL if empty.

Serialize all flash messages to a JSON object string. Caller must free the returned string.

Builds a JSON object string {"key":"value",...} with '"' and '\' escaped in keys and values, then frees every flash entry and empties the map.

Parameters
reqRequest whose flash map is serialized.
Returns
Newly allocated NUL-terminated JSON string the caller must free with cwist_free(), or NULL if there are no entries or if a buffer allocation fails (in which case the flash map is left unchanged).

Definition at line 87 of file flash.c.

References cwist_query_map::buckets, cap, count, cwist_alloc(), cwist_free(), cwist_realloc(), cwist_http_request::flash, cwist_query_bucket::key, cwist_query_bucket::next, cwist_query_map::size, and cwist_query_bucket::value.

◆ cwist_flash_set()

void cwist_flash_set ( cwist_http_request *  req,
const char *  key,
const char *  value 
)

Set a flash message on the request.

Set a flash message on the request.

Creates the request's flash query map on first use. A NULL value is stored as an empty string.

Parameters
reqRequest to attach the flash entry to; ignored if NULL.
keyEntry key; ignored if NULL.
valueEntry value; NULL is stored as "".

Definition at line 20 of file flash.c.

References cwist_query_map_create(), cwist_query_map_set(), and cwist_http_request::flash.