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

flash.c interface. More...

#include <cwist/sys/session/flash.h>
#include <cwist/core/mem/alloc.h>
#include <string.h>
#include <stdio.h>
#include <cwist/core/siphash/siphash.h>
Include dependency graph for flash.c:

Go to the source code of this file.

Functions

void cwist_flash_set (cwist_http_request *req, const char *key, const char *value)
 Store a flash entry on the request.
 
const char * cwist_flash_peek (cwist_http_request *req, const char *key)
 Read a flash entry without removing it.
 
const char * cwist_flash_get (cwist_http_request *req, const char *key)
 Read and remove a flash entry.
 
char * cwist_flash_pop_all_json (cwist_http_request *req)
 Serialize all flash entries to a JSON object and clear the map.
 

Detailed Description

flash.c interface.

Definition in file flash.c.

Function Documentation

◆ cwist_flash_get()

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

Read and remove a flash entry.

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 
)

Read a flash entry without removing it.

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 entries to a JSON object and clear the map.

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 
)

Store a flash entry 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.