| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- /* uhttp_cookie: see uhttp_cookie.h. */
- #include "uhttp_cookie.h"
- static unsigned int c_len(const char *s)
- {
- unsigned int n = 0;
- while (s[n])
- n++;
- return n;
- }
- static char lower(char c)
- {
- return (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c;
- }
- static int eq_ci(const char *a, const char *b)
- {
- while (*a && lower(*a) == lower(*b)) {
- a++;
- b++;
- }
- return lower(*a) == lower(*b);
- }
- static int eq(const char *a, const char *b)
- {
- while (*a && *a == *b) {
- a++;
- b++;
- }
- return *a == *b;
- }
- /* Copy [s, s+n) into d (cap bytes); -1 when it does not fit. */
- static int copy_n(char *d, unsigned int cap, const char *s, unsigned int n)
- {
- unsigned int i;
- if (n + 1 > cap)
- return -1;
- for (i = 0; i < n; i++)
- d[i] = s[i];
- d[n] = '\0';
- return 0;
- }
- static int is_ws(char c)
- {
- return c == ' ' || c == '\t';
- }
- /* Trim [*s, *e) in place. */
- static void trim(const char **s, const char **e)
- {
- while (*s < *e && is_ws(**s))
- (*s)++;
- while (*e > *s && is_ws((*e)[-1]))
- (*e)--;
- }
- void uhttp_jar_init(struct uhttp_jar *jar)
- {
- unsigned int i;
- for (i = 0; i < sizeof(*jar); i++)
- ((unsigned char *)jar)[i] = 0;
- }
- void uhttp_jar_clear(struct uhttp_jar *jar)
- {
- unsigned int changes = jar->changes;
- uhttp_jar_init(jar);
- jar->changes = changes + 1;
- }
- /* Length of the request path without query/fragment. */
- static unsigned int path_len(const char *path)
- {
- unsigned int n = 0;
- while (path[n] && path[n] != '?' && path[n] != '#')
- n++;
- return n;
- }
- /* RFC 6265 5.1.4 default-path: the directory of the request path. */
- static void default_path(const char *req, char *out, unsigned int cap)
- {
- unsigned int n = path_len(req), i, last = 0;
- if (n == 0 || req[0] != '/') {
- copy_n(out, cap, "/", 1);
- return;
- }
- for (i = 0; i < n; i++)
- if (req[i] == '/')
- last = i;
- if (last == 0) {
- copy_n(out, cap, "/", 1);
- return;
- }
- if (copy_n(out, cap, req, last) != 0)
- copy_n(out, cap, "/", 1);
- }
- static int path_match(const char *cpath, const char *req)
- {
- unsigned int rn = path_len(req), cn = c_len(cpath), i;
- if (cn == 0 || (cn == 1 && cpath[0] == '/'))
- return 1;
- if (rn < cn)
- return 0;
- for (i = 0; i < cn; i++)
- if (cpath[i] != req[i])
- return 0;
- if (rn == cn)
- return 1;
- return cpath[cn - 1] == '/' || req[cn] == '/';
- }
- /* An Expires value in the past deletes the cookie. Only the obvious case is
- * recognized: servers clear cookies with a 1970 (epoch) date. */
- static int expires_past(const char *s, const char *e)
- {
- while (s + 4 <= e) {
- if (s[0] >= '0' && s[0] <= '9' && s[1] >= '0' && s[1] <= '9' &&
- s[2] >= '0' && s[2] <= '9' && s[3] >= '0' && s[3] <= '9' &&
- (s + 4 == e || s[4] < '0' || s[4] > '9')) {
- unsigned int y = (unsigned int)(s[0] - '0') * 1000 +
- (unsigned int)(s[1] - '0') * 100 +
- (unsigned int)(s[2] - '0') * 10 +
- (unsigned int)(s[3] - '0');
- return y < 2000;
- }
- s++;
- }
- return 0;
- }
- static int find(const struct uhttp_jar *jar, const char *host,
- const char *path, const char *name)
- {
- int i;
- for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
- const struct uhttp_cookie *c = &jar->c[i];
- if (c->used && eq_ci(c->host, host) && eq(c->path, path) &&
- eq(c->name, name))
- return i;
- }
- return -1;
- }
- static int store(struct uhttp_jar *jar, const char *host, const char *path,
- const char *name, const char *value, int secure, int del)
- {
- struct uhttp_cookie *c;
- int i = find(jar, host, path, name);
- if (del) {
- if (i < 0)
- return 0;
- jar->c[i].used = 0;
- jar->changes++;
- return 1;
- }
- if (i >= 0) {
- c = &jar->c[i];
- if (eq(c->value, value) && c->secure == (secure ? 1 : 0))
- return 0;
- copy_n(c->value, sizeof(c->value), value, c_len(value));
- c->secure = secure ? 1 : 0;
- c->touch = ++jar->seq;
- jar->changes++;
- return 1;
- }
- for (i = 0; i < UHTTP_COOKIE_MAX; i++)
- if (!jar->c[i].used)
- break;
- if (i == UHTTP_COOKIE_MAX) {
- /* Full: evict the cookie set or updated longest ago, so a
- * session cookie the server keeps refreshing outlives a burst
- * of one-shot cookies. */
- int j, old = 0;
- for (j = 1; j < UHTTP_COOKIE_MAX; j++)
- if (jar->c[j].touch < jar->c[old].touch)
- old = j;
- i = old;
- }
- c = &jar->c[i];
- if (copy_n(c->host, sizeof(c->host), host, c_len(host)) ||
- copy_n(c->path, sizeof(c->path), path, c_len(path)) ||
- copy_n(c->name, sizeof(c->name), name, c_len(name)) ||
- copy_n(c->value, sizeof(c->value), value, c_len(value))) {
- c->used = 0;
- return -1;
- }
- c->secure = secure ? 1 : 0;
- c->used = 1;
- c->seq = ++jar->seq;
- c->touch = c->seq;
- jar->changes++;
- return 1;
- }
- int uhttp_jar_set(struct uhttp_jar *jar, const char *host, const char *path,
- const char *set_cookie)
- {
- char name[UHTTP_COOKIE_NAME_MAX];
- char value[UHTTP_COOKIE_VALUE_MAX];
- char cpath[UHTTP_COOKIE_PATH_MAX];
- const char *p = set_cookie, *s, *e, *eqp;
- int secure = 0, del = 0, have_path = 0;
- if (!jar || !host || !host[0] || !set_cookie)
- return -1;
- /* name=value up to the first ';' */
- s = p;
- while (*p && *p != ';')
- p++;
- e = p;
- trim(&s, &e);
- eqp = s;
- while (eqp < e && *eqp != '=')
- eqp++;
- if (eqp == e)
- return -1; /* no '=': ignore (RFC 6265 5.2) */
- {
- const char *ns = s, *ne = eqp, *vs = eqp + 1, *ve = e;
- trim(&ns, &ne);
- trim(&vs, &ve);
- if (ns == ne)
- return -1;
- if (copy_n(name, sizeof(name), ns, (unsigned int)(ne - ns)) ||
- copy_n(value, sizeof(value), vs, (unsigned int)(ve - vs)))
- return -1;
- }
- /* attributes */
- while (*p == ';') {
- const char *as, *ae, *ve, *vs;
- char an[16];
- p++;
- as = p;
- while (*p && *p != ';')
- p++;
- ae = p;
- trim(&as, &ae);
- vs = as;
- while (vs < ae && *vs != '=')
- vs++;
- ve = ae;
- {
- const char *ne = vs;
- while (ne > as && is_ws(ne[-1]))
- ne--;
- if (ne - as >= (int)sizeof(an))
- continue;
- copy_n(an, sizeof(an), as, (unsigned int)(ne - as));
- }
- if (vs < ae)
- vs++;
- trim(&vs, &ve);
- if (eq_ci(an, "secure")) {
- secure = 1;
- } else if (eq_ci(an, "path")) {
- if (vs < ve && *vs == '/' &&
- copy_n(cpath, sizeof(cpath), vs,
- (unsigned int)(ve - vs)) == 0)
- have_path = 1;
- } else if (eq_ci(an, "max-age")) {
- const char *q = vs;
- int neg = 0, nonzero = 0, digits = 0;
- if (q < ve && *q == '-') {
- neg = 1;
- q++;
- }
- while (q < ve && *q >= '0' && *q <= '9') {
- if (*q != '0')
- nonzero = 1;
- digits++;
- q++;
- }
- if (digits && (neg || !nonzero))
- del = 1;
- } else if (eq_ci(an, "expires")) {
- if (expires_past(vs, ve))
- del = 1;
- }
- }
- if (!have_path)
- default_path(path ? path : "/", cpath, sizeof(cpath));
- return store(jar, host, cpath, name, value, secure, del);
- }
- unsigned int uhttp_jar_header(const struct uhttp_jar *jar, const char *host,
- const char *path, int secure, char *out,
- unsigned int cap)
- {
- int idx[UHTTP_COOKIE_MAX];
- unsigned int plen[UHTTP_COOKIE_MAX];
- int n = 0, i, j;
- unsigned int len = 0;
- if (cap)
- out[0] = '\0';
- if (!jar || !host)
- return 0;
- for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
- const struct uhttp_cookie *c = &jar->c[i];
- if (!c->used || !eq_ci(c->host, host))
- continue;
- if (c->secure && !secure)
- continue;
- if (!path_match(c->path, path ? path : "/"))
- continue;
- idx[n] = i;
- plen[n] = c_len(c->path);
- n++;
- }
- /* Longer paths first, then creation order (RFC 6265 5.4). */
- for (i = 1; i < n; i++) {
- for (j = i; j > 0; j--) {
- const struct uhttp_cookie *a = &jar->c[idx[j - 1]];
- const struct uhttp_cookie *b = &jar->c[idx[j]];
- int swap = plen[j] > plen[j - 1] ||
- (plen[j] == plen[j - 1] && b->seq < a->seq);
- if (!swap)
- break;
- {
- int ti = idx[j];
- unsigned int tl = plen[j];
- idx[j] = idx[j - 1];
- plen[j] = plen[j - 1];
- idx[j - 1] = ti;
- plen[j - 1] = tl;
- }
- }
- }
- for (i = 0; i < n; i++) {
- const struct uhttp_cookie *c = &jar->c[idx[i]];
- unsigned int nl = c_len(c->name), vl = c_len(c->value);
- unsigned int need = nl + 1 + vl + (len ? 2 : 0);
- unsigned int k;
- if (len + need + 1 > cap)
- break;
- if (len) {
- out[len++] = ';';
- out[len++] = ' ';
- }
- for (k = 0; k < nl; k++)
- out[len++] = c->name[k];
- out[len++] = '=';
- for (k = 0; k < vl; k++)
- out[len++] = c->value[k];
- out[len] = '\0';
- }
- return len;
- }
- int uhttp_jar_import(struct uhttp_jar *jar, const char *host, int secure,
- const char *header)
- {
- const char *p = header;
- int count = 0;
- if (!jar || !host || !header)
- return 0;
- while (*p) {
- const char *s = p, *e, *eqp;
- char name[UHTTP_COOKIE_NAME_MAX];
- char value[UHTTP_COOKIE_VALUE_MAX];
- while (*p && *p != ';')
- p++;
- e = p;
- if (*p == ';')
- p++;
- trim(&s, &e);
- eqp = s;
- while (eqp < e && *eqp != '=')
- eqp++;
- if (eqp == e || eqp == s)
- continue;
- {
- const char *ns = s, *ne = eqp, *vs = eqp + 1, *ve = e;
- trim(&ns, &ne);
- trim(&vs, &ve);
- if (ns == ne ||
- copy_n(name, sizeof(name), ns,
- (unsigned int)(ne - ns)) ||
- copy_n(value, sizeof(value), vs,
- (unsigned int)(ve - vs)))
- continue;
- }
- if (store(jar, host, "/", name, value, secure, 0) >= 0)
- count++;
- }
- return count;
- }
- unsigned int uhttp_jar_count(const struct uhttp_jar *jar, const char *host)
- {
- unsigned int i, n = 0;
- for (i = 0; i < UHTTP_COOKIE_MAX; i++)
- if (jar->c[i].used && eq_ci(jar->c[i].host, host))
- n++;
- return n;
- }
|