uhttp_cookie.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. jar->changes++;
  154. return 1;
  155. }
  156. for (i = 0; i < UHTTP_COOKIE_MAX; i++)
  157. if (!jar->c[i].used)
  158. break;
  159. if (i == UHTTP_COOKIE_MAX) {
  160. /* Full: evict the oldest cookie. */
  161. int j, old = 0;
  162. for (j = 1; j < UHTTP_COOKIE_MAX; j++)
  163. if (jar->c[j].seq < jar->c[old].seq)
  164. old = j;
  165. i = old;
  166. }
  167. c = &jar->c[i];
  168. if (copy_n(c->host, sizeof(c->host), host, c_len(host)) ||
  169. copy_n(c->path, sizeof(c->path), path, c_len(path)) ||
  170. copy_n(c->name, sizeof(c->name), name, c_len(name)) ||
  171. copy_n(c->value, sizeof(c->value), value, c_len(value))) {
  172. c->used = 0;
  173. return -1;
  174. }
  175. c->secure = secure ? 1 : 0;
  176. c->used = 1;
  177. c->seq = ++jar->seq;
  178. jar->changes++;
  179. return 1;
  180. }
  181. int uhttp_jar_set(struct uhttp_jar *jar, const char *host, const char *path,
  182. const char *set_cookie)
  183. {
  184. char name[UHTTP_COOKIE_NAME_MAX];
  185. char value[UHTTP_COOKIE_VALUE_MAX];
  186. char cpath[UHTTP_COOKIE_PATH_MAX];
  187. const char *p = set_cookie, *s, *e, *eqp;
  188. int secure = 0, del = 0, have_path = 0;
  189. if (!jar || !host || !host[0] || !set_cookie)
  190. return -1;
  191. /* name=value up to the first ';' */
  192. s = p;
  193. while (*p && *p != ';')
  194. p++;
  195. e = p;
  196. trim(&s, &e);
  197. eqp = s;
  198. while (eqp < e && *eqp != '=')
  199. eqp++;
  200. if (eqp == e)
  201. return -1; /* no '=': ignore (RFC 6265 5.2) */
  202. {
  203. const char *ns = s, *ne = eqp, *vs = eqp + 1, *ve = e;
  204. trim(&ns, &ne);
  205. trim(&vs, &ve);
  206. if (ns == ne)
  207. return -1;
  208. if (copy_n(name, sizeof(name), ns, (unsigned int)(ne - ns)) ||
  209. copy_n(value, sizeof(value), vs, (unsigned int)(ve - vs)))
  210. return -1;
  211. }
  212. /* attributes */
  213. while (*p == ';') {
  214. const char *as, *ae, *ve, *vs;
  215. char an[16];
  216. p++;
  217. as = p;
  218. while (*p && *p != ';')
  219. p++;
  220. ae = p;
  221. trim(&as, &ae);
  222. vs = as;
  223. while (vs < ae && *vs != '=')
  224. vs++;
  225. ve = ae;
  226. {
  227. const char *ne = vs;
  228. while (ne > as && is_ws(ne[-1]))
  229. ne--;
  230. if (ne - as >= (int)sizeof(an))
  231. continue;
  232. copy_n(an, sizeof(an), as, (unsigned int)(ne - as));
  233. }
  234. if (vs < ae)
  235. vs++;
  236. trim(&vs, &ve);
  237. if (eq_ci(an, "secure")) {
  238. secure = 1;
  239. } else if (eq_ci(an, "path")) {
  240. if (vs < ve && *vs == '/' &&
  241. copy_n(cpath, sizeof(cpath), vs,
  242. (unsigned int)(ve - vs)) == 0)
  243. have_path = 1;
  244. } else if (eq_ci(an, "max-age")) {
  245. const char *q = vs;
  246. int neg = 0, nonzero = 0, digits = 0;
  247. if (q < ve && *q == '-') {
  248. neg = 1;
  249. q++;
  250. }
  251. while (q < ve && *q >= '0' && *q <= '9') {
  252. if (*q != '0')
  253. nonzero = 1;
  254. digits++;
  255. q++;
  256. }
  257. if (digits && (neg || !nonzero))
  258. del = 1;
  259. } else if (eq_ci(an, "expires")) {
  260. if (expires_past(vs, ve))
  261. del = 1;
  262. }
  263. }
  264. if (!have_path)
  265. default_path(path ? path : "/", cpath, sizeof(cpath));
  266. return store(jar, host, cpath, name, value, secure, del);
  267. }
  268. unsigned int uhttp_jar_header(const struct uhttp_jar *jar, const char *host,
  269. const char *path, int secure, char *out,
  270. unsigned int cap)
  271. {
  272. int idx[UHTTP_COOKIE_MAX];
  273. unsigned int plen[UHTTP_COOKIE_MAX];
  274. int n = 0, i, j;
  275. unsigned int len = 0;
  276. if (cap)
  277. out[0] = '\0';
  278. if (!jar || !host)
  279. return 0;
  280. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  281. const struct uhttp_cookie *c = &jar->c[i];
  282. if (!c->used || !eq_ci(c->host, host))
  283. continue;
  284. if (c->secure && !secure)
  285. continue;
  286. if (!path_match(c->path, path ? path : "/"))
  287. continue;
  288. idx[n] = i;
  289. plen[n] = c_len(c->path);
  290. n++;
  291. }
  292. /* Longer paths first, then creation order (RFC 6265 5.4). */
  293. for (i = 1; i < n; i++) {
  294. for (j = i; j > 0; j--) {
  295. const struct uhttp_cookie *a = &jar->c[idx[j - 1]];
  296. const struct uhttp_cookie *b = &jar->c[idx[j]];
  297. int swap = plen[j] > plen[j - 1] ||
  298. (plen[j] == plen[j - 1] && b->seq < a->seq);
  299. if (!swap)
  300. break;
  301. {
  302. int ti = idx[j];
  303. unsigned int tl = plen[j];
  304. idx[j] = idx[j - 1];
  305. plen[j] = plen[j - 1];
  306. idx[j - 1] = ti;
  307. plen[j - 1] = tl;
  308. }
  309. }
  310. }
  311. for (i = 0; i < n; i++) {
  312. const struct uhttp_cookie *c = &jar->c[idx[i]];
  313. unsigned int nl = c_len(c->name), vl = c_len(c->value);
  314. unsigned int need = nl + 1 + vl + (len ? 2 : 0);
  315. unsigned int k;
  316. if (len + need + 1 > cap)
  317. break;
  318. if (len) {
  319. out[len++] = ';';
  320. out[len++] = ' ';
  321. }
  322. for (k = 0; k < nl; k++)
  323. out[len++] = c->name[k];
  324. out[len++] = '=';
  325. for (k = 0; k < vl; k++)
  326. out[len++] = c->value[k];
  327. out[len] = '\0';
  328. }
  329. return len;
  330. }
  331. int uhttp_jar_import(struct uhttp_jar *jar, const char *host, int secure,
  332. const char *header)
  333. {
  334. const char *p = header;
  335. int count = 0;
  336. if (!jar || !host || !header)
  337. return 0;
  338. while (*p) {
  339. const char *s = p, *e, *eqp;
  340. char name[UHTTP_COOKIE_NAME_MAX];
  341. char value[UHTTP_COOKIE_VALUE_MAX];
  342. while (*p && *p != ';')
  343. p++;
  344. e = p;
  345. if (*p == ';')
  346. p++;
  347. trim(&s, &e);
  348. eqp = s;
  349. while (eqp < e && *eqp != '=')
  350. eqp++;
  351. if (eqp == e || eqp == s)
  352. continue;
  353. {
  354. const char *ns = s, *ne = eqp, *vs = eqp + 1, *ve = e;
  355. trim(&ns, &ne);
  356. trim(&vs, &ve);
  357. if (ns == ne ||
  358. copy_n(name, sizeof(name), ns,
  359. (unsigned int)(ne - ns)) ||
  360. copy_n(value, sizeof(value), vs,
  361. (unsigned int)(ve - vs)))
  362. continue;
  363. }
  364. if (store(jar, host, "/", name, value, secure, 0) >= 0)
  365. count++;
  366. }
  367. return count;
  368. }
  369. unsigned int uhttp_jar_count(const struct uhttp_jar *jar, const char *host)
  370. {
  371. unsigned int i, n = 0;
  372. for (i = 0; i < UHTTP_COOKIE_MAX; i++)
  373. if (jar->c[i].used && eq_ci(jar->c[i].host, host))
  374. n++;
  375. return n;
  376. }