include/punk/punk_serve.h

filter: Statementstmt 99.0% 426 / 435 (5 uncoverable) Branchbran 76.4% 385 / 544 (39 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.5% 811 / 979 (44 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_request.h ↑ Summary include/punk/punk_import.h »
1 /* punk_serve.h - the request dispatcher, in C.
2 *
3 * The whole per-request path Punk::App::compile used to build as a Perl
4 * closure: static exact hit, API-mount route+dispatch, PSGI/static mounts,
5 * dynamic hit, 404/405, then for a web route the before hooks, the guards, the
6 * controller (or a websocket upgrade), and the response finish. Everything
7 * routes and validates through the C tiers already in place; the only Perl
8 * frames are the hooks, guards, controllers and mounted PSGI apps. The
9 * boot-time half of Punk::App (the registrar and compile) stays Perl.
10 *
11 * compile() freezes a `state` hash and returns sub { Punk::_serve($state,
12 * $_[0]) }. A nonblocking Future is handed to Punk::App::_finish_future
13 * (building the ->then chain is Perl's job); everything else finishes here.
14 *
15 * Must be included after punk_route.h, punk_dispatch.h and punk_context.h.
16 */
17  
18 #ifndef PUNK_SERVE_H
19 #define PUNK_SERVE_H
20  
21 static const char PS_ERR_404[] = "{\"errors\":[{\"message\":\"Not Found\"}]}";
22 static const char PS_ERR_403[] = "{\"errors\":[{\"message\":\"Forbidden\"}]}";
23 static const char PS_ERR_405[] =
24 "{\"errors\":[{\"message\":\"Method Not Allowed\"}]}";
25  
26 /* the static mount's C body and its recogniser, defined in punk_static.h,
27 * which is included after this file (it needs the dispatcher's helpers) */
28 static int punk_static_fast_ok(pTHX_ SV *mapp, AV **cap_out);
29 static SV *punk_static_in(pTHX_ AV *cap, HV *env, const char *path,
30 STRLEN plen, int is_head);
31  
32 /*
33 # uncoverable statement
34 # uncoverable subroutine */
35 0 static SV *ps_state(pTHX_ HV *st, const char *k) {
36 0 SV **e = hv_fetch(st, k, (I32)strlen(k), 0); /* # uncoverable statement */
37 /*
38 # uncoverable statement
39 # uncoverable condition left count:1
40 # uncoverable condition right count:1
41 # uncoverable condition left count:2
42 # uncoverable condition right count:2 */
43 0 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 0 0 0
return (e && *e) ? *e : NULL; 0 0 0
44 }
45 /*
46 # uncoverable statement
47 # uncoverable subroutine */
48 0 static AV *ps_state_av(pTHX_ HV *st, const char *k) {
49 0 SV *v = ps_state(aTHX_ st, k); /* # uncoverable statement */
50 /*
51 # uncoverable statement
52 # uncoverable condition left count:1
53 # uncoverable condition right count:1
54 # uncoverable condition left count:2
55 # uncoverable condition right count:2
56 # uncoverable condition left count:3
57 # uncoverable condition right count:3 */
58 0 ▶ return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 0 0 0
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 0 0 0
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) ? (AV *)SvRV(v) : NULL; 0 0 0
59 }
60  
61 /* env{$key} as a borrowed const char*, or (def,*len). */
62 443084 static const char *ps_env_str(pTHX_ HV *env, const char *k, const char *def,
63 STRLEN *len) {
64 443084 SV **e = hv_fetch(env, k, (I32)strlen(k), 0);
65 /*
66 # uncoverable branch false
67 # uncoverable condition right count:1 class:ignore_covered_err
68 # uncoverable condition right count:2 class:ignore_covered_err
69 # uncoverable condition right count:3 class:ignore_covered_err
70 # uncoverable condition right count:4 class:ignore_covered_err */
71 886168 ▶ if (e && *e && SvOK(*e) && SvCUR(*e)) return SvPV_const(*e, *len);
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e) && SvCUR(*e)) return SvPV_const(*e, *len); 443084 0 443084
if (e && *e && SvOK(*e) && SvCUR(*e)) return SvPV_const(*e, *len); 443084 0 443084
if (e && *e && SvOK(*e) && SvCUR(*e)) return SvPV_const(*e, *len); 443084 0 443084
if (e && *e && SvOK(*e) && SvCUR(*e)) return SvPV_const(*e, *len); 443084 0 443084
72 0 *len = strlen(def); /* # uncoverable statement */
73 0 return def; /* # uncoverable statement */
74 }
75  
76 /* Bless a fresh context AV (the PCX_* layout from punk_context.h: 0=env,
77 * 1=app, 6=match) into the app's context class. match is owned. */
78 221247 static SV *ps_ctx(pTHX_ pk_state *S, HV *env, SV *match) {
79 221247 AV *av = newAV();
80 /* room for every slot the plugins fill (request id, canon), so the
81 * array is not regrown twice per request */
82 221247 av_extend(av, PCX_T0);
83 221247 (void)av_store(av, PCX_ENV, newRV_inc((SV *)env));
84 221247 (void)av_store(av, PCX_APP, newSVsv(S->app));
85 221247 (void)av_store(av, PCX_MATCH, match);
86 221247 return sv_bless(newRV_noinc((SV *)av), S->ctxstash);
87 }
88  
89 /* The request context, built once.
90 *
91 * When a before_request hook has already made one, the routed match is stored
92 * into its PCX_MATCH slot rather than a second context being built: a stash
93 * written before routing has to survive into the handler, and it does not if
94 * the handler is handed a different object. match is owned; *cp is the
95 * caller's context slot, NULL until there is one.
96 *
97 * Do NOT decrement the match being replaced. av_store already frees the
98 * element it overwrites, so a dec here is a double free - one that surfaces
99 * later in the request as unrelated corruption. The only manual dec is for
100 * the store that did not take. */
101 /* Every path that needs a context comes through here, built or reused, which
102 * is why pk_abi's current_of is set here and not at the four call sites. The
103 * restore is punk_serve's, on the save stack. */
104 221250 static SV *ps_ctx_for(pTHX_ SV **cp, pk_state *S, HV *env, SV *match) {
105 221250 ▶ if (*cp) {
Branch: 1 branch
BranchTrueFalseTotal
if (*cp) { 3 221247 221250
106 3 AV *av = (AV *)SvRV(*cp);
107 /*
108 # uncoverable branch true
109 # uncoverable condition left count:1 class:ignore_covered_err */
110 3 ▶ if (!av_store(av, PCX_MATCH, match)) SvREFCNT_dec(match);
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store(av, PCX_MATCH, match)) SvREFCNT_dec(match); 0 3 3
111 3 pk_cur_set(aTHX_ *cp);
112 3 return *cp;
113 }
114 221247 *cp = sv_2mortal(ps_ctx(aTHX_ S, env, match));
115 221247 pk_cur_set(aTHX_ *cp);
116 221247 return *cp;
117 }
118  
119 /* The match hash the routed request fills in. When a context already exists
120 * (an observer or a before_request hook built it ahead of routing) its match
121 * hash is reused rather than a second one built and the first thrown away:
122 * the hook's hash gains `route` and the captures, which is what $c->match
123 * has always documented. Returns the hash to fill; *cp ends up pointing at
124 * the context that owns it. */
125 220884 static HV *ps_match_for(pTHX_ SV **cp, pk_state *S, HV *env) {
126 220884 ▶ if (*cp) {
Branch: 1 branch
BranchTrueFalseTotal
if (*cp) { 227 220657 220884
127 227 AV *av = (AV *)SvRV(*cp);
128 227 SV *m = pcx_get(aTHX_ av, PCX_MATCH);
129 /*
130 # uncoverable branch false
131 # uncoverable condition right count:1 class:ignore_covered_err
132 # uncoverable condition right count:2 class:ignore_covered_err
133 # uncoverable condition right count:3 class:ignore_covered_err */
134 227 ▶ if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 227 0 227
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 227 0 227
if (m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV) { 227 0 227
135 227 pk_cur_set(aTHX_ *cp);
136 227 return (HV *)SvRV(m);
137 }
138 }
139 {
140 220657 HV *match = newHV();
141 220657 (void)ps_ctx_for(aTHX_ cp, S, env, newRV_noinc((SV *)match));
142 220657 return match;
143 }
144 }
145  
146 /* May a response say what went wrong? In development, where the person
147 * reading it is the one who can fix it, and under test, where the reader is
148 * an assertion. Asked on the error path only, so a method call is
149 * affordable. An application with no answer is in production. */
150 2189 static int ps_app_is_dev(pTHX_ SV *c) {
151 /*
152 # uncoverable condition right count:1 class:ignore_covered_err
153 # uncoverable condition right count:2 class:ignore_covered_err */
154 2189 ▶ AV *av = (c && SvROK(c)) ? pcx_av(aTHX_ c) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *av = (c && SvROK(c)) ? pcx_av(aTHX_ c) : NULL; 2189 0 2189
AV *av = (c && SvROK(c)) ? pcx_av(aTHX_ c) : NULL; 2189 0 2189
155 2189 ▶ SV *app = av ? pcx_get(aTHX_ av, PCX_APP) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *app = av ? pcx_get(aTHX_ av, PCX_APP) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2189 0 2189
156 SV *env;
157 2189 int dev = 0;
158 /*
159 # uncoverable branch true
160 # uncoverable condition left count:1 class:ignore_covered_err
161 # uncoverable condition left count:2 class:ignore_covered_err
162 # uncoverable condition left count:3 class:ignore_covered_err */
163 2189 ▶ if (!(app && SvROK(app) && sv_isobject(app))) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && sv_isobject(app))) return 0; 2189 0 2189
if (!(app && SvROK(app) && sv_isobject(app))) return 0; 2189 0 2189
if (!(app && SvROK(app) && sv_isobject(app))) return 0; 0 2189 2189
164 2189 env = pcx_call_meth(aTHX_ app, "env", NULL, 0, 1);
165 /*
166 # uncoverable branch false
167 # uncoverable condition right count:1 class:ignore_covered_err */
168 2189 ▶ if (env) {
Branch: 1 branch
BranchTrueFalseTotal
if (env) { 2189 0 2189
169 /*
170 # uncoverable branch false
171 # uncoverable condition right count:1 class:ignore_covered_err */
172 2189 ▶ if (SvOK(env)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(env)) { 2189 0 2189
173 STRLEN el;
174 2189 const char *ep = SvPV_const(env, el);
175 2191 ▶ dev = (el == 11 && memEQ(ep, "development", 11))
Branch: 1 branch
BranchTrueFalseTotal
dev = (el == 11 && memEQ(ep, "development", 11)) 0 2 2
176 2191 ▶ || (el == 4 && memEQ(ep, "test", 4));
Branch: 3 branches
BranchTrueFalseTotal
|| (el == 4 && memEQ(ep, "test", 4)); 2 2187 2189
|| (el == 4 && memEQ(ep, "test", 4)); 2183 4 2187
|| (el == 4 && memEQ(ep, "test", 4)); 2182 1 2183
177 }
178 2189 SvREFCNT_dec(env);
179 }
180 2189 return dev;
181 }
182  
183 /* An error line on the request's logger, the way $c->log->error would write
184 * it: with the method, the path and the request id the rest of the log has. */
185 5 static void ps_log_error(pTHX_ SV *c, const char *msg, STRLEN len) {
186 /*
187 # uncoverable condition right count:1 class:ignore_covered_err
188 # uncoverable condition right count:2 class:ignore_covered_err */
189 5 ▶ AV *av = (c && SvROK(c)) ? pcx_av(aTHX_ c) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
AV *av = (c && SvROK(c)) ? pcx_av(aTHX_ c) : NULL; 5 0 5
AV *av = (c && SvROK(c)) ? pcx_av(aTHX_ c) : NULL; 5 0 5
190 SV *st, *lg;
191 HV *stash;
192 SV **cached;
193 /*
194 # uncoverable branch true
195 # uncoverable condition left count:1 class:ignore_covered_err */
196 5 ▶ if (!av) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return; 0 5 5
197 /*
198 # uncoverable condition right count:1 class:ignore_covered_err
199 # uncoverable condition left count:3 class:ignore_covered_err */
200 9 ▶ while (len && (msg[len - 1] == '\n' || msg[len - 1] == '\r')) len--;
Branch: 3 branches
BranchTrueFalseTotal
while (len && (msg[len - 1] == '\n' || msg[len - 1] == '\r')) len--; 9 0 9
while (len && (msg[len - 1] == '\n' || msg[len - 1] == '\r')) len--; 4 5 9
while (len && (msg[len - 1] == '\n' || msg[len - 1] == '\r')) len--; 0 5 5
201 5 st = pcx_get(aTHX_ av, PCX_STASH);
202 /*
203 # uncoverable branch false
204 # uncoverable condition right count:1 class:ignore_covered_err */
205 5 ▶ if (!st) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { 5 0 5
206 5 st = newRV_noinc((SV *)newHV());
207 5 (void)av_store(av, PCX_STASH, st);
208 }
209 5 stash = (HV *)SvRV(st);
210 5 cached = hv_fetchs(stash, "punk.logger", 0);
211 /*
212 # uncoverable branch true
213 # uncoverable condition left count:1 class:ignore_covered_err
214 # uncoverable condition left count:2 class:ignore_covered_err
215 # uncoverable condition right count:2 class:ignore_covered_err
216 # uncoverable condition left count:3 class:ignore_covered_err
217 # uncoverable condition right count:3 class:ignore_covered_err */
218 5 ▶ if (cached && *cached && SvROK(*cached)) lg = *cached;
Branch: 3 branches
BranchTrueFalseTotal
if (cached && *cached && SvROK(*cached)) lg = *cached; 0 5 5
if (cached && *cached && SvROK(*cached)) lg = *cached; 0 0 0
if (cached && *cached && SvROK(*cached)) lg = *cached; 0 0 0
219 else {
220 5 lg = pl_make_logger(aTHX_ pcx_get(aTHX_ av, PCX_APP), c);
221 5 (void)hv_stores(stash, "punk.logger", newSVsv(lg));
222 5 lg = *hv_fetchs(stash, "punk.logger", 0);
223 }
224 5 pl_emit(aTHX_ lg, PL_ERROR, sv_2mortal(newSVpvn(msg, len)), NULL);
225 }
226  
227 /* $on_error->($c,$err) coerced, or the 500 default (+1). */
228 2201 static SV *punk_handle_error(pTHX_ SV *c, SV *err, SV *on_error) {
229 HV *body, *m; AV *errs; SV *bytes; STRLEN l; const char *p;
230 2201 ▶ if (on_error && SvOK(on_error)) {
Branch: 2 branches
BranchTrueFalseTotal
if (on_error && SvOK(on_error)) { 2188 13 2201
if (on_error && SvOK(on_error)) { 12 2176 2188
231 12 int died = 0;
232 12 SV *r = pcx_call2(aTHX_ on_error, c, err, &died);
233 /*
234 # uncoverable branch false
235 # uncoverable condition right count:1 class:ignore_covered_err
236 # uncoverable condition right count:2 class:ignore_covered_err
237 # uncoverable condition right count:3 class:ignore_covered_err */
238 12 ▶ if (!died && r && SvROK(r)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!died && r && SvROK(r)) { 12 0 12
if (!died && r && SvROK(r)) { 12 0 12
if (!died && r && SvROK(r)) { 12 0 12
239 12 SV *co = punk_coerce(aTHX_ c, r);
240 12 SvREFCNT_dec(r);
241 12 return co;
242 }
243 /*
244 # uncoverable statement
245 # uncoverable branch true
246 # uncoverable branch false
247 # uncoverable condition left count:1
248 # uncoverable condition right count:1 */
249 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
250 }
251 2189 body = newHV(); errs = newAV(); m = newHV();
252 4378 ▶ p = SvOK(err) ? SvPV_const(err, l) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
p = SvOK(err) ? SvPV_const(err, l) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2189 0 2189
253 /*
254 # uncoverable branch true
255 # uncoverable condition left count:1 class:ignore_covered_err */
256 2189 ▶ if (!SvOK(err)) l = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(err)) l = 0; 0 2189 2189
257 /* What a handler died with is for whoever runs the application: a file
258 * and a line, and from a database the statement, the DSN and sometimes
259 * the row. In development that is the person looking at the response.
260 * Anywhere else it goes to the log, at error, and the client is told
261 * only that it was not their fault. */
262 2189 ▶ if (!ps_app_is_dev(aTHX_ c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_app_is_dev(aTHX_ c)) { 5 2184 2189
263 5 ps_log_error(aTHX_ c, p, l);
264 5 p = "Internal Server Error";
265 5 l = 21;
266 }
267 2189 (void)hv_stores(m, "message", newSVpvn(p, l));
268 2189 av_push(errs, newRV_noinc((SV *)m));
269 2189 (void)hv_stores(body, "errors", newRV_noinc((SV *)errs));
270 2189 bytes = punk_frj(aTHX)->encode(aTHX_ sv_2mortal(newRV_noinc((SV *)body)), NULL);
271 2189 return punk_triplet(aTHX_ 500, sv_2mortal(newSVpvs("application/json")),
272 bytes, NULL);
273 }
274  
275 /* error -> handle_error; nonblocking Future -> Punk::App::_finish_future;
276 * else coerce; then deliver (+1). ret/err are borrowed. */
277 220964 static SV *punk_finish_c(pTHX_ SV *app, SV *c, SV *ret, SV *err, SV *on_error,
278 const char *method, STRLEN mlen, AV *after, HV *env) {
279 220964 ▶ int is_head = (mlen == 4 && memEQ(method, "HEAD", 4));
Branch: 2 branches
BranchTrueFalseTotal
int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); 3129 217835 220964
int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); 21 3108 3129
280 SV *resp, *out;
281 /*
282 # uncoverable condition right count:1 class:ignore_covered_err
283 # uncoverable condition right count:3 class:ignore_covered_err */
284 220964 ▶ if (err && SvOK(err) && SvTRUE(err))
Branch: 3 branches
BranchTrueFalseTotal
if (err && SvOK(err) && SvTRUE(err)) 220964 0 220964
if (err && SvOK(err) && SvTRUE(err)) 2187 218777 220964
if (err && SvOK(err) && SvTRUE(err)) 2187 0 2187
285 2187 resp = punk_handle_error(aTHX_ c, err, on_error);
286 218777 ▶ else if (SvROK(ret) && SvOBJECT(SvRV(ret)) && pcx_can(aTHX_ ret, "on_ready")) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
else if (SvROK(ret) && SvOBJECT(SvRV(ret)) && pcx_can(aTHX_ ret, "on_ready")) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 218777 0 218777
else if (SvROK(ret) && SvOBJECT(SvRV(ret)) && pcx_can(aTHX_ ret, "on_ready")) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 72 218705 218777
else if (SvROK(ret) && SvOBJECT(SvRV(ret)) && pcx_can(aTHX_ ret, "on_ready")) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 51 21 72
287 51 dSP; SV *r; int count;
288 51 ENTER; SAVETMPS;
289 51 ▶ PUSHMARK(SP); EXTEND(SP, 6);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 6); 0 51 51
PUSHMARK(SP); EXTEND(SP, 6); 0 51 51
290 51 PUSHs(app);
291 51 PUSHs(c);
292 51 PUSHs(ret);
293 51 ▶ PUSHs(on_error ? on_error : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(on_error ? on_error : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 51 0 51
294 51 PUSHs(sv_2mortal(newSVpvn(method, mlen)));
295 51 ▶ PUSHs(sv_2mortal(newRV_inc((SV *)(after ? after : newAV())))); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(sv_2mortal(newRV_inc((SV *)(after ? after : newAV())))); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 51 51
296 51 PUTBACK;
297 51 count = call_method("_finish_future", G_SCALAR);
298 51 SPAGAIN;
299 51 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 51 0 51
300 51 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 51 0 51
301 51 return r;
302 }
303 218726 else resp = punk_coerce(aTHX_ c, ret);
304 220913 out = punk_deliver(aTHX_ c, resp, is_head, after);
305 220913 SvREFCNT_dec(resp);
306 220913 return out;
307 }
308  
309 /* A constant-body 404/405 triplet, with an optional Allow header (owned). */
310 322 static SV *ps_err_triplet(pTHX_ IV status, const char *b, STRLEN bl, SV *allow) {
311 322 AV *resp = newAV(), *headers = newAV(), *bodyav = newAV();
312 322 av_push(headers, newSVpvs("Content-Type"));
313 322 av_push(headers, newSVpvs("application/json"));
314 322 av_push(headers, newSVpvs("Content-Length"));
315 322 av_push(headers, newSViv((IV)bl));
316 322 ▶ if (allow) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow) { 32 290 322
317 32 av_push(headers, newSVpvs("Allow"));
318 32 av_push(headers, allow);
319 }
320 322 av_push(bodyav, newSVpvn(b, bl));
321 322 av_push(resp, newSViv(status));
322 322 av_push(resp, newRV_noinc((SV *)headers));
323 322 av_push(resp, newRV_noinc((SV *)bodyav));
324 322 return newRV_noinc((SV *)resp);
325 }
326  
327 /* The conditional-GET check, run between a route's guards and its handler.
328 * Defined in punk_cget.h, which is included after punk_sendfile.h because it
329 * uses that file's If-None-Match comparison rather than a second copy of it -
330 * the same split ps_serve_file uses. */
331 static int pcg_check(pTHX_ SV *c, HV *rech, HV *env, const char *method,
332 STRLEN mlen, SV **out, SV **errp);
333  
334 /* The idempotency replay check, in the same slot. Defined in punk_idem.h,
335 * which is included after punk_cachefront.h because the store is where the
336 * keys live. */
337 static int pi_check(pTHX_ SV *c, HV *rech, HV *env, const char *method,
338 STRLEN mlen, SV *cfg, SV **out, SV **errp);
339  
340 /* does path (plen) start with prefix (pfl) on a segment boundary? */
341 443 static int ps_under(const char *path, STRLEN plen, const char *pf, STRLEN pfl) {
342 425 ▶ return plen >= pfl && memEQ(path, pf, pfl)
Branch: 1 branch
BranchTrueFalseTotal
return plen >= pfl && memEQ(path, pf, pfl) 355 70 425
343 868 ▶ && (plen == pfl || path[pfl] == '/');
Branch: 3 branches
BranchTrueFalseTotal
&& (plen == pfl || path[pfl] == '/'); 425 18 443
&& (plen == pfl || path[pfl] == '/'); 339 16 355
&& (plen == pfl || path[pfl] == '/'); 335 4 339
344 }
345  
346 221542 static SV *ps_serve_one(pTHX_ pk_state *S, HV *env) {
347 221542 AV *recs = S->recs;
348 221542 AV *apims = S->apims;
349 221542 AV *mounts = S->mounts;
350 221542 SV *app = S->app;
351 221542 AV *before = S->before;
352 221542 AV *after = S->after;
353 221542 SV *on_err = S->on_err;
354 221542 pr_router *rt = S->rt;
355 221542 IV ridx = -1; /* the matched record's index */
356  
357 STRLEN mlen, plen;
358 const char *method;
359 221542 int ext_connect = 0;
360  
361 /* Where a large multipart part spills. The parser has the request's env
362 * and nothing else - the env belongs to the server - so the one place
363 * this can be handed over is here, and only when an application named
364 * a directory. */
365 221542 ▶ if (S->upload_dir)
Branch: 1 branch
BranchTrueFalseTotal
if (S->upload_dir) 2065 219477 221542
366 2065 (void)hv_stores(env, "punk.upload_dir", newSVsv(S->upload_dir));
367  
368 /* Reverse-proxy trust, before anything reads the env: REMOTE_ADDR is
369 * rewritten to the real client so rate_limit, block_ip, the access log
370 * and $c->req all see it without any of them being changed. */
371 221542 ▶ if (S->proxy) pp_resolve(aTHX_ S->proxy, env);
Branch: 1 branch
BranchTrueFalseTotal
if (S->proxy) pp_resolve(aTHX_ S->proxy, env); 40 221502 221542
372  
373 221542 method = ps_env_str(aTHX_ env, "REQUEST_METHOD", "GET", &mlen);
374  
375 /* An Extended CONNECT (RFC 8441 / RFC 9220) is routed as the GET it
376 * stands in for.
377 *
378 * A `websocket` route registers as a GET route - it is a GET upgrade on
379 * HTTP/1.1 - so a CONNECT would match nothing and the client would get a
380 * 405 for a request that is a websocket handshake. That the pseudo
381 * request carries :scheme and :path at all, which an ordinary CONNECT
382 * does not, is exactly so it can be routed this way.
383 *
384 * Only the METHOD USED FOR MATCHING changes. REQUEST_METHOD in $env
385 * still reads CONNECT, which is what punk_ws_dispatch keys on and what
386 * an application should see.
387 *
388 * `ext_connect` is what the rewrite costs and has to pay back. Matching
389 * as GET reaches every GET route, every API operation and every mount,
390 * while the origin check lives in punk_ws_dispatch and so runs only once
391 * a `ws` route has matched. On this transport a 2xx IS the acceptance,
392 * written as :status on the CONNECT stream, so a page anywhere could
393 * otherwise open a socket at any path and read open-or-error as that
394 * path's status - the cross-origin status read a browser exists to
395 * prevent, with the user's cookies attached. The flag is carried to the
396 * gate below, which is where a CONNECT that did not land on a websocket
397 * route stops. */
398 221542 ▶ if (mlen == 7 && memEQ(method, "CONNECT", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mlen == 7 && memEQ(method, "CONNECT", 7)) { 29 221513 221542
if (mlen == 7 && memEQ(method, "CONNECT", 7)) { 20 9 29
399 20 SV **cp = hv_fetchs(env, "psgix.connect_protocol", 0);
400 /*
401 # uncoverable condition right count:2 class:ignore_covered_err
402 # uncoverable condition right count:3 class:ignore_covered_err
403 # uncoverable condition right count:5 class:ignore_covered_err */
404 20 ▶ if (cp && *cp && SvOK(*cp) && SvCUR(*cp) == 9
Branch: 4 branches
BranchTrueFalseTotal
if (cp && *cp && SvOK(*cp) && SvCUR(*cp) == 9 17 3 20
if (cp && *cp && SvOK(*cp) && SvCUR(*cp) == 9 17 0 17
if (cp && *cp && SvOK(*cp) && SvCUR(*cp) == 9 17 0 17
if (cp && *cp && SvOK(*cp) && SvCUR(*cp) == 9 16 1 17
405 16 ▶ && memEQ(SvPVX(*cp), "websocket", 9)) {
Branch: 1 branch
BranchTrueFalseTotal
&& memEQ(SvPVX(*cp), "websocket", 9)) { 16 0 16
406 16 ext_connect = 1;
407 16 method = "GET";
408 16 mlen = 3;
409 }
410 }
411 221542 const char *path = ps_env_str(aTHX_ env, "PATH_INFO", "/", &plen);
412 221542 ▶ int is_head = (mlen == 4 && memEQ(method, "HEAD", 4));
Branch: 2 branches
BranchTrueFalseTotal
int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); 3258 218284 221542
int is_head = (mlen == 4 && memEQ(method, "HEAD", 4)); 24 3234 3258
413 221542 SV *rec = NULL, *caps = NULL;
414 221542 AV *api_allow = NULL;
415 221542 SV *c = NULL; /* the one context, once built */
416  
417 { /* before_request: the only phase that runs before routing, so the
418 * only one a 404, a 405 or a mount ever reaches. It runs here -
419 * after the proxy resolve, because a hook that logs or samples by
420 * client address must see the corrected REMOTE_ADDR the same way
421 * rate_limit and the access log do, and after the method/path reads,
422 * because a short-circuit needs them and they allocate nothing.
423 *
424 * Being first is also what it costs: this is ahead of the csrf
425 * check, ahead of rate_limit and ahead of the max_body ceiling, so
426 * the hook sees requests all three will refuse. For a span that is
427 * the point; for work done on the client's behalf, before_dispatch
428 * is still the right phase.
429 *
430 * compile omits the state key when the chain is empty, so an app
431 * with no such hook pays one hv_fetch and one branch. */
432 221542 AV *before_req = S->before_req;
433 221542 int has_hooks = before_req != NULL;
434  
435 /* The C ABI's observers register process-globally and run here, ahead
436 * of any Perl phase - a consumer measuring a request should start
437 * before the first frame this framework hands to an application.
438 * Registering either observer is what makes the context get built,
439 * which is the guarantee that the response side sees the same one. */
440 221542 ▶ if (PK_OBS_ANY || has_hooks) {
Branch: 2 branches
BranchTrueFalseTotal
if (PK_OBS_ANY || has_hooks) { 221063 479 221542
if (PK_OBS_ANY || has_hooks) { 26 221037 221063
441 505 HV *match = newHV();
442 /* a real hashref with empty captures, not undef, so $c->match and
443 * $c->param behave inside the hook instead of croaking */
444 505 (void)hv_stores(match, "captures", newRV_noinc((SV *)newHV()));
445 505 (void)ps_ctx_for(aTHX_ &c, S, env, newRV_noinc((SV *)match));
446 }
447 221542 ▶ if (PK_OBS_WANT_REQ) pk_obs_fire_req(aTHX_ c);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_REQ) pk_obs_fire_req(aTHX_ c); 479 221063 221542
448  
449 221542 ▶ if (has_hooks) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_hooks) { 26 221516 221542
450 26 SV *ret = &PL_sv_undef, *err = &PL_sv_undef;
451 26 ▶ if (pd_run_chain(aTHX_ before_req, c, &ret, &err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pd_run_chain(aTHX_ before_req, c, &ret, &err)) { 11 15 26
452 SV *out;
453 11 ▶ if (ext_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (ext_connect) { 4 7 11
454 /* A hook that answers for itself runs ahead of the gate
455 * below, and its answer to an upgrade would be the
456 * oracle that gate closes: a page elsewhere reading a
457 * path's status as a websocket open or error. So an
458 * Extended CONNECT it short-circuits gets the one fixed
459 * answer every non-websocket path gives. */
460 /*
461 # uncoverable branch false
462 # uncoverable condition right count:1 class:ignore_covered_err */
463 4 ▶ if (ret != &PL_sv_undef) SvREFCNT_dec(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (ret != &PL_sv_undef) SvREFCNT_dec(ret); 4 0 4
464 /*
465 # uncoverable branch true
466 # uncoverable condition left count:1 class:ignore_covered_err */
467 4 ▶ if (err != &PL_sv_undef) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err != &PL_sv_undef) SvREFCNT_dec(err); 0 4 4
468 4 out = ps_err_triplet(aTHX_ 404, PS_ERR_404,
469 sizeof(PS_ERR_404) - 1, NULL);
470 /*
471 # uncoverable branch true
472 # uncoverable condition left count:1 class:ignore_covered_err */
473 4 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out); 0 4 4
474 11 return out;
475 }
476 7 out = punk_finish_c(aTHX_ app, c, ret, err, on_err,
477 method, mlen, after, env);
478 7 ▶ if (ret != &PL_sv_undef) SvREFCNT_dec(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (ret != &PL_sv_undef) SvREFCNT_dec(ret); 6 1 7
479 7 ▶ if (err != &PL_sv_undef) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err != &PL_sv_undef) SvREFCNT_dec(err); 1 6 7
480 7 return out;
481 }
482 }
483 }
484  
485 /* 1. static exact hit */
486 /*
487 # uncoverable branch false class:ignore_covered_err
488 # uncoverable condition right count:1 class:ignore_covered_err */
489 221531 ▶ if (rt) {
Branch: 1 branch
BranchTrueFalseTotal
if (rt) { 221531 0 221531
490 221531 IV i = pr_static_match(aTHX_ rt, method, mlen, path, plen);
491 221531 ▶ if (i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= 0) { 113625 107906 221531
492 113625 SV **rp = av_fetch(recs, i, 0);
493 /*
494 # uncoverable branch false
495 # uncoverable condition right count:1 class:ignore_covered_err
496 # uncoverable condition right count:2 class:ignore_covered_err */
497 113625 ▶ if (rp && *rp) { rec = *rp; ridx = i; }
Branch: 2 branches
BranchTrueFalseTotal
if (rp && *rp) { rec = *rp; ridx = i; } 113625 0 113625
if (rp && *rp) { rec = *rp; ridx = i; } 113625 0 113625
498 }
499 }
500  
501 /* 2. API mounts.
502 *
503 * The COUNT is checked, not just the array: api_mounts is always present
504 * in the compiled state, so an app that declared no `api` at all still
505 * arrives here with an empty one - and punk_oa croaks when Open::API's
506 * ABI does not match. That made Open::API an effective hard requirement
507 * of the dynamic-route path for every app in the world, and the failure
508 * it produced named the API path, which the app was not using.
509 *
510 * An Extended CONNECT skips the whole loop. An OpenAPI operation is
511 * never a websocket route, so there is nothing here it can legitimately
512 * reach, and the gate below would refuse the answer anyway - after the
513 * operation's handler had already run. */
514 221531 ▶ if (!rec && !ext_connect && apims && av_len(apims) >= 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (!rec && !ext_connect && apims && av_len(apims) >= 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 107906 113625 221531
if (!rec && !ext_connect && apims && av_len(apims) >= 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 107901 5 107906
if (!rec && !ext_connect && apims && av_len(apims) >= 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 107901 0 107901
if (!rec && !ext_connect && apims && av_len(apims) >= 0) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 100 107801 107901
515 100 const oa_abi *A = punk_oa(aTHX);
516 100 SSize_t ai, an = av_len(apims) + 1;
517 121 ▶ for (ai = 0; ai < an; ai++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai < an; ai++) { 100 21 121
518 100 SV **mp = av_fetch(apims, ai, 0);
519 HV *am; SV *api; SV **x;
520 const char *pf, *rest; STRLEN pfl, restl;
521 void *api_c, *op; HV *capsh; AV *allowv; SV *opid; HE *he; SV *oprec;
522 IV maxb; SV *mon_err; HV *ops;
523 /*
524 # uncoverable branch true class:ignore_covered_err
525 # uncoverable condition left count:1 class:ignore_covered_err
526 # uncoverable condition left count:2 class:ignore_covered_err
527 # uncoverable condition left count:3 class:ignore_covered_err */
528 121 ▶ if (!mp || !*mp || !SvROK(*mp)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!mp || !*mp || !SvROK(*mp)) continue; 100 0 100
if (!mp || !*mp || !SvROK(*mp)) continue; 100 0 100
if (!mp || !*mp || !SvROK(*mp)) continue; 0 100 100
529 100 am = (HV *)SvRV(*mp);
530 100 x = hv_fetchs(am, "prefix", 0);
531 /*
532 # uncoverable condition right count:1 class:ignore_covered_err
533 # uncoverable condition right count:2 class:ignore_covered_err
534 # uncoverable condition right count:3 class:ignore_covered_err */
535 200 ▶ if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; } 100 0 100
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; } 100 0 100
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; } 100 0 100
536 100 ▶ if (!ps_under(path, plen, pf, pfl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_under(path, plen, pf, pfl)) continue; 6 94 100
537 94 ▶ rest = plen > pfl ? path + pfl : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
rest = plen > pfl ? path + pfl : "/"; /* # uncoverable condition right count:1 class:ignore_covered_err */ 94 0 94
538 94 ▶ restl = plen > pfl ? plen - pfl : 1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
restl = plen > pfl ? plen - pfl : 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 94 0 94
539 94 api = *hv_fetchs(am, "api", 0);
540 94 api_c = A->api_of(aTHX_ api);
541 94 capsh = (HV *)sv_2mortal((SV *)newHV());
542 94 allowv = (AV *)sv_2mortal((SV *)newAV());
543 94 ▶ op = api_c ? A->route(aTHX_ api_c, method, mlen, rest, restl, capsh, allowv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
op = api_c ? A->route(aTHX_ api_c, method, mlen, rest, restl, capsh, allowv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 94 0 94
544 94 ▶ if (!op && is_head)
Branch: 2 branches
BranchTrueFalseTotal
if (!op && is_head) 16 78 94
if (!op && is_head) 1 15 16
545 1 ▶ op = api_c ? A->route(aTHX_ api_c, "GET", 3, rest, restl, capsh, allowv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
op = api_c ? A->route(aTHX_ api_c, "GET", 3, rest, restl, capsh, allowv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 0 1
546 94 ▶ if (!op) {
Branch: 1 branch
BranchTrueFalseTotal
if (!op) { 15 79 94
547 15 ▶ if (av_len(allowv) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(allowv) >= 0) { 2 13 15
548 2 SSize_t j, n = av_len(allowv) + 1;
549 /*
550 # uncoverable branch false
551 # uncoverable condition right count:1 class:ignore_covered_err */
552 2 ▶ if (!api_allow) api_allow = (AV *)sv_2mortal((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
if (!api_allow) api_allow = (AV *)sv_2mortal((SV *)newAV()); 2 0 2
553 6 ▶ for (j = 0; j < n; j++) { SV **a = av_fetch(allowv, j, 0); /* # uncoverable branch true count:2 */
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { SV **a = av_fetch(allowv, j, 0); /* # uncoverable branch true count:2 */ 4 2 6
554 /*
555 # uncoverable branch false class:ignore_covered_err
556 # uncoverable condition right count:1 class:ignore_covered_err
557 # uncoverable condition right count:2 class:ignore_covered_err */
558 4 ▶ if (a && *a) av_push(api_allow, newSVsv(*a)); }
Branch: 2 branches
BranchTrueFalseTotal
if (a && *a) av_push(api_allow, newSVsv(*a)); } 4 0 4
if (a && *a) av_push(api_allow, newSVsv(*a)); } 4 0 4
559 }
560 15 continue;
561 }
562 79 opid = A->op_id(aTHX_ op);
563 79 x = hv_fetchs(am, "ops", 0);
564 /*
565 # uncoverable condition right count:1 class:ignore_covered_err
566 # uncoverable condition right count:2 class:ignore_covered_err
567 # uncoverable condition right count:3 class:ignore_covered_err */
568 79 ▶ ops = (x && *x && SvROK(*x)) ? (HV *)SvRV(*x) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
ops = (x && *x && SvROK(*x)) ? (HV *)SvRV(*x) : NULL; 79 0 79
ops = (x && *x && SvROK(*x)) ? (HV *)SvRV(*x) : NULL; 79 0 79
ops = (x && *x && SvROK(*x)) ? (HV *)SvRV(*x) : NULL; 79 0 79
569 79 ▶ he = ops ? hv_fetch_ent(ops, opid, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
he = ops ? hv_fetch_ent(ops, opid, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 79 0 79
570 79 ▶ oprec = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
oprec = he ? HeVAL(he) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 79 0 79
571 79 x = hv_fetchs(am, "max_body", 0);
572 /*
573 # uncoverable condition right count:1 class:ignore_covered_err
574 # uncoverable condition right count:2 class:ignore_covered_err
575 # uncoverable condition right count:3 class:ignore_covered_err */
576 79 ▶ maxb = (x && *x && SvOK(*x)) ? SvIV(*x) : 0;
Branch: 3 branches
BranchTrueFalseTotal
maxb = (x && *x && SvOK(*x)) ? SvIV(*x) : 0; 79 0 79
maxb = (x && *x && SvOK(*x)) ? SvIV(*x) : 0; 79 0 79
maxb = (x && *x && SvOK(*x)) ? SvIV(*x) : 0; 79 0 79
577 79 x = hv_fetchs(am, "on_error", 0);
578 /*
579 # uncoverable condition right count:1 class:ignore_covered_err
580 # uncoverable condition right count:2 class:ignore_covered_err */
581 79 ▶ mon_err = (x && *x && SvOK(*x)) ? *x : on_err;
Branch: 3 branches
BranchTrueFalseTotal
mon_err = (x && *x && SvOK(*x)) ? *x : on_err; 79 0 79
mon_err = (x && *x && SvOK(*x)) ? *x : on_err; 79 0 79
mon_err = (x && *x && SvOK(*x)) ? *x : on_err; 3 76 79
582 {
583 79 HV *match = newHV();
584 79 SV *ret = &PL_sv_undef, *err = &PL_sv_undef, *out;
585 79 (void)hv_stores(match, "captures", newRV_inc((SV *)capsh));
586 79 (void)hv_stores(match, "operation", newSVsv(opid));
587 79 (void)ps_ctx_for(aTHX_ &c, S, env, newRV_noinc((SV *)match));
588 /*
589 # uncoverable branch false
590 # uncoverable condition right count:1 class:ignore_covered_err
591 # uncoverable condition right count:2 class:ignore_covered_err
592 # uncoverable condition right count:3 class:ignore_covered_err */
593 79 ▶ if (oprec && SvROK(oprec) && SvTYPE(SvRV(oprec)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (oprec && SvROK(oprec) && SvTYPE(SvRV(oprec)) == SVt_PVHV) 79 0 79
if (oprec && SvROK(oprec) && SvTYPE(SvRV(oprec)) == SVt_PVHV) 79 0 79
if (oprec && SvROK(oprec) && SvTYPE(SvRV(oprec)) == SVt_PVHV) 79 0 79
594 79 punk_oa_dispatch(aTHX_ c, before, (HV *)SvRV(oprec), api, opid,
595 sv_2mortal(newRV_inc((SV *)capsh)), maxb, &ret, &err);
596 79 out = punk_finish_c(aTHX_ app, c, ret, err, mon_err, method, mlen, after, env);
597 79 ▶ if (ret != &PL_sv_undef) SvREFCNT_dec(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (ret != &PL_sv_undef) SvREFCNT_dec(ret); 78 1 79
598 79 ▶ if (err != &PL_sv_undef) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err != &PL_sv_undef) SvREFCNT_dec(err); 1 78 79
599 79 return out;
600 }
601 }
602 }
603  
604 /* 3. PSGI / static mounts.
605 *
606 * Also skipped on an Extended CONNECT, and for a sharper reason than the
607 * API mounts: a mounted app owns its answer and never comes back through
608 * the gate below, so delegating one would hand a request Punk's own
609 * router is about to refuse to an app that sees REQUEST_METHOD CONNECT
610 * and answers on path alone. A mounted PUNK app with websocket routes of
611 * its own is the case this gives up; it is not reachable over HTTP/2 or
612 * HTTP/3 until a mount can declare that it wants the handshake. */
613 221452 ▶ if (!rec && !ext_connect && mounts) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!rec && !ext_connect && mounts) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 107827 113625 221452
if (!rec && !ext_connect && mounts) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 107822 5 107827
if (!rec && !ext_connect && mounts) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 107822 0 107822
614 107822 SSize_t mi, mn = av_len(mounts) + 1;
615 107898 ▶ for (mi = 0; mi < mn; mi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (mi = 0; mi < mn; mi++) { 321 107577 107898
616 321 SV **mp = av_fetch(mounts, mi, 0);
617 HV *m; SV **x; const char *pf; STRLEN pfl; SV *mapp;
618 /*
619 # uncoverable branch true class:ignore_covered_err
620 # uncoverable condition left count:1 class:ignore_covered_err
621 # uncoverable condition left count:2 class:ignore_covered_err
622 # uncoverable condition left count:3 class:ignore_covered_err */
623 397 ▶ if (!mp || !*mp || !SvROK(*mp)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!mp || !*mp || !SvROK(*mp)) continue; 321 0 321
if (!mp || !*mp || !SvROK(*mp)) continue; 321 0 321
if (!mp || !*mp || !SvROK(*mp)) continue; 0 321 321
624 321 m = (HV *)SvRV(*mp);
625 321 x = hv_fetchs(m, "prefix", 0);
626 /*
627 # uncoverable condition right count:1 class:ignore_covered_err
628 # uncoverable condition right count:2 class:ignore_covered_err
629 # uncoverable condition right count:3 class:ignore_covered_err */
630 642 ▶ if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; } 321 0 321
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; } 321 0 321
if (x && *x && SvOK(*x)) pf = SvPV_const(*x, pfl); else { pf = ""; pfl = 0; } 321 0 321
631 321 ▶ if (!ps_under(path, plen, pf, pfl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_under(path, plen, pf, pfl)) continue; 76 245 321
632 245 mapp = *hv_fetchs(m, "app", 0);
633 { /* A `static` mount of this framework's own, answering a GET
634 * or HEAD: its C body, handed the sub-path. The env rewrite
635 * and the Perl frame below exist for a mounted PSGI app;
636 * this one reads neither SCRIPT_NAME nor PATH_INFO. */
637 AV *scap;
638 245 ▶ if ((is_head || (mlen == 3 && memEQ(method, "GET", 3)))
Branch: 3 branches
BranchTrueFalseTotal
if ((is_head || (mlen == 3 && memEQ(method, "GET", 3))) 243 2 245
if ((is_head || (mlen == 3 && memEQ(method, "GET", 3))) 235 8 243
if ((is_head || (mlen == 3 && memEQ(method, "GET", 3))) 234 1 235
639 236 ▶ && punk_static_fast_ok(aTHX_ mapp, &scap)) {
Branch: 1 branch
BranchTrueFalseTotal
&& punk_static_fast_ok(aTHX_ mapp, &scap)) { 85 151 236
640 85 SV *r = punk_static_in(aTHX_ scap, env,
641 85 ▶ plen > pfl ? path + pfl : "/", /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
plen > pfl ? path + pfl : "/", /* # uncoverable condition right count:1 class:ignore_covered_err */ 85 0 85
642 85 ▶ plen > pfl ? plen - pfl : 1, /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
plen > pfl ? plen - pfl : 1, /* # uncoverable condition right count:1 class:ignore_covered_err */ 85 0 85
643 is_head);
644 85 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r); 5 80 85
645 85 return r;
646 }
647 }
648 {
649 SV *save_sn, *save_pi, *newsn, *newpi, *r;
650 160 SV **sn = hv_fetchs(env, "SCRIPT_NAME", 0);
651 160 SV **pi = hv_fetchs(env, "PATH_INFO", 0);
652 160 dSP; int count;
653 160 ▶ save_sn = (sn && *sn) ? newSVsv(*sn) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
save_sn = (sn && *sn) ? newSVsv(*sn) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 155 160
save_sn = (sn && *sn) ? newSVsv(*sn) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
654 /*
655 # uncoverable condition right count:1 class:ignore_covered_err
656 # uncoverable condition right count:2 class:ignore_covered_err */
657 160 ▶ save_pi = (pi && *pi) ? newSVsv(*pi) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
save_pi = (pi && *pi) ? newSVsv(*pi) : NULL; 160 0 160
save_pi = (pi && *pi) ? newSVsv(*pi) : NULL; 160 0 160
658 160 ▶ newsn = save_sn ? newSVsv(save_sn) : newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
newsn = save_sn ? newSVsv(save_sn) : newSVpvs(""); 5 155 160
659 160 sv_catpvn(newsn, pf, pfl);
660 160 (void)hv_stores(env, "SCRIPT_NAME", newsn);
661 160 ▶ newpi = plen > pfl ? newSVpvn(path + pfl, plen - pfl) : newSVpvs("/");
Branch: 1 branch
BranchTrueFalseTotal
newpi = plen > pfl ? newSVpvn(path + pfl, plen - pfl) : newSVpvs("/"); 146 14 160
662 160 (void)hv_stores(env, "PATH_INFO", newpi);
663 160 ENTER; SAVETMPS;
664 160 ▶ PUSHMARK(SP); EXTEND(SP, 1);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); 0 160 160
PUSHMARK(SP); EXTEND(SP, 1); 0 160 160
665 160 PUSHs(sv_2mortal(newRV_inc((SV *)env)));
666 160 PUTBACK;
667 160 count = call_sv(mapp, G_SCALAR);
668 158 SPAGAIN;
669 158 ▶ r = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
r = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 158 0 158
670 158 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 158 0 158
671 158 ▶ (void)hv_stores(env, "SCRIPT_NAME", save_sn ? save_sn : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(env, "SCRIPT_NAME", save_sn ? save_sn : newSV(0)); 153 5 158
672 158 ▶ (void)hv_stores(env, "PATH_INFO", save_pi ? save_pi : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(env, "PATH_INFO", save_pi ? save_pi : newSV(0)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 158 158
673 /* a mounted app owns its answer and never comes through
674 * punk_deliver, so the observers are fired here */
675 158 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, r); 8 150 158
676 158 return r;
677 }
678 }
679 }
680  
681 221207 ▶ if (!rec && rt) { /* 4. dynamic hit, else allow */ /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!rec && rt) { /* 4. dynamic hit, else allow */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 107582 113625 221207
if (!rec && rt) { /* 4. dynamic hit, else allow */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 107582 0 107582
682 107582 HV *capsh = (HV *)sv_2mortal((SV *)newHV());
683 107582 IV idx = pr_match(aTHX_ rt, method, mlen, path, plen, capsh);
684 107582 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 107258 324 107582
685 107258 SV **rp = av_fetch(recs, idx, 0);
686 /*
687 # uncoverable branch false
688 # uncoverable condition right count:1 class:ignore_covered_err
689 # uncoverable condition right count:2 class:ignore_covered_err */
690 107258 ▶ if (rp && *rp) { rec = *rp; ridx = idx; }
Branch: 2 branches
BranchTrueFalseTotal
if (rp && *rp) { rec = *rp; ridx = idx; } 107258 0 107258
if (rp && *rp) { rec = *rp; ridx = idx; } 107258 0 107258
691 107258 caps = sv_2mortal(newRV_inc((SV *)capsh));
692 }
693 else {
694 324 AV *allowv = (AV *)sv_2mortal((SV *)newAV());
695 324 ▶ if (pr_allow(aTHX_ rt, method, mlen, path, plen, allowv))
Branch: 1 branch
BranchTrueFalseTotal
if (pr_allow(aTHX_ rt, method, mlen, path, plen, allowv)) 30 294 324
696 30 caps = sv_2mortal(newRV_inc((SV *)allowv));
697 }
698 }
699  
700 /* 5. trailing-slash rescue: "/account/" answers the route declared as
701 * "/account". It runs only once everything above has missed, so a
702 * route, API operation or mount that really does want the slash still
703 * wins on its own terms first. Mounts are deliberately not retried:
704 * a mounted app is entitled to tell "/docs" from "/docs/", and is the
705 * one that should decide. */
706 221207 ▶ if (!rec && rt && plen > 1 && path[plen - 1] == '/') { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (!rec && rt && plen > 1 && path[plen - 1] == '/') { /* # uncoverable condition right count:2 class:ignore_covered_err */ 324 220883 221207
if (!rec && rt && plen > 1 && path[plen - 1] == '/') { /* # uncoverable condition right count:2 class:ignore_covered_err */ 324 0 324
if (!rec && rt && plen > 1 && path[plen - 1] == '/') { /* # uncoverable condition right count:2 class:ignore_covered_err */ 322 2 324
if (!rec && rt && plen > 1 && path[plen - 1] == '/') { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 312 322
707 10 STRLEN tl = plen;
708 IV i;
709 22 ▶ while (tl > 1 && path[tl - 1] == '/') tl--; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (tl > 1 && path[tl - 1] == '/') tl--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 22 0 22
while (tl > 1 && path[tl - 1] == '/') tl--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 10 22
710 10 i = pr_static_match(aTHX_ rt, method, mlen, path, tl);
711 10 ▶ if (i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= 0) { 4 6 10
712 4 SV **rp = av_fetch(recs, i, 0);
713 /*
714 # uncoverable branch false
715 # uncoverable condition right count:1 class:ignore_covered_err
716 # uncoverable condition right count:2 class:ignore_covered_err */
717 4 ▶ if (rp && *rp) { rec = *rp; ridx = i; caps = NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (rp && *rp) { rec = *rp; ridx = i; caps = NULL; } 4 0 4
if (rp && *rp) { rec = *rp; ridx = i; caps = NULL; } 4 0 4
718 }
719 10 ▶ if (!rec) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rec) { 6 4 10
720 6 HV *capsh = (HV *)sv_2mortal((SV *)newHV());
721 6 IV idx = pr_match(aTHX_ rt, method, mlen, path, tl, capsh);
722 6 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 1 5 6
723 1 SV **rp = av_fetch(recs, idx, 0);
724 /*
725 # uncoverable branch false class:ignore_covered_err
726 # uncoverable branch true count:1 class:ignore_covered_err
727 # uncoverable branch false count:2 class:ignore_covered_err
728 # uncoverable branch true count:3 class:ignore_covered_err
729 # uncoverable condition right count:1 class:ignore_covered_err
730 # uncoverable condition right count:2 class:ignore_covered_err */
731 1 ▶ if (rp && *rp) { rec = *rp; ridx = idx;
Branch: 2 branches
BranchTrueFalseTotal
if (rp && *rp) { rec = *rp; ridx = idx; 1 0 1
if (rp && *rp) { rec = *rp; ridx = idx; 1 0 1
732 1 caps = sv_2mortal(newRV_inc((SV *)capsh)); }
733 }
734 /*
735 # uncoverable branch false
736 # uncoverable condition right count:1 class:ignore_covered_err */
737 5 ▶ else if (!caps) { /* keep the untrimmed Allow if any */
Branch: 1 branch
BranchTrueFalseTotal
else if (!caps) { /* keep the untrimmed Allow if any */ 5 0 5
738 5 AV *allowv = (AV *)sv_2mortal((SV *)newAV());
739 5 ▶ if (pr_allow(aTHX_ rt, method, mlen, path, tl, allowv))
Branch: 1 branch
BranchTrueFalseTotal
if (pr_allow(aTHX_ rt, method, mlen, path, tl, allowv)) 1 4 5
740 1 caps = sv_2mortal(newRV_inc((SV *)allowv));
741 }
742 }
743 }
744  
745 /* The Extended CONNECT gate: an upgrade is answerable by a websocket
746 * route and by nothing else.
747 *
748 * Everything above matched as GET, which is the point and also the whole
749 * exposure: without this, a CONNECT landing on an ordinary route ran its
750 * guards and its handler, and the status came back as :status on the
751 * CONNECT stream, where 2xx means accepted. A cross-origin page reads
752 * that as open-or-error and has a one-bit oracle on any path, cookies
753 * included - and the body that follows is written as DATA frames the
754 * client parses as RFC 6455, so it is not reliably one bit either.
755 *
756 * The refusal is ONE status for every miss, and deliberately not the 405
757 * merge or on_not_found below: "this path is a route under some other
758 * method" and "the app has a custom 404" are both answers, and an answer
759 * that varies by path is the same oracle in a smaller font. A route that
760 * IS a websocket falls through to the dispatch, where the origin check
761 * and the route's own `origin` option decide. */
762 221207 ▶ if (ext_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (ext_connect) { 12 221195 221207
763 /*
764 # uncoverable condition right count:2 class:ignore_covered_err
765 # uncoverable condition right count:3 class:ignore_covered_err */
766 12 ▶ int is_ws = (rec && ridx >= 0 && ridx < S->nrec && S->rec[ridx].ws);
Branch: 4 branches
BranchTrueFalseTotal
int is_ws = (rec && ridx >= 0 && ridx < S->nrec && S->rec[ridx].ws); 8 4 12
int is_ws = (rec && ridx >= 0 && ridx < S->nrec && S->rec[ridx].ws); 8 0 8
int is_ws = (rec && ridx >= 0 && ridx < S->nrec && S->rec[ridx].ws); 8 0 8
int is_ws = (rec && ridx >= 0 && ridx < S->nrec && S->rec[ridx].ws); 4 4 8
767 12 ▶ if (!is_ws) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_ws) { 8 4 12
768 8 SV *out = ps_err_triplet(aTHX_ 404, PS_ERR_404,
769 sizeof(PS_ERR_404) - 1, NULL);
770 /*
771 # uncoverable branch true
772 # uncoverable condition left count:1 class:ignore_covered_err */
773 8 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out); 0 8 8
774 8 return out;
775 }
776 }
777  
778 221199 ▶ if (!rec) { /* 405 (merge allow) or 404 */
Branch: 1 branch
BranchTrueFalseTotal
if (!rec) { /* 405 (merge allow) or 404 */ 315 220884 221199
779 315 HV *seen = (HV *)sv_2mortal((SV *)newHV());
780 AV *lists[2];
781 int li;
782 30 ▶ lists[0] = (caps && SvROK(caps) && SvTYPE(SvRV(caps)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
lists[0] = (caps && SvROK(caps) && SvTYPE(SvRV(caps)) == SVt_PVAV) 30 0 30
lists[0] = (caps && SvROK(caps) && SvTYPE(SvRV(caps)) == SVt_PVAV) 30 0 30
783 345 ▶ ? (AV *)SvRV(caps) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(caps) : NULL; 30 285 315
784 315 lists[1] = api_allow;
785 945 ▶ for (li = 0; li < 2; li++) {
Branch: 1 branch
BranchTrueFalseTotal
for (li = 0; li < 2; li++) { 630 315 945
786 630 AV *l = lists[li]; SSize_t j, n;
787 630 ▶ if (!l) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) continue; 598 32 630
788 32 n = av_len(l) + 1;
789 73 ▶ for (j = 0; j < n; j++) { SV **a = av_fetch(l, j, 0); /* # uncoverable branch true count:2 */
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { SV **a = av_fetch(l, j, 0); /* # uncoverable branch true count:2 */ 41 32 73
790 /*
791 # uncoverable branch false class:ignore_covered_err
792 # uncoverable condition right count:1 class:ignore_covered_err
793 # uncoverable condition right count:2 class:ignore_covered_err */
794 41 ▶ if (a && *a) (void)hv_store_ent(seen, *a, PUNK_SET_TRUE, 0); }
Branch: 2 branches
BranchTrueFalseTotal
if (a && *a) (void)hv_store_ent(seen, *a, PUNK_SET_TRUE, 0); } 41 0 41
if (a && *a) (void)hv_store_ent(seen, *a, PUNK_SET_TRUE, 0); } 41 0 41
795 }
796 315 (void)hv_delete(seen, method, (I32)mlen, 0);
797 315 ▶ if (HvUSEDKEYS(seen)) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(seen)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 315 315
if (HvUSEDKEYS(seen)) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 32 283 315
798 32 AV *sorted = (AV *)sv_2mortal((SV *)newAV());
799 32 SV *joined; HE *he; SSize_t na = 0, k;
800 32 hv_iterinit(seen);
801 73 ▶ while ((he = hv_iternext(seen))) { av_push(sorted, newSVsv(hv_iterkeysv(he))); na++; }
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(seen))) { av_push(sorted, newSVsv(hv_iterkeysv(he))); na++; } 41 32 73
802 32 ▶ if (na > 1) sortsv(AvARRAY(sorted), (STRLEN)na, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (na > 1) sortsv(AvARRAY(sorted), (STRLEN)na, Perl_sv_cmp); 7 25 32
803 32 joined = newSVpvs("");
804 73 ▶ for (k = 0; k < na; k++) { SV **a = av_fetch(sorted, k, 0);
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < na; k++) { SV **a = av_fetch(sorted, k, 0); 41 32 73
805 /*
806 # uncoverable branch false class:ignore_covered_err
807 # uncoverable condition right count:1 class:ignore_covered_err
808 # uncoverable condition right count:2 class:ignore_covered_err */
809 41 ▶ if (k) sv_catpvs(joined, ", "); if (a && *a) sv_catsv(joined, *a); }
Branch: 3 branches
BranchTrueFalseTotal
if (k) sv_catpvs(joined, ", "); if (a && *a) sv_catsv(joined, *a); } 9 32 41
if (k) sv_catpvs(joined, ", "); if (a && *a) sv_catsv(joined, *a); } 41 0 41
if (k) sv_catpvs(joined, ", "); if (a && *a) sv_catsv(joined, *a); } 41 0 41
810 { /* the house 405 runs no after-hooks and so never reaches
811 * punk_deliver; the observers still get their one event */
812 32 SV *out = ps_err_triplet(aTHX_ 405, PS_ERR_405,
813 sizeof(PS_ERR_405) - 1, joined);
814 32 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out); 6 26 32
815 32 return out;
816 }
817 }
818 { /* on_not_found: the app's own 404, the on_error contract - a
819 * reference return becomes the response (after hooks run, a
820 * Future is awaited), anything else keeps the default below,
821 * and a die inside it goes through on_error. The 405 above is
822 * deliberately not covered: its Allow semantics stay. */
823 283 SV *on_nf = S->on_nf;
824 283 ▶ if (on_nf) {
Branch: 1 branch
BranchTrueFalseTotal
if (on_nf) { 9 274 283
825 9 HV *match = newHV();
826 9 SV *r = NULL;
827 int died;
828 9 (void)hv_stores(match, "captures", newRV_noinc((SV *)newHV()));
829 9 (void)ps_ctx_for(aTHX_ &c, S, env, newRV_noinc((SV *)match));
830 {
831 9 dSP; int count;
832 9 ENTER; SAVETMPS;
833 9 ▶ PUSHMARK(SP); EXTEND(SP, 1);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); 0 9 9
PUSHMARK(SP); EXTEND(SP, 1); 0 9 9
834 9 PUSHs(c);
835 9 PUTBACK;
836 9 count = call_sv(on_nf, G_SCALAR | G_EVAL);
837 9 SPAGAIN;
838 /*
839 # uncoverable branch false
840 # uncoverable condition right count:1 class:ignore_covered_err */
841 9 ▶ if (count > 0) { SV *t = POPs; r = SvREFCNT_inc(t); }
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { SV *t = POPs; r = SvREFCNT_inc(t); } 9 0 9
842 9 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 9 0 9
843 9 ▶ died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 0 9
844 }
845 9 ▶ if (died) {
Branch: 1 branch
BranchTrueFalseTotal
if (died) { 1 8 9
846 1 ▶ SV *errsv = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
SV *errsv = sv_2mortal(newSVsv(ERRSV)); 1 0 1
847 1 SV *out = punk_finish_c(aTHX_ app, c, &PL_sv_undef, errsv,
848 on_err, method, mlen, after, env);
849 /*
850 # uncoverable branch false
851 # uncoverable condition right count:1 class:ignore_covered_err */
852 1 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 1 0 1
853 1 return out;
854 }
855 8 ▶ if (r && SvROK(r)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (r && SvROK(r)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 0 8
if (r && SvROK(r)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 1 8
856 7 SV *out = punk_finish_c(aTHX_ app, c, r, &PL_sv_undef,
857 on_err, method, mlen, after, env);
858 7 SvREFCNT_dec(r);
859 7 return out;
860 }
861 /*
862 # uncoverable branch false
863 # uncoverable condition right count:1 class:ignore_covered_err */
864 1 ▶ if (r) SvREFCNT_dec(r); /* declined: the default stands */
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); /* declined: the default stands */ 1 0 1
865 }
866 }
867 { /* likewise the house 404 (on_not_found returning a response goes
868 * through punk_finish_c and is covered there instead) */
869 275 SV *out = ps_err_triplet(aTHX_ 404, PS_ERR_404,
870 sizeof(PS_ERR_404) - 1, NULL);
871 275 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, out); 239 36 275
872 275 return out;
873 }
874 }
875  
876 /* 5. web route: ctx, before + guards, controller/ws, finish */
877 {
878 220884 HV *rech = (HV *)SvRV(rec);
879 /* the record's hot fields, mirrored at compile (punk_state.h) */
880 220884 ▶ const pk_rec_c *R = (ridx >= 0 && ridx < S->nrec) ? &S->rec[ridx]
Branch: 1 branch
BranchTrueFalseTotal
const pk_rec_c *R = (ridx >= 0 && ridx < S->nrec) ? &S->rec[ridx] 220884 0 220884
881 441768 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 220884 0 220884
882 pk_rec_c none;
883 HV *match;
884 220884 SV *ret = &PL_sv_undef, *err = &PL_sv_undef, *out;
885 int shorted;
886 /*
887 # uncoverable branch true
888 # uncoverable condition left count:1 class:ignore_covered_err */
889 220884 ▶ if (!R) { pk_rec_mirror(aTHX_ &none, rec); R = &none; }
Branch: 1 branch
BranchTrueFalseTotal
if (!R) { pk_rec_mirror(aTHX_ &none, rec); R = &none; } 0 220884 220884
890 /* the hook's match hash when a context exists already, else a new
891 * one in a new context */
892 220884 match = ps_match_for(aTHX_ &c, S, env);
893 /*
894 # uncoverable condition right count:2 class:ignore_covered_err
895 # uncoverable condition right count:3 class:ignore_covered_err */
896 220884 ▶ if (caps && SvROK(caps) && SvTYPE(SvRV(caps)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (caps && SvROK(caps) && SvTYPE(SvRV(caps)) == SVt_PVHV) 107259 113625 220884
if (caps && SvROK(caps) && SvTYPE(SvRV(caps)) == SVt_PVHV) 107259 0 107259
if (caps && SvROK(caps) && SvTYPE(SvRV(caps)) == SVt_PVHV) 107259 0 107259
897 107259 (void)hv_stores(match, "captures", newRV_inc(SvRV(caps)));
898 else
899 113625 (void)hv_stores(match, "captures", newRV_noinc((SV *)newHV()));
900 /* the matched route record, which Punk::Context has always documented
901 * $c->match as carrying and which nothing actually put there. It is
902 * what names the route as DECLARED - "/users/:id" - for anything
903 * grouping by route rather than by path, the C ABI included. */
904 220884 (void)hv_stores(match, "route", newSVsv(rec));
905  
906 { /* The body ceiling, before the hook chain and before the guards:
907 * an oversize request should cost no auth lookup, no validation,
908 * no body parse and no Perl frame. A route's own value wins over
909 * the app-wide one, and 0 on a route disables the check there. */
910 441768 IV lim = R->has_max_body ? R->max_body
911 439689 ▶ : S->has_max_body ? S->max_body
Branch: 1 branch
BranchTrueFalseTotal
: S->has_max_body ? S->max_body 2079 218805 220884
912 218805 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
: 0; 19 218786 218805
913 220884 ▶ if (lim > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lim > 0) { 2081 218803 220884
914 2081 SV *over = pd_body_limit(aTHX_ env, lim);
915 2081 ▶ if (over) { /* the 413 refuses before the hook chain, so it
Branch: 1 branch
BranchTrueFalseTotal
if (over) { /* the 413 refuses before the hook chain, so it 14 2067 2081
916 * too skips punk_deliver */
917 14 ▶ if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, over);
Branch: 1 branch
BranchTrueFalseTotal
if (PK_OBS_WANT_RES) pk_obs_fire_res(aTHX_ c, over); 1 13 14
918 14 return over;
919 }
920 }
921 }
922  
923 220870 shorted = pd_run_chain(aTHX_ before, c, &ret, &err);
924 220870 ▶ if (!shorted && R->guards)
Branch: 2 branches
BranchTrueFalseTotal
if (!shorted && R->guards) 220829 41 220870
if (!shorted && R->guards) 114 220715 220829
925 114 shorted = pd_run_chain(aTHX_ R->guards, c, &ret, &err);
926 220870 ▶ if (!shorted) {
Branch: 1 branch
BranchTrueFalseTotal
if (!shorted) { 220774 96 220870
927 /* The conditional GET, after the guards and before the handler.
928 * Not earlier: before_dispatch runs ahead of the guards, and a
929 * 304 there would answer a request an authentication guard was
930 * about to refuse. Inert unless the plugin is registered, and
931 * one hv_fetch when it is not - the `sitemap` arrangement. */
932 220774 SV *cg = S->cget;
933 220774 ▶ if (cg) {
Branch: 1 branch
BranchTrueFalseTotal
if (cg) { 104 220670 220774
934 104 SV *cgout = NULL, *cgerr = NULL;
935 /* `etag => 1` hashes the body this route renders, so the
936 * body has to come out the same way every time */
937 104 ▶ if (R->etag)
Branch: 1 branch
BranchTrueFalseTotal
if (R->etag) 38 66 104
938 38 (void)av_store(pcx_av(aTHX_ c), PCX_CANON, newSViv(1));
939 104 ▶ if (pcg_check(aTHX_ c, rech, env, method, mlen,
Branch: 1 branch
BranchTrueFalseTotal
if (pcg_check(aTHX_ c, rech, env, method, mlen, 22 82 104
940 &cgout, &cgerr)) {
941 22 ▶ if (cgerr) err = cgerr; /* the app's 500 */
Branch: 1 branch
BranchTrueFalseTotal
if (cgerr) err = cgerr; /* the app's 500 */ 2 20 22
942 20 else ret = cgout; /* the 304 */
943 22 shorted = 1;
944 }
945 }
946 }
947 220870 ▶ if (!shorted) {
Branch: 1 branch
BranchTrueFalseTotal
if (!shorted) { 220752 118 220870
948 /* Idempotency, in the same slot and for the same reason: a
949 * replay returns a stored response BODY, so answering it ahead
950 * of the guards would hand somebody else's order to a caller
951 * the guard was about to refuse. The two never both fire - one
952 * answers GET and HEAD, the other only unsafe methods. */
953 220752 SV *ic = S->idem;
954 220752 ▶ if (ic && R->idempotent) {
Branch: 2 branches
BranchTrueFalseTotal
if (ic && R->idempotent) { 65 220687 220752
if (ic && R->idempotent) { 63 2 65
955 63 SV *iout = NULL, *ierr = NULL;
956 63 ▶ if (pi_check(aTHX_ c, rech, env, method, mlen, ic,
Branch: 1 branch
BranchTrueFalseTotal
if (pi_check(aTHX_ c, rech, env, method, mlen, ic, 26 37 63
957 &iout, &ierr)) {
958 /*
959 # uncoverable branch true
960 # uncoverable condition left count:1 class:ignore_covered_err */
961 26 ▶ if (ierr) err = ierr;
Branch: 1 branch
BranchTrueFalseTotal
if (ierr) err = ierr; 0 26 26
962 26 else ret = iout; /* the replay, 400 or 422 */
963 26 shorted = 1;
964 }
965 }
966 }
967 220870 ▶ if (!shorted) {
Branch: 1 branch
BranchTrueFalseTotal
if (!shorted) { 220726 144 220870
968 220726 int died = 0;
969 220726 ▶ if (R->ws || R->sse) {
Branch: 2 branches
BranchTrueFalseTotal
if (R->ws || R->sse) { 220625 101 220726
if (R->ws || R->sse) { 41 220584 220625
970 284 const char *disp = R->ws
971 142 ▶ ? "Punk::WebSocket::_dispatch" : "Punk::SSE::_dispatch";
Branch: 1 branch
BranchTrueFalseTotal
? "Punk::WebSocket::_dispatch" : "Punk::SSE::_dispatch"; 101 41 142
972 142 dSP; int count;
973 142 ENTER; SAVETMPS;
974 142 ▶ PUSHMARK(SP); EXTEND(SP, 3);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 3); 0 142 142
PUSHMARK(SP); EXTEND(SP, 3); 0 142 142
975 142 PUSHs(c); PUSHs(rec);
976 142 PUSHs(sv_2mortal(newRV_inc((SV *)env)));
977 142 PUTBACK;
978 142 count = call_pv(disp, G_SCALAR | G_EVAL);
979 142 SPAGAIN;
980 142 ▶ ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 142 0 142
981 142 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 142 0 142
982 142 ▶ died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 142 0 142
983 }
984 else {
985 220584 ▶ ret = pd_call(aTHX_ R->code ? R->code : &PL_sv_undef, c, &died); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ret = pd_call(aTHX_ R->code ? R->code : &PL_sv_undef, c, &died); /* # uncoverable condition right count:1 class:ignore_covered_err */ 220584 0 220584
986 }
987 220726 ▶ if (died) err = newSVsv(ERRSV); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (died) err = newSVsv(ERRSV); /* # uncoverable condition right count:2 class:ignore_covered_err */ 2180 218546 220726
if (died) err = newSVsv(ERRSV); /* # uncoverable condition right count:2 class:ignore_covered_err */ 2180 0 2180
988 }
989 220870 out = punk_finish_c(aTHX_ app, c, ret, err, on_err, method, mlen, after, env);
990 { /* compress => 0: say so in the one place the server will look.
991 * `identity` is the documented hands-off spelling (Hyperman
992 * strips it before writing); a response that already declares
993 * an encoding has said something more specific and is left. */
994 /*
995 # uncoverable condition right count:2 class:ignore_covered_err
996 # uncoverable condition right count:3 class:ignore_covered_err
997 # uncoverable condition right count:4 class:ignore_covered_err */
998 220870 ▶ if (R->nocompress
Branch: 1 branch
BranchTrueFalseTotal
if (R->nocompress 6 220864 220870
999 6 ▶ && out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
&& out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) { 6 0 6
&& out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) { 6 0 6
&& out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) { 6 0 6
1000 6 AV *ra = (AV *)SvRV(out);
1001 6 SV **hp = av_fetch(ra, 1, 0);
1002 /*
1003 # uncoverable branch false
1004 # uncoverable condition right count:1 class:ignore_covered_err
1005 # uncoverable condition right count:2 class:ignore_covered_err
1006 # uncoverable condition right count:3 class:ignore_covered_err
1007 # uncoverable condition right count:4 class:ignore_covered_err */
1008 6 ▶ if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 6 0 6
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 6 0 6
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 6 0 6
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 6 0 6
1009 6 AV *hdrs = (AV *)SvRV(*hp);
1010 6 SSize_t j, hn = av_len(hdrs) + 1;
1011 6 int seen = 0;
1012 18 ▶ for (j = 0; j + 1 < hn; j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j + 1 < hn; j += 2) { 13 5 18
1013 13 SV **k = av_fetch(hdrs, j, 0);
1014 STRLEN kl;
1015 const char *ks;
1016 /*
1017 # uncoverable branch true
1018 # uncoverable condition left count:1 class:ignore_covered_err
1019 # uncoverable condition left count:2 class:ignore_covered_err */
1020 13 ▶ if (!(k && *k)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(k && *k)) continue; 13 0 13
if (!(k && *k)) continue; 0 13 13
1021 13 ks = SvPV_const(*k, kl);
1022 13 ▶ if (kl == 16 && foldEQ(ks, "Content-Encoding", 16)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 16 && foldEQ(ks, "Content-Encoding", 16)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 12 13
if (kl == 16 && foldEQ(ks, "Content-Encoding", 16)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
1023 1 { seen = 1; break; }
1024 }
1025 6 ▶ if (!seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) { 5 1 6
1026 5 av_push(hdrs, newSVpvs("Content-Encoding"));
1027 5 av_push(hdrs, newSVpvs("identity"));
1028 }
1029 }
1030 }
1031 }
1032 220870 ▶ if (ret != &PL_sv_undef) SvREFCNT_dec(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (ret != &PL_sv_undef) SvREFCNT_dec(ret); 218686 2184 220870
1033 220870 ▶ if (err != &PL_sv_undef) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err != &PL_sv_undef) SvREFCNT_dec(err); 2184 218686 220870
1034 220870 return out;
1035 }
1036 }
1037  
1038 /* The dispatch frame, which is what makes pk_abi's current_of safe to read.
1039 *
1040 * ps_serve_one has nine returns and a croak can leave it by none of them, so
1041 * the restore goes on the save stack rather than at the end of the body. The
1042 * PREVIOUS value is what goes back, not NULL: a mounted application inside
1043 * another one has to leave the outer context where the outer frame will find
1044 * it. */
1045 221542 static SV *punk_serve(pTHX_ pk_state *S, HV *env) {
1046 SV *out;
1047 221542 ENTER;
1048 221542 SAVEDESTRUCTOR_X(pk_cur_restore, (void *)PK_CURRENT);
1049 221542 out = ps_serve_one(aTHX_ S, env);
1050 221540 LEAVE;
1051 221540 return out;
1052 }
1053  
1054 #endif /* PUNK_SERVE_H */
« include/punk/punk_request.h ↑ Summary include/punk/punk_import.h »