CHAPTER 4REQUESTS13

CHAPTER 4Requests

Everything about the request is read through req, the first argument of every handler. The functions in this chapter return strings that last until the request ends. They return NULL when what was asked for isn't there, so check before using a result, or pass it straight to cg_esc and its relatives, which treat NULL as an empty string.

4.1 Method and Path

cg_method gives the method, in capitals, and cg_path the path, without the query string and with any percent-encoding decoded:

/* GET /notes/caf%C3%A9?sort=date */
cg_method(req)   /* "GET" */
cg_path(req)     /* "/notes/café" */

Routes are matched against the path as it was sent, before decoding, which is why a %2F in it doesn't split a segment. Read the parts of the path that matter with cg_param (Section 3.2) rather than taking cg_path apart.

4.2 The Query String

cg_query gives the value of one parameter of the query string, decoded: %XX becomes the byte it stands for, and + becomes a space, as browsers send it from forms.

/* GET /search?q=c+programming&page=2&exact */
cg_query(req, "q")      /* "c programming" */
cg_query(req, "page")   /* "2" */
cg_query(req, "exact")  /* "" */
cg_query(req, "sort")   /* NULL */

If a name appears more than once, the first value is returned. Numbers arrive as strings; convert them with strtol or atoi, remembering that the visitor can type anything:

const char *p = cg_query(req, "page");
long page = p ? strtol(p, NULL, 10) : 1;

if (page < 1)
    page = 1;

4.3 Headers

cg_header gives the value of a request header. Header names are not case-sensitive, so "User-Agent" and "user-agent" are the same:

const char *agent = cg_header(req, "User-Agent");
const char *lang = cg_header(req, "Accept-Language");

A header sent more than once arrives as one, its values joined by ", ". Cloudflare adds headers of its own; among the useful ones are CF-Connecting-IP, the visitor's address, and CF-IPCountry, their country as a two-letter code.

4.4 Cookies

cg_cookie picks one cookie out of the Cookie header and decodes it:

const char *theme = cg_cookie(req, "theme");

if (theme == NULL)
    theme = "light";

Section 5.5 shows how to set one.

4.5 Bodies and Forms

A form posted from a page, with <form method=post>, arrives as an application/x-www-form-urlencoded body, and cg_form reads its fields as cg_query reads the query string:

const char *title = cg_form(req, "title");
const char *body = cg_form(req, "body");

if (title == NULL || *title == '\0') {
    cg_html(res, 422, "<p>A note needs a title.</p>");
    return;
}

cg_form returns NULL for a body of any other type. Forms that upload files are sent as multipart/form-data, which cgull does not yet take apart.

For anything else, cg_body gives the body as it was sent, with its length:

size_t len;
const char *body = cg_body(req, &len);
const char *type = cg_header(req, "Content-Type");

A '\0' is added after the body, so a text body can be used as a string. A binary one may contain '\0' bytes of its own, so use len for it. The body can be read any number of times, and after cg_form too.

There is no JSON parser in cgull. A small one, such as cJSON or jsmn, can be dropped into src/ and will be compiled with the rest.

Exercise 4-1. Write a handler for POST /count that answers with the number of lines in the body.

Exercise 4-2. Greet the visitor in their language: read Accept-Language, and say hello in French if it starts with fr.

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