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