include/punk/punk_serve.h

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