uhttp_cookie.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* uhttp_cookie: see uhttp_cookie.h. */
  2. #include "uhttp_cookie.h"
  3. static unsigned int c_len(const char *s)
  4. {
  5. unsigned int n = 0;
  6. while (s[n])
  7. n++;
  8. return n;
  9. }
  10. static char lower(char c)
  11. {
  12. return (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c;
  13. }
  14. static int eq_ci(const char *a, const char *b)
  15. {
  16. while (*a && lower(*a) == lower(*b)) {
  17. a++;
  18. b++;
  19. }
  20. return lower(*a) == lower(*b);
  21. }
  22. static int eq(const char *a, const char *b)
  23. {
  24. while (*a && *a == *b) {
  25. a++;
  26. b++;
  27. }
  28. return *a == *b;
  29. }
  30. /* Copy [s, s+n) into d (cap bytes); -1 when it does not fit. */
  31. static int copy_n(char *d, unsigned int cap, const char *s, unsigned int n)
  32. {
  33. unsigned int i;
  34. if (n + 1 > cap)
  35. return -1;
  36. for (i = 0; i < n; i++)
  37. d[i] = s[i];
  38. d[n] = '\0';
  39. return 0;
  40. }
  41. static int is_ws(char c)
  42. {
  43. return c == ' ' || c == '\t';
  44. }
  45. /* Trim [*s, *e) in place. */
  46. static void trim(const char **s, const char **e)
  47. {
  48. while (*s < *e && is_ws(**s))
  49. (*s)++;
  50. while (*e > *s && is_ws((*e)[-1]))
  51. (*e)--;
  52. }
  53. void uhttp_jar_init(struct uhttp_jar *jar)
  54. {
  55. unsigned int i;
  56. for (i = 0; i < sizeof(*jar); i++)
  57. ((unsigned char *)jar)[i] = 0;
  58. }
  59. void uhttp_jar_clear(struct uhttp_jar *jar)
  60. {
  61. unsigned int changes = jar->changes;
  62. uhttp_jar_init(jar);
  63. jar->changes = changes + 1;
  64. }
  65. /* Length of the request path without query/fragment. */
  66. static unsigned int path_len(const char *path)
  67. {
  68. unsigned int n = 0;
  69. while (path[n] && path[n] != '?' && path[n] != '#')
  70. n++;
  71. return n;
  72. }
  73. /* RFC 6265 5.1.4 default-path: the directory of the request path. */
  74. static void default_path(const char *req, char *out, unsigned int cap)
  75. {
  76. unsigned int n = path_len(req), i, last = 0;
  77. if (n == 0 || req[0] != '/') {
  78. copy_n(out, cap, "/", 1);
  79. return;
  80. }
  81. for (i = 0; i < n; i++)
  82. if (req[i] == '/')
  83. last = i;
  84. if (last == 0) {
  85. copy_n(out, cap, "/", 1);
  86. return;
  87. }
  88. if (copy_n(out, cap, req, last) != 0)
  89. copy_n(out, cap, "/", 1);
  90. }
  91. static int path_match(const char *cpath, const char *req)
  92. {
  93. unsigned int rn = path_len(req), cn = c_len(cpath), i;
  94. if (cn == 0 || (cn == 1 && cpath[0] == '/'))
  95. return 1;
  96. if (rn < cn)
  97. return 0;
  98. for (i = 0; i < cn; i++)
  99. if (cpath[i] != req[i])
  100. return 0;
  101. if (rn == cn)
  102. return 1;
  103. return cpath[cn - 1] == '/' || req[cn] == '/';
  104. }
  105. /* An Expires value in the past deletes the cookie. Only the obvious case is
  106. * recognized: servers clear cookies with a 1970 (epoch) date. */
  107. static int expires_past(const char *s, const char *e)
  108. {
  109. while (s + 4 <= e) {
  110. if (s[0] >= '0' && s[0] <= '9' && s[1] >= '0' && s[1] <= '9' &&
  111. s[2] >= '0' && s[2] <= '9' && s[3] >= '0' && s[3] <= '9' &&
  112. (s + 4 == e || s[4] < '0' || s[4] > '9')) {
  113. unsigned int y = (unsigned int)(s[0] - '0') * 1000 +
  114. (unsigned int)(s[1] - '0') * 100 +
  115. (unsigned int)(s[2] - '0') * 10 +
  116. (unsigned int)(s[3] - '0');
  117. return y < 2000;
  118. }
  119. s++;
  120. }
  121. return 0;
  122. }
  123. static int find(const struct uhttp_jar *jar, const char *host,
  124. const char *path, const char *name)
  125. {
  126. int i;
  127. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  128. const struct uhttp_cookie *c = &jar->c[i];
  129. if (c->used && eq_ci(c->host, host) && eq(c->path, path) &&
  130. eq(c->name, name))
  131. return i;
  132. }
  133. return -1;
  134. }
  135. static int store(struct uhttp_jar *jar, const char *host, const char *path,
  136. const char *name, const char *value, int secure, int del)
  137. {
  138. struct uhttp_cookie *c;
  139. int i = find(jar, host, path, name);
  140. if (del) {
  141. if (i < 0)
  142. return 0;
  143. jar->c[i].used = 0;
  144. jar->changes++;
  145. return 1;
  146. }
  147. if (i >= 0) {
  148. c = &jar->c[i];
  149. if (eq(c->value, value) && c->secure == (secure ? 1 : 0))
  150. return 0;
  151. copy_n(c->value, sizeof(c->value), value, c_len(value));
  152. c->secure = secure ? 1 : 0;
  153. c->touch = ++jar->seq;
  154. jar->changes++;
  155. return 1;
  156. }
  157. for (i = 0; i < UHTTP_COOKIE_MAX; i++)
  158. if (!jar->c[i].used)
  159. break;
  160. if (i == UHTTP_COOKIE_MAX) {
  161. /* Full: evict the cookie set or updated longest ago, so a
  162. * session cookie the server keeps refreshing outlives a burst
  163. * of one-shot cookies. */
  164. int j, old = 0;
  165. for (j = 1; j < UHTTP_COOKIE_MAX; j++)
  166. if (jar->c[j].touch < jar->c[old].touch)
  167. old = j;
  168. i = old;
  169. }
  170. c = &jar->c[i];
  171. if (copy_n(c->host, sizeof(c->host), host, c_len(host)) ||
  172. copy_n(c->path, sizeof(c->path), path, c_len(path)) ||
  173. copy_n(c->name, sizeof(c->name), name, c_len(name)) ||
  174. copy_n(c->value, sizeof(c->value), value, c_len(value))) {
  175. c->used = 0;
  176. return -1;
  177. }
  178. c->secure = secure ? 1 : 0;
  179. c->used = 1;
  180. c->seq = ++jar->seq;
  181. c->touch = c->seq;
  182. jar->changes++;
  183. return 1;
  184. }
  185. int uhttp_jar_set(struct uhttp_jar *jar, const char *host, const char *path,
  186. const char *set_cookie)
  187. {
  188. char name[UHTTP_COOKIE_NAME_MAX];
  189. char value[UHTTP_COOKIE_VALUE_MAX];
  190. char cpath[UHTTP_COOKIE_PATH_MAX];
  191. const char *p = set_cookie, *s, *e, *eqp;
  192. int secure = 0, del = 0, have_path = 0;
  193. if (!jar || !host || !host[0] || !set_cookie)
  194. return -1;
  195. /* name=value up to the first ';' */
  196. s = p;
  197. while (*p && *p != ';')
  198. p++;
  199. e = p;
  200. trim(&s, &e);
  201. eqp = s;
  202. while (eqp < e && *eqp != '=')
  203. eqp++;
  204. if (eqp == e)
  205. return -1; /* no '=': ignore (RFC 6265 5.2) */
  206. {
  207. const char *ns = s, *ne = eqp, *vs = eqp + 1, *ve = e;
  208. trim(&ns, &ne);
  209. trim(&vs, &ve);
  210. if (ns == ne)
  211. return -1;
  212. if (copy_n(name, sizeof(name), ns, (unsigned int)(ne - ns)) ||
  213. copy_n(value, sizeof(value), vs, (unsigned int)(ve - vs)))
  214. return -1;
  215. }
  216. /* attributes */
  217. while (*p == ';') {
  218. const char *as, *ae, *ve, *vs;
  219. char an[16];
  220. p++;
  221. as = p;
  222. while (*p && *p != ';')
  223. p++;
  224. ae = p;
  225. trim(&as, &ae);
  226. vs = as;
  227. while (vs < ae && *vs != '=')
  228. vs++;
  229. ve = ae;
  230. {
  231. const char *ne = vs;
  232. while (ne > as && is_ws(ne[-1]))
  233. ne--;
  234. if (ne - as >= (int)sizeof(an))
  235. continue;
  236. copy_n(an, sizeof(an), as, (unsigned int)(ne - as));
  237. }
  238. if (vs < ae)
  239. vs++;
  240. trim(&vs, &ve);
  241. if (eq_ci(an, "secure")) {
  242. secure = 1;
  243. } else if (eq_ci(an, "path")) {
  244. if (vs < ve && *vs == '/' &&
  245. copy_n(cpath, sizeof(cpath), vs,
  246. (unsigned int)(ve - vs)) == 0)
  247. have_path = 1;
  248. } else if (eq_ci(an, "max-age")) {
  249. const char *q = vs;
  250. int neg = 0, nonzero = 0, digits = 0;
  251. if (q < ve && *q == '-') {
  252. neg = 1;
  253. q++;
  254. }
  255. while (q < ve && *q >= '0' && *q <= '9') {
  256. if (*q != '0')
  257. nonzero = 1;
  258. digits++;
  259. q++;
  260. }
  261. if (digits && (neg || !nonzero))
  262. del = 1;
  263. } else if (eq_ci(an, "expires")) {
  264. if (expires_past(vs, ve))
  265. del = 1;
  266. }
  267. }
  268. if (!have_path)
  269. default_path(path ? path : "/", cpath, sizeof(cpath));
  270. return store(jar, host, cpath, name, value, secure, del);
  271. }
  272. unsigned int uhttp_jar_header(const struct uhttp_jar *jar, const char *host,
  273. const char *path, int secure, char *out,
  274. unsigned int cap)
  275. {
  276. int idx[UHTTP_COOKIE_MAX];
  277. unsigned int plen[UHTTP_COOKIE_MAX];
  278. int n = 0, i, j;
  279. unsigned int len = 0;
  280. if (cap)
  281. out[0] = '\0';
  282. if (!jar || !host)
  283. return 0;
  284. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  285. const struct uhttp_cookie *c = &jar->c[i];
  286. if (!c->used || !eq_ci(c->host, host))
  287. continue;
  288. if (c->secure && !secure)
  289. continue;
  290. if (!path_match(c->path, path ? path : "/"))
  291. continue;
  292. idx[n] = i;
  293. plen[n] = c_len(c->path);
  294. n++;
  295. }
  296. /* Longer paths first, then creation order (RFC 6265 5.4). */
  297. for (i = 1; i < n; i++) {
  298. for (j = i; j > 0; j--) {
  299. const struct uhttp_cookie *a = &jar->c[idx[j - 1]];
  300. const struct uhttp_cookie *b = &jar->c[idx[j]];
  301. int swap = plen[j] > plen[j - 1] ||
  302. (plen[j] == plen[j - 1] && b->seq < a->seq);
  303. if (!swap)
  304. break;
  305. {
  306. int ti = idx[j];
  307. unsigned int tl = plen[j];
  308. idx[j] = idx[j - 1];
  309. plen[j] = plen[j - 1];
  310. idx[j - 1] = ti;
  311. plen[j - 1] = tl;
  312. }
  313. }
  314. }
  315. for (i = 0; i < n; i++) {
  316. const struct uhttp_cookie *c = &jar->c[idx[i]];
  317. unsigned int nl = c_len(c->name), vl = c_len(c->value);
  318. unsigned int need = nl + 1 + vl + (len ? 2 : 0);
  319. unsigned int k;
  320. if (len + need + 1 > cap)
  321. break;
  322. if (len) {
  323. out[len++] = ';';
  324. out[len++] = ' ';
  325. }
  326. for (k = 0; k < nl; k++)
  327. out[len++] = c->name[k];
  328. out[len++] = '=';
  329. for (k = 0; k < vl; k++)
  330. out[len++] = c->value[k];
  331. out[len] = '\0';
  332. }
  333. return len;
  334. }
  335. int uhttp_jar_import(struct uhttp_jar *jar, const char *host, int secure,
  336. const char *header)
  337. {
  338. const char *p = header;
  339. int count = 0;
  340. if (!jar || !host || !header)
  341. return 0;
  342. while (*p) {
  343. const char *s = p, *e, *eqp;
  344. char name[UHTTP_COOKIE_NAME_MAX];
  345. char value[UHTTP_COOKIE_VALUE_MAX];
  346. while (*p && *p != ';')
  347. p++;
  348. e = p;
  349. if (*p == ';')
  350. p++;
  351. trim(&s, &e);
  352. eqp = s;
  353. while (eqp < e && *eqp != '=')
  354. eqp++;
  355. if (eqp == e || eqp == s)
  356. continue;
  357. {
  358. const char *ns = s, *ne = eqp, *vs = eqp + 1, *ve = e;
  359. trim(&ns, &ne);
  360. trim(&vs, &ve);
  361. if (ns == ne ||
  362. copy_n(name, sizeof(name), ns,
  363. (unsigned int)(ne - ns)) ||
  364. copy_n(value, sizeof(value), vs,
  365. (unsigned int)(ve - vs)))
  366. continue;
  367. }
  368. if (store(jar, host, "/", name, value, secure, 0) >= 0)
  369. count++;
  370. }
  371. return count;
  372. }
  373. unsigned int uhttp_jar_count(const struct uhttp_jar *jar, const char *host)
  374. {
  375. unsigned int i, n = 0;
  376. for (i = 0; i < UHTTP_COOKIE_MAX; i++)
  377. if (jar->c[i].used && eq_ci(jar->c[i].host, host))
  378. n++;
  379. return n;
  380. }