include/punk/punk_lang.h

filter: Statementstmt 98.7% 234 / 238 (1 uncoverable) Branchbran 73.5% 267 / 378 (24 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.2% 501 / 616 (25 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/metrics.xs ↑ Summary include/punk/punk_blob.h »
1 #ifndef PUNK_LANG_H
2 #define PUNK_LANG_H
3  
4 /* Accept-Language negotiation.
5 *
6 * ---- why this is not punk_accept.h ----------------------------------------
7 *
8 * punk_accept.h looks like it already does this, and it does the hard half:
9 * q-values, the ordering they imply, and the rule that `q=0` is an EXCLUSION
10 * rather than an absence. All of that is reused - pa_q_of is called directly.
11 *
12 * What it cannot do is parse the header. pa_parse accepts a segment only when
13 * it finds a slash in it, or when the segment is a bare `*`:
14 *
15 * if (slash < sub_end && slash > p && sub_end > slash + 1) { ... }
16 * else if (sub_end == p + 1 && *p == '*') { ... }
17 *
18 * A language tag has no slash, so `Accept-Language: en-GB,en;q=0.9` parses to
19 * ZERO ranges through it, and pa_range has nowhere to put a tag that is not
20 * type/subtype. Hence a sibling parser, in the same style: parse in place, a
21 * bounded number of stack entries, no allocation, and a malformed header
22 * ignored rather than fatal - a client must not be able to error a response
23 * with a bad header.
24 *
25 * ---- and why the MATCH is not the same either -----------------------------
26 *
27 * Media ranges have a three-rung specificity ladder: exact, "type slash star", and the bare wildcard.
28 * Language tags have prefix fallback, which is asymmetric:
29 *
30 * - `en-GB` requested matches a catalogue holding `en`. Truncate the
31 * request at `-` and try again: zh-Hant-TW, zh-Hant, zh.
32 * - `en` requested does NOT match a catalogue holding only `en-GB`. Serving
33 * pt-BR to a request for pt is how a Portuguese speaker gets Brazilian
34 * spelling with no way to refuse. An application that wants that can
35 * configure an alias.
36 * - The truncation is on SUBTAG BOUNDARIES, never a byte prefix. `en` must
37 * not match `ens`, which is what a bare strncmp would do.
38 */
39  
40 #define PL_TAG_MAX_N 32 /* the sibling of PA_RANGE_MAX */
41  
42 typedef struct pl_tag {
43 const char *t; STRLEN tl; /* the tag, pointing into the header */
44 int q; /* per mille: 0..1000 */
45 } pl_tag;
46  
47 /* Parse an Accept-Language into `out`. Returns how many tags were taken.
48 * Anything past PL_TAG_MAX_N is ignored; so is a segment with no shape. */
49 126 static int pl_parse(const char *a, STRLEN al, pl_tag *out, int max) {
50 126 const char *p = a, *end = a + al;
51 126 int n = 0;
52 354 ▶ while (p < end && n < max) {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && n < max) { 231 123 354
while (p < end && n < max) { 228 3 231
53 228 const char *seg_end = p, *tag_end, *par;
54 1796 ▶ while (seg_end < end && *seg_end != ',') seg_end++;
Branch: 2 branches
BranchTrueFalseTotal
while (seg_end < end && *seg_end != ',') seg_end++; 1674 122 1796
while (seg_end < end && *seg_end != ',') seg_end++; 1568 106 1674
55 233 ▶ while (p < seg_end && pa_ws(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < seg_end && pa_ws(*p)) p++; 230 3 233
while (p < seg_end && pa_ws(*p)) p++; 5 225 230
56 228 par = p;
57 1727 ▶ while (par < seg_end && *par != ';') par++;
Branch: 2 branches
BranchTrueFalseTotal
while (par < seg_end && *par != ';') par++; 1512 215 1727
while (par < seg_end && *par != ';') par++; 1499 13 1512
58 228 tag_end = par;
59 /*
60 # uncoverable branch true class:ignore_covered_err
61 # uncoverable condition left count:1 class:ignore_covered_err
62 # uncoverable condition left count:2 class:ignore_covered_err */
63 228 ▶ while (tag_end > p && pa_ws(tag_end[-1])) tag_end--;
Branch: 2 branches
BranchTrueFalseTotal
while (tag_end > p && pa_ws(tag_end[-1])) tag_end--; 224 4 228
while (tag_end > p && pa_ws(tag_end[-1])) tag_end--; 0 224 224
64  
65 228 ▶ if (tag_end > p && (STRLEN)(tag_end - p) <= PI_TAG_MAX) {
Branch: 2 branches
BranchTrueFalseTotal
if (tag_end > p && (STRLEN)(tag_end - p) <= PI_TAG_MAX) { 224 4 228
if (tag_end > p && (STRLEN)(tag_end - p) <= PI_TAG_MAX) { 222 2 224
66 /* A tag is alphanumerics and hyphens, or the wildcard. Anything
67 * else is a header this code should not be guessing about. */
68 const char *q;
69 222 int ok = 1;
70 222 ▶ if (!(tag_end == p + 1 && *p == '*')) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(tag_end == p + 1 && *p == '*')) { 6 216 222
if (!(tag_end == p + 1 && *p == '*')) { 2 4 6
71 691 ▶ for (q = p; q < tag_end; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = p; q < tag_end; q++) { 479 212 691
72 479 char c = *q;
73 /*
74 # uncoverable condition left count:2 class:ignore_covered_err
75 # uncoverable condition left count:4 class:ignore_covered_err
76 # uncoverable condition right count:5 class:ignore_covered_err
77 # uncoverable condition right count:6 class:ignore_covered_err */
78 502 ▶ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
Branch: 5 branches
BranchTrueFalseTotal
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 433 46 479
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 433 433
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 23 23 46
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 0 23 23
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 6 17 23
79 29 ▶ || (c >= '0' && c <= '9') || c == '-')) { ok = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '-')) { ok = 0; break; } 3 20 23
|| (c >= '0' && c <= '9') || c == '-')) { ok = 0; break; } 3 0 3
80 }
81 }
82 222 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 216 6 222
83 216 out[n].t = p;
84 216 out[n].tl = (STRLEN)(tag_end - p);
85 216 out[n].q = pa_q_of(par, seg_end);
86 216 n++;
87 }
88 }
89 228 p = seg_end + 1;
90 }
91 126 return n;
92 }
93  
94 793 ▶static int pl_star(const char *s, STRLEN l) { return l == 1 && *s == '*'; }
Branch: 2 branches
BranchTrueFalseTotal
static int pl_star(const char *s, STRLEN l) { return l == 1 && *s == '*'; } 10 783 793
static int pl_star(const char *s, STRLEN l) { return l == 1 && *s == '*'; } 8 2 10
95  
96 /* Does the catalogue tag `c` satisfy the requested tag `t`?
97 *
98 * Returns the specificity of the match - 2 exact, 1 through prefix fallback,
99 * 0 the wildcard, -1 no match - mirroring pa_match, because the RFC rule that
100 * the MOST SPECIFIC match decides the q-value is the same rule here. */
101 793 static int pl_spec(const char *t, STRLEN tl, const char *c, STRLEN cl) {
102 793 ▶ if (pl_star(t, tl)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pl_star(t, tl)) return 0; 8 785 793
103 785 ▶ if (pi_tag_eq(t, tl, c, cl)) return 2;
Branch: 1 branch
BranchTrueFalseTotal
if (pi_tag_eq(t, tl, c, cl)) return 2; 144 641 785
104 /* The catalogue tag must be a prefix of the REQUESTED one, on a subtag
105 * boundary. Never the other way round. */
106 641 ▶ if (cl < tl && t[cl] == '-' && pi_tag_eq(t, cl, c, cl)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (cl < tl && t[cl] == '-' && pi_tag_eq(t, cl, c, cl)) return 1; 52 589 641
if (cl < tl && t[cl] == '-' && pi_tag_eq(t, cl, c, cl)) return 1; 51 1 52
if (cl < tl && t[cl] == '-' && pi_tag_eq(t, cl, c, cl)) return 1; 12 39 51
107 629 return -1;
108 }
109  
110 /* Which catalogue does this header ask for? Returns an index into the arena,
111 * or -1 when nothing is acceptable.
112 *
113 * A catalogue's score is the q of the most specific tag that matches it, and
114 * a q of 0 there is an exclusion: `fr;q=0, *` means never French, including
115 * through the wildcard, which is the case naive negotiators get wrong.
116 * Ties break on specificity, then on the order the tags appeared. */
117 116 static int pl_pick(const pl_tag *tags, int n, const pi_arena *ar) {
118 116 int i, best = -1, best_q = 0, best_spec = -1, best_order = 0;
119  
120 /*
121 # uncoverable branch true
122 # uncoverable condition left count:1 class:ignore_covered_err */
123 116 ▶ if (!ar) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!ar) return -1; 0 116 116
124 758 ▶ for (i = 0; i < ar->ncat; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ar->ncat; i++) { 642 116 758
125 642 const pi_cat *c = &ar->cat[i];
126 642 int j, q = -1, spec = -1, order = 0;
127  
128 1421 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 779 642 1421
129 779 int s = pl_spec(tags[j].t, tags[j].tl, c->tag, c->taglen);
130 779 ▶ if (s > spec) { spec = s; q = tags[j].q; order = j; }
Branch: 1 branch
BranchTrueFalseTotal
if (s > spec) { spec = s; q = tags[j].q; order = j; } 126 653 779
131 }
132 642 ▶ if (spec < 0 || q <= 0) continue; /* unmentioned, or excluded */
Branch: 2 branches
BranchTrueFalseTotal
if (spec < 0 || q <= 0) continue; /* unmentioned, or excluded */ 124 518 642
if (spec < 0 || q <= 0) continue; /* unmentioned, or excluded */ 3 121 124
133  
134 121 ▶ if (q > best_q
Branch: 1 branch
BranchTrueFalseTotal
if (q > best_q 12 109 121
135 12 ▶ || (q == best_q && spec > best_spec)
Branch: 2 branches
BranchTrueFalseTotal
|| (q == best_q && spec > best_spec) 11 1 12
|| (q == best_q && spec > best_spec) 5 6 11
136 6 ▶ || (q == best_q && spec == best_spec && order < best_order)) {
Branch: 3 branches
BranchTrueFalseTotal
|| (q == best_q && spec == best_spec && order < best_order)) { 5 1 6
|| (q == best_q && spec == best_spec && order < best_order)) { 4 1 5
|| (q == best_q && spec == best_spec && order < best_order)) { 1 3 4
137 116 best = i; best_q = q; best_spec = spec; best_order = order;
138 }
139 }
140 116 return best;
141 }
142  
143 /* The whole header to an index, or -1. */
144 128 static int pl_negotiate(const char *hdr, STRLEN hl, const pi_arena *ar) {
145 pl_tag tags[PL_TAG_MAX_N];
146 int n;
147 128 ▶ if (!hdr || !hl || !ar) return -1; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!hdr || !hl || !ar) return -1; /* # uncoverable condition left count:3 class:ignore_covered_err */ 127 1 128
if (!hdr || !hl || !ar) return -1; /* # uncoverable condition left count:3 class:ignore_covered_err */ 126 1 127
if (!hdr || !hl || !ar) return -1; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 126 126
148 126 n = pl_parse(hdr, hl, tags, PL_TAG_MAX_N);
149 126 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 10 116 126
150 116 return pl_pick(tags, n, ar);
151 }
152  
153 /* An explicitly named locale - from ?lang= or a stored choice - to an index.
154 *
155 * Exact first, then the same prefix fallback the header gets, so `?lang=en-GB`
156 * lands on the `en` catalogue rather than falling through to the default. A
157 * name with no catalogue is NOT an error: it falls through to the next source.
158 * A 500 on a hand-edited URL is a worse answer than the default language, and
159 * ?lang= is exactly the parameter people hand-edit.
160 */
161 7 static int pl_named(const char *tag, STRLEN tl, const pi_arena *ar) {
162 7 int i, best = -1;
163 7 STRLEN bestlen = 0;
164 /*
165 # uncoverable branch true
166 # uncoverable condition left count:1 class:ignore_covered_err
167 # uncoverable condition left count:2 class:ignore_covered_err
168 # uncoverable condition left count:3 class:ignore_covered_err
169 # uncoverable condition left count:4 class:ignore_covered_err */
170 7 ▶ if (!tag || !tl || !ar || tl > PI_TAG_MAX) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (!tag || !tl || !ar || tl > PI_TAG_MAX) return -1; 7 0 7
if (!tag || !tl || !ar || tl > PI_TAG_MAX) return -1; 7 0 7
if (!tag || !tl || !ar || tl > PI_TAG_MAX) return -1; 7 0 7
if (!tag || !tl || !ar || tl > PI_TAG_MAX) return -1; 0 7 7
171 16 ▶ for (i = 0; i < ar->ncat; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ar->ncat; i++) { 14 2 16
172 14 const pi_cat *c = &ar->cat[i];
173 14 int s = pl_spec(tag, tl, c->tag, c->taglen);
174 14 ▶ if (s == 2) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (s == 2) return i; 5 9 14
175 9 ▶ if (s == 1 && c->taglen > bestlen) { best = i; bestlen = c->taglen; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (s == 1 && c->taglen > bestlen) { best = i; bestlen = c->taglen; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 8 9
if (s == 1 && c->taglen > bestlen) { best = i; bestlen = c->taglen; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
176 }
177 2 return best;
178 }
179  
180 /* ---- the order a language is chosen ---------------------------------------
181 *
182 * Explicit beats implicit, most explicit first:
183 *
184 * 1. ?lang= - an explicit act by the user, on this request
185 * 2. the stored choice (session, or a cookie) - an explicit act on an
186 * earlier one
187 * 3. Accept-Language - what the browser was configured with, which is
188 * frequently not what the user wants and is never something they did on
189 * this site
190 * 4. the configured default
191 *
192 * 2 is what makes a language switcher work on the SECOND page. Without it an
193 * explicit choice loses to the browser on the next link, and the switcher
194 * appears to be broken - which it is.
195 */
196  
197 #define PL_ENV_KEY "punk.i18n_locale"
198  
199 typedef struct pl_choice {
200 int idx; /* catalogue index */
201 int explicit_; /* the request named it, so it is worth persisting */
202 } pl_choice;
203  
204 /* Resolve, given the pieces. Kept free of the context so it can be tested
205 * directly: every interesting case here is a table. */
206 2185 static pl_choice pl_resolve(const char *param, STRLEN paraml,
207 const char *stored, STRLEN storedl,
208 const char *header, STRLEN headerl,
209 const pi_arena *ar) {
210 pl_choice ch;
211 2185 ch.idx = -1;
212 2185 ch.explicit_ = 0;
213  
214 2185 ▶ if (param && paraml) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (param && paraml) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 2181 2185
if (param && paraml) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
215 4 ch.idx = pl_named(param, paraml, ar);
216 4 ▶ if (ch.idx >= 0) { ch.explicit_ = 1; return ch; }
Branch: 1 branch
BranchTrueFalseTotal
if (ch.idx >= 0) { ch.explicit_ = 1; return ch; } 3 1 4
217 /* named a locale with no catalogue: fall through, never 500 */
218 }
219 2182 ▶ if (stored && storedl) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (stored && storedl) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 2179 2182
if (stored && storedl) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
220 3 ch.idx = pl_named(stored, storedl, ar);
221 /*
222 # uncoverable branch false
223 # uncoverable condition right count:1 class:ignore_covered_err */
224 3 ▶ if (ch.idx >= 0) return ch;
Branch: 1 branch
BranchTrueFalseTotal
if (ch.idx >= 0) return ch; 3 0 3
225 }
226 2179 ▶ if (header && headerl) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (header && headerl) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 88 2091 2179
if (header && headerl) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 88 0 88
227 88 ch.idx = pl_negotiate(header, headerl, ar);
228 /*
229 # uncoverable branch false
230 # uncoverable condition right count:1 class:ignore_covered_err */
231 88 ▶ if (ch.idx >= 0) return ch;
Branch: 1 branch
BranchTrueFalseTotal
if (ch.idx >= 0) return ch; 88 0 88
232 }
233 2091 ▶ ch.idx = ar ? ar->def : -1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ch.idx = ar ? ar->def : -1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2091 0 2091
234 2091 return ch;
235 }
236  
237 /* ---- one lookup, two callers ----------------------------------------------
238 *
239 * `$c->locale` and the template's `locale` hash must agree about what a key
240 * resolves to, what counts as untranslated, and when a missing key warns.
241 * Two implementations of that would drift, and the drift would be silent:
242 * a page whose handler strings are fine and whose template strings are not.
243 */
244 #define PI_HIT 0 /* a translation */
245 #define PI_LEVEL 1 /* a level - keys continue below this one */
246 #define PI_MISSING 2 /* nowhere at all */
247  
248 159 static int pi_lookup(pTHX_ const pi_arena *ar, const pi_cat *cat,
249 const char *k, STRLEN kl, const char **vp, STRLEN *vl,
250 int *utf8) {
251 const pi_cat *d;
252  
253 /*
254 # uncoverable branch false
255 # uncoverable condition right count:1 class:ignore_covered_err */
256 159 ▶ if (utf8) *utf8 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) *utf8 = 0; 159 0 159
257 159 *vp = pi_get(cat, k, kl, vl, utf8);
258 159 ▶ if (*vp) return PI_HIT;
Branch: 1 branch
BranchTrueFalseTotal
if (*vp) return PI_HIT; 108 51 159
259  
260 /* The default catalogue before giving up: a key present in `en` and
261 * missing from `fr-CA` is UNTRANSLATED, not missing. */
262 /*
263 # uncoverable condition right count:1 class:ignore_covered_err
264 # uncoverable condition right count:2 class:ignore_covered_err */
265 51 ▶ d = (ar && ar->def >= 0) ? &ar->cat[ar->def] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
d = (ar && ar->def >= 0) ? &ar->cat[ar->def] : NULL; 51 0 51
d = (ar && ar->def >= 0) ? &ar->cat[ar->def] : NULL; 51 0 51
266 51 ▶ if (d && d != cat) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (d && d != cat) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 51 0 51
if (d && d != cat) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 48 51
267 3 *vp = pi_get(d, k, kl, vl, utf8);
268 3 ▶ if (*vp) { pi_n_untranslated++; return PI_HIT; }
Branch: 1 branch
BranchTrueFalseTotal
if (*vp) { pi_n_untranslated++; return PI_HIT; } 2 1 3
269 }
270  
271 /*
272 # uncoverable condition left count:2 class:ignore_covered_err
273 # uncoverable condition right count:2 class:ignore_covered_err
274 # uncoverable condition left count:3 class:ignore_covered_err
275 # uncoverable condition left count:4 class:ignore_covered_err */
276 49 ▶ if (pi_is_prefix(cat, k, kl) || (d && d != cat && pi_is_prefix(d, k, kl)))
Branch: 4 branches
BranchTrueFalseTotal
if (pi_is_prefix(cat, k, kl) || (d && d != cat && pi_is_prefix(d, k, kl))) 47 2 49
if (pi_is_prefix(cat, k, kl) || (d && d != cat && pi_is_prefix(d, k, kl))) 47 0 47
if (pi_is_prefix(cat, k, kl) || (d && d != cat && pi_is_prefix(d, k, kl))) 1 46 47
if (pi_is_prefix(cat, k, kl) || (d && d != cat && pi_is_prefix(d, k, kl))) 0 1 1
277 2 return PI_LEVEL;
278  
279 47 pi_n_missing++;
280 47 return PI_MISSING;
281 }
282  
283 /* The same three answers, one step at a time.
284 *
285 * pi_lookup takes a whole dotted key and walks it from the catalogue root.
286 * This takes ONE segment and the node the last step reached, which is what
287 * the tied hash needs: a template descends one key per FETCH, and re-walking
288 * the whole path each time is what made the tied door cost six probes for
289 * three segments.
290 *
291 * The default catalogue is probed LAZILY. On a hit in the negotiated
292 * catalogue - the common case - it is not probed at all, so the fast path is
293 * a single probe. It is only consulted to fall back, or to carry the
294 * default's position down into a nested level.
295 *
296 * The counters live here, next to pi_lookup's, so the two doors cannot come
297 * to different conclusions about what is missing and what is merely
298 * untranslated. */
299 2033 static int pi_step(pTHX_ const pi_arena *ar, const pi_cat *cat,
300 uint32_t node, uint32_t dnode,
301 const char *k, STRLEN kl,
302 const char **vp, STRLEN *vl, int *utf8,
303 uint32_t *child, uint32_t *dchild) {
304 /*
305 # uncoverable condition right count:1 class:ignore_covered_err
306 # uncoverable condition right count:2 class:ignore_covered_err */
307 2033 ▶ const pi_cat *d = (ar && ar->def >= 0) ? &ar->cat[ar->def] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const pi_cat *d = (ar && ar->def >= 0) ? &ar->cat[ar->def] : NULL; 2033 0 2033
const pi_cat *d = (ar && ar->def >= 0) ? &ar->cat[ar->def] : NULL; 2033 0 2033
308 2033 uint32_t slot = FZ_NOHANDLE, dslot = FZ_NOHANDLE;
309 2033 int r = FZ_ABSENT, dr = FZ_ABSENT;
310  
311 /*
312 # uncoverable branch false
313 # uncoverable condition right count:1 class:ignore_covered_err */
314 2033 ▶ if (utf8) *utf8 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) *utf8 = 0; 2033 0 2033
315 2033 *child = *dchild = FZ_NOHANDLE;
316  
317 /*
318 # uncoverable condition right count:1 class:ignore_covered_err
319 # uncoverable condition right count:2 class:ignore_covered_err
320 # uncoverable condition right count:4 class:ignore_covered_err */
321 2033 ▶ if (cat && cat->fz && node != FZ_NOHANDLE && PUNK_FZ)
Branch: 4 branches
BranchTrueFalseTotal
if (cat && cat->fz && node != FZ_NOHANDLE && PUNK_FZ) 2033 0 2033
if (cat && cat->fz && node != FZ_NOHANDLE && PUNK_FZ) 2033 0 2033
if (cat && cat->fz && node != FZ_NOHANDLE && PUNK_FZ) 2032 1 2033
if (cat && cat->fz && node != FZ_NOHANDLE && PUNK_FZ) 2032 0 2032
322 2032 r = (PUNK_FZ->probe)(cat->fz, node, k, kl, &slot);
323  
324 2033 ▶ if (r == FZ_LEAF) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == FZ_LEAF) { 2023 10 2033
325 2023 *child = slot;
326 2023 *vp = (PUNK_FZ->str)(cat->fz, slot, vl, utf8);
327 /*
328 # uncoverable branch false
329 # uncoverable condition right count:1 class:ignore_covered_err */
330 2023 ▶ if (*vp) return PI_HIT;
Branch: 1 branch
BranchTrueFalseTotal
if (*vp) return PI_HIT; 2023 0 2023
331 }
332  
333 /* Only now is the default worth asking. */
334 10 ▶ if (d == cat) { dr = r; dslot = slot; }
Branch: 1 branch
BranchTrueFalseTotal
if (d == cat) { dr = r; dslot = slot; } 7 3 10
335 /*
336 # uncoverable branch false
337 # uncoverable condition right count:1 class:ignore_covered_err
338 # uncoverable condition right count:2 class:ignore_covered_err
339 # uncoverable condition right count:3 class:ignore_covered_err
340 # uncoverable condition right count:4 class:ignore_covered_err */
341 3 ▶ else if (d && d->fz && dnode != FZ_NOHANDLE && PUNK_FZ)
Branch: 4 branches
BranchTrueFalseTotal
else if (d && d->fz && dnode != FZ_NOHANDLE && PUNK_FZ) 3 0 3
else if (d && d->fz && dnode != FZ_NOHANDLE && PUNK_FZ) 3 0 3
else if (d && d->fz && dnode != FZ_NOHANDLE && PUNK_FZ) 3 0 3
else if (d && d->fz && dnode != FZ_NOHANDLE && PUNK_FZ) 3 0 3
342 3 dr = (PUNK_FZ->probe)(d->fz, dnode, k, kl, &dslot);
343  
344 10 ▶ if (r != FZ_ABSENT) *child = slot;
Branch: 1 branch
BranchTrueFalseTotal
if (r != FZ_ABSENT) *child = slot; 6 4 10
345 10 ▶ if (dr != FZ_ABSENT) *dchild = dslot;
Branch: 1 branch
BranchTrueFalseTotal
if (dr != FZ_ABSENT) *dchild = dslot; 9 1 10
346  
347 10 ▶ if (dr == FZ_LEAF && d != cat) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (dr == FZ_LEAF && d != cat) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 9 10
if (dr == FZ_LEAF && d != cat) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
348 1 *vp = (PUNK_FZ->str)(d->fz, dslot, vl, utf8);
349 /*
350 # uncoverable branch false
351 # uncoverable condition right count:1 class:ignore_covered_err */
352 1 ▶ if (*vp) { pi_n_untranslated++; return PI_HIT; }
Branch: 1 branch
BranchTrueFalseTotal
if (*vp) { pi_n_untranslated++; return PI_HIT; } 1 0 1
353 }
354 9 ▶ if (r == FZ_BRANCH || dr == FZ_BRANCH) return PI_LEVEL;
Branch: 2 branches
BranchTrueFalseTotal
if (r == FZ_BRANCH || dr == FZ_BRANCH) return PI_LEVEL; 3 6 9
if (r == FZ_BRANCH || dr == FZ_BRANCH) return PI_LEVEL; 2 1 3
355  
356 1 pi_n_missing++;
357 1 return PI_MISSING;
358 }
359  
360 /* ---- the request side ------------------------------------------------------
361 *
362 * Everything below needs punk_context.h, so this header is included after it.
363 */
364  
365 /* The plugin's config, off the app the context carries. */
366 2337 static HV *pi_cfg_of(pTHX_ SV *c) {
367 AV *av;
368 SV *app, **e;
369 /*
370 # uncoverable branch true
371 # uncoverable condition left count:1 class:ignore_covered_err
372 # uncoverable condition left count:2 class:ignore_covered_err */
373 2337 ▶ if (!c || !SvROK(c)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!c || !SvROK(c)) return NULL; 2337 0 2337
if (!c || !SvROK(c)) return NULL; 0 2337 2337
374 2337 av = pcx_av(aTHX_ c);
375 /*
376 # uncoverable branch true
377 # uncoverable condition left count:1 class:ignore_covered_err */
378 2337 ▶ if (!av) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return NULL; 0 2337 2337
379 2337 app = pcx_get(aTHX_ av, PCX_APP);
380 /*
381 # uncoverable branch true
382 # uncoverable condition left count:1 class:ignore_covered_err
383 # uncoverable condition left count:2 class:ignore_covered_err
384 # uncoverable condition left count:3 class:ignore_covered_err */
385 2337 ▶ if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 2337 0 2337
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 2337 0 2337
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 0 2337 2337
386 2337 e = hv_fetchs((HV *)SvRV(app), K_I18N, 0);
387 2188 ▶ return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 2188 0 2188
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 2188 0 2188
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 2188 0 2188
388 4525 ▶ ? (HV *)SvRV(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*e) : NULL; 2188 149 2337
389 }
390  
391 /* Is this application in development?
392 *
393 * Frozen into the config at compile time, the way CSP does it: `plugin` is
394 * written at the top of an application and `config` below it, so at
395 * registration time the answer is not known yet.
396 *
397 * A missing key on a production page is ALREADY visible - it renders as the
398 * key - so warning about it there is a log line per request telling somebody
399 * something they can see and cannot act on at that moment. */
400 48 static int pi_dev(pTHX_ HV *cfg) {
401 48 ▶ SV **e = cfg ? hv_fetchs(cfg, "dev", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **e = cfg ? hv_fetchs(cfg, "dev", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 48 0 48
402 PERL_UNUSED_CONTEXT;
403 /*
404 # uncoverable condition right count:1 class:ignore_covered_err
405 # uncoverable condition right count:2 class:ignore_covered_err */
406 48 ▶ return (e && *e && SvTRUE(*e));
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvTRUE(*e)); 48 0 48
return (e && *e && SvTRUE(*e)); 48 0 48
return (e && *e && SvTRUE(*e)); 21 27 48
407 }
408  
409 /* Warn once per key, through the application's own logger. */
410 21 static void pi_warn_missing(pTHX_ SV *c, const char *k, STRLEN kl) {
411 21 dSP;
412 SV *msg;
413  
414 21 ▶ if (!pi_seen) pi_seen = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!pi_seen) pi_seen = newHV(); 1 20 21
415 21 ▶ if (hv_exists(pi_seen, k, (I32)kl)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(pi_seen, k, (I32)kl)) return; 19 2 21
416 2 (void)hv_store(pi_seen, k, (I32)kl, &PL_sv_yes, 0);
417  
418 2 msg = sv_2mortal(newSVpvf(
419 "i18n: no translation for '%.*s' in any catalogue - it is rendering "
420 "as the key", (int)kl, k));
421  
422 2 ENTER; SAVETMPS;
423 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 2 2
424 /*
425 # uncoverable branch false
426 # uncoverable condition right count:1 class:ignore_covered_err */
427 2 ▶ if (call_method("log", G_SCALAR | G_EVAL) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (call_method("log", G_SCALAR | G_EVAL) > 0) { 2 0 2
428 2 SV *lg = SvREFCNT_inc(POPs);
429 2 PUTBACK;
430 /*
431 # uncoverable branch false
432 # uncoverable condition right count:1 class:ignore_covered_err
433 # uncoverable condition right count:2 class:ignore_covered_err */
434 2 ▶ if (!SvTRUE(ERRSV) && SvOK(lg)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(lg)) { 2 0 2
if (!SvTRUE(ERRSV) && SvOK(lg)) { 2 0 2
if (!SvTRUE(ERRSV) && SvOK(lg)) { 2 0 2
435 2 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 2 2
PUSHMARK(SP); EXTEND(SP, 2); 0 2 2
436 2 PUSHs(lg); PUSHs(msg);
437 2 PUTBACK;
438 2 (void)call_method("warn", G_DISCARD | G_EVAL);
439 2 SPAGAIN;
440 }
441 2 SvREFCNT_dec(lg);
442 }
443 0 else PUTBACK; /* # uncoverable statement */
444 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2 0 2
445 2 pi_n_warned++;
446 }
447  
448 /* One level of the `locale` hash a template reads. punk_i18n.h says why it is
449 * tied rather than built, and what it needed from Template::Stencil 0.10. */
450 2031 static SV *pi_tied_hash(pTHX_ pi_arena *ar, int idx, SV *prefix, HV *cfg,
451 SV *c, uint32_t node, uint32_t dnode) {
452 2031 AV *o = newAV();
453 2031 HV *h = newHV();
454 SV *obj;
455  
456 2031 av_extend(o, PIT_MAX - 1);
457 2031 av_store(o, PIT_ARENA, newSViv(PTR2IV(ar)));
458 2031 av_store(o, PIT_CAT, newSViv(idx));
459 2031 ▶ av_store(o, PIT_PREFIX, prefix ? newSVsv(prefix) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_store(o, PIT_PREFIX, prefix ? newSVsv(prefix) : newSV(0)); 8 2023 2031
460 2031 ▶ av_store(o, PIT_CFG, cfg ? newRV_inc((SV *)cfg) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_store(o, PIT_CFG, cfg ? newRV_inc((SV *)cfg) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2031 0 2031
461 2031 ▶ av_store(o, PIT_CTX, c ? newSVsv(c) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_store(o, PIT_CTX, c ? newSVsv(c) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2031 0 2031
462 2031 av_store(o, PIT_NODE, newSVuv((UV)node));
463 2031 av_store(o, PIT_DNODE, newSVuv((UV)dnode));
464 2031 av_store(o, PIT_ITER, newSViv(0));
465  
466 2031 obj = sv_bless(newRV_noinc((SV *)o),
467 gv_stashpv("Punk::Plugin::I18n::Cat", GV_ADD));
468 2031 hv_magic(h, (GV *)obj, PERL_MAGIC_tied);
469 2031 SvREFCNT_dec(obj); /* hv_magic took its own reference */
470 2031 return newRV_noinc((SV *)h);
471 }
472  
473 /* The i'th key of the node a tied hash is sitting on.
474 *
475 * Frozen sorts hash keys when it builds the block, so this order is stable
476 * and IDENTICAL in every process reading the same block. A Perl hash is
477 * neither, which is worth knowing before a template relies on it. */
478 60 static SV *pi_key_at(pTHX_ AV *o, uint32_t i) {
479 60 SV **arsv = av_fetch(o, PIT_ARENA, 0);
480 60 SV **idxs = av_fetch(o, PIT_CAT, 0);
481 60 SV **nds = av_fetch(o, PIT_NODE, 0);
482 /*
483 # uncoverable condition right count:1 class:ignore_covered_err
484 # uncoverable condition right count:2 class:ignore_covered_err */
485 60 ▶ pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL; 60 0 60
pi_arena *ar = (arsv && *arsv) ? INT2PTR(pi_arena *, SvIV(*arsv)) : NULL; 60 0 60
486 /*
487 # uncoverable condition right count:1 class:ignore_covered_err
488 # uncoverable condition right count:2 class:ignore_covered_err */
489 60 ▶ int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1;
Branch: 2 branches
BranchTrueFalseTotal
int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1; 60 0 60
int idx = (idxs && *idxs) ? (int)SvIV(*idxs) : -1; 60 0 60
490 /*
491 # uncoverable condition right count:1 class:ignore_covered_err
492 # uncoverable condition right count:2 class:ignore_covered_err */
493 60 ▶ uint32_t node = (nds && *nds) ? (uint32_t)SvUV(*nds) : FZ_NOHANDLE;
Branch: 2 branches
BranchTrueFalseTotal
uint32_t node = (nds && *nds) ? (uint32_t)SvUV(*nds) : FZ_NOHANDLE; 60 0 60
uint32_t node = (nds && *nds) ? (uint32_t)SvUV(*nds) : FZ_NOHANDLE; 60 0 60
494 60 const char *k = NULL;
495 60 STRLEN kl = 0;
496 60 int u = 0;
497 SV *sv;
498  
499 /*
500 # uncoverable condition left count:1 class:ignore_covered_err
501 # uncoverable condition left count:2 class:ignore_covered_err
502 # uncoverable condition left count:3 class:ignore_covered_err
503 # uncoverable condition left count:5 class:ignore_covered_err */
504 60 ▶ if (!ar || idx < 0 || idx >= ar->ncat || node == FZ_NOHANDLE || !PUNK_FZ)
Branch: 5 branches
BranchTrueFalseTotal
if (!ar || idx < 0 || idx >= ar->ncat || node == FZ_NOHANDLE || !PUNK_FZ) 60 0 60
if (!ar || idx < 0 || idx >= ar->ncat || node == FZ_NOHANDLE || !PUNK_FZ) 60 0 60
if (!ar || idx < 0 || idx >= ar->ncat || node == FZ_NOHANDLE || !PUNK_FZ) 60 0 60
if (!ar || idx < 0 || idx >= ar->ncat || node == FZ_NOHANDLE || !PUNK_FZ) 59 1 60
if (!ar || idx < 0 || idx >= ar->ncat || node == FZ_NOHANDLE || !PUNK_FZ) 0 59 59
505 1 return newSV(0);
506 59 ▶ if (!(PUNK_FZ->key_at)(ar->cat[idx].fz, node, i, &k, &kl, &u))
Branch: 1 branch
BranchTrueFalseTotal
if (!(PUNK_FZ->key_at)(ar->cat[idx].fz, node, i, &k, &kl, &u)) 9 50 59
507 9 return newSV(0);
508 50 sv = newSVpvn(k, kl);
509 /*
510 # uncoverable branch true
511 # uncoverable condition left count:1 class:ignore_covered_err */
512 50 ▶ if (u) SvUTF8_on(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (u) SvUTF8_on(sv); 0 50 50
513 50 return sv;
514 }
515  
516 2337 static pi_arena *pi_arena_of(pTHX_ HV *cfg) {
517 2337 ▶ SV **e = cfg ? hv_fetchs(cfg, "_arena", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = cfg ? hv_fetchs(cfg, "_arena", 0) : NULL; 2188 149 2337
518 /*
519 # uncoverable condition right count:2 class:ignore_covered_err
520 # uncoverable condition right count:3 class:ignore_covered_err */
521 2337 ▶ return (e && *e && SvIOK(*e)) ? INT2PTR(pi_arena *, SvIV(*e)) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvIOK(*e)) ? INT2PTR(pi_arena *, SvIV(*e)) : NULL; 2188 149 2337
return (e && *e && SvIOK(*e)) ? INT2PTR(pi_arena *, SvIV(*e)) : NULL; 2188 0 2188
return (e && *e && SvIOK(*e)) ? INT2PTR(pi_arena *, SvIV(*e)) : NULL; 2188 0 2188
522 }
523  
524 4370 static const char *pi_cfg_str(pTHX_ HV *cfg, const char *k, const char *dflt,
525 STRLEN *len) {
526 4370 ▶ SV **e = cfg ? hv_fetch(cfg, k, (I32)strlen(k), 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **e = cfg ? hv_fetch(cfg, k, (I32)strlen(k), 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4370 0 4370
527 /*
528 # uncoverable branch false
529 # uncoverable condition right count:1 class:ignore_covered_err
530 # uncoverable condition right count:2 class:ignore_covered_err
531 # uncoverable condition right count:3 class:ignore_covered_err */
532 8740 ▶ if (e && *e && SvOK(*e)) return SvPV_const(*e, *len);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 4370 0 4370
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 4370 0 4370
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 4370 0 4370
533 0 *len = strlen(dflt); /* # uncoverable statement */
534 0 return dflt; /* # uncoverable statement */
535 }
536  
537 /* One cookie's value out of a Cookie header, or NULL. Points into the
538 * header's own bytes. */
539 5 static const char *pi_cookie(const char *h, STRLEN hl, const char *n,
540 STRLEN nl, STRLEN *vl) {
541 5 STRLEN i = 0;
542 7 ▶ while (i < hl) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < hl) { 6 1 7
543 STRLEN start, eq, end;
544 /*
545 # uncoverable condition right count:1 class:ignore_covered_err
546 # uncoverable condition left count:4 class:ignore_covered_err */
547 8 ▶ while (i < hl && (h[i] == ' ' || h[i] == ';' || h[i] == '\t')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < hl && (h[i] == ' ' || h[i] == ';' || h[i] == '\t')) i++; 8 0 8
while (i < hl && (h[i] == ' ' || h[i] == ';' || h[i] == '\t')) i++; 1 7 8
while (i < hl && (h[i] == ' ' || h[i] == ';' || h[i] == '\t')) i++; 1 6 7
while (i < hl && (h[i] == ' ' || h[i] == ';' || h[i] == '\t')) i++; 0 6 6
548 6 start = i;
549 /*
550 # uncoverable condition right count:1 class:ignore_covered_err
551 # uncoverable condition right count:3 class:ignore_covered_err */
552 57 ▶ while (i < hl && h[i] != '=' && h[i] != ';') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < hl && h[i] != '=' && h[i] != ';') i++; 57 0 57
while (i < hl && h[i] != '=' && h[i] != ';') i++; 51 6 57
while (i < hl && h[i] != '=' && h[i] != ';') i++; 51 0 51
553 6 ▶ if (i >= hl || h[i] != '=') { while (i < hl && h[i] != ';') i++; continue; } /* # uncoverable branch true */
Branch: 4 branches
BranchTrueFalseTotal
if (i >= hl || h[i] != '=') { while (i < hl && h[i] != ';') i++; continue; } /* # uncoverable branch true */ 6 0 6
if (i >= hl || h[i] != '=') { while (i < hl && h[i] != ';') i++; continue; } /* # uncoverable branch true */ 0 6 6
if (i >= hl || h[i] != '=') { while (i < hl && h[i] != ';') i++; continue; } /* # uncoverable branch true */ 0 0 0
if (i >= hl || h[i] != '=') { while (i < hl && h[i] != ';') i++; continue; } /* # uncoverable branch true */ 0 0 0
554 6 eq = i++;
555 6 end = i;
556 17 ▶ while (end < hl && h[end] != ';') end++;
Branch: 2 branches
BranchTrueFalseTotal
while (end < hl && h[end] != ';') end++; 13 4 17
while (end < hl && h[end] != ';') end++; 11 2 13
557 6 ▶ if (eq - start == nl && memcmp(h + start, n, nl) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (eq - start == nl && memcmp(h + start, n, nl) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 2 6
if (eq - start == nl && memcmp(h + start, n, nl) == 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
558 4 *vl = end - i;
559 4 return h + i;
560 }
561 2 i = end;
562 }
563 1 return NULL;
564 }
565  
566 /* Resolve this request's catalogue, once, and remember it in the env.
567 *
568 * The env rather than a C static: Punk dispatches asynchronously through
569 * Punk::Future, so a worker can have several requests in flight at once and a
570 * "current locale" in a static would render one user's page in another user's
571 * language - invisibly, and only under load.
572 */
573 2188 static const pi_cat *pi_for_request(pTHX_ SV *c, HV *cfg, pi_arena *ar) {
574 AV *av;
575 SV *esv, **cached;
576 HV *env;
577 2188 const char *pp = NULL, *sp = NULL, *hp = NULL;
578 2188 STRLEN pl = 0, sl = 0, hl = 0;
579 2188 SV *pv = NULL;
580 pl_choice ch;
581  
582 /*
583 # uncoverable branch true
584 # uncoverable condition left count:1 class:ignore_covered_err
585 # uncoverable condition left count:2 class:ignore_covered_err */
586 2188 ▶ if (!ar || !ar->ncat) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!ar || !ar->ncat) return NULL; 2188 0 2188
if (!ar || !ar->ncat) return NULL; 0 2188 2188
587 2188 av = pcx_av(aTHX_ c);
588 /*
589 # uncoverable branch true
590 # uncoverable condition left count:1 class:ignore_covered_err */
591 2188 ▶ if (!av) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return NULL; 0 2188 2188
592 2188 esv = pcx_get(aTHX_ av, PCX_ENV);
593 /*
594 # uncoverable branch true
595 # uncoverable condition left count:1 class:ignore_covered_err
596 # uncoverable condition left count:2 class:ignore_covered_err
597 # uncoverable condition left count:3 class:ignore_covered_err */
598 2188 ▶ if (!(esv && SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(esv && SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV)) 2188 0 2188
if (!(esv && SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV)) 2188 0 2188
if (!(esv && SvROK(esv) && SvTYPE(SvRV(esv)) == SVt_PVHV)) 0 2188 2188
599 /*
600 # uncoverable statement
601 # uncoverable condition left count:1
602 # uncoverable condition right count:1 */
603 0 ▶ return (ar->def >= 0) ? &ar->cat[ar->def] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return (ar->def >= 0) ? &ar->cat[ar->def] : NULL; 0 0 0
604 2188 env = (HV *)SvRV(esv);
605  
606 2188 cached = hv_fetchs(env, PL_ENV_KEY, 0);
607 /*
608 # uncoverable condition right count:2 class:ignore_covered_err
609 # uncoverable condition right count:3 class:ignore_covered_err */
610 2188 ▶ if (cached && *cached && SvIOK(*cached)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cached && *cached && SvIOK(*cached)) { 3 2185 2188
if (cached && *cached && SvIOK(*cached)) { 3 0 3
if (cached && *cached && SvIOK(*cached)) { 3 0 3
611 3 IV i = SvIV(*cached);
612 /*
613 # uncoverable branch false
614 # uncoverable condition right count:1 class:ignore_covered_err
615 # uncoverable condition right count:2 class:ignore_covered_err */
616 3 ▶ if (i >= 0 && i < ar->ncat) return &ar->cat[i];
Branch: 2 branches
BranchTrueFalseTotal
if (i >= 0 && i < ar->ncat) return &ar->cat[i]; 3 0 3
if (i >= 0 && i < ar->ncat) return &ar->cat[i]; 3 0 3
617 }
618  
619 { /* 1. ?lang= */
620 STRLEN nl;
621 2185 const char *pname = pi_cfg_str(aTHX_ cfg, "param", "lang", &nl);
622 2185 SV *nsv = sv_2mortal(newSVpvn(pname, nl));
623 2185 pv = pcx_param(aTHX_ av, nsv);
624 2189 ▶ if (pv && SvOK(pv)) pp = SvPV_const(pv, pl); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (pv && SvOK(pv)) pp = SvPV_const(pv, pl); /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 2181 2185
if (pv && SvOK(pv)) pp = SvPV_const(pv, pl); /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
625 }
626 { /* 2. the stored choice */
627 STRLEN nl;
628 2185 const char *cname = pi_cfg_str(aTHX_ cfg, "cookie", "punk.lang", &nl);
629 2185 SV **ck = hv_fetchs(env, "HTTP_COOKIE", 0);
630 /*
631 # uncoverable condition right count:2 class:ignore_covered_err
632 # uncoverable condition right count:3 class:ignore_covered_err */
633 2185 ▶ if (ck && *ck && SvOK(*ck)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ck && *ck && SvOK(*ck)) { 5 2180 2185
if (ck && *ck && SvOK(*ck)) { 5 0 5
if (ck && *ck && SvOK(*ck)) { 5 0 5
634 STRLEN chl;
635 5 const char *chp = SvPV_const(*ck, chl);
636 5 sp = pi_cookie(chp, chl, cname, nl, &sl);
637 }
638 }
639 { /* 3. Accept-Language */
640 2185 SV **al = hv_fetchs(env, "HTTP_ACCEPT_LANGUAGE", 0);
641 /*
642 # uncoverable condition right count:2 class:ignore_covered_err
643 # uncoverable condition right count:3 class:ignore_covered_err */
644 2277 ▶ if (al && *al && SvOK(*al)) hp = SvPV_const(*al, hl);
Branch: 3 branches
BranchTrueFalseTotal
if (al && *al && SvOK(*al)) hp = SvPV_const(*al, hl); 92 2093 2185
if (al && *al && SvOK(*al)) hp = SvPV_const(*al, hl); 92 0 92
if (al && *al && SvOK(*al)) hp = SvPV_const(*al, hl); 92 0 92
645 }
646  
647 2185 ch = pl_resolve(pp, pl, sp, sl, hp, hl, ar);
648 /*
649 # uncoverable branch true
650 # uncoverable condition left count:1 class:ignore_covered_err */
651 2185 ▶ if (ch.idx < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ch.idx < 0) return NULL; 0 2185 2185
652  
653 2185 (void)hv_stores(env, PL_ENV_KEY, newSViv(ch.idx));
654  
655 /* There was a second write here - PL_ENV_KEY "_set" - with a comment
656 * saying "the env flag is what the response path reads to decide whether
657 * to write the cookie". NOTHING EVER READ IT, and no code in this
658 * distribution has ever written a punk.lang cookie. The POD promised the
659 * feature and described its absence as a caveat.
660 *
661 * `ch.explicit_` stays, because it is the fact a future implementation
662 * would derive the decision from: it says the locale came from ?lang= or
663 * the cookie rather than from Accept-Language. Nothing consumes it today.
664 * Writing the cookie is a response-path change with its own SameSite,
665 * Secure, Max-Age and Path decisions, and it belongs in its own release
666 * where a bisect can see it. */
667 2185 return &ar->cat[ch.idx];
668 }
669  
670 /* Put this request's catalogue into the variables a template is about to be
671 * rendered with, so `{% locale.welcome %}` works with nothing passed by the
672 * handler.
673 *
674 * SET, not set-if-absent, for the reason CSP gives about its nonce: a handler
675 * rendering with a hashref it keeps between requests would otherwise serve
676 * the first request's language for ever, and a page in the wrong language
677 * looks like a page, which is the failure nobody reports. */
678 2172 static void pi_bind_vars(pTHX_ SV *c, SV *data) {
679 2172 HV *cfg = pi_cfg_of(aTHX_ c);
680 2172 pi_arena *ar = pi_arena_of(aTHX_ cfg);
681 const pi_cat *cat;
682  
683 2172 ▶ if (!ar) return; /* the plugin is not registered */
Branch: 1 branch
BranchTrueFalseTotal
if (!ar) return; /* the plugin is not registered */ 149 2023 2172
684 /*
685 # uncoverable branch true
686 # uncoverable condition left count:1 class:ignore_covered_err
687 # uncoverable condition left count:2 class:ignore_covered_err
688 # uncoverable condition left count:3 class:ignore_covered_err */
689 2023 ▶ if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 2023 0 2023
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 2023 0 2023
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 0 2023 2023
690  
691 2023 cat = pi_for_request(aTHX_ c, cfg, ar);
692 /*
693 # uncoverable branch true
694 # uncoverable condition left count:1 class:ignore_covered_err */
695 2023 ▶ if (!cat) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cat) return; 0 2023 2023
696  
697 /* SET, not set-if-absent, for the reason CSP gives about its nonce: a
698 * handler rendering with a hashref it keeps between requests would
699 * otherwise serve the first request's language for ever, and a page in
700 * the wrong language still looks like a page. */
701 {
702 2023 ▶ const pi_cat *d = (ar->def >= 0) ? &ar->cat[ar->def] : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const pi_cat *d = (ar->def >= 0) ? &ar->cat[ar->def] : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2023 0 2023
703 2023 ▶ (void)hv_stores((HV *)SvRV(data), "locale",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores((HV *)SvRV(data), "locale", 2023 0 2023
704 pi_tied_hash(aTHX_ ar, (int)(cat - ar->cat), NULL,
705 cfg, c, cat->node,
706 d ? d->node : FZ_NOHANDLE));
707 }
708 }
709  
710 #endif /* PUNK_LANG_H */
« xs/metrics.xs ↑ Summary include/punk/punk_blob.h »