include/punk/punk_cget.h

filter: Statementstmt 98.5% 208 / 211 Branchbran 69.6% 222 / 320 (20 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.1% 430 / 531 (20 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_obs_selftest.h ↑ Summary include/punk/punk_accept.h »
1 #ifndef PUNK_CGET_H
2 #define PUNK_CGET_H
3  
4 /* Punk::Plugin::ConditionalGet - the strong validator.
5 *
6 * get '/api/orders' => sub { ... }, {
7 * etag => sub { $_[0]->model('Order')->max_updated_at },
8 * };
9 *
10 * The application supplies something it knows cheaply that identifies the
11 * entity. An unchanged one answers 304 WITHOUT RUNNING THE HANDLER: no
12 * render, no queries, no serialisation. That is the whole point - the body
13 * ETag saves bandwidth, this saves the server.
14 *
15 * ---- why this is here and not in a hook --------------------------------
16 *
17 * A web route runs the before_dispatch chain, THEN the route's guards, then
18 * the handler. So a validator on the public before_dispatch hook would run
19 * BEFORE authentication - and a user who has logged out, still holding a tag
20 * from when they were logged in, would send it, get a 304, and their browser
21 * would render the private page it still has instead of the login redirect
22 * the guard would have produced. The plugin would have turned an
23 * authorisation check into a cache hit.
24 *
25 * So the check sits between the guards and the handler, where no public hook
26 * phase runs. That gap is recorded rather than worked around: as it stands,
27 * `before_dispatch` cannot be used for anything that must respect a guard.
28 */
29  
30 /* Is this a method a 304 may answer? RFC 9110 13.1.3 - a conditional POST
31 * means If-Match and optimistic concurrency, which is a different feature
32 * wearing the same header family, and is deliberately out of scope. */
33 152 static int pcg_cond_method(const char *method, STRLEN mlen) {
34 142 ▶ return (mlen == 3 && memEQ(method, "GET", 3))
Branch: 1 branch
BranchTrueFalseTotal
return (mlen == 3 && memEQ(method, "GET", 3)) 0 142 142
35 294 ▶ || (mlen == 4 && memEQ(method, "HEAD", 4));
Branch: 3 branches
BranchTrueFalseTotal
|| (mlen == 4 && memEQ(method, "HEAD", 4)); 142 10 152
|| (mlen == 4 && memEQ(method, "HEAD", 4)); 10 0 10
|| (mlen == 4 && memEQ(method, "HEAD", 4)); 8 2 10
36 }
37  
38 /* A strong entity-tag from whatever the application returned.
39 *
40 * Quoting is done here so an application returning 42 and one returning "42"
41 * cannot produce two different tags for one entity - the same rule
42 * psf_respond already applies to its `etag` option.
43 *
44 * Strong, never W/: the application is asserting that this value identifies
45 * the entity, which is exactly what a strong validator means. */
46 42 static SV *pcg_quote(pTHX_ SV *v) {
47 STRLEN l;
48 42 const char *p = SvPV_const(v, l);
49 /* already quoted: the quotes are kept and what is between them is
50 * filtered exactly as an unquoted value is */
51 /*
52 # uncoverable condition right count:1 class:ignore_covered_err
53 # uncoverable condition right count:3 class:ignore_covered_err */
54 42 ▶ if (l >= 2 && p[0] == '"' && p[l - 1] == '"') { p++; l -= 2; }
Branch: 3 branches
BranchTrueFalseTotal
if (l >= 2 && p[0] == '"' && p[l - 1] == '"') { p++; l -= 2; } 42 0 42
if (l >= 2 && p[0] == '"' && p[l - 1] == '"') { p++; l -= 2; } 6 36 42
if (l >= 2 && p[0] == '"' && p[l - 1] == '"') { p++; l -= 2; } 6 0 6
55 {
56 42 SV *out = newSVpvs("\"");
57 STRLEN i;
58 241 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 199 42 241
59 /* A validator is application data - a column value, a build
60 * revision - and it is about to become a response header. A quote
61 * or a control byte in it would end the tag early or split the
62 * response, so they are dropped rather than escaped: an
63 * entity-tag has no escaping mechanism to use. */
64 199 unsigned char ch = (unsigned char)p[i];
65 /*
66 # uncoverable condition left count:3 class:ignore_covered_err */
67 199 ▶ if (ch < 0x21 || ch == '"' || ch == 0x7f) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (ch < 0x21 || ch == '"' || ch == 0x7f) continue; 196 3 199
if (ch < 0x21 || ch == '"' || ch == 0x7f) continue; 195 1 196
if (ch < 0x21 || ch == '"' || ch == 0x7f) continue; 0 195 195
68 195 sv_catpvn(out, (const char *)&ch, 1);
69 }
70 42 sv_catpvs(out, "\"");
71 42 return out;
72 }
73 }
74  
75 /* Set a validator header on the response object the handler will add to, so
76 * the 200 carries it as well. A client never given a validator can never send
77 * one back, which would make the whole feature inert on its second request.
78 *
79 * The response object is FORCED here even when the handler would never have
80 * touched it - that is the cost of putting a tag on a plain `$c->text`
81 * response, and it is one allocation on a route that opted in. */
82 54 static void pcg_set_hdr(pTHX_ SV *c, const char *name, SV *val) {
83 54 AV *cav = pcx_av(aTHX_ c);
84 SV *resobj;
85 AV *res, *hdrs;
86 /*
87 # uncoverable branch true
88 # uncoverable condition left count:1 class:ignore_covered_err */
89 54 ▶ if (!cav) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cav) return; 0 54 54
90 54 resobj = pcx_force(aTHX_ cav, PCX_RES, "Punk::Response", NULL);
91 /*
92 # uncoverable branch true
93 # uncoverable condition left count:1 class:ignore_covered_err
94 # uncoverable condition left count:2 class:ignore_covered_err
95 # uncoverable condition left count:3 class:ignore_covered_err */
96 54 ▶ if (!resobj || !SvROK(resobj) || SvTYPE(SvRV(resobj)) != SVt_PVAV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!resobj || !SvROK(resobj) || SvTYPE(SvRV(resobj)) != SVt_PVAV) return; 54 0 54
if (!resobj || !SvROK(resobj) || SvTYPE(SvRV(resobj)) != SVt_PVAV) return; 54 0 54
if (!resobj || !SvROK(resobj) || SvTYPE(SvRV(resobj)) != SVt_PVAV) return; 0 54 54
97 54 res = (AV *)SvRV(resobj);
98 /* punk_res_headers, not pcx_res_headers: the second only reads, and a
99 * response nothing has set a header on yet has no pair-AV to read - the
100 * tag would be dropped on exactly the plain `$c->text` route this is
101 * most worth having on. This is the accessor $c->header reaches. */
102 54 hdrs = punk_res_headers(aTHX_ res);
103 /*
104 # uncoverable branch true
105 # uncoverable condition left count:1 class:ignore_covered_err */
106 54 ▶ if (!hdrs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hdrs) return; 0 54 54
107 54 av_push(hdrs, newSVpv(name, 0));
108 54 av_push(hdrs, newSVsv(val));
109 }
110  
111 /* Run one validator coderef over $c. Returns the result (+1, or undef);
112 * *errp gets the croak (+1) and the caller answers with the app's 500 -
113 * application code running before the handler must not fail silently. */
114 58 static SV *pcg_run_validator(pTHX_ SV *cb, SV *c, SV **errp) {
115 58 dSP; int count; SV *val;
116 58 ENTER; SAVETMPS;
117 58 ▶ PUSHMARK(SP); EXTEND(SP, 1);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); 0 58 58
PUSHMARK(SP); EXTEND(SP, 1); 0 58 58
118 58 PUSHs(c);
119 58 PUTBACK;
120 58 count = call_sv(cb, G_SCALAR | G_EVAL);
121 58 SPAGAIN;
122 58 ▶ val = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
val = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 58 0 58
123 58 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 58 0 58
124 58 ▶ if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 58 0 58
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 56 58
125 2 SvREFCNT_dec(val);
126 2 ▶ *errp = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
*errp = newSVsv(ERRSV); 2 0 2
127 2 return NULL;
128 }
129 56 return val;
130 }
131  
132 /* ---- the 304, in one place ---------------------------------------------------
133 *
134 * A 304 is a response whose correctness is mostly about what is ABSENT, and
135 * getting that wrong produces the worst failure in this feature: a client
136 * that waits for a body which will never arrive, or a cache that stores an
137 * entry it can never validate again. Neither is caught by a test asserting
138 * the status is 304.
139 *
140 * Both halves of this plugin build theirs here, and it has to agree with the
141 * third producer in the dist - psf_respond's, for files - because two
142 * answers to "what does a 304 look like" inside one framework is a bug
143 * waiting for somebody to compare them.
144 *
145 * RFC 9110 15.4.5: send the headers that would have gone on the 200 and that
146 * the cache needs to update its entry. So everything the response had
147 * gathered is kept - Cache-Control, Vary, the security headers, the
148 * Set-Cookie the session write-back just added - EXCEPT the ones describing
149 * a representation that is not being sent.
150 */
151  
152 /* Does this header describe the representation rather than the resource?
153 * Content-Length above all: a bodyless response that claims a length is how
154 * a client is taught to wait. psf_respond's 304 sends none of these either. */
155 49 static int pcg_entity_header(pTHX_ const char *k, STRLEN l) {
156 8 ▶ return (l == 12 && foldEQ(k, "Content-Type", 12))
Branch: 1 branch
BranchTrueFalseTotal
return (l == 12 && foldEQ(k, "Content-Type", 12)) 0 8 8
157 41 ▶ || (l == 14 && foldEQ(k, "Content-Length", 14))
Branch: 2 branches
BranchTrueFalseTotal
|| (l == 14 && foldEQ(k, "Content-Length", 14)) 10 31 41
|| (l == 14 && foldEQ(k, "Content-Length", 14)) 2 8 10
158 98 ▶ || (l == 16 && foldEQ(k, "Content-Encoding", 16));
Branch: 3 branches
BranchTrueFalseTotal
|| (l == 16 && foldEQ(k, "Content-Encoding", 16)); 8 41 49
|| (l == 16 && foldEQ(k, "Content-Encoding", 16)); 0 33 33
|| (l == 16 && foldEQ(k, "Content-Encoding", 16)); 0 0 0
159 }
160  
161 /* The 304 triplet (+1), with `tag` (ownership moves; NULL on a route whose
162 * only validator is a date - the Last-Modified travels in `src`, set on the
163 * response object before the check) and everything in `src` that a 304 may
164 * carry. src may be NULL - a route where nothing set a header before the
165 * check ran. */
166 28 static SV *pcg_304(pTHX_ SV *tag, AV *src) {
167 28 AV *resp = newAV(), *hdr = newAV(), *body = newAV();
168 28 ▶ SSize_t i, n = src ? av_len(src) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = src ? av_len(src) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 28 0 28
169  
170 28 ▶ if (tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag) { 26 2 28
171 26 av_push(hdr, newSVpvs("ETag"));
172 26 av_push(hdr, tag); /* ownership moves */
173 }
174 77 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 49 28 77
175 49 SV **k = av_fetch(src, i, 0);
176 49 SV **v = av_fetch(src, i + 1, 0);
177 STRLEN kl;
178 const char *kp;
179 /*
180 # uncoverable branch true class:ignore_covered_err
181 # uncoverable condition left count:1 class:ignore_covered_err
182 # uncoverable condition left count:2 class:ignore_covered_err
183 # uncoverable condition left count:3 class:ignore_covered_err */
184 83 ▶ if (!(k && *k && SvOK(*k))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(k && *k && SvOK(*k))) continue; 49 0 49
if (!(k && *k && SvOK(*k))) continue; 49 0 49
if (!(k && *k && SvOK(*k))) continue; 0 49 49
185 49 kp = SvPV_const(*k, kl);
186 49 ▶ if (pcg_entity_header(aTHX_ kp, kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pcg_entity_header(aTHX_ kp, kl)) continue; 16 33 49
187 /* the tag just added wins over one already in the source - which is
188 * the strong half's own, put there so the 200 would carry it */
189 33 ▶ if (tag && kl == 4 && foldEQ(kp, "ETag", 4)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (tag && kl == 4 && foldEQ(kp, "ETag", 4)) continue; 31 2 33
if (tag && kl == 4 && foldEQ(kp, "ETag", 4)) continue; 22 9 31
if (tag && kl == 4 && foldEQ(kp, "ETag", 4)) continue; 18 4 22
190 15 av_push(hdr, newSVsv(*k));
191 /*
192 # uncoverable condition right count:1 class:ignore_covered_err
193 # uncoverable condition right count:2 class:ignore_covered_err */
194 15 ▶ av_push(hdr, (v && *v) ? newSVsv(*v) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(hdr, (v && *v) ? newSVsv(*v) : newSV(0)); 15 0 15
av_push(hdr, (v && *v) ? newSVsv(*v) : newSV(0)); 15 0 15
195 }
196 28 av_push(resp, newSViv(304));
197 28 av_push(resp, newRV_noinc((SV *)hdr));
198 28 av_push(resp, newRV_noinc((SV *)body)); /* empty: a 304 has no body */
199 28 return newRV_noinc((SV *)resp);
200 }
201  
202 /* The check, run between a route's guards and its handler.
203 *
204 * Returns 1 when the request has been ANSWERED and the handler must be
205 * skipped, with exactly one of *out (the 304 triplet) and *errp (what the
206 * validator croaked with) set. Returns 0 to carry on.
207 *
208 * A croaking validator is the app's 500, not a silent full response: it is
209 * application code running before the handler, and swallowing its error
210 * would hide a broken query behind a page that merely got slower.
211 */
212 104 static int pcg_check(pTHX_ SV *c, HV *rech, HV *env, const char *method,
213 STRLEN mlen, SV **out, SV **errp) {
214 104 SV **ep = hv_fetchs(rech, K_ETAG, 0);
215 104 SV **lp = hv_fetchs(rech, K_LAST_MODIFIED, 0);
216 /*
217 # uncoverable condition right count:2 class:ignore_covered_err
218 # uncoverable condition right count:4 class:ignore_covered_err */
219 104 ▶ int has_et = ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVCV;
Branch: 4 branches
BranchTrueFalseTotal
int has_et = ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVCV; 83 21 104
int has_et = ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVCV; 83 0 83
int has_et = ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVCV; 45 38 83
int has_et = ep && *ep && SvROK(*ep) && SvTYPE(SvRV(*ep)) == SVt_PVCV; 45 0 45
220 /*
221 # uncoverable condition right count:2 class:ignore_covered_err
222 # uncoverable condition right count:3 class:ignore_covered_err
223 # uncoverable condition right count:4 class:ignore_covered_err */
224 104 ▶ int has_lm = lp && *lp && SvROK(*lp) && SvTYPE(SvRV(*lp)) == SVt_PVCV;
Branch: 4 branches
BranchTrueFalseTotal
int has_lm = lp && *lp && SvROK(*lp) && SvTYPE(SvRV(*lp)) == SVt_PVCV; 14 90 104
int has_lm = lp && *lp && SvROK(*lp) && SvTYPE(SvRV(*lp)) == SVt_PVCV; 14 0 14
int has_lm = lp && *lp && SvROK(*lp) && SvTYPE(SvRV(*lp)) == SVt_PVCV; 14 0 14
int has_lm = lp && *lp && SvROK(*lp) && SvTYPE(SvRV(*lp)) == SVt_PVCV; 14 0 14
225 104 SV *val, *tag = NULL;
226 char ldate[64];
227 104 int have_date = 0, matched;
228  
229 104 *out = NULL;
230 104 *errp = NULL;
231 104 ▶ if (!has_et && !has_lm) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!has_et && !has_lm) return 0; 59 45 104
if (!has_et && !has_lm) return 0; 48 11 59
232 56 ▶ if (!pcg_cond_method(method, mlen)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pcg_cond_method(method, mlen)) return 0; 1 55 56
233  
234 /* The validators run on EVERY request to the route, not only the
235 * conditional ones: the 200 needs its validators too, and a client never
236 * given one can never send one back.
237 *
238 * undef means "I do not know", and that validator contributes nothing. A
239 * validator that cannot answer must not be able to produce a wrong 304,
240 * and saying so has to be cheaper to write than guessing. */
241 55 ▶ if (has_et) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_et) { 44 11 55
242 44 val = pcg_run_validator(aTHX_ *ep, c, errp);
243 44 ▶ if (!val) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!val) return 1; 1 43 44
244 43 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 42 1 43
245 42 tag = pcg_quote(aTHX_ val);
246 /* Two characters is an empty tag - nothing the validator
247 * returned survived. Treat it as undef rather than emitting
248 * `""`, which every client would then match against for ever. */
249 /*
250 # uncoverable branch true
251 # uncoverable condition left count:1 class:ignore_covered_err */
252 42 ▶ if (SvCUR(tag) <= 2) { SvREFCNT_dec(tag); tag = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(tag) <= 2) { SvREFCNT_dec(tag); tag = NULL; } 0 42 42
253 /*
254 # uncoverable branch false
255 # uncoverable condition right count:1 class:ignore_covered_err */
256 42 ▶ if (tag) pcg_set_hdr(aTHX_ c, "ETag", tag);
Branch: 1 branch
BranchTrueFalseTotal
if (tag) pcg_set_hdr(aTHX_ c, "ETag", tag); 42 0 42
257 }
258 43 SvREFCNT_dec(val);
259 }
260 54 ▶ if (has_lm) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_lm) { 14 40 54
261 14 val = pcg_run_validator(aTHX_ *lp, c, errp);
262 14 ▶ if (!val) { if (tag) SvREFCNT_dec(tag); return 1; } /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!val) { if (tag) SvREFCNT_dec(tag); return 1; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 13 14
if (!val) { if (tag) SvREFCNT_dec(tag); return 1; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 1 1
263 13 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 12 1 13
264 /* An epoch, clamped to now: a Last-Modified in the future is a
265 * clock-skew statement no cache can use well, and RFC 9110
266 * 8.8.2 says a recipient replaces one with the response's Date
267 * anyway - clamping at generation is the same answer, earlier. */
268 12 time_t when = (time_t)SvIV(val);
269 12 time_t now = time(NULL);
270 12 ▶ if (when > now) when = now;
Branch: 1 branch
BranchTrueFalseTotal
if (when > now) when = now; 1 11 12
271 12 ps_http_date(ldate, sizeof ldate, when);
272 12 have_date = 1;
273 {
274 12 SV *dsv = sv_2mortal(newSVpv(ldate, 0));
275 12 pcg_set_hdr(aTHX_ c, "Last-Modified", dsv);
276 }
277 }
278 13 SvREFCNT_dec(val);
279 }
280 53 ▶ if (!tag && !have_date) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!tag && !have_date) return 0; 11 42 53
if (!tag && !have_date) return 0; 2 9 11
281  
282 /* One function holds the whole decision, and it is the file path's:
283 * If-None-Match wins over If-Modified-Since (RFC 9110 13.1.3), and the
284 * date comparison is exact - the date they were given is the date we
285 * would send. Exact is deliberate: there is no date parser in this
286 * dist, an unrecognised or stale date simply fails to match, and the
287 * failure direction is a re-send, never a wrong 304. The cost is the
288 * one-second grain any HTTP date already has. */
289 51 ▶ { STRLEN tl = tag ? SvCUR(tag) : 0;
Branch: 1 branch
BranchTrueFalseTotal
{ STRLEN tl = tag ? SvCUR(tag) : 0; 42 9 51
290 93 ▶ const char *tp = tag ? SvPV_const(tag, tl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *tp = tag ? SvPV_const(tag, tl) : NULL; 42 9 51
291 51 ▶ matched = psf_not_modified(aTHX_ env, tp, tl,
Branch: 1 branch
BranchTrueFalseTotal
matched = psf_not_modified(aTHX_ env, tp, tl, 12 39 51
292 have_date ? ldate : NULL);
293 }
294 51 ▶ if (!matched) { if (tag) SvREFCNT_dec(tag); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!matched) { if (tag) SvREFCNT_dec(tag); return 0; } 31 20 51
if (!matched) { if (tag) SvREFCNT_dec(tag); return 0; } 24 7 31
295  
296 { /* A 304 is a RESPONSE, not an absence of one. It goes back as an
297 * ordinary triplet and through punk_finish_c like any other, so
298 * everything that decorates a response still decorates it - the
299 * session write-back, the security headers - and only the handler is
300 * skipped.
301 *
302 * The headers a guard or a before_dispatch hook already set come with
303 * it. They were set by code that DID run, on a request that is being
304 * answered, and a `Vary` or a `Cache-Control` among them is exactly
305 * what a 304 must not lose. */
306 20 AV *cav = pcx_av(aTHX_ c);
307 20 ▶ AV *res = cav ? pcx_res_av(aTHX_ cav) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
AV *res = cav ? pcx_res_av(aTHX_ cav) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 20 0 20
308 20 ▶ *out = pcg_304(aTHX_ tag, res ? pcx_res_headers(aTHX_ res) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
*out = pcg_304(aTHX_ tag, res ? pcx_res_headers(aTHX_ res) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 20 0 20
309 20 return 1;
310 }
311 }
312  
313 /* ---- the body ETag ----------------------------------------------------------
314 *
315 * get '/dashboard' => sub { ... }, { etag => 1 };
316 *
317 * The rendered bytes are hashed and an unchanged response becomes a 304.
318 *
319 * BE HONEST ABOUT WHAT THIS BUYS. It saves the wire and the client's parse
320 * and re-render, which on a polled JSON route is most of what the client was
321 * spending. It saves the server NOTHING: the queries ran, the template
322 * rendered, the encoder ran, and the hash is added cost on top - paid on
323 * every request to the route, including the ones that end in a full 200.
324 * Measured at ~3.5 us on a 1.7 KB body against a route that took 1.3 us to
325 * produce it. Against a route that spends 5 ms in the database it is 0.07%
326 * and worth having; against a route like that one it is not.
327 *
328 * It runs on the after-dispatch chain, which is public, and it is appended
329 * LAST at compile: an application hook that rewrites the body must have
330 * already run, or the tag would describe bytes nobody was sent. Being last
331 * also puts it after the session write-back, which is why the 304 below
332 * keeps the 200's headers rather than building a fresh set - dropping a
333 * Set-Cookie the write-back had just added would log the user out. */
334  
335 /* Weak, not strong: two responses with identical bytes are the same entity
336 * for the purpose of not re-sending them, but nothing here can promise
337 * byte-equality across a Vary axis this does not control, and a strong tag
338 * would license If-Range on a body that has no ranges. */
339 35 static SV *pcg_body_tag(pTHX_ AV *body) {
340 U32 h[8];
341 unsigned char sum[32];
342 char hex[PA_DIGEST_LEN + 1];
343 unsigned char buf[64];
344 35 size_t total = 0, rem = 0;
345 35 SSize_t i, n = av_len(body) + 1;
346 SV *out;
347  
348 35 memcpy(h, ps_sha256_iv, sizeof h);
349 72 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 37 35 72
350 37 SV **e = av_fetch(body, i, 0);
351 const char *p;
352 STRLEN l;
353 /*
354 # uncoverable branch true
355 # uncoverable condition left count:1 class:ignore_covered_err
356 # uncoverable condition left count:2 class:ignore_covered_err
357 # uncoverable condition left count:3 class:ignore_covered_err */
358 37 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 37 0 37
if (!(e && *e && SvOK(*e))) continue; 37 0 37
if (!(e && *e && SvOK(*e))) continue; 0 37 37
359 37 p = SvPV_const(*e, l);
360 37 total += l;
361 /* The pieces are hashed as one stream, so how the handler happened
362 * to split its body does not change the tag: a template that
363 * returns one string and one that returns three must agree. */
364 277 ▶ while (l) {
Branch: 1 branch
BranchTrueFalseTotal
while (l) { 240 37 277
365 240 ▶ size_t take = (rem + l >= 64) ? 64 - rem : l;
Branch: 1 branch
BranchTrueFalseTotal
size_t take = (rem + l >= 64) ? 64 - rem : l; 203 37 240
366 240 memcpy(buf + rem, p, take);
367 240 rem += take; p += take; l -= take;
368 240 ▶ if (rem == 64) { ps_sha256_blocks(h, buf, 1); rem = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (rem == 64) { ps_sha256_blocks(h, buf, 1); rem = 0; } 203 37 240
369 }
370 }
371 35 ps_sha256_final(h, buf, rem, total, sum);
372 35 pa_hex(sum, PA_DIGEST_LEN / 2, hex);
373 35 out = newSVpvs("W/\"");
374 35 sv_catpvn(out, hex, PA_DIGEST_LEN);
375 35 sv_catpvs(out, "\"");
376 35 return out;
377 }
378  
379 /* ---- what a validator does to a shared cache --------------------------------
380 *
381 * An ETag is a storage instruction. A response that is per-user, or
382 * per-encoding, and that carries a validator with nothing saying what it
383 * depends on, can be stored by any intermediary and handed to the next
384 * request for the same URL - and then confidently revalidated, because now
385 * there is a tag to revalidate with. For a per-user dashboard that is a
386 * disclosure, and this plugin is exactly what will be reached for on those
387 * routes.
388 *
389 * Two rules, and both resolve ambiguity by saying MORE rather than less. A
390 * missing header costs a re-send; a wrong one costs correctness, and in the
391 * authenticated case, confidentiality.
392 */
393  
394 /* Is a header present, and what is its value? */
395 167 static SV *pcg_hdr_get(pTHX_ AV *h, const char *name, STRLEN nl) {
396 167 ▶ SSize_t i, n = h ? av_len(h) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = h ? av_len(h) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 167 0 167
397 718 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 563 155 718
398 563 SV **k = av_fetch(h, i, 0);
399 STRLEN kl;
400 const char *kp;
401 /*
402 # uncoverable branch true
403 # uncoverable condition left count:1 class:ignore_covered_err
404 # uncoverable condition left count:2 class:ignore_covered_err
405 # uncoverable condition left count:3 class:ignore_covered_err */
406 563 ▶ if (!(k && *k && SvOK(*k))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(k && *k && SvOK(*k))) continue; 563 0 563
if (!(k && *k && SvOK(*k))) continue; 563 0 563
if (!(k && *k && SvOK(*k))) continue; 0 563 563
407 563 kp = SvPV_const(*k, kl);
408 563 ▶ if (kl == nl && foldEQ(kp, name, nl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == nl && foldEQ(kp, name, nl)) { 24 539 563
if (kl == nl && foldEQ(kp, name, nl)) { 12 12 24
409 12 SV **v = av_fetch(h, i + 1, 0);
410 /*
411 # uncoverable condition right count:1 class:ignore_covered_err
412 # uncoverable condition right count:2 class:ignore_covered_err */
413 12 ▶ return (v && *v) ? *v : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
return (v && *v) ? *v : &PL_sv_undef; 12 0 12
return (v && *v) ? *v : &PL_sv_undef; 12 0 12
414 }
415 }
416 155 return NULL;
417 }
418  
419 /* The cache-safety pass, run over anything this plugin tagged - the 200 and
420 * the 304, from either half. */
421 88 static void pcg_cache_safety(pTHX_ AV *h, HV *env) {
422 /*
423 # uncoverable branch true
424 # uncoverable condition left count:1 class:ignore_covered_err */
425 88 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 88 88
426  
427 /* Vary: Accept-Encoding, always. Punk does not compress - Hyperman does,
428 * on the write path, after Punk has stopped looking - so two clients,
429 * one accepting gzip and one not, get the same entity and the same tag
430 * from here and different BYTES from the server. A shared cache needs to
431 * be told that, or it stores one and serves it to both. It is the same
432 * reasoning that puts this header on every response from a static mount,
433 * compressed or not, and it is the honest place for it: we are the ones
434 * handing out the validator the cache will key on.
435 *
436 * Merged through pa_vary_add, so an application that already declared
437 * what it varies on keeps every token it named. */
438 88 pa_vary_add(aTHX_ h, "Accept-Encoding", 15);
439  
440 /* Cache-Control: private, when the response could be about one person
441 * and the application has not said what it wants.
442 *
443 * Two signals, and the second is the one that matters. A Set-Cookie on
444 * the RESPONSE is the obvious one - a session, an auth identity, a CSRF
445 * mirror - but it is not enough on its own: a signed-in user reading a
446 * page that changes nothing gets no write-back and no Set-Cookie, which
447 * is precisely the per-user response somebody put an ETag on. So a
448 * Cookie or Authorization on the REQUEST counts too.
449 *
450 * That is deliberately broad. A public page carrying an analytics
451 * cookie loses shared-cache storage on the routes that opted into
452 * ETags, which costs a re-send; the other direction costs one user
453 * being handed another user's dashboard. An application that knows the
454 * page is public says so, and is never overruled - it may have meant
455 * `public` and known why.
456 *
457 * (Authorization is already covered for shared caches by RFC 9111 3.5;
458 * saying `private` as well costs a header and covers the caches that
459 * read this one and not that rule.) */
460 88 ▶ if (!pcg_hdr_get(aTHX_ h, "Cache-Control", 13)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pcg_hdr_get(aTHX_ h, "Cache-Control", 13)) { 79 9 88
461 79 int personal = pcg_hdr_get(aTHX_ h, "Set-Cookie", 10) ? 1 : 0;
462 79 ▶ if (!personal && env) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!personal && env) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 76 3 79
if (!personal && env) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 76 0 76
463 76 SV **e = hv_fetchs(env, "HTTP_COOKIE", 0);
464 /*
465 # uncoverable condition right count:2 class:ignore_covered_err
466 # uncoverable condition right count:3 class:ignore_covered_err
467 # uncoverable condition right count:4 class:ignore_covered_err */
468 76 ▶ if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1;
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 9 67 76
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 9 0 9
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 9 0 9
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 9 0 9
469 else {
470 67 e = hv_fetchs(env, "HTTP_AUTHORIZATION", 0);
471 /*
472 # uncoverable condition right count:2 class:ignore_covered_err
473 # uncoverable condition right count:3 class:ignore_covered_err
474 # uncoverable condition right count:4 class:ignore_covered_err */
475 67 ▶ if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1;
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 1 66 67
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 1 0 1
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 1 0 1
if (e && *e && SvOK(*e) && SvCUR(*e)) personal = 1; 1 0 1
476 }
477 }
478 79 ▶ if (personal) {
Branch: 1 branch
BranchTrueFalseTotal
if (personal) { 13 66 79
479 13 av_push(h, newSVpvs("Cache-Control"));
480 13 av_push(h, newSVpvs("private"));
481 }
482 }
483 }
484  
485 /* The after-dispatch hook: ($c, $triplet) -> a triplet, or nothing.
486 *
487 * It does two jobs, and runs for BOTH halves. For `etag => 1` it hashes the
488 * body and may turn the response into a 304; for either half it then runs
489 * the cache-safety pass, which has to happen here rather than at the check
490 * because the Set-Cookie it reads is added by the session write-back - and
491 * this hook is appended after that one. */
492 XS_INTERNAL(pcg_after_cb);
493 106 XS_INTERNAL(pcg_after_cb) {
494 106 dXSARGS;
495 SV *c, *resp;
496 106 AV *ra, *hdrs = NULL, *body;
497 SV **sp2, **hp, **bp;
498 106 HV *env, *rech = NULL;
499 const char *method;
500 106 STRLEN mlen = 3;
501 SV *tag;
502 SSize_t i, hn;
503 106 int strong = 0;
504  
505 /*
506 # uncoverable branch true
507 # uncoverable condition left count:1 class:ignore_covered_err */
508 106 ▶ if (items < 2) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) XSRETURN_EMPTY; 0 106 106
509 106 c = ST(0);
510 106 resp = ST(1);
511 /*
512 # uncoverable branch true
513 # uncoverable condition left count:1 class:ignore_covered_err
514 # uncoverable condition left count:2 class:ignore_covered_err */
515 106 ▶ if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) XSRETURN_EMPTY; 106 0 106
if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) XSRETURN_EMPTY; 0 106 106
516 106 ra = (AV *)SvRV(resp);
517  
518 { /* the matched route, and its option. A route that did not ask for a
519 * body ETag costs two hv_fetches on the way out, which is what an
520 * app-wide hook has to cost when most routes are not using it. */
521 106 AV *cav = pcx_av(aTHX_ c);
522 106 ▶ SV *m = cav ? pcx_get(aTHX_ cav, PCX_MATCH) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *m = cav ? pcx_get(aTHX_ cav, PCX_MATCH) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 106 0 106
523 SV **rp;
524 /*
525 # uncoverable branch true
526 # uncoverable condition left count:1 class:ignore_covered_err
527 # uncoverable condition left count:2 class:ignore_covered_err
528 # uncoverable condition left count:3 class:ignore_covered_err */
529 106 ▶ if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) XSRETURN_EMPTY;
Branch: 3 branches
BranchTrueFalseTotal
if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) XSRETURN_EMPTY; 106 0 106
if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) XSRETURN_EMPTY; 106 0 106
if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) XSRETURN_EMPTY; 0 106 106
530 106 rp = hv_fetchs((HV *)SvRV(m), "route", 0);
531 /*
532 # uncoverable branch true
533 # uncoverable condition left count:1 class:ignore_covered_err
534 # uncoverable condition left count:2 class:ignore_covered_err
535 # uncoverable condition left count:3 class:ignore_covered_err
536 # uncoverable condition left count:4 class:ignore_covered_err */
537 106 ▶ if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 106 0 106
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 106 0 106
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 106 0 106
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 0 106 106
538 0 XSRETURN_EMPTY; /* # uncoverable statement */
539 106 rech = (HV *)SvRV(*rp);
540 }
541 { /* the body ETag is `etag => 1`; a coderef is the strong validator,
542 * and `last_modified` is always one - both answered for themselves
543 * before the handler ran and only want the cache-safety pass below.
544 * A route with last_modified alone still hands out a validator a
545 * cache will key on, so it is not exempt from saying what that
546 * depends on. */
547 106 SV **et = hv_fetchs(rech, K_ETAG, 0);
548 106 SV **lm = hv_fetchs(rech, K_LAST_MODIFIED, 0);
549 /*
550 # uncoverable condition right count:2 class:ignore_covered_err
551 # uncoverable condition right count:3 class:ignore_covered_err */
552 106 ▶ int has_et = et && *et && SvOK(*et);
Branch: 3 branches
BranchTrueFalseTotal
int has_et = et && *et && SvOK(*et); 85 21 106
int has_et = et && *et && SvOK(*et); 85 0 85
int has_et = et && *et && SvOK(*et); 85 0 85
553 /*
554 # uncoverable condition right count:2 class:ignore_covered_err
555 # uncoverable condition right count:3 class:ignore_covered_err */
556 106 ▶ int has_lm = lm && *lm && SvOK(*lm);
Branch: 3 branches
BranchTrueFalseTotal
int has_lm = lm && *lm && SvOK(*lm); 14 92 106
int has_lm = lm && *lm && SvOK(*lm); 14 0 14
int has_lm = lm && *lm && SvOK(*lm); 14 0 14
557 106 ▶ if (!has_et && !has_lm) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!has_et && !has_lm) XSRETURN_EMPTY; 21 85 106
if (!has_et && !has_lm) XSRETURN_EMPTY; 10 11 21
558 96 ▶ strong = (has_et && SvROK(*et)) || !has_et ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
strong = (has_et && SvROK(*et)) || !has_et ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 85 11 96
strong = (has_et && SvROK(*et)) || !has_et ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 38 47 85
strong = (has_et && SvROK(*et)) || !has_et ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 11 38 49
559 }
560  
561 { /* GET and HEAD. Only a 200 is hashed - an error response is not the
562 * resource, and a 500 that renders identically twice must not be
563 * cached as though it were - but a 304 still gets the cache-safety
564 * pass, since it is the response a cache will store against. */
565 96 AV *cav = pcx_av(aTHX_ c);
566 96 ▶ SV *envsv = cav ? pcx_get(aTHX_ cav, PCX_ENV) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *envsv = cav ? pcx_get(aTHX_ cav, PCX_ENV) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 96 0 96
567 96 SV **stp = av_fetch(ra, 0, 0);
568 IV status;
569 /*
570 # uncoverable branch true
571 # uncoverable condition left count:1 class:ignore_covered_err
572 # uncoverable condition left count:2 class:ignore_covered_err
573 # uncoverable condition left count:3 class:ignore_covered_err */
574 96 ▶ if (!(envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)) 96 0 96
if (!(envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)) 96 0 96
if (!(envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)) 0 96 96
575 0 XSRETURN_EMPTY; /* # uncoverable statement */
576 96 env = (HV *)SvRV(envsv);
577 /*
578 # uncoverable condition right count:1 class:ignore_covered_err
579 # uncoverable condition right count:2 class:ignore_covered_err */
580 96 ▶ status = (stp && *stp) ? SvIV(*stp) : 0;
Branch: 2 branches
BranchTrueFalseTotal
status = (stp && *stp) ? SvIV(*stp) : 0; 96 0 96
status = (stp && *stp) ? SvIV(*stp) : 0; 96 0 96
581 96 sp2 = hv_fetchs(env, "REQUEST_METHOD", 0);
582 /*
583 # uncoverable condition right count:1 class:ignore_covered_err
584 # uncoverable condition right count:2 class:ignore_covered_err
585 # uncoverable condition right count:3 class:ignore_covered_err */
586 192 ▶ method = (sp2 && *sp2 && SvOK(*sp2)) ? SvPV_const(*sp2, mlen) : "GET";
Branch: 3 branches
BranchTrueFalseTotal
method = (sp2 && *sp2 && SvOK(*sp2)) ? SvPV_const(*sp2, mlen) : "GET"; 96 0 96
method = (sp2 && *sp2 && SvOK(*sp2)) ? SvPV_const(*sp2, mlen) : "GET"; 96 0 96
method = (sp2 && *sp2 && SvOK(*sp2)) ? SvPV_const(*sp2, mlen) : "GET"; 96 0 96
587 96 ▶ if (!pcg_cond_method(method, mlen)) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!pcg_cond_method(method, mlen)) XSRETURN_EMPTY; 1 95 96
588 95 ▶ if (status != 200 && status != 304) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (status != 200 && status != 304) XSRETURN_EMPTY; 26 69 95
if (status != 200 && status != 304) XSRETURN_EMPTY; 6 20 26
589  
590 /* the strong half, and any 304: nothing to hash, but the response
591 * carries a validator and needs saying what it depends on */
592 89 ▶ if (strong || status == 304) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (strong || status == 304) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 36 53 89
if (strong || status == 304) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 36 36
593 53 SV **hh = av_fetch(ra, 1, 0);
594 /*
595 # uncoverable branch false
596 # uncoverable condition right count:1 class:ignore_covered_err
597 # uncoverable condition right count:2 class:ignore_covered_err
598 # uncoverable condition right count:3 class:ignore_covered_err
599 # uncoverable condition right count:4 class:ignore_covered_err */
600 53 ▶ if (hh && *hh && SvROK(*hh) && SvTYPE(SvRV(*hh)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (hh && *hh && SvROK(*hh) && SvTYPE(SvRV(*hh)) == SVt_PVAV) 53 0 53
if (hh && *hh && SvROK(*hh) && SvTYPE(SvRV(*hh)) == SVt_PVAV) 53 0 53
if (hh && *hh && SvROK(*hh) && SvTYPE(SvRV(*hh)) == SVt_PVAV) 53 0 53
if (hh && *hh && SvROK(*hh) && SvTYPE(SvRV(*hh)) == SVt_PVAV) 53 0 53
601 53 pcg_cache_safety(aTHX_ (AV *)SvRV(*hh), env);
602 53 XSRETURN_EMPTY;
603 }
604 }
605  
606 36 hp = av_fetch(ra, 1, 0);
607 36 bp = av_fetch(ra, 2, 0);
608 /*
609 # uncoverable branch false
610 # uncoverable condition right count:1 class:ignore_covered_err
611 # uncoverable condition right count:2 class:ignore_covered_err
612 # uncoverable condition right count:3 class:ignore_covered_err
613 # uncoverable condition right count:4 class:ignore_covered_err */
614 36 ▶ if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)
Branch: 4 branches
BranchTrueFalseTotal
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 36 0 36
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 36 0 36
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 36 0 36
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) 36 0 36
615 36 hdrs = (AV *)SvRV(*hp);
616 /* Only an arrayref body can be hashed. A filehandle or a
617 * Punk::SendFile::Reader would have to be consumed to be read, and a
618 * send_file response already carries a strong validator of its own -
619 * there is nothing to add there and something to break. */
620 /*
621 # uncoverable condition left count:1 class:ignore_covered_err
622 # uncoverable condition left count:2 class:ignore_covered_err
623 # uncoverable condition left count:3 class:ignore_covered_err */
624 36 ▶ if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) 36 0 36
if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) 36 0 36
if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) 36 0 36
if (!(bp && *bp && SvROK(*bp) && SvTYPE(SvRV(*bp)) == SVt_PVAV)) 1 35 36
625 1 XSRETURN_EMPTY;
626 35 body = (AV *)SvRV(*bp);
627  
628 /* A response that already carries a tag has been validated by something
629 * that knew more than this does - send_file, or the strong validator on
630 * the same route. Do not replace it. */
631 35 ▶ hn = hdrs ? av_len(hdrs) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
hn = hdrs ? av_len(hdrs) + 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 35 0 35
632 124 ▶ for (i = 0; i + 1 < hn; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < hn; i += 2) { 89 35 124
633 89 SV **k = av_fetch(hdrs, i, 0);
634 STRLEN kl;
635 const char *kp;
636 /*
637 # uncoverable branch true
638 # uncoverable condition left count:1 class:ignore_covered_err
639 # uncoverable condition left count:2 class:ignore_covered_err
640 # uncoverable condition left count:3 class:ignore_covered_err */
641 89 ▶ if (!(k && *k && SvOK(*k))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(k && *k && SvOK(*k))) continue; 89 0 89
if (!(k && *k && SvOK(*k))) continue; 89 0 89
if (!(k && *k && SvOK(*k))) continue; 0 89 89
642 89 kp = SvPV_const(*k, kl);
643 /*
644 # uncoverable branch true
645 # uncoverable condition left count:1 class:ignore_covered_err
646 # uncoverable condition left count:2 class:ignore_covered_err */
647 89 ▶ if (kl == 4 && foldEQ(kp, "ETag", 4)) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && foldEQ(kp, "ETag", 4)) XSRETURN_EMPTY; 8 81 89
if (kl == 4 && foldEQ(kp, "ETag", 4)) XSRETURN_EMPTY; 0 8 8
648 }
649  
650 35 tag = pcg_body_tag(aTHX_ body);
651  
652 /* Compare the QUOTED part, not the `W/` prefix. psf_etag_in strips a
653 * weak prefix off each list entry before comparing, so handing it our
654 * own prefix would mean a tag we issued could never match itself coming
655 * back - which looks exactly like a working feature that never 304s. */
656 35 ▶ if (!psf_not_modified(aTHX_ env, SvPVX(tag) + 2, SvCUR(tag) - 2, NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!psf_not_modified(aTHX_ env, SvPVX(tag) + 2, SvCUR(tag) - 2, NULL)) { 27 8 35
657 /*
658 # uncoverable branch false
659 # uncoverable condition right count:1 class:ignore_covered_err */
660 27 ▶ if (hdrs) { /* the 200 carries it */
Branch: 1 branch
BranchTrueFalseTotal
if (hdrs) { /* the 200 carries it */ 27 0 27
661 27 av_push(hdrs, newSVpvs("ETag"));
662 27 av_push(hdrs, tag);
663 27 pcg_cache_safety(aTHX_ hdrs, env);
664 }
665 0 else SvREFCNT_dec(tag); /* # uncoverable statement */
666 27 XSRETURN_EMPTY; /* the response stands */
667 }
668  
669 /* the 304, through the one builder - the 200's headers minus the ones
670 * describing a body that is no longer being sent. Everything else the
671 * response gathered is kept, and being LAST on the after chain means
672 * that includes the session write-back's Set-Cookie. */
673 {
674 8 SV *out = pcg_304(aTHX_ tag, hdrs);
675 8 SV **h2 = av_fetch((AV *)SvRV(out), 1, 0);
676 /*
677 # uncoverable branch false
678 # uncoverable condition right count:1 class:ignore_covered_err
679 # uncoverable condition right count:2 class:ignore_covered_err
680 # uncoverable condition right count:3 class:ignore_covered_err */
681 8 ▶ if (h2 && *h2 && SvROK(*h2))
Branch: 3 branches
BranchTrueFalseTotal
if (h2 && *h2 && SvROK(*h2)) 8 0 8
if (h2 && *h2 && SvROK(*h2)) 8 0 8
if (h2 && *h2 && SvROK(*h2)) 8 0 8
682 8 pcg_cache_safety(aTHX_ (AV *)SvRV(*h2), env);
683 8 ST(0) = sv_2mortal(out);
684 }
685 8 XSRETURN(1);
686 }
687  
688 #endif /* PUNK_CGET_H */
« include/punk/punk_obs_selftest.h ↑ Summary include/punk/punk_accept.h »