Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
url.c File Reference

URL path splitting and percent-decoding utilities. More...

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "context_internal.h"
#include "csilk/core/internal.h"
#include "csilk/csilk.h"
Include dependency graph for url.c:

Functions

static int hex_to_int (char c)
 Helper: convert a hexadecimal character to its integer value.
 
size_t csilk_url_decode (char *str)
 URL-decode a percent-encoded string in-place.
 
void csilk_split_url (const char *url, char **path, char **query)
 Split a full URL into its path and query string components.
 

Detailed Description

URL path splitting and percent-decoding utilities.

Provides the two fundamental URL operations needed by the HTTP server:

  1. csilk_url_decode() — in-place percent-decoding (XX -> byte, '+' -> space)
  2. csilk_split_url() — separates "path?query" into decoded path + raw query

These are used by the HTTP parser during request finalization to populate the request context's path and query_params fields.

Function Documentation

◆ csilk_split_url()

void csilk_split_url ( const char *  url,
char **  path,
char **  query 
)

Split a full URL into its path and query string components.

Split a URL into path and query-string components.

Finds the first '?' separator. The path portion is URL-decoded and returned in path. The query portion (everything after '?') is returned raw in query (NOT URL-decoded — use csilk_parse_query() for that). If there is no '?', the entire URL is treated as the path and query is set to NULL.

Parameters
urlFull URL string (e.g., "/foo/bar?key=val").
path[out] Receives a malloc'd, URL-decoded path string.
query[out] Receives a malloc'd raw query string, or NULL if no query was present.
Note
Both output strings must be freed by the caller with free(). On allocation failure, both outputs may be NULL.

◆ csilk_url_decode()

size_t csilk_url_decode ( char *  str)

URL-decode a percent-encoded string in-place.

Replaces XX sequences with the corresponding byte value and '+' with space. The decoding is done in-place so the output is never longer than the input.

Parameters
strNull-terminated string to decode (modified in-place).
Returns
The length of the decoded string (may be shorter than original).
Note
If str contains invalid % sequences (e.g., "%ZZ"), they are left as-is.

◆ hex_to_int()

static int hex_to_int ( char  c)
static

Helper: convert a hexadecimal character to its integer value.

Handles both uppercase ('A'-'F') and lowercase ('a'-'f') hex digits.

Parameters
cHex character ('0'-'9', 'a'-'f', or 'A'-'F').
Returns
Integer value 0-15, or -1 if c is not a valid hex digit.