| 1 |
|
| /* punk_accept.h - Accept negotiation for $c->respond_to, in C. |
| 2 |
|
| * |
| 3 |
|
| * A small RFC 9110 media-range parser: no regex engine, no allocation - the |
| 4 |
|
| * ranges point into the header's own buffer. Enough Accept to be correct |
| 5 |
|
| * about the two things naive matching gets wrong: q-values order the |
| 6 |
|
| * browser's `text/html,application/xml;q=0.9,* / *;q=0.8` correctly, and |
| 7 |
|
| * `q=0` excludes rather than matches. |
| 8 |
|
| * |
| 9 |
|
| * Include after punk_response.h; xs/context.xs is the consumer. |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #ifndef PUNK_ACCEPT_H |
| 13 |
|
| #define PUNK_ACCEPT_H |
| 14 |
|
| |
| 15 |
|
| #define PA_RANGE_MAX 32 |
| 16 |
|
| |
| 17 |
|
| typedef struct pa_range { |
| 18 |
|
| const char *t; STRLEN tl; /* type, pointing into the header */ |
| 19 |
|
| const char *s; STRLEN sl; /* subtype, pointing into the header */ |
| 20 |
|
| int q; /* per mille: 0..1000 */ |
| 21 |
|
| } pa_range; |
| 22 |
|
| |
| 23 |
599 |
▶ | static int pa_ws(char c) { return c == ' ' || c == '\t'; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int pa_ws(char c) { return c == ' ' || c == '\t'; } |
593 |
6 |
599 |
| static int pa_ws(char c) { return c == ' ' || c == '\t'; } |
0 |
593 |
593 |
|
| 24 |
|
| |
| 25 |
|
| /* the q of one parameter list: ";q=0.8" -> 800; absent or malformed -> 1000 */ |
| 26 |
237 |
| static int pa_q_of(const char *p, const char *end) { |
| 27 |
237 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
19 |
218 |
237 |
|
| 28 |
39 |
▶ | while (p < end && (pa_ws(*p) || *p == ';')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (pa_ws(*p) || *p == ';')) p++; |
39 |
0 |
39 |
| while (p < end && (pa_ws(*p) || *p == ';')) p++; |
0 |
39 |
39 |
| while (p < end && (pa_ws(*p) || *p == ';')) p++; |
20 |
19 |
39 |
|
| 29 |
19 |
▶ | if (p + 1 < end && (*p == 'q' || *p == 'Q')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (p + 1 < end && (*p == 'q' || *p == 'Q')) { |
19 |
0 |
19 |
| if (p + 1 < end && (*p == 'q' || *p == 'Q')) { |
0 |
19 |
19 |
| if (p + 1 < end && (*p == 'q' || *p == 'Q')) { |
0 |
0 |
0 |
|
| 30 |
19 |
| const char *v = p + 1; |
| 31 |
19 |
▶ | while (v < end && pa_ws(*v)) v++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (v < end && pa_ws(*v)) v++; |
19 |
0 |
19 |
| while (v < end && pa_ws(*v)) v++; |
0 |
19 |
19 |
|
| 32 |
19 |
▶ | if (v < end && *v == '=') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (v < end && *v == '=') { |
19 |
0 |
19 |
| if (v < end && *v == '=') { |
19 |
0 |
19 |
|
| 33 |
|
| int q; |
| 34 |
19 |
| v++; |
| 35 |
19 |
▶ | while (v < end && pa_ws(*v)) v++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (v < end && pa_ws(*v)) v++; |
17 |
2 |
19 |
| while (v < end && pa_ws(*v)) v++; |
0 |
17 |
17 |
|
| 36 |
19 |
▶ | if (v >= end) return 1000; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v >= end) return 1000; |
2 |
17 |
19 |
|
| 37 |
17 |
▶ | if (*v == '1') return 1000; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*v == '1') return 1000; |
0 |
17 |
17 |
|
| 38 |
17 |
▶ | if (*v != '0') return 1000; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*v != '0') return 1000; |
1 |
16 |
17 |
|
| 39 |
16 |
| q = 0; v++; |
| 40 |
16 |
▶ | if (v < end && *v == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (v < end && *v == '.') { |
12 |
4 |
16 |
| if (v < end && *v == '.') { |
12 |
0 |
12 |
|
| 41 |
12 |
| int place = 100; |
| 42 |
12 |
| v++; |
| 43 |
24 |
▶ | while (v < end && *v >= '0' && *v <= '9' && place) { |
Branch: 4 branches
| Branch | True | False | Total |
| while (v < end && *v >= '0' && *v <= '9' && place) { |
12 |
12 |
24 |
| while (v < end && *v >= '0' && *v <= '9' && place) { |
12 |
0 |
12 |
| while (v < end && *v >= '0' && *v <= '9' && place) { |
12 |
0 |
12 |
| while (v < end && *v >= '0' && *v <= '9' && place) { |
12 |
0 |
12 |
|
| 44 |
12 |
| q += (*v - '0') * place; |
| 45 |
12 |
| place /= 10; v++; |
| 46 |
|
| } |
| 47 |
|
| } |
| 48 |
16 |
| return q; |
| 49 |
|
| } |
| 50 |
|
| } |
| 51 |
0 |
▶ | while (p < end && *p != ';') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != ';') p++; |
0 |
0 |
0 |
| while (p < end && *p != ';') p++; |
0 |
0 |
0 |
|
| 52 |
|
| } |
| 53 |
218 |
| return 1000; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| /* Split an Accept value into media ranges. Returns how many were kept; |
| 57 |
|
| * anything past PA_RANGE_MAX or with no shape is ignored, never fatal - |
| 58 |
|
| * a malformed Accept from a client must not be able to error a response. */ |
| 59 |
41 |
| static int pa_parse(const char *a, STRLEN al, pa_range *out, int max) { |
| 60 |
41 |
| const char *p = a, *end = a + al; |
| 61 |
41 |
| int n = 0; |
| 62 |
90 |
▶ | while (p < end && n < max) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && n < max) { |
49 |
41 |
90 |
| while (p < end && n < max) { |
49 |
0 |
49 |
|
| 63 |
49 |
| const char *seg_end = p, *slash, *sub_end, *par; |
| 64 |
724 |
▶ | while (seg_end < end && *seg_end != ',') seg_end++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (seg_end < end && *seg_end != ',') seg_end++; |
683 |
41 |
724 |
| while (seg_end < end && *seg_end != ',') seg_end++; |
675 |
8 |
683 |
|
| 65 |
50 |
▶ | while (p < seg_end && pa_ws(*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < seg_end && pa_ws(*p)) p++; |
50 |
0 |
50 |
| while (p < seg_end && pa_ws(*p)) p++; |
1 |
49 |
50 |
|
| 66 |
49 |
| par = p; |
| 67 |
683 |
▶ | while (par < seg_end && *par != ';') par++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (par < seg_end && *par != ';') par++; |
641 |
42 |
683 |
| while (par < seg_end && *par != ';') par++; |
634 |
7 |
641 |
|
| 68 |
49 |
| sub_end = par; |
| 69 |
49 |
▶ | while (sub_end > p && pa_ws(sub_end[-1])) sub_end--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (sub_end > p && pa_ws(sub_end[-1])) sub_end--; |
49 |
0 |
49 |
| while (sub_end > p && pa_ws(sub_end[-1])) sub_end--; |
0 |
49 |
49 |
|
| 70 |
49 |
| slash = p; |
| 71 |
442 |
▶ | while (slash < sub_end && *slash != '/') slash++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (slash < sub_end && *slash != '/') slash++; |
442 |
0 |
442 |
| while (slash < sub_end && *slash != '/') slash++; |
393 |
49 |
442 |
|
| 72 |
49 |
▶ | if (slash < sub_end && slash > p && sub_end > slash + 1) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (slash < sub_end && slash > p && sub_end > slash + 1) { |
49 |
0 |
49 |
| if (slash < sub_end && slash > p && sub_end > slash + 1) { |
49 |
0 |
49 |
| if (slash < sub_end && slash > p && sub_end > slash + 1) { |
49 |
0 |
49 |
|
| 73 |
49 |
| out[n].t = p; out[n].tl = (STRLEN)(slash - p); |
| 74 |
49 |
| out[n].s = slash + 1; out[n].sl = (STRLEN)(sub_end - slash - 1); |
| 75 |
49 |
| out[n].q = pa_q_of(par, seg_end); |
| 76 |
49 |
| n++; |
| 77 |
|
| } |
| 78 |
0 |
▶ | else if (sub_end == p + 1 && *p == '*') { /* a bare '*' is '* / *' */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (sub_end == p + 1 && *p == '*') { /* a bare '*' is '* / *' */ |
0 |
0 |
0 |
| else if (sub_end == p + 1 && *p == '*') { /* a bare '*' is '* / *' */ |
0 |
0 |
0 |
|
| 79 |
0 |
| out[n].t = p; out[n].tl = 1; |
| 80 |
0 |
| out[n].s = p; out[n].sl = 1; |
| 81 |
0 |
| out[n].q = pa_q_of(par, seg_end); |
| 82 |
0 |
| n++; |
| 83 |
|
| } |
| 84 |
49 |
| p = seg_end + 1; |
| 85 |
|
| } |
| 86 |
41 |
| return n; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
115 |
▶ | static int pa_star(const char *s, STRLEN l) { return l == 1 && *s == '*'; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int pa_star(const char *s, STRLEN l) { return l == 1 && *s == '*'; } |
10 |
105 |
115 |
| static int pa_star(const char *s, STRLEN l) { return l == 1 && *s == '*'; } |
10 |
0 |
10 |
|
| 90 |
|
| |
| 91 |
|
| /* How well the ranges accept one media type: returns the specificity of the |
| 92 |
|
| * most specific matching range (2 exact, 1 type / *, 0 * / *, -1 none), and |
| 93 |
|
| * through q/order that range's q-value and position. RFC 9110's rule: the |
| 94 |
|
| * most specific match decides the q, and q=0 is an exclusion (reported as a |
| 95 |
|
| * match with *q 0, so the caller can tell "refused" from "unmentioned"). */ |
| 96 |
61 |
| static int pa_match(pTHX_ const pa_range *r, int n, |
| 97 |
|
| const char *t, STRLEN tl, const char *s, STRLEN sl, |
| 98 |
|
| int *q, int *order) { |
| 99 |
61 |
| int i, spec = -1; |
| 100 |
138 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
77 |
61 |
138 |
|
| 101 |
|
| int this_spec; |
| 102 |
77 |
▶ | if (pa_star(r[i].t, r[i].tl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pa_star(r[i].t, r[i].tl)) |
10 |
67 |
77 |
|
| 103 |
10 |
| this_spec = 0; |
| 104 |
67 |
▶ | else if (r[i].tl == tl && foldEQ(r[i].t, t, (I32)tl)) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (r[i].tl == tl && foldEQ(r[i].t, t, (I32)tl)) |
38 |
29 |
67 |
| else if (r[i].tl == tl && foldEQ(r[i].t, t, (I32)tl)) |
38 |
0 |
38 |
|
| 105 |
76 |
| this_spec = pa_star(r[i].s, r[i].sl) ? 1 |
| 106 |
101 |
▶ | : (r[i].sl == sl && foldEQ(r[i].s, s, (I32)sl)) ? 2 |
Branch: 2 branches
| Branch | True | False | Total |
| : (r[i].sl == sl && foldEQ(r[i].s, s, (I32)sl)) ? 2 |
38 |
0 |
38 |
| : (r[i].sl == sl && foldEQ(r[i].s, s, (I32)sl)) ? 2 |
25 |
0 |
25 |
|
| 107 |
63 |
▶ | : -1; |
Branch: 1 branch
| Branch | True | False | Total |
| : -1; |
25 |
13 |
38 |
|
| 108 |
|
| else |
| 109 |
29 |
| this_spec = -1; |
| 110 |
77 |
▶ | if (this_spec > spec) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (this_spec > spec) { |
32 |
45 |
77 |
|
| 111 |
32 |
| spec = this_spec; |
| 112 |
32 |
| *q = r[i].q; |
| 113 |
32 |
| *order = i; |
| 114 |
|
| } |
| 115 |
|
| } |
| 116 |
61 |
| return spec; |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| /* The media type a respond_to format name means. A name with a '/' is taken |
| 120 |
|
| * literally; the short names cover what an application negotiates in |
| 121 |
|
| * practice. Returns 0 for a name it does not know. */ |
| 122 |
61 |
| static int pa_fmt_mime(const char *name, STRLEN nl, |
| 123 |
|
| const char **t, STRLEN *tl, |
| 124 |
|
| const char **s, STRLEN *sl) { |
| 125 |
61 |
| const char *slash = (const char *)memchr(name, '/', nl); |
| 126 |
61 |
▶ | if (slash && slash > name && (STRLEN)(slash - name) < nl - 1) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (slash && slash > name && (STRLEN)(slash - name) < nl - 1) { |
18 |
43 |
61 |
| if (slash && slash > name && (STRLEN)(slash - name) < nl - 1) { |
18 |
0 |
18 |
| if (slash && slash > name && (STRLEN)(slash - name) < nl - 1) { |
18 |
0 |
18 |
|
| 127 |
18 |
| *t = name; *tl = (STRLEN)(slash - name); |
| 128 |
18 |
| *s = slash + 1; *sl = nl - *tl - 1; |
| 129 |
18 |
| return 1; |
| 130 |
|
| } |
| 131 |
43 |
▶ | if (nl == 4 && memEQ(name, "json", 4)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nl == 4 && memEQ(name, "json", 4)) { |
35 |
8 |
43 |
| if (nl == 4 && memEQ(name, "json", 4)) { |
23 |
12 |
35 |
|
| 132 |
23 |
| *t = "application"; *tl = 11; *s = "json"; *sl = 4; return 1; |
| 133 |
|
| } |
| 134 |
20 |
▶ | if (nl == 4 && memEQ(name, "html", 4)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nl == 4 && memEQ(name, "html", 4)) { |
12 |
8 |
20 |
| if (nl == 4 && memEQ(name, "html", 4)) { |
12 |
0 |
12 |
|
| 135 |
12 |
| *t = "text"; *tl = 4; *s = "html"; *sl = 4; return 1; |
| 136 |
|
| } |
| 137 |
8 |
▶ | if (nl == 4 && memEQ(name, "text", 4)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nl == 4 && memEQ(name, "text", 4)) { |
0 |
8 |
8 |
| if (nl == 4 && memEQ(name, "text", 4)) { |
0 |
0 |
0 |
|
| 138 |
0 |
| *t = "text"; *tl = 4; *s = "plain"; *sl = 5; return 1; |
| 139 |
|
| } |
| 140 |
8 |
▶ | if (nl == 3 && memEQ(name, "xml", 3)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nl == 3 && memEQ(name, "xml", 3)) { |
7 |
1 |
8 |
| if (nl == 3 && memEQ(name, "xml", 3)) { |
7 |
0 |
7 |
|
| 141 |
7 |
| *t = "application"; *tl = 11; *s = "xml"; *sl = 3; return 1; |
| 142 |
|
| } |
| 143 |
1 |
| return 0; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| /* Add a token to the response's Vary - creating the pair, or appending to an |
| 147 |
|
| * existing value that does not already carry it (a whole-token check: |
| 148 |
|
| * `Vary: Accept-Encoding` does not contain Accept). Merging rather than |
| 149 |
|
| * replacing is the whole point: an application that already said what it |
| 150 |
|
| * varies on knows something we do not, and a response that varies on |
| 151 |
|
| * something it did not declare poisons every shared cache between here and |
| 152 |
|
| * the client. |
| 153 |
|
| * |
| 154 |
|
| * Used for `Accept` by respond_to and for `Accept-Encoding` by |
| 155 |
|
| * Punk::Plugin::ConditionalGet, which must not grow a second copy of this |
| 156 |
|
| * walk. */ |
| 157 |
111 |
| static void pa_vary_add(pTHX_ AV *headers, const char *tok, STRLEN tl) { |
| 158 |
111 |
| SSize_t i, n = av_len(headers) + 1; |
| 159 |
111 |
| SV *first_vary = NULL; |
| 160 |
336 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
232 |
104 |
336 |
|
| 161 |
232 |
| SV **k = av_fetch(headers, i, 0); |
| 162 |
|
| STRLEN kl; const char *kp; |
| 163 |
232 |
▶ | if (!(k && *k)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(k && *k)) continue; |
232 |
0 |
232 |
| if (!(k && *k)) continue; |
0 |
232 |
232 |
|
| 164 |
232 |
| kp = SvPV_const(*k, kl); |
| 165 |
232 |
▶ | if (kl == 4 && foldEQ(kp, "Vary", 4)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (kl == 4 && foldEQ(kp, "Vary", 4)) { |
88 |
144 |
232 |
| if (kl == 4 && foldEQ(kp, "Vary", 4)) { |
13 |
75 |
88 |
|
| 166 |
13 |
| SV **v = av_fetch(headers, i + 1, 0); |
| 167 |
|
| STRLEN vl; const char *vp, *p, *vend; |
| 168 |
13 |
▶ | if (!(v && *v && SvOK(*v))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(v && *v && SvOK(*v))) continue; |
13 |
0 |
13 |
| if (!(v && *v && SvOK(*v))) continue; |
13 |
0 |
13 |
| if (!(v && *v && SvOK(*v))) continue; |
0 |
13 |
13 |
|
| 169 |
13 |
| vp = SvPV_const(*v, vl); |
| 170 |
13 |
| p = vp; vend = vp + vl; |
| 171 |
19 |
▶ | while (p < vend) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < vend) { |
13 |
6 |
19 |
|
| 172 |
13 |
| const char *tok_end = p, *t; |
| 173 |
172 |
▶ | while (tok_end < vend && *tok_end != ',') tok_end++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (tok_end < vend && *tok_end != ',') tok_end++; |
159 |
13 |
172 |
| while (tok_end < vend && *tok_end != ',') tok_end++; |
159 |
0 |
159 |
|
| 174 |
13 |
| t = p; |
| 175 |
13 |
▶ | while (t < tok_end && pa_ws(*t)) t++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (t < tok_end && pa_ws(*t)) t++; |
13 |
0 |
13 |
| while (t < tok_end && pa_ws(*t)) t++; |
0 |
13 |
13 |
|
| 176 |
|
| { |
| 177 |
13 |
| const char *te = tok_end; |
| 178 |
13 |
▶ | while (te > t && pa_ws(te[-1])) te--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (te > t && pa_ws(te[-1])) te--; |
13 |
0 |
13 |
| while (te > t && pa_ws(te[-1])) te--; |
0 |
13 |
13 |
|
| 179 |
|
| /* `Vary: *` says it varies on everything, so there is |
| 180 |
|
| * nothing to add and nothing a cache may reuse */ |
| 181 |
13 |
▶ | if ((te - t == 1 && *t == '*') |
Branch: 2 branches
| Branch | True | False | Total |
| if ((te - t == 1 && *t == '*') |
0 |
13 |
13 |
| if ((te - t == 1 && *t == '*') |
0 |
0 |
0 |
|
| 182 |
13 |
▶ | || ((STRLEN)(te - t) == tl && foldEQ(t, tok, tl))) |
Branch: 2 branches
| Branch | True | False | Total |
| || ((STRLEN)(te - t) == tl && foldEQ(t, tok, tl))) |
8 |
5 |
13 |
| || ((STRLEN)(te - t) == tl && foldEQ(t, tok, tl))) |
7 |
1 |
8 |
|
| 183 |
7 |
| return; |
| 184 |
|
| } |
| 185 |
6 |
| p = tok_end + 1; |
| 186 |
|
| } |
| 187 |
6 |
▶ | if (!first_vary) first_vary = *v; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!first_vary) first_vary = *v; |
6 |
0 |
6 |
|
| 188 |
|
| } |
| 189 |
|
| } |
| 190 |
104 |
▶ | if (first_vary) { sv_catpvs(first_vary, ", "); sv_catpvn(first_vary, tok, tl); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (first_vary) { sv_catpvs(first_vary, ", "); sv_catpvn(first_vary, tok, tl); } |
6 |
98 |
104 |
|
| 191 |
|
| else { |
| 192 |
98 |
| av_push(headers, newSVpvs("Vary")); |
| 193 |
98 |
| av_push(headers, newSVpvn(tok, tl)); |
| 194 |
|
| } |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
23 |
| static void pa_vary_accept(pTHX_ AV *headers) { |
| 198 |
23 |
| pa_vary_add(aTHX_ headers, "Accept", 6); |
| 199 |
23 |
| } |
| 200 |
|
| |
| 201 |
|
| #endif /* PUNK_ACCEPT_H */ |