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

Request parameter validation middleware (required, type checking, email). More...

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

Functions

static const char * str_find (const char *s, char c)
 Find the first occurrence of a character in a string (helper).
 
static int is_valid_email (const char *s)
 Check if a string looks like a valid email address.
 
const char * csilk_validate (csilk_ctx_t *c, const csilk_valid_rule_t *rules)
 Run all validation rules and return the first error, or NULL on success.
 

Detailed Description

Request parameter validation middleware (required, type checking, email).

Function Documentation

◆ csilk_validate()

const char * csilk_validate ( csilk_ctx_t *  c,
const csilk_valid_rule_t rules 
)

Run all validation rules and return the first error, or NULL on success.

Validate request parameters against a set of rules.

Iterates through an array of validation rules, stopping at the first rule that fails. Each rule specifies the source of the value (query, form, header, cookie, or automatic fallback) and the validation flags to apply (CSILK_VALID_REQUIRED, CSILK_VALID_INT, CSILK_VALID_STRING, CSILK_VALID_EMAIL).

For integer validation, an optional [min, max] range can be specified. For string validation, an optional [min, max] length range can be specified. Ranges are ignored when min >= max.

Parameters
cThe request context to extract values from.
rulesA null-terminated array of csilk_valid_rule_t rules (the last entry must have field == NULL). Must not be NULL.
Returns
NULL if all rules pass, or a pointer to the field name (string from the rule definition) of the first field that fails.
Note
When no explicit source is specified, the function first tries the query string, then falls back to the form body.
Warning
The returned error pointer points into the rules array; it must not be freed or dereferenced after the rules array goes out of scope.

◆ is_valid_email()

static int is_valid_email ( const char *  s)
static

Check if a string looks like a valid email address.

Performs a basic syntactic validation: the string must contain exactly one '@' character, the local part and domain must be non-empty, and the domain must contain at least one dot after the '@' with non-empty segments. No whitespace characters are permitted anywhere in the address.

Parameters
sThe string to validate. Must be null-terminated.
Returns
1 if the string passes the basic email format check, 0 otherwise.
Note
This is NOT a full RFC 5322 validator. It does not check for quoted local parts, IP address literals, IDN, or special characters. Use a dedicated validation library for strict email validation.

◆ str_find()

static const char * str_find ( const char *  s,
char  c 
)
static

Find the first occurrence of a character in a string (helper).

Parameters
sThe string to search (may be NULL).
cThe character to locate.
Returns
Pointer to the first occurrence of c in s, or NULL if c is not found or s is NULL.