URL, path, and query helpers

Module <cweb/pathutils.h>

Parsing and normalization of paths and query strings, plus simple MIME helpers by file extension.

Constant: UTILS_MAX_PATH_LEN — maximum length per path segment (256).


Query and base path

char *cweb_get_route_base(const char *path)

  • Purpose: Extracts the “base” path without query (?) and without the dynamic remainder after the first segment (framework-specific).
  • Parameters: path — full request path.
  • Return: Newly malloc-allocated string or NULL.
  • Memory: caller must free.

char *cweb_get_query_string(char *path)

  • Purpose: Points to the part after the first ? within the same buffer (no allocation).
  • Return: Pointer into path or NULL.

bool cweb_has_query_param(char *path, char *key)

  • Purpose: Checks whether a query parameter exists.
  • Parameters: path is used for query search (not necessarily mutated).

char *cweb_get_query_param(char *path, char *key)

  • Purpose: Value of a query key.
  • Return: Newly allocated string (strdup; leak tracker may register) or NULL.
  • Memory: release with free.

int cweb_get_query_param_int(char *path, char *key, int default_value)

  • Purpose: Same as string variant but via atoi; if the key is missing returns default_value.
  • Memory: internal free of temporary string — caller only receives the int.

Path segments

int cweb_split_path_segments(const char *path, char segments[][UTILS_MAX_PATH_LEN], int max_segments)

  • Purpose: Splits path on / into fixed segments fields.
  • Return: Number of segments.

char *cweb_get_path_segment(const char *path, int index)

  • Purpose: Returns segment index as a new string.
  • Memory: caller must free.

int cweb_path_segment_count(const char *path)

  • Purpose: Segment count without returning each segment individually.

Normalization and matching

void cweb_normalize_path(char *path)

  • Purpose: Removes duplicate slashes and optionally trailing / (except root).
  • Parameters: mutates the buffer in place.

bool cweb_path_matches(char *pattern, char *path)

  • Purpose: Compares pattern with optional placeholders (: parameters, * — wildcard per implementation).
  • Parameters: may be modified depending on routing; use copies when in doubt.

char *cweb_get_path_param(char *pattern, char *path, char *param_name)

  • Purpose: Extracts the value of a named path parameter (:name in the pattern).
  • Return: strdup string or NULL.
  • Memory: caller must free.

URL decoding

void cweb_url_decode(char *dst, const char *src, size_t max_len)

  • Purpose: Decodes %HH and + as space into dst.
  • Parameters: max_len — limit for dst (incl. terminator).

Type helpers (file type by extension)

int cweb_is_path_file(const char *path)

  • Purpose: Heuristic: behaves like “path points to file with extension” (1) vs. plain path (0).

int cweb_path_isnt_textbased(const char *path)

  • Purpose: Returns a value classifying “not primarily text-based” (consult implementation).

int cweb_is_path_img(const char *path)

  • Purpose: Non-zero for typical image extensions (png, jpg, …).

Query as map

Type QueryParam

key[64], value[256] — predefined field sizes.

int cweb_parse_query_params(char *query, QueryParam *params, int max_params)

  • Purpose: Parses a query string key=value&… into an array of QueryParam.
  • Parameters: query should be passed without a leading ? (a typical call uses the result of cweb_get_query_string).
  • Return: Number of filled entries.

See also