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_runwrites the whole response at once. typedef void (*cg_handler)(cg_req *req, cg_res *res);- A route handler. It reads
reqand writesres; 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
trueto carry on, orfalseto stop and send whatever it has written tores. #define CGULL_VERSION "0.1.0"- The cgull version, as a string literal.
cgull builddefines 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
methodand whose path matchespatterngo tohandler. 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. Usecg_methodto 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
handlerruns, butcg_htmland 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:namein the pattern), percent-decoded, orNULLif the route has none by that name. The rest of the path matched by a trailing*iscg_param(req, "*"). const char *cg_query(cg_req *req, const char *name);- The first value of
namein the query string, decoded (+becomes a space), orNULLif 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
namefrom theCookieheader, percent-decoded, orNULL. const char *cg_form(cg_req *req, const char *name);- A field of an
application/x-www-form-urlencodedbody, which is what an HTML<form method=post>sends. Decoded likecg_query.NULLif the field is missing or the body is of another type. It reads the body withcg_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. Iflenisn'tNULL, 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
NULLvalue sets an empty one. If noContent-Typeis set, the response getstext/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-Cookieheader.attrs, if notNULLor empty, follows the value as is, as in"Path=/; HttpOnly; Max-Age=3600". The value is not encoded; pass it throughcg_url_encodeif it might hold;, spaces or other awkward characters. void cg_write(cg_res *res, const void *data, size_t len);- Appends
lenbytes 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
sto the body. Unlikeputs, 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 incg_esc. void cg_vprintf(cg_res *res, const char *fmt, va_list ap);cg_printfwith ava_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 appendss(if notNULL). void cg_html(cg_res *res, int status, const char *s);- As
cg_text, withtext/html; charset=utf-8. void cg_json(cg_res *res, int status, const char *json);- As
cg_text, withapplication/json.jsonis sent as it is; build it withcg_sprintfandcg_json_str. void cg_redirect(cg_res *res, int status, const char *location);- Sets the status, usually 302, 303 or 307, and the
Locationheader. 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
sthat is safe in HTML text and in quoted attribute values:&,<,>,"and'are replaced by entities.NULLgives"". char *cg_json_str(const char *s);sas a JSON string literal, quotes included:say "hi"becomes"say \"hi\"". Control characters are escaped; UTF-8 passes through.NULLgives""(the two characters).char *cg_url_encode(const char *s);spercent-encoded for use in a path segment or a query value: every byte but letters, digits and-_.~becomes%XX.NULLgives"".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"inwrangler.jsonc, secrets fromwrangler 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 runningcgull dev).