| 1 |
|
| #ifndef FT_COOKIEJAR_H |
| 2 |
|
| #define FT_COOKIEJAR_H |
| 3 |
|
| |
| 4 |
|
| /* Fetch::CookieJar - a cookie store, roughly RFC 6265. Parses Set-Cookie |
| 5 |
|
| * response headers and produces the matching Cookie header for a request, |
| 6 |
|
| * honouring domain (host-only + subdomain), path, expiry (Expires/Max-Age) |
| 7 |
|
| * and the Secure flag. A cookie is identified by the triple (domain, path, |
| 8 |
|
| * name); setting the same triple replaces it. The jar is a C struct behind a |
| 9 |
|
| * blessed IV; xs/cookiejar.xs is a thin layer over these helpers. */ |
| 10 |
|
| |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include "ft_win.h" /* strncasecmp: <strings.h>, or the CRT name under MSVC */ |
| 13 |
|
| #include <stdlib.h> |
| 14 |
|
| #include <ctype.h> |
| 15 |
|
| #include <time.h> |
| 16 |
|
| |
| 17 |
|
| typedef struct { |
| 18 |
|
| char *name, *value, *domain, *path; |
| 19 |
|
| int secure, httponly, host_only; |
| 20 |
|
| int has_expires; |
| 21 |
|
| time_t expires; |
| 22 |
|
| } ft_cookie; |
| 23 |
|
| |
| 24 |
|
| typedef struct { |
| 25 |
|
| ft_cookie *v; |
| 26 |
|
| size_t n, cap; |
| 27 |
|
| } ft_jar; |
| 28 |
|
| |
| 29 |
9 |
| static ft_jar *ft_jar_new(void) { |
| 30 |
9 |
| ft_jar *j = (ft_jar *)calloc(1, sizeof(ft_jar)); |
| 31 |
9 |
| return j; |
| 32 |
|
| } |
| 33 |
|
| |
| 34 |
12 |
| static void ft_cookie_wipe(ft_cookie *c) { |
| 35 |
12 |
| free(c->name); free(c->value); free(c->domain); free(c->path); |
| 36 |
12 |
| } |
| 37 |
|
| |
| 38 |
9 |
| static void ft_jar_free(ft_jar *j) { |
| 39 |
|
| size_t i; |
| 40 |
9 |
▶ | if (!j) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!j) return; |
0 |
9 |
9 |
|
| 41 |
17 |
▶ | for (i = 0; i < j->n; i++) ft_cookie_wipe(&j->v[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < j->n; i++) ft_cookie_wipe(&j->v[i]); |
8 |
9 |
17 |
|
| 42 |
9 |
| free(j->v); |
| 43 |
9 |
| free(j); |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| /* unwrap the blessed IV a Fetch::CookieJar carries */ |
| 47 |
42 |
| static ft_jar *ft_jar_of(pTHX_ SV *sv) { |
| 48 |
42 |
▶ | if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
42 |
0 |
42 |
| if (!(SvROK(sv) && SvIOK(SvRV(sv)))) |
0 |
42 |
42 |
|
| 49 |
0 |
| croak("Fetch::CookieJar: not a jar"); |
| 50 |
42 |
| return INT2PTR(ft_jar *, SvIV(SvRV(sv))); |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
454 |
| static char *ft_strdup_n(const char *s, size_t n) { |
| 54 |
454 |
| char *r = (char *)malloc(n + 1); |
| 55 |
454 |
▶ | if (r) { memcpy(r, s, n); r[n] = '\0'; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) { memcpy(r, s, n); r[n] = '\0'; } |
454 |
0 |
454 |
|
| 56 |
454 |
| return r; |
| 57 |
|
| } |
| 58 |
215 |
| static char *ft_strdup0(const char *s) { return ft_strdup_n(s, strlen(s)); } |
| 59 |
|
| |
| 60 |
|
| /* case-insensitive C-string equality */ |
| 61 |
33 |
| static int ft_ci_streq(const char *a, const char *b) { |
| 62 |
325 |
▶ | while (*a && *b) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (*a && *b) { |
296 |
29 |
325 |
| while (*a && *b) { |
296 |
0 |
296 |
|
| 63 |
296 |
▶ | if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) return 0; |
4 |
292 |
296 |
|
| 64 |
292 |
| a++; b++; |
| 65 |
|
| } |
| 66 |
29 |
| return *a == *b; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* --- date parsing (Expires) --------------------------------------------- */ |
| 70 |
|
| |
| 71 |
|
| /* days-since-epoch -> time_t for a UTC calendar date (mon 0-11, year full) */ |
| 72 |
0 |
| static time_t ft_timegm(int sec, int min, int hour, int mday, int mon, int year) { |
| 73 |
|
| static const int cum[12] = { 0,31,59,90,120,151,181,212,243,273,304,334 }; |
| 74 |
0 |
| long y = year, days, leaps = 0, yy; |
| 75 |
|
| int is_leap; |
| 76 |
0 |
▶ | for (yy = 1970; yy < y; yy++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (yy = 1970; yy < y; yy++) |
0 |
0 |
0 |
|
| 77 |
0 |
▶ | if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) leaps++; |
Branch: 3 branches
| Branch | True | False | Total |
| if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) leaps++; |
0 |
0 |
0 |
| if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) leaps++; |
0 |
0 |
0 |
| if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) leaps++; |
0 |
0 |
0 |
|
| 78 |
0 |
▶ | is_leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; |
Branch: 3 branches
| Branch | True | False | Total |
| is_leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; |
0 |
0 |
0 |
| is_leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; |
0 |
0 |
0 |
| is_leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; |
0 |
0 |
0 |
|
| 79 |
0 |
| days = 365L * (y - 1970) + leaps + cum[mon] + (mday - 1); |
| 80 |
0 |
▶ | if (mon >= 2 && is_leap) days += 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mon >= 2 && is_leap) days += 1; |
0 |
0 |
0 |
| if (mon >= 2 && is_leap) days += 1; |
0 |
0 |
0 |
|
| 81 |
0 |
| return (time_t)(days * 86400L + hour * 3600L + min * 60L + sec); |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| static const char *const ft_mon[12] = { |
| 85 |
|
| "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" |
| 86 |
|
| }; |
| 87 |
|
| |
| 88 |
|
| /* Parse "DD Mon YYYY HH:MM:SS" / "DD-Mon-YY HH:MM:SS" anywhere in $s. Returns |
| 89 |
|
| * 1 and *out on success, 0 otherwise. */ |
| 90 |
0 |
| static int ft_parse_date(const char *s, time_t *out) { |
| 91 |
0 |
| size_t len = strlen(s), i; |
| 92 |
0 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
0 |
0 |
0 |
|
| 93 |
0 |
| unsigned day = 0, year = 0, H = 0, M = 0, S = 0; |
| 94 |
|
| char mon[8]; |
| 95 |
0 |
| int nfields = sscanf(s + i, |
| 96 |
|
| "%2u%*[ -]%3[A-Za-z]%*[ -]%u %2u:%2u:%2u", |
| 97 |
|
| &day, mon, &year, &H, &M, &S); |
| 98 |
0 |
▶ | if (nfields == 6) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nfields == 6) { |
0 |
0 |
0 |
|
| 99 |
|
| int m; |
| 100 |
|
| char lm[4]; |
| 101 |
0 |
▶ | if (strlen(mon) != 3) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strlen(mon) != 3) continue; |
0 |
0 |
0 |
|
| 102 |
0 |
| lm[0] = (char)tolower((unsigned char)mon[0]); |
| 103 |
0 |
| lm[1] = (char)tolower((unsigned char)mon[1]); |
| 104 |
0 |
| lm[2] = (char)tolower((unsigned char)mon[2]); |
| 105 |
0 |
| lm[3] = '\0'; |
| 106 |
0 |
▶ | for (m = 0; m < 12; m++) if (strcmp(lm, ft_mon[m]) == 0) break; |
Branch: 2 branches
| Branch | True | False | Total |
| for (m = 0; m < 12; m++) if (strcmp(lm, ft_mon[m]) == 0) break; |
0 |
0 |
0 |
| for (m = 0; m < 12; m++) if (strcmp(lm, ft_mon[m]) == 0) break; |
0 |
0 |
0 |
|
| 107 |
0 |
▶ | if (m == 12) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (m == 12) continue; |
0 |
0 |
0 |
|
| 108 |
0 |
▶ | if (year < 100) year += (year < 70 ? 2000 : 1900); |
Branch: 2 branches
| Branch | True | False | Total |
| if (year < 100) year += (year < 70 ? 2000 : 1900); |
0 |
0 |
0 |
| if (year < 100) year += (year < 70 ? 2000 : 1900); |
0 |
0 |
0 |
|
| 109 |
0 |
▶ | if (day < 1 || day > 31 || H > 23 || M > 59 || S > 61) continue; |
Branch: 5 branches
| Branch | True | False | Total |
| if (day < 1 || day > 31 || H > 23 || M > 59 || S > 61) continue; |
0 |
0 |
0 |
| if (day < 1 || day > 31 || H > 23 || M > 59 || S > 61) continue; |
0 |
0 |
0 |
| if (day < 1 || day > 31 || H > 23 || M > 59 || S > 61) continue; |
0 |
0 |
0 |
| if (day < 1 || day > 31 || H > 23 || M > 59 || S > 61) continue; |
0 |
0 |
0 |
| if (day < 1 || day > 31 || H > 23 || M > 59 || S > 61) continue; |
0 |
0 |
0 |
|
| 110 |
0 |
| *out = ft_timegm((int)S, (int)M, (int)H, (int)day, m, (int)year); |
| 111 |
0 |
| return 1; |
| 112 |
|
| } |
| 113 |
|
| } |
| 114 |
0 |
| return 0; |
| 115 |
|
| } |
| 116 |
|
| |
| 117 |
|
| /* --- matching ----------------------------------------------------------- */ |
| 118 |
|
| |
| 119 |
22 |
| static int ft_domain_match(const char *host, const char *domain, int host_only) { |
| 120 |
22 |
| size_t hl = strlen(host), dl = strlen(domain); |
| 121 |
22 |
▶ | if (host_only) return ft_ci_streq(host, domain); |
Branch: 1 branch
| Branch | True | False | Total |
| if (host_only) return ft_ci_streq(host, domain); |
18 |
4 |
22 |
|
| 122 |
4 |
▶ | if (ft_ci_streq(host, domain)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ft_ci_streq(host, domain)) return 1; |
2 |
2 |
4 |
|
| 123 |
|
| /* subdomain: host ends with ".domain" (case-insensitive) */ |
| 124 |
2 |
▶ | if (hl > dl + 1 && host[hl - dl - 1] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (hl > dl + 1 && host[hl - dl - 1] == '.') { |
2 |
0 |
2 |
| if (hl > dl + 1 && host[hl - dl - 1] == '.') { |
1 |
1 |
2 |
|
| 125 |
1 |
| const char *tail = host + (hl - dl); |
| 126 |
1 |
| return ft_ci_streq(tail, domain); |
| 127 |
|
| } |
| 128 |
1 |
| return 0; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
17 |
| static int ft_path_match(const char *req, const char *cookie) { |
| 132 |
17 |
| size_t cl = strlen(cookie); |
| 133 |
17 |
▶ | if (strcmp(req, cookie) == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(req, cookie) == 0) return 1; |
2 |
15 |
17 |
|
| 134 |
15 |
▶ | if (strncmp(req, cookie, cl) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(req, cookie, cl) != 0) return 0; |
2 |
13 |
15 |
|
| 135 |
13 |
▶ | if (cl && cookie[cl - 1] == '/') return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cl && cookie[cl - 1] == '/') return 1; |
13 |
0 |
13 |
| if (cl && cookie[cl - 1] == '/') return 1; |
10 |
3 |
13 |
|
| 136 |
3 |
| return req[cl] == '/'; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| /* default-path per RFC 6265 5.1.4, from the request path (query stripped) */ |
| 140 |
2 |
| static char *ft_default_path(const char *p) { |
| 141 |
2 |
| const char *q = strchr(p, '?'); |
| 142 |
2 |
▶ | size_t plen = q ? (size_t)(q - p) : strlen(p); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t plen = q ? (size_t)(q - p) : strlen(p); |
0 |
2 |
2 |
|
| 143 |
|
| const char *last; |
| 144 |
2 |
▶ | if (plen == 0 || p[0] != '/') return ft_strdup0("/"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (plen == 0 || p[0] != '/') return ft_strdup0("/"); |
2 |
0 |
2 |
| if (plen == 0 || p[0] != '/') return ft_strdup0("/"); |
0 |
2 |
2 |
|
| 145 |
|
| /* last '/' within [0,plen) */ |
| 146 |
4 |
▶ | { size_t i; last = NULL; for (i = 0; i < plen; i++) if (p[i] == '/') last = p + i; } |
Branch: 2 branches
| Branch | True | False | Total |
| { size_t i; last = NULL; for (i = 0; i < plen; i++) if (p[i] == '/') last = p + i; } |
2 |
0 |
2 |
| { size_t i; last = NULL; for (i = 0; i < plen; i++) if (p[i] == '/') last = p + i; } |
2 |
2 |
4 |
|
| 147 |
2 |
▶ | if (!last || last == p) return ft_strdup0("/"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!last || last == p) return ft_strdup0("/"); |
2 |
0 |
2 |
| if (!last || last == p) return ft_strdup0("/"); |
2 |
0 |
2 |
|
| 148 |
0 |
| return ft_strdup_n(p, (size_t)(last - p)); |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
|
| /* --- store -------------------------------------------------------------- */ |
| 152 |
|
| |
| 153 |
10 |
| static int ft_cookie_same(const ft_cookie *c, const char *dom, const char *path, |
| 154 |
|
| const char *name) { |
| 155 |
10 |
| return ft_ci_streq(c->domain, dom) |
| 156 |
10 |
▶ | && strcmp(c->path, path) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| && strcmp(c->path, path) == 0 |
7 |
3 |
10 |
|
| 157 |
20 |
▶ | && strcmp(c->name, name) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| && strcmp(c->name, name) == 0; |
10 |
0 |
10 |
| && strcmp(c->name, name) == 0; |
2 |
5 |
7 |
|
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| /* index of the (domain,path,name) triple, or -1 */ |
| 161 |
11 |
| static long ft_jar_find(ft_jar *j, const char *dom, const char *path, |
| 162 |
|
| const char *name) { |
| 163 |
|
| size_t i; |
| 164 |
19 |
▶ | for (i = 0; i < j->n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < j->n; i++) |
10 |
9 |
19 |
|
| 165 |
10 |
▶ | if (ft_cookie_same(&j->v[i], dom, path, name)) return (long)i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ft_cookie_same(&j->v[i], dom, path, name)) return (long)i; |
2 |
8 |
10 |
|
| 166 |
9 |
| return -1; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
1 |
| static void ft_jar_delete_at(ft_jar *j, size_t idx) { |
| 170 |
1 |
| ft_cookie_wipe(&j->v[idx]); |
| 171 |
1 |
▶ | if (idx < j->n - 1) j->v[idx] = j->v[j->n - 1]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx < j->n - 1) j->v[idx] = j->v[j->n - 1]; |
0 |
1 |
1 |
|
| 172 |
1 |
| j->n--; |
| 173 |
1 |
| } |
| 174 |
|
| |
| 175 |
|
| /* move ownership of *src into the store, replacing any same-triple cookie */ |
| 176 |
10 |
| static void ft_jar_put(ft_jar *j, ft_cookie *src) { |
| 177 |
10 |
| long idx = ft_jar_find(j, src->domain, src->path, src->name); |
| 178 |
10 |
▶ | if (idx >= 0) { ft_cookie_wipe(&j->v[idx]); j->v[idx] = *src; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx >= 0) { ft_cookie_wipe(&j->v[idx]); j->v[idx] = *src; return; } |
1 |
9 |
10 |
|
| 179 |
9 |
▶ | if (j->n == j->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j->n == j->cap) { |
5 |
4 |
9 |
|
| 180 |
5 |
▶ | size_t nc = j->cap ? j->cap * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t nc = j->cap ? j->cap * 2 : 8; |
0 |
5 |
5 |
|
| 181 |
5 |
| ft_cookie *nv = (ft_cookie *)realloc(j->v, nc * sizeof(ft_cookie)); |
| 182 |
5 |
▶ | if (!nv) { ft_cookie_wipe(src); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nv) { ft_cookie_wipe(src); return; } |
0 |
5 |
5 |
|
| 183 |
5 |
| j->v = nv; j->cap = nc; |
| 184 |
|
| } |
| 185 |
9 |
| j->v[j->n++] = *src; |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
|
| /* --- Set-Cookie parsing ------------------------------------------------- */ |
| 189 |
|
| |
| 190 |
77 |
| static void ft_trim(const char **ps, size_t *pn) { |
| 191 |
77 |
| const char *s = *ps; size_t n = *pn; |
| 192 |
91 |
▶ | while (n && (s[0]==' '||s[0]=='\t'||s[0]=='\r'||s[0]=='\n'||s[0]=='\f')) { s++; n--; } |
Branch: 6 branches
| Branch | True | False | Total |
| while (n && (s[0]==' '||s[0]=='\t'||s[0]=='\r'||s[0]=='\n'||s[0]=='\f')) { s++; n--; } |
91 |
0 |
91 |
| while (n && (s[0]==' '||s[0]=='\t'||s[0]=='\r'||s[0]=='\n'||s[0]=='\f')) { s++; n--; } |
14 |
77 |
91 |
| while (n && (s[0]==' '||s[0]=='\t'||s[0]=='\r'||s[0]=='\n'||s[0]=='\f')) { s++; n--; } |
0 |
77 |
77 |
| while (n && (s[0]==' '||s[0]=='\t'||s[0]=='\r'||s[0]=='\n'||s[0]=='\f')) { s++; n--; } |
0 |
77 |
77 |
| while (n && (s[0]==' '||s[0]=='\t'||s[0]=='\r'||s[0]=='\n'||s[0]=='\f')) { s++; n--; } |
0 |
77 |
77 |
| while (n && (s[0]==' '||s[0]=='\t'||s[0]=='\r'||s[0]=='\n'||s[0]=='\f')) { s++; n--; } |
0 |
77 |
77 |
|
| 193 |
77 |
▶ | while (n && (s[n-1]==' '||s[n-1]=='\t'||s[n-1]=='\r'||s[n-1]=='\n'||s[n-1]=='\f')) n--; |
Branch: 6 branches
| Branch | True | False | Total |
| while (n && (s[n-1]==' '||s[n-1]=='\t'||s[n-1]=='\r'||s[n-1]=='\n'||s[n-1]=='\f')) n--; |
77 |
0 |
77 |
| while (n && (s[n-1]==' '||s[n-1]=='\t'||s[n-1]=='\r'||s[n-1]=='\n'||s[n-1]=='\f')) n--; |
0 |
77 |
77 |
| while (n && (s[n-1]==' '||s[n-1]=='\t'||s[n-1]=='\r'||s[n-1]=='\n'||s[n-1]=='\f')) n--; |
0 |
77 |
77 |
| while (n && (s[n-1]==' '||s[n-1]=='\t'||s[n-1]=='\r'||s[n-1]=='\n'||s[n-1]=='\f')) n--; |
0 |
77 |
77 |
| while (n && (s[n-1]==' '||s[n-1]=='\t'||s[n-1]=='\r'||s[n-1]=='\n'||s[n-1]=='\f')) n--; |
0 |
77 |
77 |
| while (n && (s[n-1]==' '||s[n-1]=='\t'||s[n-1]=='\r'||s[n-1]=='\n'||s[n-1]=='\f')) n--; |
0 |
77 |
77 |
|
| 194 |
77 |
| *ps = s; *pn = n; |
| 195 |
77 |
| } |
| 196 |
|
| |
| 197 |
|
| /* Parse one Set-Cookie value for request host/req_path and store or delete it. |
| 198 |
|
| * Returns 1 if the jar was touched (stored or deleted), 0 on a malformed value |
| 199 |
|
| * that was ignored. */ |
| 200 |
12 |
| static int ft_jar_set_cookie(ft_jar *j, const char *str, const char *host, |
| 201 |
|
| const char *req_path) { |
| 202 |
|
| ft_cookie c; |
| 203 |
|
| const char *p, *end, *seg; |
| 204 |
12 |
| const char *dom_attr = NULL; size_t dom_len = 0; |
| 205 |
12 |
| const char *path_attr = NULL; size_t path_len = 0; |
| 206 |
12 |
| int max_age_delete = 0; |
| 207 |
12 |
| time_t now = time(NULL); |
| 208 |
12 |
| int first = 1; |
| 209 |
|
| |
| 210 |
12 |
▶ | if (!str || !*str) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!str || !*str) return 0; |
12 |
0 |
12 |
| if (!str || !*str) return 0; |
0 |
12 |
12 |
|
| 211 |
12 |
| memset(&c, 0, sizeof c); |
| 212 |
|
| |
| 213 |
12 |
| p = str; end = str + strlen(str); |
| 214 |
38 |
▶ | while (p <= end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p <= end) { |
26 |
12 |
38 |
|
| 215 |
26 |
| const char *semi = memchr(p, ';', (size_t)(end - p)); |
| 216 |
26 |
| const char *sstart = p; |
| 217 |
26 |
▶ | size_t slen = semi ? (size_t)(semi - p) : (size_t)(end - p); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t slen = semi ? (size_t)(semi - p) : (size_t)(end - p); |
14 |
12 |
26 |
|
| 218 |
26 |
| seg = sstart; |
| 219 |
26 |
| ft_trim(&seg, &slen); |
| 220 |
26 |
▶ | p = semi ? semi + 1 : end + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| p = semi ? semi + 1 : end + 1; |
14 |
12 |
26 |
|
| 221 |
|
| |
| 222 |
26 |
▶ | if (first) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (first) { |
12 |
14 |
26 |
|
| 223 |
|
| /* name=value */ |
| 224 |
12 |
| const char *eq = memchr(seg, '=', slen); |
| 225 |
|
| const char *nm, *vl; size_t nl, vln; |
| 226 |
12 |
| first = 0; |
| 227 |
12 |
▶ | if (!eq) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!eq) return 0; |
0 |
12 |
12 |
|
| 228 |
12 |
| nm = seg; nl = (size_t)(eq - seg); |
| 229 |
12 |
| vl = eq + 1; vln = slen - nl - 1; |
| 230 |
12 |
| ft_trim(&nm, &nl); ft_trim(&vl, &vln); |
| 231 |
12 |
▶ | if (!nl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nl) return 0; |
0 |
12 |
12 |
|
| 232 |
12 |
| c.name = ft_strdup_n(nm, nl); |
| 233 |
12 |
| c.value = ft_strdup_n(vl, vln); |
| 234 |
12 |
| continue; |
| 235 |
|
| } |
| 236 |
14 |
▶ | if (!slen) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!slen) continue; |
0 |
14 |
14 |
|
| 237 |
|
| { |
| 238 |
14 |
| const char *eq = memchr(seg, '=', slen); |
| 239 |
14 |
| const char *kk = seg, *vv = NULL; size_t kl = slen, vl = 0; |
| 240 |
14 |
▶ | if (eq) { kl = (size_t)(eq - seg); vv = eq + 1; vl = slen - kl - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eq) { kl = (size_t)(eq - seg); vv = eq + 1; vl = slen - kl - 1; |
13 |
1 |
14 |
|
| 241 |
13 |
| ft_trim(&kk, &kl); ft_trim(&vv, &vl); } |
| 242 |
1 |
| else { ft_trim(&kk, &kl); } |
| 243 |
14 |
▶ | if (kl == 6 && strncasecmp(kk, "domain", 6) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 6 && strncasecmp(kk, "domain", 6) == 0) { |
4 |
10 |
14 |
| if (kl == 6 && strncasecmp(kk, "domain", 6) == 0) { |
3 |
1 |
4 |
|
| 244 |
3 |
▶ | if (vv) { if (vl && vv[0] == '.') { vv++; vl--; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (vv) { if (vl && vv[0] == '.') { vv++; vl--; } |
3 |
0 |
3 |
| if (vv) { if (vl && vv[0] == '.') { vv++; vl--; } |
3 |
0 |
3 |
| if (vv) { if (vl && vv[0] == '.') { vv++; vl--; } |
0 |
3 |
3 |
|
| 245 |
3 |
| dom_attr = vv; dom_len = vl; } |
| 246 |
11 |
▶ | } else if (kl == 4 && strncasecmp(kk, "path", 4) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (kl == 4 && strncasecmp(kk, "path", 4) == 0) { |
9 |
2 |
11 |
| } else if (kl == 4 && strncasecmp(kk, "path", 4) == 0) { |
9 |
0 |
9 |
|
| 247 |
9 |
▶ | if (vv) { path_attr = vv; path_len = vl; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (vv) { path_attr = vv; path_len = vl; } |
9 |
0 |
9 |
|
| 248 |
2 |
▶ | } else if (kl == 6 && strncasecmp(kk, "secure", 6) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (kl == 6 && strncasecmp(kk, "secure", 6) == 0) { |
1 |
1 |
2 |
| } else if (kl == 6 && strncasecmp(kk, "secure", 6) == 0) { |
1 |
0 |
1 |
|
| 249 |
1 |
| c.secure = 1; |
| 250 |
1 |
▶ | } else if (kl == 8 && strncasecmp(kk, "httponly", 8) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (kl == 8 && strncasecmp(kk, "httponly", 8) == 0) { |
0 |
1 |
1 |
| } else if (kl == 8 && strncasecmp(kk, "httponly", 8) == 0) { |
0 |
0 |
0 |
|
| 251 |
0 |
| c.httponly = 1; |
| 252 |
2 |
▶ | } else if (kl == 7 && strncasecmp(kk, "max-age", 7) == 0 && vv) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (kl == 7 && strncasecmp(kk, "max-age", 7) == 0 && vv) { |
1 |
0 |
1 |
| } else if (kl == 7 && strncasecmp(kk, "max-age", 7) == 0 && vv) { |
1 |
0 |
1 |
| } else if (kl == 7 && strncasecmp(kk, "max-age", 7) == 0 && vv) { |
1 |
0 |
1 |
|
| 253 |
1 |
| char *tmp = ft_strdup_n(vv, vl), *endp = NULL; |
| 254 |
1 |
▶ | if (tmp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp) { |
1 |
0 |
1 |
|
| 255 |
1 |
| long ma = strtol(tmp, &endp, 10); |
| 256 |
1 |
▶ | if (endp && *endp == '\0' && endp != tmp) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (endp && *endp == '\0' && endp != tmp) { |
1 |
0 |
1 |
| if (endp && *endp == '\0' && endp != tmp) { |
1 |
0 |
1 |
| if (endp && *endp == '\0' && endp != tmp) { |
1 |
0 |
1 |
|
| 257 |
1 |
| c.expires = now + ma; c.has_expires = 1; |
| 258 |
1 |
▶ | if (ma <= 0) max_age_delete = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ma <= 0) max_age_delete = 1; |
1 |
0 |
1 |
|
| 259 |
|
| } |
| 260 |
1 |
| free(tmp); |
| 261 |
|
| } |
| 262 |
0 |
▶ | } else if (kl == 7 && strncasecmp(kk, "expires", 7) == 0 |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (kl == 7 && strncasecmp(kk, "expires", 7) == 0 |
0 |
0 |
0 |
| } else if (kl == 7 && strncasecmp(kk, "expires", 7) == 0 |
0 |
0 |
0 |
|
| 263 |
0 |
▶ | && vv && !c.has_expires) { |
Branch: 2 branches
| Branch | True | False | Total |
| && vv && !c.has_expires) { |
0 |
0 |
0 |
| && vv && !c.has_expires) { |
0 |
0 |
0 |
|
| 264 |
0 |
| char *tmp = ft_strdup_n(vv, vl); |
| 265 |
|
| time_t t; |
| 266 |
0 |
▶ | if (tmp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp) { |
0 |
0 |
0 |
|
| 267 |
0 |
▶ | if (ft_parse_date(tmp, &t)) { c.expires = t; c.has_expires = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ft_parse_date(tmp, &t)) { c.expires = t; c.has_expires = 1; } |
0 |
0 |
0 |
|
| 268 |
0 |
| free(tmp); |
| 269 |
|
| } |
| 270 |
|
| } |
| 271 |
|
| } |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| /* domain: default to host (host-only); else must domain-match */ |
| 275 |
12 |
▶ | if (dom_attr && dom_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (dom_attr && dom_len) { |
3 |
9 |
12 |
| if (dom_attr && dom_len) { |
3 |
0 |
3 |
|
| 276 |
3 |
| c.host_only = 0; |
| 277 |
3 |
| c.domain = ft_strdup_n(dom_attr, dom_len); |
| 278 |
|
| /* Reject a single-label / public-suffix Domain (e.g. "com", ".com", |
| 279 |
|
| * "localhost"): a cookie must not be scoped to a whole TLD, or it would |
| 280 |
|
| * be sent to every host under it. Full Public Suffix List handling is |
| 281 |
|
| * out of scope; requiring an interior dot blocks the blatant TLD |
| 282 |
|
| * supercookie. */ |
| 283 |
|
| { |
| 284 |
3 |
| const char *d = c.domain; |
| 285 |
3 |
▶ | if (*d == '.') d++; /* ignore a single leading dot */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*d == '.') d++; /* ignore a single leading dot */ |
0 |
3 |
3 |
|
| 286 |
3 |
▶ | if (!strchr(d, '.')) { ft_cookie_wipe(&c); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!strchr(d, '.')) { ft_cookie_wipe(&c); return 0; } |
0 |
3 |
3 |
|
| 287 |
|
| } |
| 288 |
3 |
▶ | if (!ft_domain_match(host, c.domain, 0)) { ft_cookie_wipe(&c); return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_domain_match(host, c.domain, 0)) { ft_cookie_wipe(&c); return 0; } |
1 |
2 |
3 |
|
| 289 |
|
| } else { |
| 290 |
9 |
| c.host_only = 1; |
| 291 |
9 |
| c.domain = ft_strdup0(host); |
| 292 |
|
| } |
| 293 |
11 |
▶ | if (path_attr && path_len) c.path = ft_strdup_n(path_attr, path_len); |
Branch: 2 branches
| Branch | True | False | Total |
| if (path_attr && path_len) c.path = ft_strdup_n(path_attr, path_len); |
9 |
2 |
11 |
| if (path_attr && path_len) c.path = ft_strdup_n(path_attr, path_len); |
9 |
0 |
9 |
|
| 294 |
2 |
| else c.path = ft_default_path(req_path); |
| 295 |
|
| |
| 296 |
11 |
▶ | if ((c.has_expires && c.expires <= now) || max_age_delete) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((c.has_expires && c.expires <= now) || max_age_delete) { |
1 |
10 |
11 |
| if ((c.has_expires && c.expires <= now) || max_age_delete) { |
0 |
1 |
1 |
| if ((c.has_expires && c.expires <= now) || max_age_delete) { |
0 |
10 |
10 |
|
| 297 |
1 |
| long idx = ft_jar_find(j, c.domain, c.path, c.name); |
| 298 |
1 |
▶ | if (idx >= 0) ft_jar_delete_at(j, (size_t)idx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx >= 0) ft_jar_delete_at(j, (size_t)idx); |
1 |
0 |
1 |
|
| 299 |
1 |
| ft_cookie_wipe(&c); |
| 300 |
1 |
| return 1; |
| 301 |
|
| } |
| 302 |
10 |
| ft_jar_put(j, &c); |
| 303 |
10 |
| return 1; |
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
|
| /* --- housekeeping ------------------------------------------------------- */ |
| 307 |
|
| |
| 308 |
0 |
| static void ft_jar_clear(ft_jar *j) { |
| 309 |
|
| size_t i; |
| 310 |
0 |
▶ | for (i = 0; i < j->n; i++) ft_cookie_wipe(&j->v[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < j->n; i++) ft_cookie_wipe(&j->v[i]); |
0 |
0 |
0 |
|
| 311 |
0 |
| j->n = 0; |
| 312 |
0 |
| } |
| 313 |
|
| |
| 314 |
0 |
| static int ft_jar_purge(ft_jar *j) { |
| 315 |
0 |
| time_t now = time(NULL); |
| 316 |
0 |
| size_t i = 0; int removed = 0; |
| 317 |
0 |
▶ | while (i < j->n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < j->n) { |
0 |
0 |
0 |
|
| 318 |
0 |
▶ | if (j->v[i].has_expires && j->v[i].expires <= now) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (j->v[i].has_expires && j->v[i].expires <= now) { |
0 |
0 |
0 |
| if (j->v[i].has_expires && j->v[i].expires <= now) { |
0 |
0 |
0 |
|
| 319 |
0 |
| ft_jar_delete_at(j, i); removed++; |
| 320 |
0 |
| } else i++; |
| 321 |
|
| } |
| 322 |
0 |
| return removed; |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
|
| /* longest-path-first ordering for the Cookie header (RFC 6265 5.4) */ |
| 326 |
|
| typedef struct { size_t len; size_t i; } ft_match; |
| 327 |
5 |
| static int ft_match_cmp(const void *a, const void *b) { |
| 328 |
5 |
| size_t la = ((const ft_match *)a)->len, lb = ((const ft_match *)b)->len; |
| 329 |
5 |
| return (la < lb) - (la > lb); /* descending */ |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
|
| /* Build the "n1=v1; n2=v2" Cookie header for a request, longest path first, or |
| 333 |
|
| * NULL if nothing matches. Caller frees the returned buffer. */ |
| 334 |
16 |
| static char *ft_jar_cookie_str(ft_jar *j, const char *host, |
| 335 |
|
| const char *req_path, int secure) { |
| 336 |
16 |
| const char *q = strchr(req_path, '?'); |
| 337 |
16 |
▶ | size_t plen = q ? (size_t)(q - req_path) : strlen(req_path); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t plen = q ? (size_t)(q - req_path) : strlen(req_path); |
0 |
16 |
16 |
|
| 338 |
16 |
▶ | char *path = plen ? ft_strdup_n(req_path, plen) : ft_strdup0("/"); |
Branch: 1 branch
| Branch | True | False | Total |
| char *path = plen ? ft_strdup_n(req_path, plen) : ft_strdup0("/"); |
16 |
0 |
16 |
|
| 339 |
16 |
| time_t now = time(NULL); |
| 340 |
|
| ft_match *ms; |
| 341 |
16 |
| size_t m = 0, k, total = 0; |
| 342 |
16 |
| char *out = NULL; |
| 343 |
|
| |
| 344 |
16 |
▶ | ms = (ft_match *)malloc((j->n ? j->n : 1) * sizeof(ft_match)); |
Branch: 1 branch
| Branch | True | False | Total |
| ms = (ft_match *)malloc((j->n ? j->n : 1) * sizeof(ft_match)); |
13 |
3 |
16 |
|
| 345 |
38 |
▶ | for (k = 0; k < j->n; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < j->n; k++) { |
22 |
16 |
38 |
|
| 346 |
22 |
| ft_cookie *c = &j->v[k]; |
| 347 |
22 |
▶ | if (c->secure && !secure) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->secure && !secure) continue; |
4 |
18 |
22 |
| if (c->secure && !secure) continue; |
3 |
1 |
4 |
|
| 348 |
19 |
▶ | if (c->has_expires && c->expires <= now) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->has_expires && c->expires <= now) continue; |
0 |
19 |
19 |
| if (c->has_expires && c->expires <= now) continue; |
0 |
0 |
0 |
|
| 349 |
19 |
▶ | if (!ft_domain_match(host, c->domain, c->host_only)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_domain_match(host, c->domain, c->host_only)) continue; |
2 |
17 |
19 |
|
| 350 |
17 |
▶ | if (!ft_path_match(path, c->path)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ft_path_match(path, c->path)) continue; |
2 |
15 |
17 |
|
| 351 |
15 |
| ms[m].len = strlen(c->path); |
| 352 |
15 |
| ms[m].i = k; |
| 353 |
15 |
| m++; |
| 354 |
|
| } |
| 355 |
16 |
▶ | if (m) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (m) { |
10 |
6 |
16 |
|
| 356 |
|
| char *p; |
| 357 |
10 |
| qsort(ms, m, sizeof(ft_match), ft_match_cmp); |
| 358 |
25 |
▶ | for (k = 0; k < m; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < m; k++) { |
15 |
10 |
25 |
|
| 359 |
15 |
| ft_cookie *c = &j->v[ms[k].i]; |
| 360 |
15 |
| total += strlen(c->name) + 1 + strlen(c->value); /* name=value */ |
| 361 |
15 |
▶ | if (k) total += 2; /* "; " */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (k) total += 2; /* "; " */ |
5 |
10 |
15 |
|
| 362 |
|
| } |
| 363 |
10 |
| out = (char *)malloc(total + 1); |
| 364 |
10 |
| p = out; |
| 365 |
25 |
▶ | for (k = 0; k < m; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < m; k++) { |
15 |
10 |
25 |
|
| 366 |
15 |
| ft_cookie *c = &j->v[ms[k].i]; |
| 367 |
15 |
| size_t nl = strlen(c->name), vl = strlen(c->value); |
| 368 |
15 |
▶ | if (k) { *p++ = ';'; *p++ = ' '; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (k) { *p++ = ';'; *p++ = ' '; } |
5 |
10 |
15 |
|
| 369 |
15 |
| memcpy(p, c->name, nl); p += nl; |
| 370 |
15 |
| *p++ = '='; |
| 371 |
15 |
| memcpy(p, c->value, vl); p += vl; |
| 372 |
|
| } |
| 373 |
10 |
| *p = '\0'; |
| 374 |
|
| } |
| 375 |
16 |
| free(ms); |
| 376 |
16 |
| free(path); |
| 377 |
16 |
| return out; |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
|
| #endif /* FT_COOKIEJAR_H */ |