| 1 |
|
| /* punk_dispatch.h - the Open::API C ABI dispatch path. |
| 2 |
|
| * |
| 3 |
|
| * Punk reaches Open::API's router and validator through its public C ABI |
| 4 |
|
| * (oa_abi.h, reached through ExtUtils::Depends). Open::API 0.04+ is a hard |
| 5 |
|
| * prerequisite, so the table is always there - punk_oa croaks if it is not, |
| 6 |
|
| * the same startup-environment error punk_frj raises. There is no Perl |
| 7 |
|
| * fallback. |
| 8 |
|
| * |
| 9 |
|
| * punk_oa_dispatch runs a matched API operation end to end in C: the |
| 10 |
|
| * before-dispatch hooks and the operation's guards (Perl coderefs, via |
| 11 |
|
| * call_sv, still short-circuitable), the body-size and no-handler checks, the |
| 12 |
|
| * raw-input assembly and validation through the ABI, stashing the typed |
| 13 |
|
| * params, and the controller call - returning the controller's value (or an |
| 14 |
|
| * early PSGI triplet) and any die to Punk::App's finish path. The only Perl |
| 15 |
|
| * frames are the hooks, the guards and the controller themselves. |
| 16 |
|
| * |
| 17 |
|
| * Perl headers, punk_response.h (punk_triplet), punk_request.h, |
| 18 |
|
| * punk_context.h (pcx_av and the PCX_* slots) and the punk_frj resolver must |
| 19 |
|
| * be in scope before this file. |
| 20 |
|
| */ |
| 21 |
|
| |
| 22 |
|
| #ifndef PUNK_DISPATCH_H |
| 23 |
|
| #define PUNK_DISPATCH_H |
| 24 |
|
| |
| 25 |
|
| #include "oa_abi.h" /* Open::API's public ABI, via ExtUtils::Depends */ |
| 26 |
|
| |
| 27 |
|
| #define PD_ERR_413 "{\"errors\":[{\"message\":\"Payload Too Large\"}]}" |
| 28 |
|
| #define PD_ERR_501 "{\"errors\":[{\"message\":\"Not Implemented\"}]}" |
| 29 |
|
| |
| 30 |
|
| static const oa_abi *PUNK_OA = NULL; |
| 31 |
|
| static int PUNK_OA_TRIED = 0; |
| 32 |
|
| |
| 33 |
|
| /* Resolve (once) Open::API's ABI table, or NULL. PUNK_FAKE_OA_BAD simulates a |
| 34 |
|
| * version mismatch for the guard test. */ |
| 35 |
199 |
| static const oa_abi *punk_oa_try(pTHX) { |
| 36 |
199 |
▶ | if (!PUNK_OA_TRIED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PUNK_OA_TRIED) { |
22 |
177 |
199 |
|
| 37 |
22 |
| dSP; int count, ok; IV p = 0; |
| 38 |
22 |
| PUNK_OA_TRIED = 1; |
| 39 |
22 |
| ok = pk_require_once(aTHX_ "Open::API", FALSE); |
| 40 |
22 |
| SPAGAIN; /* the require may have reallocated the value stack */ |
| 41 |
|
| /* |
| 42 |
|
| # uncoverable branch false |
| 43 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 44 |
22 |
▶ | if (ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) { |
22 |
0 |
22 |
|
| 45 |
22 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
22 |
22 |
|
| 46 |
22 |
| count = call_pv("Open::API::_abi_ptr", G_SCALAR | G_EVAL); |
| 47 |
22 |
| SPAGAIN; |
| 48 |
|
| /* |
| 49 |
|
| # uncoverable branch false |
| 50 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 51 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 52 |
22 |
▶ | if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
22 |
0 |
22 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
22 |
0 |
22 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
22 |
0 |
22 |
|
| 53 |
|
| /* |
| 54 |
|
| # uncoverable statement |
| 55 |
|
| # uncoverable branch true |
| 56 |
|
| # uncoverable branch false |
| 57 |
|
| # uncoverable condition left count:1 |
| 58 |
|
| # uncoverable condition right count:1 */ |
| 59 |
0 |
▶ | else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 60 |
22 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
22 |
0 |
22 |
|
| 61 |
|
| /* |
| 62 |
|
| # uncoverable branch false |
| 63 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 64 |
22 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
22 |
0 |
22 |
|
| 65 |
22 |
| const oa_abi *a = INT2PTR(const oa_abi *, p); |
| 66 |
|
| /* |
| 67 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 68 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 69 |
22 |
▶ | if (a && !getenv("PUNK_FAKE_OA_BAD") |
Branch: 2 branches
| Branch | True | False | Total |
| if (a && !getenv("PUNK_FAKE_OA_BAD") |
22 |
0 |
22 |
| if (a && !getenv("PUNK_FAKE_OA_BAD") |
21 |
1 |
22 |
|
| 70 |
21 |
▶ | && a->abi_version >= OA_ABI_VERSION) |
Branch: 1 branch
| Branch | True | False | Total |
| && a->abi_version >= OA_ABI_VERSION) |
21 |
0 |
21 |
|
| 71 |
21 |
| PUNK_OA = a; |
| 72 |
|
| } |
| 73 |
|
| } |
| 74 |
|
| } |
| 75 |
199 |
| return PUNK_OA; |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| /* The ABI table, croaking if it is missing or the version does not match - |
| 79 |
|
| * Open::API 0.04+ is required, so this is a boot-environment error, not a |
| 80 |
|
| * per-request fallback. */ |
| 81 |
179 |
| static const oa_abi *punk_oa(pTHX) { |
| 82 |
179 |
| const oa_abi *a = punk_oa_try(aTHX); |
| 83 |
|
| /* |
| 84 |
|
| # uncoverable branch true |
| 85 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 86 |
179 |
▶ | if (!a) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) |
0 |
179 |
179 |
|
| 87 |
0 |
| croak("Punk: the API path needs Open::API with a compatible C ABI " /* # uncoverable statement */ |
| 88 |
|
| "(OA_ABI_VERSION %d); upgrade Open::API to 0.04+", OA_ABI_VERSION); |
| 89 |
179 |
| return a; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| /* The env hashref of a Punk::Context (its env accessor). */ |
| 93 |
63 |
| static HV *pd_c_env(pTHX_ SV *c) { |
| 94 |
63 |
| dSP; HV *env = NULL; int count; |
| 95 |
63 |
| ENTER; SAVETMPS; |
| 96 |
63 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
63 |
63 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
63 |
63 |
|
| 97 |
63 |
| count = call_method("env", G_SCALAR); |
| 98 |
63 |
| SPAGAIN; |
| 99 |
|
| /* |
| 100 |
|
| # uncoverable branch false |
| 101 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 102 |
63 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
63 |
0 |
63 |
|
| 103 |
63 |
| SV *e = POPs; |
| 104 |
|
| /* |
| 105 |
|
| # uncoverable branch false |
| 106 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 107 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 108 |
63 |
▶ | if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) env = (HV *)SvRV(e); |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) env = (HV *)SvRV(e); |
63 |
0 |
63 |
| if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) env = (HV *)SvRV(e); |
63 |
0 |
63 |
|
| 109 |
|
| } |
| 110 |
63 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
63 |
0 |
63 |
|
| 111 |
63 |
| return env; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
|
| /* Stash the validated params in the context's openapi_params slot. |
| 115 |
|
| * |
| 116 |
|
| * A direct store into the slot, not a call to the accessor: |
| 117 |
|
| * punk_context.h's PCX_* enum is the same slot contract xs/context.xs reads |
| 118 |
|
| * through, so there is nothing a Perl call would add except a frame on the |
| 119 |
|
| * hot path. |
| 120 |
|
| * |
| 121 |
|
| * It used to be call_pv(..., G_VOID), which was wrong in a way worth |
| 122 |
|
| * recording. G_VOID is not G_DISCARD: it calls the sub in void context but |
| 123 |
|
| * still leaves whatever the sub pushed on the stack for the caller to remove, |
| 124 |
|
| * and the accessor pushes the value it just set. Nothing here removed it, so |
| 125 |
|
| * every validated request left one stray SV on the Perl stack. It went |
| 126 |
|
| * unnoticed until the frame unwound: Punk::Mount::OpenAPI::dispatch returns |
| 127 |
|
| * ($ret, $err), so pp_leavesub found three values where two were expected and |
| 128 |
|
| * died copying the extra one ("Bizarre copy of HASH in subroutine exit"), |
| 129 |
|
| * then took the temps stack down with it. |
| 130 |
|
| * |
| 131 |
|
| * Takes ownership of typed_ref (+1), which the slot then holds. */ |
| 132 |
45 |
| static void pd_stash(pTHX_ SV *c, SV *typed_ref) { |
| 133 |
45 |
| AV *av = pcx_av(aTHX_ c); |
| 134 |
|
| /* |
| 135 |
|
| # uncoverable branch true |
| 136 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 137 |
45 |
▶ | if (!av_store(av, PCX_OPENAPI, typed_ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av_store(av, PCX_OPENAPI, typed_ref)) |
0 |
45 |
45 |
|
| 138 |
0 |
| SvREFCNT_dec(typed_ref); /* the store did not take it */ /* # uncoverable statement */ |
| 139 |
45 |
| } |
| 140 |
|
| |
| 141 |
|
| /* Call $code->($c) trapping a die. Returns the result (+1 owned); *died is set |
| 142 |
|
| * when the call croaked (the message is left in ERRSV). */ |
| 143 |
221010 |
| static SV *pd_call(pTHX_ SV *code, SV *c, int *died) { |
| 144 |
221010 |
| dSP; SV *ret; int count; |
| 145 |
221010 |
| ENTER; SAVETMPS; |
| 146 |
221010 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
221010 |
221010 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
221010 |
221010 |
|
| 147 |
221010 |
| count = call_sv(code, G_SCALAR | G_EVAL); |
| 148 |
221010 |
| SPAGAIN; |
| 149 |
221010 |
▶ | ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| ret = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
221010 |
0 |
221010 |
|
| 150 |
221010 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
220925 |
85 |
221010 |
|
| 151 |
221010 |
▶ | *died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| *died = SvTRUE(ERRSV) ? 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
221010 |
0 |
221010 |
|
| 152 |
221010 |
| return ret; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| /* Run a chain of hooks/guards over $c. Returns 1 (with *ret / *err set) when |
| 156 |
|
| * one short-circuits with a reference or dies; 0 to continue. */ |
| 157 |
221168 |
| static int pd_run_chain(pTHX_ AV *chain, SV *c, SV **ret, SV **err) { |
| 158 |
|
| SSize_t i, n; |
| 159 |
|
| /* |
| 160 |
|
| # uncoverable branch true |
| 161 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 162 |
221168 |
▶ | if (!chain) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!chain) return 0; |
0 |
221168 |
221168 |
|
| 163 |
221168 |
| n = av_len(chain) + 1; |
| 164 |
221425 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
380 |
221045 |
221425 |
|
| 165 |
380 |
| SV **cp = av_fetch(chain, i, 0); |
| 166 |
380 |
| int died = 0; |
| 167 |
|
| SV *r; |
| 168 |
|
| /* |
| 169 |
|
| # uncoverable branch true |
| 170 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 171 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 172 |
380 |
▶ | if (!cp || !*cp) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cp || !*cp) continue; |
380 |
0 |
380 |
| if (!cp || !*cp) continue; |
0 |
380 |
380 |
|
| 173 |
380 |
| r = pd_call(aTHX_ *cp, c, &died); |
| 174 |
380 |
▶ | if (died) { *err = newSVsv(ERRSV); *ret = &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (died) { *err = newSVsv(ERRSV); *ret = &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
3 |
377 |
380 |
| if (died) { *err = newSVsv(ERRSV); *ret = &PL_sv_undef; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
3 |
0 |
3 |
|
| 175 |
123 |
| SvREFCNT_dec(r); return 1; } |
| 176 |
377 |
▶ | if (SvROK(r)) { *ret = r; return 1; } /* short-circuit response */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(r)) { *ret = r; return 1; } /* short-circuit response */ |
120 |
257 |
377 |
|
| 177 |
257 |
| SvREFCNT_dec(r); |
| 178 |
|
| } |
| 179 |
221045 |
| return 0; |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
|
| /* Read CONTENT_LENGTH bytes of the request body from the env's psgi.input, |
| 183 |
|
| * rewinding after. Returns an owned SV, or NULL for no body; *cl_out gets the |
| 184 |
|
| * CONTENT_LENGTH. */ |
| 185 |
58 |
| static SV *punk_oa_env_body(pTHX_ HV *env, IV *cl_out, IV max_body, |
| 186 |
|
| int *over) { |
| 187 |
58 |
▶ | SV **in = env ? hv_fetchs(env, "psgi.input", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **in = env ? hv_fetchs(env, "psgi.input", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
58 |
0 |
58 |
|
| 188 |
58 |
▶ | SV **cl = env ? hv_fetchs(env, "CONTENT_LENGTH", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **cl = env ? hv_fetchs(env, "CONTENT_LENGTH", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
58 |
0 |
58 |
|
| 189 |
58 |
▶ | IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
41 |
17 |
58 |
| IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
41 |
0 |
41 |
| IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
39 |
2 |
41 |
|
| 190 |
58 |
| *over = 0; |
| 191 |
|
| /* No length and a buffered input (HTTP/2 and /3 frame the body without |
| 192 |
|
| * one) reads to EOF, as pq_body does. Not reading it validated an |
| 193 |
|
| * absent body while the controller read the real one. */ |
| 194 |
58 |
▶ | if (len < 0 && !(env && pq_input_buffered(aTHX_ env))) len = 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| if (len < 0 && !(env && pq_input_buffered(aTHX_ env))) len = 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
19 |
39 |
58 |
| if (len < 0 && !(env && pq_input_buffered(aTHX_ env))) len = 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
19 |
0 |
19 |
| if (len < 0 && !(env && pq_input_buffered(aTHX_ env))) len = 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
17 |
2 |
19 |
|
| 195 |
|
| /* |
| 196 |
|
| # uncoverable branch false |
| 197 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 198 |
58 |
▶ | if (cl_out) *cl_out = len > 0 ? len : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cl_out) *cl_out = len > 0 ? len : 0; |
58 |
0 |
58 |
|
| 199 |
|
| /* |
| 200 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 201 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 202 |
58 |
▶ | if (!(in && *in && SvTRUE(*in) && len != 0)) return NULL; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(in && *in && SvTRUE(*in) && len != 0)) return NULL; |
42 |
16 |
58 |
| if (!(in && *in && SvTRUE(*in) && len != 0)) return NULL; |
42 |
0 |
42 |
| if (!(in && *in && SvTRUE(*in) && len != 0)) return NULL; |
42 |
0 |
42 |
| if (!(in && *in && SvTRUE(*in) && len != 0)) return NULL; |
32 |
10 |
42 |
|
| 203 |
|
| { |
| 204 |
10 |
| IO *io = sv_2io(*in); |
| 205 |
10 |
▶ | PerlIO *fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| PerlIO *fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
10 |
0 |
10 |
|
| 206 |
|
| SV *raw; |
| 207 |
10 |
| IV got = 0; |
| 208 |
|
| /* |
| 209 |
|
| # uncoverable branch true |
| 210 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 211 |
10 |
▶ | if (!fp) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fp) return NULL; |
0 |
10 |
10 |
|
| 212 |
|
| /* sized for what arrives, not for what the header declared */ |
| 213 |
10 |
▶ | raw = newSV((len > 0 && len < PQ_STREAM_CHUNK ? (STRLEN)len |
Branch: 2 branches
| Branch | True | False | Total |
| raw = newSV((len > 0 && len < PQ_STREAM_CHUNK ? (STRLEN)len |
8 |
2 |
10 |
| raw = newSV((len > 0 && len < PQ_STREAM_CHUNK ? (STRLEN)len |
7 |
1 |
8 |
|
| 214 |
|
| : PQ_STREAM_CHUNK) + 1); |
| 215 |
10 |
| SvPOK_on(raw); |
| 216 |
10 |
| SvCUR_set(raw, 0); |
| 217 |
10 |
| *SvPVX(raw) = '\0'; |
| 218 |
10 |
| for (;;) { |
| 219 |
20 |
| STRLEN want = (STRLEN)PQ_STREAM_CHUNK; |
| 220 |
|
| SSize_t n; |
| 221 |
|
| char *d; |
| 222 |
20 |
▶ | if (len >= 0 && len - got < (IV)want) want = (STRLEN)(len - got); |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 0 && len - got < (IV)want) want = (STRLEN)(len - got); |
16 |
4 |
20 |
| if (len >= 0 && len - got < (IV)want) want = (STRLEN)(len - got); |
14 |
2 |
16 |
|
| 223 |
20 |
▶ | if (!want) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!want) break; |
7 |
13 |
20 |
|
| 224 |
13 |
▶ | if ((STRLEN)got + want + 1 > SvLEN(raw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STRLEN)got + want + 1 > SvLEN(raw)) { |
3 |
10 |
13 |
|
| 225 |
3 |
| STRLEN need = (STRLEN)got + want + 1; |
| 226 |
3 |
| STRLEN grow = SvLEN(raw) * 2; |
| 227 |
|
| /* |
| 228 |
|
| # uncoverable branch true |
| 229 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 230 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 231 |
3 |
▶ | if (len >= 0 && grow > (STRLEN)len + 1) grow = (STRLEN)len + 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 0 && grow > (STRLEN)len + 1) grow = (STRLEN)len + 1; |
1 |
2 |
3 |
| if (len >= 0 && grow > (STRLEN)len + 1) grow = (STRLEN)len + 1; |
0 |
1 |
1 |
|
| 232 |
|
| /* |
| 233 |
|
| # uncoverable branch true |
| 234 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 235 |
3 |
▶ | if (grow < need) grow = need; |
Branch: 1 branch
| Branch | True | False | Total |
| if (grow < need) grow = need; |
0 |
3 |
3 |
|
| 236 |
3 |
▶ | (void)SvGROW(raw, grow); |
Branch: 2 branches
| Branch | True | False | Total |
| (void)SvGROW(raw, grow); |
3 |
0 |
3 |
| (void)SvGROW(raw, grow); |
3 |
0 |
3 |
|
| 237 |
|
| } |
| 238 |
13 |
| d = SvPVX(raw); |
| 239 |
13 |
| n = PerlIO_read(fp, d + got, want); |
| 240 |
13 |
▶ | if (n <= 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) break; |
3 |
10 |
13 |
|
| 241 |
10 |
| got += (IV)n; |
| 242 |
10 |
| SvCUR_set(raw, (STRLEN)got); |
| 243 |
10 |
| SvPVX(raw)[got] = '\0'; |
| 244 |
|
| /* |
| 245 |
|
| # uncoverable branch true |
| 246 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 247 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 248 |
10 |
▶ | if (max_body > 0 && got > max_body) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (max_body > 0 && got > max_body) { |
7 |
3 |
10 |
| if (max_body > 0 && got > max_body) { |
0 |
7 |
7 |
|
| 249 |
0 |
| SvREFCNT_dec(raw); /* # uncoverable statement */ |
| 250 |
0 |
| *over = 1; /* # uncoverable statement */ |
| 251 |
0 |
| (void)PerlIO_seek(fp, 0, SEEK_SET); /* # uncoverable statement */ |
| 252 |
0 |
| return NULL; /* # uncoverable statement */ |
| 253 |
|
| } |
| 254 |
|
| } |
| 255 |
|
| /* |
| 256 |
|
| # uncoverable branch false |
| 257 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 258 |
10 |
▶ | if (cl_out) *cl_out = got; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cl_out) *cl_out = got; |
10 |
0 |
10 |
|
| 259 |
10 |
| (void)PerlIO_seek(fp, 0, SEEK_SET); |
| 260 |
10 |
| return raw; |
| 261 |
|
| } |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
|
| /* Assemble the validate_request raw-inputs hash in C from the env and the |
| 265 |
|
| * routed captures: { path => \%caps, query => $qs, header => \%lc, |
| 266 |
|
| * body => $bytes }. HTTP_* -> lowercased dash-separated names, CONTENT_TYPE |
| 267 |
|
| * and a truthy CONTENT_LENGTH folded in, body only when present. Owned (+1). */ |
| 268 |
58 |
| static HV *punk_oa_build_raw(pTHX_ HV *env, HV *caps, SV *body, IV cl) { |
| 269 |
58 |
| HV *raw = newHV(); |
| 270 |
58 |
| HV *headers = pq_headers(aTHX_ env); /* punk_request.h, shared with |
| 271 |
|
| * Punk::Request::headers */ |
| 272 |
|
| SV **e; |
| 273 |
|
| |
| 274 |
58 |
| (void)hv_stores(raw, "path", newRV_inc((SV *)caps)); |
| 275 |
58 |
▶ | e = env ? hv_fetchs(env, "QUERY_STRING", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| e = env ? hv_fetchs(env, "QUERY_STRING", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
58 |
0 |
58 |
|
| 276 |
58 |
▶ | (void)hv_stores(raw, "query", |
Branch: 3 branches
| Branch | True | False | Total |
| (void)hv_stores(raw, "query", |
56 |
2 |
58 |
| (void)hv_stores(raw, "query", |
56 |
0 |
56 |
| (void)hv_stores(raw, "query", |
56 |
0 |
56 |
|
| 277 |
|
| (e && *e && SvOK(*e)) ? newSVsv(*e) : newSVpvs("")); |
| 278 |
|
| |
| 279 |
|
| /* the length actually read, not the one the environment claims - the |
| 280 |
|
| * body was framed by this, and validation should see the same */ |
| 281 |
58 |
▶ | if (cl) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cl) |
10 |
48 |
58 |
|
| 282 |
10 |
| (void)hv_stores(headers, "content-length", newSViv(cl)); |
| 283 |
58 |
| (void)hv_stores(raw, "header", newRV_noinc((SV *)headers)); |
| 284 |
|
| |
| 285 |
|
| /* |
| 286 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 287 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 288 |
58 |
▶ | if (body && SvOK(body) && SvCUR(body) > 0) |
Branch: 3 branches
| Branch | True | False | Total |
| if (body && SvOK(body) && SvCUR(body) > 0) |
10 |
48 |
58 |
| if (body && SvOK(body) && SvCUR(body) > 0) |
10 |
0 |
10 |
| if (body && SvOK(body) && SvCUR(body) > 0) |
10 |
0 |
10 |
|
| 289 |
10 |
| (void)hv_stores(raw, "body", newSVsv(body)); |
| 290 |
58 |
| return raw; |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
|
| /* CONTENT_LENGTH against a ceiling: the 413 triplet (+1) when the declared |
| 294 |
|
| * body is over it, else NULL. One implementation, two callers - the OpenAPI |
| 295 |
|
| * mount's `max_body_size` and the `max_body` keyword - so a plain route and |
| 296 |
|
| * an API operation answer a client byte for byte identically. |
| 297 |
|
| * |
| 298 |
|
| * A request whose length was never declared cannot be answered here, and |
| 299 |
|
| * HTTP/2 and HTTP/3 make that shape ordinary rather than exotic: both forbid |
| 300 |
|
| * Transfer-Encoding, so a streamed upload arrives with no length at all. |
| 301 |
|
| * Returning NULL and stopping there left those requests with no ceiling |
| 302 |
|
| * whatsoever. Instead the ceiling is published into the environment, where |
| 303 |
|
| * every body reader picks it up as its default `max` (pq_env_ceiling) and |
| 304 |
|
| * stops on the bytes that actually arrive. That is a later and less thrifty |
| 305 |
|
| * refusal than a 413 before the guards - the body has been read up to the |
| 306 |
|
| * ceiling by then - but it is a refusal, and it is the earliest one the |
| 307 |
|
| * declared length permits. |
| 308 |
|
| * |
| 309 |
|
| * Note what this is NOT. By the time any of this runs the body is already |
| 310 |
|
| * resident in the server's buffer; the memory was spent before Punk was |
| 311 |
|
| * called. What the check buys is the parse, the multipart walk, the guards, |
| 312 |
|
| * the handler, and an honest answer instead of a mysterious success. The |
| 313 |
|
| * memory bound is the server's (Hyperman's `max_body`), and this cannot |
| 314 |
|
| * stand in for it. */ |
| 315 |
2144 |
| static SV *pd_body_limit(pTHX_ HV *env, IV max_body) { |
| 316 |
|
| SV **e; |
| 317 |
|
| IV cl; |
| 318 |
2144 |
▶ | if (max_body <= 0 || !env) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (max_body <= 0 || !env) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
2141 |
3 |
2144 |
| if (max_body <= 0 || !env) return NULL; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
2141 |
2141 |
|
| 319 |
2141 |
| (void)hv_stores(env, PQ_ENV_MAX_BODY, newSViv(max_body)); |
| 320 |
2141 |
| e = hv_fetchs(env, "CONTENT_LENGTH", 0); |
| 321 |
|
| /* |
| 322 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 323 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 324 |
2141 |
▶ | cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
88 |
2053 |
2141 |
| cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
88 |
0 |
88 |
| cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
88 |
0 |
88 |
|
| 325 |
2141 |
▶ | if (cl <= max_body) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cl <= max_body) return NULL; |
2125 |
16 |
2141 |
|
| 326 |
16 |
| return punk_triplet(aTHX_ 413, sv_2mortal(newSVpvs("application/json")), |
| 327 |
|
| newSVpvs(PD_ERR_413), NULL); |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
|
| /* Dispatch one matched operation in C. *out_ret is the controller's return |
| 331 |
|
| * value or an early PSGI triplet (+1 owned, or the immortal undef); *out_err |
| 332 |
|
| * is a die message SV (+1) or the immortal undef. Mirrors the old Perl |
| 333 |
|
| * Punk::Mount::OpenAPI::dispatch exactly. */ |
| 334 |
79 |
| static void punk_oa_dispatch(pTHX_ SV *c, AV *before, HV *rec, SV *api, |
| 335 |
|
| SV *op_id, SV *caps_sv, IV max_body, |
| 336 |
|
| SV **out_ret, SV **out_err) { |
| 337 |
79 |
| const oa_abi *A = punk_oa(aTHX); /* croaks if the ABI is unavailable */ |
| 338 |
79 |
| SV *ret = &PL_sv_undef, *err = &PL_sv_undef; |
| 339 |
|
| SV **codep, **guardsp; |
| 340 |
|
| HV *env, *rawh, *typed, *caps; |
| 341 |
|
| AV *errs; |
| 342 |
|
| SV *body; |
| 343 |
79 |
| IV cl = 0; |
| 344 |
|
| void *apip, *op; |
| 345 |
79 |
| int ok, died = 0; |
| 346 |
|
| |
| 347 |
|
| /* |
| 348 |
|
| # uncoverable branch true |
| 349 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 350 |
79 |
▶ | if (pd_run_chain(aTHX_ before, c, &ret, &err)) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pd_run_chain(aTHX_ before, c, &ret, &err)) goto done; |
0 |
79 |
79 |
|
| 351 |
79 |
| guardsp = hv_fetchs(rec, "guards", 0); |
| 352 |
|
| /* |
| 353 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 354 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 355 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 356 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 357 |
79 |
▶ | if (guardsp && *guardsp && SvROK(*guardsp) |
Branch: 3 branches
| Branch | True | False | Total |
| if (guardsp && *guardsp && SvROK(*guardsp) |
79 |
0 |
79 |
| if (guardsp && *guardsp && SvROK(*guardsp) |
79 |
0 |
79 |
| if (guardsp && *guardsp && SvROK(*guardsp) |
79 |
0 |
79 |
|
| 358 |
79 |
▶ | && SvTYPE(SvRV(*guardsp)) == SVt_PVAV |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*guardsp)) == SVt_PVAV |
79 |
0 |
79 |
|
| 359 |
79 |
▶ | && pd_run_chain(aTHX_ (AV *)SvRV(*guardsp), c, &ret, &err)) |
Branch: 1 branch
| Branch | True | False | Total |
| && pd_run_chain(aTHX_ (AV *)SvRV(*guardsp), c, &ret, &err)) |
16 |
63 |
79 |
|
| 360 |
16 |
| goto done; |
| 361 |
|
| |
| 362 |
63 |
| env = pd_c_env(aTHX_ c); |
| 363 |
|
| /* |
| 364 |
|
| # uncoverable branch false |
| 365 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 366 |
63 |
▶ | if (env) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (env) { |
63 |
0 |
63 |
|
| 367 |
63 |
| SV **e = hv_fetchs(env, "CONTENT_LENGTH", 0); |
| 368 |
63 |
▶ | cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
45 |
18 |
63 |
| cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
45 |
0 |
45 |
| cl = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
43 |
2 |
45 |
|
| 369 |
|
| } |
| 370 |
|
| { |
| 371 |
63 |
| SV *over = pd_body_limit(aTHX_ env, max_body); |
| 372 |
63 |
▶ | if (over) { ret = over; goto done; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (over) { ret = over; goto done; } |
2 |
61 |
63 |
|
| 373 |
|
| } |
| 374 |
61 |
| codep = hv_fetchs(rec, "code", 0); |
| 375 |
|
| /* |
| 376 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 377 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 378 |
61 |
▶ | if (!(codep && *codep && SvOK(*codep))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(codep && *codep && SvOK(*codep))) { |
61 |
0 |
61 |
| if (!(codep && *codep && SvOK(*codep))) { |
61 |
0 |
61 |
| if (!(codep && *codep && SvOK(*codep))) { |
3 |
58 |
61 |
|
| 379 |
3 |
| ret = punk_triplet(aTHX_ 501, sv_2mortal(newSVpvs("application/json")), |
| 380 |
|
| newSVpvs(PD_ERR_501), NULL); |
| 381 |
3 |
| goto done; |
| 382 |
|
| } |
| 383 |
|
| |
| 384 |
58 |
▶ | caps = (caps_sv && SvROK(caps_sv) && SvTYPE(SvRV(caps_sv)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| caps = (caps_sv && SvROK(caps_sv) && SvTYPE(SvRV(caps_sv)) == SVt_PVHV) |
58 |
0 |
58 |
| caps = (caps_sv && SvROK(caps_sv) && SvTYPE(SvRV(caps_sv)) == SVt_PVHV) |
58 |
0 |
58 |
|
| 385 |
116 |
▶ | ? (HV *)SvRV(caps_sv) : (HV *)sv_2mortal((SV *)newHV()); |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(caps_sv) : (HV *)sv_2mortal((SV *)newHV()); |
58 |
0 |
58 |
|
| 386 |
58 |
| apip = A->api_of(aTHX_ api); |
| 387 |
58 |
▶ | op = apip ? A->op_by_id(aTHX_ apip, op_id) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| op = apip ? A->op_by_id(aTHX_ apip, op_id) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
58 |
0 |
58 |
|
| 388 |
|
| /* |
| 389 |
|
| # uncoverable branch true |
| 390 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 391 |
58 |
▶ | if (!op) croak("Punk: unknown operationId '%s'", SvPV_nolen(op_id)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!op) croak("Punk: unknown operationId '%s'", SvPV_nolen(op_id)); |
0 |
58 |
58 |
|
| 392 |
|
| |
| 393 |
|
| { |
| 394 |
58 |
| int over = 0; |
| 395 |
58 |
| body = punk_oa_env_body(aTHX_ env, &cl, max_body, &over); |
| 396 |
|
| /* |
| 397 |
|
| # uncoverable branch true |
| 398 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 399 |
58 |
▶ | if (over) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (over) { |
0 |
58 |
58 |
|
| 400 |
0 |
| ret = punk_triplet(aTHX_ 413, /* # uncoverable statement */ |
| 401 |
|
| sv_2mortal(newSVpvs("application/json")), /* # uncoverable statement */ |
| 402 |
|
| newSVpvs(PD_ERR_413), NULL); /* # uncoverable statement */ |
| 403 |
0 |
| goto done; /* # uncoverable statement */ |
| 404 |
|
| } |
| 405 |
|
| } |
| 406 |
58 |
▶ | if (body) body = sv_2mortal(body); |
Branch: 1 branch
| Branch | True | False | Total |
| if (body) body = sv_2mortal(body); |
10 |
48 |
58 |
|
| 407 |
58 |
| rawh = punk_oa_build_raw(aTHX_ env, caps, body, cl); |
| 408 |
58 |
| typed = (HV *)sv_2mortal((SV *)newHV()); |
| 409 |
58 |
| errs = (AV *)sv_2mortal((SV *)newAV()); |
| 410 |
58 |
| ok = A->validate(aTHX_ apip, op, rawh, typed, errs); |
| 411 |
58 |
| SvREFCNT_dec((SV *)rawh); |
| 412 |
58 |
▶ | if (!ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) { |
13 |
45 |
58 |
|
| 413 |
13 |
| HV *e = newHV(); |
| 414 |
|
| SV *bytes; |
| 415 |
13 |
| (void)hv_stores(e, "errors", newRV_inc((SV *)errs)); |
| 416 |
13 |
| bytes = punk_frj(aTHX)->encode(aTHX_ |
| 417 |
|
| sv_2mortal(newRV_noinc((SV *)e)), NULL); |
| 418 |
13 |
| ret = punk_triplet(aTHX_ 400, sv_2mortal(newSVpvs("application/json")), |
| 419 |
|
| bytes, NULL); |
| 420 |
13 |
| goto done; |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
45 |
| pd_stash(aTHX_ c, newRV_inc((SV *)typed)); /* pd_stash takes the ref */ |
| 424 |
45 |
| ret = pd_call(aTHX_ *codep, c, &died); |
| 425 |
45 |
▶ | if (died) { err = newSVsv(ERRSV); SvREFCNT_dec(ret); ret = &PL_sv_undef; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (died) { err = newSVsv(ERRSV); SvREFCNT_dec(ret); ret = &PL_sv_undef; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
44 |
1 |
45 |
| if (died) { err = newSVsv(ERRSV); SvREFCNT_dec(ret); ret = &PL_sv_undef; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
1 |
0 |
1 |
|
| 426 |
|
| |
| 427 |
44 |
| done: |
| 428 |
79 |
| *out_ret = ret; |
| 429 |
79 |
| *out_err = err; |
| 430 |
79 |
| } |
| 431 |
|
| |
| 432 |
|
| #endif /* PUNK_DISPATCH_H */ |