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

Hash-map based storage and parsing helpers for URL query parameters. More...

#include <cwist/net/http/query.h>
#include <cwist/core/siphash/siphash.h>
#include <cwist/core/mem/alloc.h>
#include <cwist/core/mem/arena.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
Include dependency graph for query.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define CWIST_QUERY_MAP_DEFAULT_SIZE   16
 

Functions

cwist_query_map * cwist_query_map_create (void)
 Allocate a query map and seed its SipHash key material.
 
cwist_query_map * cwist_query_map_create_in_arena (void *arena)
 Allocate a query map within a memory arena to avoid GC/heap allocations.
 
void cwist_query_map_destroy (cwist_query_map *map)
 Release every bucket node and the map container itself.
 
void cwist_query_map_clear (cwist_query_map *map)
 Remove every key/value pair while keeping the bucket array allocated.
 
void cwist_query_map_set (cwist_query_map *map, const char *key, const char *value)
 Insert or replace a decoded query parameter in the map.
 
const char * cwist_query_map_get (cwist_query_map *map, const char *key)
 Look up a decoded query parameter by key.
 
void cwist_query_map_delete (cwist_query_map *map, const char *key)
 Remove a key/value pair from the map if present.
 
void cwist_query_map_foreach (cwist_query_map *map, cwist_query_map_iter_func cb, void *ctx)
 Invoke a callback for every key/value pair stored in the map.
 
void cwist_query_map_parse (cwist_query_map *map, const char *raw_query)
 Parse a raw a=1&b=2 query string into the existing map.
 

Detailed Description

Hash-map based storage and parsing helpers for URL query parameters.

Definition in file query.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 1 of file query.c.

◆ CWIST_QUERY_MAP_DEFAULT_SIZE

#define CWIST_QUERY_MAP_DEFAULT_SIZE   16

Definition at line 16 of file query.c.

Function Documentation

◆ cwist_query_map_clear()

void cwist_query_map_clear ( cwist_query_map *  map)

Remove every key/value pair while keeping the bucket array allocated.

Clear all entries.

Parameters
mapQuery map to clear in-place.

Definition at line 93 of file query.c.

References cap, and cwist_query_bucket::next.

Referenced by cwist_query_map_destroy().

◆ cwist_query_map_create()

cwist_query_map * cwist_query_map_create ( void  )

Allocate a query map and seed its SipHash key material.

Create a new query map.

Returns
Newly allocated map, or NULL when memory allocation fails.

Definition at line 54 of file query.c.

References cap, and cwist_query_map_create_in_arena().

Referenced by cwist_flash_set(), and cwist_session_start().

◆ cwist_query_map_create_in_arena()

cwist_query_map * cwist_query_map_create_in_arena ( void *  arena)

Allocate a query map within a memory arena to avoid GC/heap allocations.

Definition at line 61 of file query.c.

References cap, cwist_generate_hash_seed(), and CWIST_QUERY_MAP_DEFAULT_SIZE.

Referenced by cwist_query_map_create().

◆ cwist_query_map_delete()

void cwist_query_map_delete ( cwist_query_map *  map,
const char *  key 
)

Remove a key/value pair from the map if present.

Delete a key from the map.

Parameters
mapQuery map to modify; NULL is a no-op.
keyKey to delete; NULL is a no-op.
Return values
Inheap-managed maps the node and its strings are freed.
Inarena-managed maps the node is only unlinked; storage is reclaimed with the arena. (Nothing is freed.)

Definition at line 220 of file query.c.

References cap, cwist_query_bucket::next, and siphash24().

Referenced by cwist_session_delete().

◆ cwist_query_map_destroy()

void cwist_query_map_destroy ( cwist_query_map *  map)

Release every bucket node and the map container itself.

Destroy a query map.

Parameters
mapQuery map to destroy.

Definition at line 81 of file query.c.

References cap, and cwist_query_map_clear().

Referenced by cwist_http_request_destroy(), cwist_mux_find_route(), cwist_mux_serve(), cwist_session_destroy(), and cwist_session_start().

◆ cwist_query_map_foreach()

void cwist_query_map_foreach ( cwist_query_map *  map,
cwist_query_map_iter_func  cb,
void *  ctx 
)

Invoke a callback for every key/value pair stored in the map.

Iterate over all key-value pairs in the map.

Parameters
mapQuery map to iterate; NULL is a no-op.
cbCallback invoked as cb(key, value, ctx) for each entry; NULL is a no-op.
ctxOpaque pointer forwarded to the callback for each entry.
Note
Iteration order is bucket order and is not sorted.

Definition at line 252 of file query.c.

References cap.

Referenced by cwist_session_commit().

◆ cwist_query_map_get()

const char * cwist_query_map_get ( cwist_query_map *  map,
const char *  key 
)

Look up a decoded query parameter by key.

Get a value by key.

Parameters
mapQuery map to search.
keyDecoded key to retrieve.
Returns
Stored value, or NULL when the key is absent.

Definition at line 196 of file query.c.

References cap, siphash24(), and cwist_query_bucket::value.

Referenced by cwist_cookie_get(), cwist_flash_peek(), cwist_query_map_parse(), cwist_session_get(), and cwist_url_for().

◆ cwist_query_map_parse()

void cwist_query_map_parse ( cwist_query_map *  map,
const char *  raw_query 
)

Parse a raw a=1&b=2 query string into the existing map.

Parse raw query strings (e.g., "a=1&b=2") into the map.

Parameters
mapDestination map that receives decoded keys and values.
raw_queryRaw query substring without the leading question mark.

Definition at line 319 of file query.c.

References cap, cwist_query_map_get(), and cwist_query_map_set().

◆ cwist_query_map_set()

void cwist_query_map_set ( cwist_query_map *  map,
const char *  key,
const char *  value 
)

Insert or replace a decoded query parameter in the map.

Set a key-value pair.

Parameters
mapQuery map that owns the entry storage.
keyDecoded query-string key.
valueDecoded query-string value.

Definition at line 146 of file query.c.

References cap, cwist_query_bucket::key, cwist_query_bucket::next, siphash24(), and cwist_query_bucket::value.

Referenced by cwist_cookie_parse(), cwist_flash_set(), cwist_query_map_parse(), and cwist_session_set().