APPENDIX AREFERENCE MANUAL25

APPENDIX AReference Manual

This appendix lists every type, macro and function in <cgull.h>, by topic. Strings the functions return are valid until the request ends, and need not be freed (Chapter 6). Each name also has an entry in the Index.

A.1 Types and Macros

typedef struct cg_req cg_req;
The request being handled. It is opaque: read it with the functions in A.3. There is only ever one, and it lives until the program exits.
typedef struct cg_res cg_res;
The response being built. It is opaque: change it with the functions in A.4. Nothing is sent until the handler returns; then cg_run writes the whole response at once.
typedef void (*cg_handler)(cg_req *req, cg_res *res);
A route handler. It reads req and writes res; when it returns, the response is sent.
typedef bool (*cg_middleware)(cg_req *req, cg_res *res);
A function that runs before the handler. It returns true to carry on, or false to stop and send whatever it has written to res.
#define CGULL_VERSION "0.1.0"
The cgull version, as a string literal. cgull build defines it on the command line; the header defines it as "unknown" otherwise.

A.2 Routing

void cg_route(const char *method, const char *pattern, cg_handler handler);
Adds a route: requests whose method is method and whose path matches pattern go to handler. A method of "*" matches every method. Routes are tried in the order they were added, and the first match wins. Patterns are described in Section 3.2. Both strings must outlive the request; string literals do.
void cg_get(const char *pattern, cg_handler handler); /* macro */
cg_route("GET", pattern, handler). A GET route also answers HEAD requests, without the body.
void cg_post(const char *pattern, cg_handler handler); /* macro */
cg_route("POST", pattern, handler).
void cg_put(const char *pattern, cg_handler handler); /* macro */
cg_route("PUT", pattern, handler).
void cg_patch(const char *pattern, cg_handler handler); /* macro */
cg_route("PATCH", pattern, handler).
void cg_delete(const char *pattern, cg_handler handler); /* macro */
cg_route("DELETE", pattern, handler).
void cg_any(const char *pattern, cg_handler handler); /* macro */
cg_route("*", pattern, handler): every method. Use cg_method to tell them apart.
void cg_use(cg_middleware middleware);
Adds middleware. Every request, including ones that will end in a 404 or 405, passes through each middleware function in the order they were added, before its handler. Route parameters are already set. If one returns false, the rest and the handler are skipped.
void cg_not_found(cg_handler handler);
Replaces the default 404 page, which is a line of plain text. The status is already 404 when handler runs, but cg_html and the other shortcuts set it again, so pass them 404.
int cg_run(void);
Reads the request, runs middleware and the matching handler, and writes the response to standard output. It returns 0; return that from main. Add every route before calling it.

A.3 The Request

const char *cg_method(cg_req *req);
The request method in capitals: "GET", "POST", and so on.
const char *cg_path(cg_req *req);
The path, percent-decoded, without the query string: "/hello/Ada Lovelace" for /hello/Ada%20Lovelace?x=1.
const char *cg_param(cg_req *req, const char *name);
The value of the route parameter name (written :name in the pattern), percent-decoded, or NULL if the route has none by that name. The rest of the path matched by a trailing * is cg_param(req, "*").
const char *cg_query(cg_req *req, const char *name);
The first value of name in the query string, decoded (+ becomes a space), or NULL if it isn't there. A name with no = gives "".
const char *cg_header(cg_req *req, const char *name);
The value of a request header, or NULL. The name is not case-sensitive. Repeated headers arrive joined with ", ".
The value of the cookie name from the Cookie header, percent-decoded, or NULL.
const char *cg_form(cg_req *req, const char *name);
A field of an application/x-www-form-urlencoded body, which is what an HTML <form method=post> sends. Decoded like cg_query. NULL if the field is missing or the body is of another type. It reads the body with cg_body, which can still be called afterwards.
const char *cg_body(cg_req *req, size_t *len);
The request body, as sent, with a '\0' added after it so text bodies can be used as strings. If len isn't NULL, the length (without the '\0') is stored there; use it for binary bodies, which may contain '\0'. An empty body gives "".

A.4 The Response

void cg_status(cg_res *res, int status);
Sets the status code. It starts as 200.
void cg_set_header(cg_res *res, const char *name, const char *value);
Sets a response header, replacing any of the same name (not case-sensitive). Carriage returns and newlines in either string become spaces, so a value can never start a header of its own. A NULL value sets an empty one. If no Content-Type is set, the response gets text/html; charset=utf-8.
void cg_add_header(cg_res *res, const char *name, const char *value);
Like cg_set_header, but keeps any existing headers of the same name.
Adds a Set-Cookie header. attrs, if not NULL or empty, follows the value as is, as in "Path=/; HttpOnly; Max-Age=3600". The value is not encoded; pass it through cg_url_encode if it might hold ;, spaces or other awkward characters.
void cg_write(cg_res *res, const void *data, size_t len);
Appends len bytes to the body. The body is kept in memory until the handler returns.
void cg_puts(cg_res *res, const char *s);
Appends the string s to the body. Unlike puts, it adds no newline.
void cg_printf(cg_res *res, const char *fmt, ...);
Appends formatted output to the body, with the conversions of printf. The compiler checks the arguments against the format. It does not escape anything: wrap text from the request in cg_esc.
void cg_vprintf(cg_res *res, const char *fmt, va_list ap);
cg_printf with a va_list, for writing your own formatting functions.
void cg_text(cg_res *res, int status, const char *s);
Sets the status and Content-Type: text/plain; charset=utf-8, then appends s (if not NULL).
void cg_html(cg_res *res, int status, const char *s);
As cg_text, with text/html; charset=utf-8.
void cg_json(cg_res *res, int status, const char *json);
As cg_text, with application/json. json is sent as it is; build it with cg_sprintf and cg_json_str.
void cg_redirect(cg_res *res, int status, const char *location);
Sets the status, usually 302, 303 or 307, and the Location header. After a form post, use 303 so the browser follows with a GET.

A.5 Strings

char *cg_esc(const char *s);
A copy of s that is safe in HTML text and in quoted attribute values: &, <, >, " and ' are replaced by entities. NULL gives "".
char *cg_json_str(const char *s);
s as a JSON string literal, quotes included: say "hi" becomes "say \"hi\"". Control characters are escaped; UTF-8 passes through. NULL gives "" (the two characters).
char *cg_url_encode(const char *s);
s percent-encoded for use in a path segment or a query value: every byte but letters, digits and -_.~ becomes %XX. NULL gives "".
char *cg_sprintf(const char *fmt, ...);
A new string formatted as by printf, of whatever length it needs.

A.6 The Environment

const char *cg_env(const char *name);
The value of a Worker variable or secret, or NULL. Variables come from "vars" in wrangler.jsonc, secrets from wrangler secret put. Bindings such as KV namespaces are not strings and are not visible. The CGI variables of Section 8.2 are here too.
void cg_log(const char *fmt, ...);
Writes a formatted line, with a newline added, to standard error, which goes to the Worker's logs (wrangler tail, or the terminal running cgull dev).

Set in Tinos and Courier Prime. Served by a C program on Cloudflare Workers, cgull 0.1.0. Source on GitHub, BSD licensed.