| 1 |
|
| #ifndef OA_PLACK_H |
| 2 |
|
| #define OA_PLACK_H |
| 3 |
|
| |
| 4 |
|
| /* The PSGI adapter, all in C: $api->to_app(handlers => {...}) returns a PSGI |
| 5 |
|
| * coderef (a magic CV carrying its captures) whose request path is |
| 6 |
|
| * route -> validate -> dispatch -> respond. A handler may return a PSGI |
| 7 |
|
| * triplet, plain data (JSON-encoded as a 200), or - on a non-blocking server |
| 8 |
|
| * (psgi.nonblocking, e.g. Hyperman) - a Future-like object resolving to one |
| 9 |
|
| * of those, which is passed straight through for the server to await. On |
| 10 |
|
| * blocking servers a returned Future is ->get awaited inline. */ |
| 11 |
|
| |
| 12 |
|
| /* ---- closures: a CV carrying captured SVs (the rp_closure pattern) -------- */ |
| 13 |
|
| |
| 14 |
|
| typedef struct oa_clos { AV *cap; } oa_clos; |
| 15 |
|
| |
| 16 |
578 |
| static int oa_clos_free(pTHX_ SV *sv, MAGIC *mg) { |
| 17 |
578 |
| oa_clos *c = (oa_clos *)mg->mg_ptr; |
| 18 |
|
| PERL_UNUSED_ARG(sv); |
| 19 |
578 |
▶ | if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
578 |
0 |
578 |
| if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
578 |
0 |
578 |
|
| 20 |
578 |
| return 0; |
| 21 |
|
| } |
| 22 |
|
| static MGVTBL oa_clos_vtbl = { NULL, NULL, NULL, NULL, oa_clos_free, |
| 23 |
|
| NULL, NULL, NULL }; |
| 24 |
|
| |
| 25 |
579 |
| static SV *oa_closure(pTHX_ XSUBADDR_t body, AV *cap) { |
| 26 |
579 |
| CV *cv = (CV *)newXS(NULL, body, __FILE__); |
| 27 |
|
| oa_clos *c; |
| 28 |
579 |
| Newxz(c, 1, oa_clos); |
| 29 |
579 |
| c->cap = cap; /* takes ownership */ |
| 30 |
579 |
| sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &oa_clos_vtbl, (char *)c, 0); |
| 31 |
579 |
| return newRV_noinc((SV *)cv); |
| 32 |
|
| } |
| 33 |
621 |
| static AV *oa_clos_cap(pTHX_ CV *cv) { |
| 34 |
621 |
| MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &oa_clos_vtbl); |
| 35 |
621 |
▶ | return mg ? ((oa_clos *)mg->mg_ptr)->cap : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return mg ? ((oa_clos *)mg->mg_ptr)->cap : NULL; |
621 |
0 |
621 |
|
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| /* ---- JSON responses -------------------------------------------------------- */ |
| 39 |
|
| |
| 40 |
|
| /* Encode a Perl value through the frj ABI. The ABI croaks on an unencodable |
| 41 |
|
| * shape, so the encode runs inside the private _frj_encode trampoline (a |
| 42 |
|
| * direct ABI call), invoked on its cached CV with G_EVAL - a bad handler |
| 43 |
|
| * return degrades instead of escaping the request. Mortal SV on success, |
| 44 |
|
| * NULL on failure. OA_ENC_CV is resolved in BOOT (API.xs). */ |
| 45 |
563 |
| static SV *oa_json_encode(pTHX_ SV *val) { |
| 46 |
563 |
| dSP; int count; SV *ret = NULL; |
| 47 |
563 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
563 |
563 |
|
| 48 |
563 |
▶ | XPUSHs(val); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(val); PUTBACK; |
0 |
563 |
563 |
|
| 49 |
563 |
| count = call_sv(OA_ENC_CV, G_SCALAR | G_EVAL); |
| 50 |
563 |
| SPAGAIN; |
| 51 |
563 |
▶ | if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) ret = newSVsv(r); } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) ret = newSVsv(r); } |
563 |
0 |
563 |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) ret = newSVsv(r); } |
563 |
0 |
563 |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) ret = newSVsv(r); } |
563 |
0 |
563 |
| if (!SvTRUE(ERRSV) && count > 0) { SV *r = POPs; if (SvOK(r)) ret = newSVsv(r); } |
563 |
0 |
563 |
|
| 52 |
0 |
▶ | else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 53 |
563 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
563 |
0 |
563 |
|
| 54 |
563 |
▶ | return ret ? sv_2mortal(ret) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return ret ? sv_2mortal(ret) : NULL; |
563 |
0 |
563 |
|
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| /* [ $status, ['Content-Type','application/json'], [$json] ] (+1) */ |
| 58 |
558 |
| static SV *oa_json_response(pTHX_ int status, SV *val) { |
| 59 |
558 |
| AV *h = newAV(), *b = newAV(), *r = newAV(); |
| 60 |
558 |
| SV *json = oa_json_encode(aTHX_ val); |
| 61 |
558 |
| av_push(h, newSVpvs("Content-Type")); |
| 62 |
558 |
| av_push(h, newSVpvs("application/json")); |
| 63 |
558 |
▶ | av_push(b, json ? newSVsv(json) : newSVpvs("null")); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(b, json ? newSVsv(json) : newSVpvs("null")); |
558 |
0 |
558 |
|
| 64 |
558 |
| av_push(r, newSViv(status)); |
| 65 |
558 |
| av_push(r, newRV_noinc((SV *)h)); |
| 66 |
558 |
| av_push(r, newRV_noinc((SV *)b)); |
| 67 |
558 |
| return newRV_noinc((SV *)r); |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| /* { errors => \@errs } as a JSON response (+1). errs borrowed. */ |
| 71 |
39 |
| static SV *oa_error_response(pTHX_ int status, AV *errs) { |
| 72 |
39 |
| HV *wrap = newHV(); |
| 73 |
|
| SV *rv, *w; |
| 74 |
39 |
| (void)hv_stores(wrap, "errors", newRV_inc((SV *)errs)); |
| 75 |
39 |
| w = sv_2mortal(newRV_noinc((SV *)wrap)); |
| 76 |
39 |
| rv = oa_json_response(aTHX_ status, w); |
| 77 |
39 |
| return rv; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| /* one-message error response (+1) */ |
| 81 |
29 |
| static SV *oa_error_msg(pTHX_ int status, const char *msg) { |
| 82 |
29 |
| AV *errs = (AV *)sv_2mortal((SV *)newAV()); |
| 83 |
29 |
| HV *e = newHV(); |
| 84 |
29 |
| (void)hv_stores(e, "message", newSVpv(msg, 0)); |
| 85 |
29 |
| av_push(errs, newRV_noinc((SV *)e)); |
| 86 |
29 |
| return oa_error_response(aTHX_ status, errs); |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| /* ---- env access -------------------------------------------------------------- */ |
| 90 |
|
| |
| 91 |
4235 |
| static SV *oa_env_get(pTHX_ HV *env, const char *k) { |
| 92 |
4235 |
| SV **e = hv_fetch(env, k, (I32)strlen(k), 0); |
| 93 |
4235 |
▶ | return (e && *e && SvOK(*e)) ? *e : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
1967 |
2268 |
4235 |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
1967 |
0 |
1967 |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
1967 |
0 |
1967 |
|
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
|
| /* read CONTENT_LENGTH bytes from psgi.input (the rp_read_body pattern) */ |
| 97 |
12 |
| static SV *oa_read_body_env(pTHX_ HV *env) { |
| 98 |
12 |
| SV *cl = oa_env_get(aTHX_ env, "CONTENT_LENGTH"); |
| 99 |
12 |
| SV *in = oa_env_get(aTHX_ env, "psgi.input"); |
| 100 |
12 |
| IV len, off = 0; |
| 101 |
|
| SV *body; |
| 102 |
12 |
▶ | if (!cl || !in) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cl || !in) return NULL; |
12 |
0 |
12 |
| if (!cl || !in) return NULL; |
0 |
12 |
12 |
|
| 103 |
12 |
| len = SvIV(cl); |
| 104 |
12 |
▶ | if (len <= 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len <= 0) return NULL; |
0 |
12 |
12 |
|
| 105 |
|
| /* psgi.input is usually a plain filehandle, and read() on one is a method |
| 106 |
|
| * call. Perl only auto-loads IO::Handle to resolve filehandle methods from |
| 107 |
|
| * 5.14 on; before that the call simply fails, and under the G_EVAL below |
| 108 |
|
| * that failure is silent - the body read back empty and a perfectly good |
| 109 |
|
| * POST was rejected as "missing required request body". Load it once. */ |
| 110 |
|
| { |
| 111 |
|
| static int oa_io_handle_loaded = 0; |
| 112 |
12 |
▶ | if (!oa_io_handle_loaded) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_io_handle_loaded) { |
3 |
9 |
12 |
|
| 113 |
3 |
| oa_io_handle_loaded = 1; |
| 114 |
3 |
| eval_pv("require IO::Handle;", FALSE); |
| 115 |
|
| } |
| 116 |
|
| } |
| 117 |
12 |
| body = sv_2mortal(newSVpvs("")); |
| 118 |
24 |
▶ | while (off < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (off < len) { |
12 |
12 |
24 |
|
| 119 |
12 |
| dSP; int n; IV got; |
| 120 |
12 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
12 |
12 |
|
| 121 |
12 |
▶ | EXTEND(SP, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 4); |
0 |
12 |
12 |
|
| 122 |
12 |
| PUSHs(in); |
| 123 |
12 |
| PUSHs(body); |
| 124 |
12 |
| PUSHs(sv_2mortal(newSViv(len - off))); |
| 125 |
12 |
| PUSHs(sv_2mortal(newSViv(off))); |
| 126 |
12 |
| PUTBACK; |
| 127 |
12 |
| n = call_method("read", G_SCALAR | G_EVAL); |
| 128 |
12 |
| SPAGAIN; |
| 129 |
12 |
▶ | got = (!SvTRUE(ERRSV) && n > 0) ? POPi : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| got = (!SvTRUE(ERRSV) && n > 0) ? POPi : 0; |
12 |
0 |
12 |
| got = (!SvTRUE(ERRSV) && n > 0) ? POPi : 0; |
12 |
0 |
12 |
| got = (!SvTRUE(ERRSV) && n > 0) ? POPi : 0; |
12 |
0 |
12 |
|
| 130 |
12 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
12 |
0 |
12 |
|
| 131 |
12 |
▶ | if (got <= 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (got <= 0) break; |
0 |
12 |
12 |
|
| 132 |
12 |
| off += got; |
| 133 |
|
| } |
| 134 |
12 |
| return body; |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
|
| /* Build the minimal lowercased header HV oa_validate_op needs for this op: |
| 138 |
|
| * content-type, cookie, and each declared header parameter (fresh HV). */ |
| 139 |
594 |
| static HV *oa_headers_for(pTHX_ oa_op *o, HV *env) { |
| 140 |
594 |
| HV *out = newHV(); |
| 141 |
|
| SV *v; |
| 142 |
|
| int i; |
| 143 |
594 |
▶ | if ((v = oa_env_get(aTHX_ env, "CONTENT_TYPE"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_env_get(aTHX_ env, "CONTENT_TYPE"))) |
29 |
565 |
594 |
|
| 144 |
29 |
| (void)hv_stores(out, "content-type", newSVsv(v)); |
| 145 |
594 |
▶ | if ((v = oa_env_get(aTHX_ env, "HTTP_COOKIE"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v = oa_env_get(aTHX_ env, "HTTP_COOKIE"))) |
11 |
583 |
594 |
|
| 146 |
11 |
| (void)hv_stores(out, "cookie", newSVsv(v)); |
| 147 |
1126 |
▶ | for (i = 0; i < o->nparams[OA_IN_HEADER]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < o->nparams[OA_IN_HEADER]; i++) { |
532 |
594 |
1126 |
|
| 148 |
532 |
| STRLEN nl; const char *np = SvPV_const(o->params[OA_IN_HEADER][i].name, nl); |
| 149 |
|
| char key[160], lc[128]; |
| 150 |
|
| STRLEN k; |
| 151 |
|
| SV **e; |
| 152 |
532 |
▶ | if (nl + 5 >= sizeof key || nl >= sizeof lc) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (nl + 5 >= sizeof key || nl >= sizeof lc) continue; |
532 |
0 |
532 |
| if (nl + 5 >= sizeof key || nl >= sizeof lc) continue; |
0 |
532 |
532 |
|
| 153 |
532 |
| memcpy(key, "HTTP_", 5); |
| 154 |
6916 |
▶ | for (k = 0; k < nl; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nl; k++) { |
6384 |
532 |
6916 |
|
| 155 |
6384 |
| char c = np[k]; |
| 156 |
6384 |
▶ | key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
Branch: 2 branches
| Branch | True | False | Total |
| key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
5320 |
1064 |
6384 |
| key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
3724 |
1596 |
5320 |
|
| 157 |
6384 |
▶ | lc[k] = (char)toLOWER((U8)c); |
Branch: 1 branch
| Branch | True | False | Total |
| lc[k] = (char)toLOWER((U8)c); |
1596 |
4788 |
6384 |
|
| 158 |
|
| } |
| 159 |
532 |
| key[5 + nl] = '\0'; |
| 160 |
532 |
| e = hv_fetch(env, key, (I32)(5 + nl), 0); |
| 161 |
532 |
▶ | if (e && *e && SvOK(*e)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) |
0 |
532 |
532 |
| if (e && *e && SvOK(*e)) |
0 |
0 |
0 |
| if (e && *e && SvOK(*e)) |
0 |
0 |
0 |
|
| 162 |
0 |
| (void)hv_store(out, lc, (I32)nl, newSVsv(*e), 0); |
| 163 |
|
| } |
| 164 |
594 |
| return out; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* ---- handler result mapping ---------------------------------------------------- */ |
| 168 |
|
| |
| 169 |
|
| /* a PSGI triplet: [ status, \@headers, ... ] */ |
| 170 |
3033 |
| static int oa_is_triplet(pTHX_ SV *sv) { |
| 171 |
|
| AV *av; |
| 172 |
|
| SV **s, **h; |
| 173 |
3033 |
▶ | if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return 0; |
3033 |
0 |
3033 |
| if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return 0; |
3026 |
7 |
3033 |
| if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV) return 0; |
506 |
2520 |
3026 |
|
| 174 |
2520 |
| av = (AV *)SvRV(sv); |
| 175 |
2520 |
▶ | if (av_len(av) + 1 != 3) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(av) + 1 != 3) return 0; |
13 |
2507 |
2520 |
|
| 176 |
2507 |
| s = av_fetch(av, 0, 0); |
| 177 |
2507 |
| h = av_fetch(av, 1, 0); |
| 178 |
2507 |
▶ | if (!s || !*s || !SvOK(*s) || SvROK(*s)) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!s || !*s || !SvOK(*s) || SvROK(*s)) return 0; |
2507 |
0 |
2507 |
| if (!s || !*s || !SvOK(*s) || SvROK(*s)) return 0; |
2507 |
0 |
2507 |
| if (!s || !*s || !SvOK(*s) || SvROK(*s)) return 0; |
2507 |
0 |
2507 |
| if (!s || !*s || !SvOK(*s) || SvROK(*s)) return 0; |
0 |
2507 |
2507 |
|
| 179 |
2507 |
▶ | if (!looks_like_number(*s)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!looks_like_number(*s)) return 0; |
0 |
2507 |
2507 |
|
| 180 |
2507 |
▶ | if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVAV) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVAV) return 0; |
2507 |
0 |
2507 |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVAV) return 0; |
2507 |
0 |
2507 |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVAV) return 0; |
2507 |
0 |
2507 |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVAV) return 0; |
0 |
2507 |
2507 |
|
| 181 |
2507 |
| return 1; |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
|
| /* an object with on_ready (a Future the server can await) */ |
| 185 |
583 |
| static int oa_is_future(pTHX_ SV *sv) { |
| 186 |
583 |
▶ | return sv && SvROK(sv) && SvOBJECT(SvRV(sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| return sv && SvROK(sv) && SvOBJECT(SvRV(sv)) |
583 |
0 |
583 |
| return sv && SvROK(sv) && SvOBJECT(SvRV(sv)) |
2 |
581 |
583 |
|
| 187 |
1166 |
▶ | && gv_fetchmethod_autoload(SvSTASH(SvRV(sv)), "on_ready", 0) != NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| && gv_fetchmethod_autoload(SvSTASH(SvRV(sv)), "on_ready", 0) != NULL; |
583 |
0 |
583 |
| && gv_fetchmethod_autoload(SvSTASH(SvRV(sv)), "on_ready", 0) != NULL; |
2 |
0 |
2 |
|
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* triplet passes through; anything else JSON-encodes as a 200 (+1) */ |
| 191 |
584 |
| static SV *oa_map_result(pTHX_ SV *res) { |
| 192 |
584 |
▶ | if (oa_is_triplet(aTHX_ res)) return newSVsv(res); |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_is_triplet(aTHX_ res)) return newSVsv(res); |
65 |
519 |
584 |
|
| 193 |
519 |
| return oa_json_response(aTHX_ 200, res); |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| /* ---- response validation ------------------------------------------------------- * |
| 197 |
|
| * |
| 198 |
|
| * Two modes over one code path: |
| 199 |
|
| * |
| 200 |
|
| * OA_RV_REPLACE (validate_responses => 1) - the dev tool. A mismatch |
| 201 |
|
| * *replaces* the response with a 500 describing it. Unchanged from |
| 202 |
|
| * 0.06, defaults included: somebody's dev server depends on it. |
| 203 |
|
| * OA_RV_REPORT (validate_responses => 'report') - the product. The |
| 204 |
|
| * errors go to a coderef and NULL comes back, so the triplet the |
| 205 |
|
| * client receives is the one the handler built, byte for byte. |
| 206 |
|
| * |
| 207 |
|
| * Before any of that, a sampling gate: a per-operation counter, so an |
| 208 |
|
| * unsampled response costs one increment and one comparison and never |
| 209 |
|
| * touches the body. Then the skip rules, in order, each with its own |
| 210 |
|
| * counter - coverage is a reported number, not a silent gap. */ |
| 211 |
|
| |
| 212 |
|
| #define OA_RV_OFF 0 |
| 213 |
|
| #define OA_RV_REPLACE 1 |
| 214 |
|
| #define OA_RV_REPORT 2 |
| 215 |
|
| |
| 216 |
|
| /* defined below, with the rest of the response-finalization helpers */ |
| 217 |
|
| static AV *oa_resp_headers(pTHX_ SV *resp); |
| 218 |
|
| |
| 219 |
|
| typedef struct oa_rvcfg { |
| 220 |
|
| int mode; /* OA_RV_* */ |
| 221 |
|
| SV *report; /* coderef for report mode (borrowed), or NULL */ |
| 222 |
|
| IV sample; /* validate 1 response in N; 0/1 = all of them */ |
| 223 |
|
| IV max_body; /* skip bodies over this many bytes; 0 = no cap */ |
| 224 |
|
| } oa_rvcfg; |
| 225 |
|
| |
| 226 |
|
| /* the value of a response header, or NULL (borrowed) */ |
| 227 |
601 |
| static SV *oa_hdr_get(pTHX_ AV *ha, const char *name, STRLEN nl) { |
| 228 |
601 |
▶ | SSize_t i, n = ha ? av_len(ha) + 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t i, n = ha ? av_len(ha) + 1 : 0; |
601 |
0 |
601 |
|
| 229 |
613 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
600 |
13 |
613 |
|
| 230 |
600 |
| SV **k = av_fetch(ha, i, 0); |
| 231 |
600 |
▶ | if (k && *k) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (k && *k) { |
600 |
0 |
600 |
| if (k && *k) { |
600 |
0 |
600 |
|
| 232 |
600 |
| STRLEN kl; const char *kp = SvPV_const(*k, kl); |
| 233 |
600 |
▶ | if (oa_ci_eq(kp, kl, name, nl)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_ci_eq(kp, kl, name, nl)) { |
588 |
12 |
600 |
|
| 234 |
588 |
| SV **v = av_fetch(ha, i + 1, 0); |
| 235 |
588 |
▶ | return (v && *v && SvOK(*v)) ? *v : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (v && *v && SvOK(*v)) ? *v : NULL; |
588 |
0 |
588 |
| return (v && *v && SvOK(*v)) ? *v : NULL; |
588 |
0 |
588 |
| return (v && *v && SvOK(*v)) ? *v : NULL; |
588 |
0 |
588 |
|
| 236 |
|
| } |
| 237 |
|
| } |
| 238 |
|
| } |
| 239 |
13 |
| return NULL; |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
|
| /* the declared schema for this status (exact, then `default`), or NULL. |
| 243 |
|
| * `ctype_ok` reports whether the operation declares a JSON body for it, |
| 244 |
|
| * which is a different question from whether one was compiled. */ |
| 245 |
580 |
| static SV *oa_resp_handle(pTHX_ oa_op *o, int status) { |
| 246 |
|
| char sbuf[8]; |
| 247 |
|
| STRLEN sl; |
| 248 |
|
| int i; |
| 249 |
580 |
| sl = (STRLEN)my_snprintf(sbuf, sizeof sbuf, "%d", status); |
| 250 |
587 |
▶ | for (i = 0; i < o->nresps; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < o->nresps; i++) { |
579 |
8 |
587 |
|
| 251 |
579 |
| STRLEN rl; const char *rp = SvPV_const(o->resps[i].status, rl); |
| 252 |
579 |
▶ | if (rl == sl && memEQ(rp, sbuf, sl)) return o->resps[i].handle; |
Branch: 2 branches
| Branch | True | False | Total |
| if (rl == sl && memEQ(rp, sbuf, sl)) return o->resps[i].handle; |
578 |
1 |
579 |
| if (rl == sl && memEQ(rp, sbuf, sl)) return o->resps[i].handle; |
572 |
6 |
578 |
|
| 253 |
|
| } |
| 254 |
9 |
▶ | for (i = 0; i < o->nresps; i++) { /* a range key: 2XX, 4XX, ... */ |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < o->nresps; i++) { /* a range key: 2XX, 4XX, ... */ |
7 |
2 |
9 |
|
| 255 |
7 |
| STRLEN rl; const char *rp = SvPV_const(o->resps[i].status, rl); |
| 256 |
7 |
▶ | if (oa_status_covers(rp, rl, sbuf, sl)) return o->resps[i].handle; |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_status_covers(rp, rl, sbuf, sl)) return o->resps[i].handle; |
6 |
1 |
7 |
|
| 257 |
|
| } |
| 258 |
2 |
▶ | for (i = 0; i < o->nresps; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < o->nresps; i++) { |
1 |
1 |
2 |
|
| 259 |
1 |
| STRLEN rl; const char *rp = SvPV_const(o->resps[i].status, rl); |
| 260 |
1 |
▶ | if (rl == 7 && memEQ(rp, "default", 7)) return o->resps[i].handle; |
Branch: 2 branches
| Branch | True | False | Total |
| if (rl == 7 && memEQ(rp, "default", 7)) return o->resps[i].handle; |
1 |
0 |
1 |
| if (rl == 7 && memEQ(rp, "default", 7)) return o->resps[i].handle; |
1 |
0 |
1 |
|
| 261 |
|
| } |
| 262 |
1 |
| return NULL; |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| /* $report->($op_id, $status, \@errors, $env) - never allowed to break the |
| 266 |
|
| * response, so it runs under G_EVAL and a die is swallowed after a warn. */ |
| 267 |
35 |
| static void oa_rv_report(pTHX_ SV *cb, SV *op_id, int status, AV *errs, SV *env_rv) { |
| 268 |
35 |
| dSP; |
| 269 |
35 |
▶ | if (!cb || !SvOK(cb)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cb || !SvOK(cb)) return; |
15 |
20 |
35 |
| if (!cb || !SvOK(cb)) return; |
0 |
15 |
15 |
|
| 270 |
15 |
| ENTER; SAVETMPS; |
| 271 |
15 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
15 |
15 |
|
| 272 |
15 |
▶ | EXTEND(SP, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 4); |
0 |
15 |
15 |
|
| 273 |
15 |
▶ | PUSHs(op_id ? op_id : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(op_id ? op_id : &PL_sv_undef); |
15 |
0 |
15 |
|
| 274 |
15 |
| PUSHs(sv_2mortal(newSViv(status))); |
| 275 |
15 |
| PUSHs(sv_2mortal(newRV_inc((SV *)errs))); |
| 276 |
15 |
▶ | PUSHs(env_rv ? env_rv : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(env_rv ? env_rv : &PL_sv_undef); |
15 |
0 |
15 |
|
| 277 |
15 |
| PUTBACK; |
| 278 |
15 |
| (void)call_sv(cb, G_VOID | G_DISCARD | G_EVAL); |
| 279 |
15 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
15 |
0 |
15 |
| if (SvTRUE(ERRSV)) |
1 |
14 |
15 |
|
| 280 |
1 |
▶ | warn("Open::API: response report callback died: %" SVf, SVfARG(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Open::API: response report callback died: %" SVf, SVfARG(ERRSV)); |
1 |
0 |
1 |
|
| 281 |
15 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
15 |
0 |
15 |
|
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
|
| /* The check itself. Returns NULL when there is nothing to answer with - |
| 285 |
|
| * which is every case in report mode, and the conforming case in replace |
| 286 |
|
| * mode - or a 500 triplet (+1) for a replace-mode mismatch. |
| 287 |
|
| * |
| 288 |
|
| * `out_errs`, when given, receives the error AV (borrowed, mortal) so a |
| 289 |
|
| * direct caller can have the findings without a callback. */ |
| 290 |
600 |
| static SV *oa_check_response_cfg(pTHX_ oa_op *o, SV *trip, const oa_rvcfg *rv, |
| 291 |
|
| SV *env_rv, AV **out_errs) { |
| 292 |
|
| AV *tv, *ba, *errs, *ha; |
| 293 |
|
| SV **st, **bd, **b0; |
| 294 |
|
| SV *handle, *data, *cl; |
| 295 |
|
| int status; |
| 296 |
|
| SSize_t j; |
| 297 |
600 |
▶ | if (out_errs) *out_errs = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_errs) *out_errs = NULL; |
560 |
40 |
600 |
|
| 298 |
600 |
▶ | if (!o || rv->mode == OA_RV_OFF || !oa_is_triplet(aTHX_ trip)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!o || rv->mode == OA_RV_OFF || !oa_is_triplet(aTHX_ trip)) return NULL; |
600 |
0 |
600 |
| if (!o || rv->mode == OA_RV_OFF || !oa_is_triplet(aTHX_ trip)) return NULL; |
600 |
0 |
600 |
| if (!o || rv->mode == OA_RV_OFF || !oa_is_triplet(aTHX_ trip)) return NULL; |
0 |
600 |
600 |
|
| 299 |
|
| |
| 300 |
600 |
| o->rv.total++; |
| 301 |
|
| |
| 302 |
|
| /* the sampling gate, before anything touches the body */ |
| 303 |
600 |
| o->rv.seen++; |
| 304 |
600 |
▶ | if (rv->sample > 1 && (o->rv.seen % (unsigned long)rv->sample) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (rv->sample > 1 && (o->rv.seen % (unsigned long)rv->sample) != 0) |
12 |
588 |
600 |
| if (rv->sample > 1 && (o->rv.seen % (unsigned long)rv->sample) != 0) |
9 |
3 |
12 |
|
| 305 |
9 |
| return NULL; |
| 306 |
591 |
| o->rv.sampled++; |
| 307 |
|
| |
| 308 |
591 |
| tv = (AV *)SvRV(trip); |
| 309 |
591 |
| st = av_fetch(tv, 0, 0); |
| 310 |
591 |
| status = (int)SvIV(*st); |
| 311 |
591 |
| ha = oa_resp_headers(aTHX_ trip); |
| 312 |
|
| |
| 313 |
|
| /* `errs` is created up front, and every skip below jumps to `verdict` |
| 314 |
|
| * rather than returning. The declared HEADERS are checked FIRST, above |
| 315 |
|
| * the body skip rules, because those rules return early for a non-JSON, |
| 316 |
|
| * oversized or streaming body - which is exactly when a Location or an |
| 317 |
|
| * X-Rate-Limit header still has to be right. Returning from a skip would |
| 318 |
|
| * throw away findings already made. */ |
| 319 |
591 |
| errs = (AV *)sv_2mortal((SV *)newAV()); |
| 320 |
|
| |
| 321 |
|
| /* 0. declared response headers, by status (exact, range, then default) */ |
| 322 |
591 |
▶ | if (o->nrhdrs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->nrhdrs) { |
8 |
583 |
591 |
|
| 323 |
|
| char sbuf[8]; |
| 324 |
8 |
| STRLEN sl = (STRLEN)my_snprintf(sbuf, sizeof sbuf, "%d", status); |
| 325 |
8 |
| int hi, seen_any = 0; |
| 326 |
16 |
▶ | for (hi = 0; hi < o->nrhdrs; hi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (hi = 0; hi < o->nrhdrs; hi++) { |
8 |
8 |
16 |
|
| 327 |
8 |
| STRLEN rl; const char *rp = SvPV_const(o->rhdrs[hi].status, rl); |
| 328 |
|
| oa_param *pp; |
| 329 |
|
| SV *hv_; |
| 330 |
|
| STRLEN nl; const char *np; |
| 331 |
8 |
▶ | if (!(rl == sl && memEQ(rp, sbuf, sl)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(rl == sl && memEQ(rp, sbuf, sl)) |
8 |
0 |
8 |
| if (!(rl == sl && memEQ(rp, sbuf, sl)) |
1 |
7 |
8 |
|
| 332 |
1 |
▶ | && !oa_status_covers(rp, rl, sbuf, sl) |
Branch: 1 branch
| Branch | True | False | Total |
| && !oa_status_covers(rp, rl, sbuf, sl) |
0 |
1 |
1 |
|
| 333 |
0 |
▶ | && !(rl == 7 && memEQ(rp, "default", 7))) |
Branch: 2 branches
| Branch | True | False | Total |
| && !(rl == 7 && memEQ(rp, "default", 7))) |
0 |
0 |
0 |
| && !(rl == 7 && memEQ(rp, "default", 7))) |
0 |
0 |
0 |
|
| 334 |
5 |
| continue; |
| 335 |
8 |
| seen_any = 1; |
| 336 |
8 |
| pp = &o->rhdrs[hi].p; |
| 337 |
8 |
| np = SvPV_const(pp->name, nl); |
| 338 |
8 |
| hv_ = oa_hdr_get(aTHX_ ha, np, nl); |
| 339 |
8 |
▶ | if (!hv_ || !SvOK(hv_)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!hv_ || !SvOK(hv_)) { |
3 |
5 |
8 |
| if (!hv_ || !SvOK(hv_)) { |
0 |
3 |
3 |
|
| 340 |
5 |
▶ | if (pp->required) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pp->required) { |
5 |
0 |
5 |
|
| 341 |
5 |
| o->rv.hdr_missing++; |
| 342 |
5 |
| oa_err_push(aTHX_ errs, "response", pp->name, "required", |
| 343 |
|
| "missing required response header"); |
| 344 |
|
| } |
| 345 |
5 |
| continue; |
| 346 |
|
| } |
| 347 |
3 |
▶ | if (!oa_check_param(aTHX_ pp, hv_, errs, "response")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_check_param(aTHX_ pp, hv_, errs, "response")) |
1 |
2 |
3 |
|
| 348 |
1 |
| o->rv.hdr_invalid++; |
| 349 |
|
| } |
| 350 |
8 |
▶ | if (seen_any) o->rv.hdr_checked++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (seen_any) o->rv.hdr_checked++; |
8 |
0 |
8 |
|
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
|
| /* 1. not a declared JSON content type */ |
| 354 |
|
| { |
| 355 |
591 |
| SV *ct = oa_hdr_get(aTHX_ ha, "content-type", 12); |
| 356 |
|
| STRLEN ctl; const char *ctp; |
| 357 |
592 |
▶ | if (!ct) { o->rv.skip_ctype++; goto verdict; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ct) { o->rv.skip_ctype++; goto verdict; } |
7 |
584 |
591 |
|
| 358 |
584 |
| ctp = SvPV_const(ct, ctl); |
| 359 |
584 |
▶ | if (!oa_ctype_is_json(ctp, ctl)) { o->rv.skip_ctype++; goto verdict; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_ctype_is_json(ctp, ctl)) { o->rv.skip_ctype++; goto verdict; } |
1 |
583 |
584 |
|
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
|
| /* 2. Content-Length absent or over the cap. Only with a cap set: without |
| 363 |
|
| * one there is nothing to be over, and demanding the header would stop |
| 364 |
|
| * validating the handlers that never set it. */ |
| 365 |
583 |
▶ | if (rv->max_body > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv->max_body > 0) { |
2 |
581 |
583 |
|
| 366 |
2 |
| cl = oa_hdr_get(aTHX_ ha, "content-length", 14); |
| 367 |
2 |
▶ | if (!cl || !looks_like_number(cl) || SvIV(cl) > rv->max_body) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cl || !looks_like_number(cl) || SvIV(cl) > rv->max_body) { |
1 |
1 |
2 |
| if (!cl || !looks_like_number(cl) || SvIV(cl) > rv->max_body) { |
1 |
0 |
1 |
| if (!cl || !looks_like_number(cl) || SvIV(cl) > rv->max_body) { |
1 |
0 |
1 |
|
| 368 |
2 |
| o->rv.skip_size++; |
| 369 |
2 |
| goto verdict; |
| 370 |
|
| } |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
|
| /* 3. the body is not a plain scalar: a filehandle, a streaming coderef, |
| 374 |
|
| * an SSE writer. Never buffered, never measured, forwarded as it is. */ |
| 375 |
581 |
| bd = av_fetch(tv, 2, 0); |
| 376 |
581 |
▶ | if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) { |
581 |
0 |
581 |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) { |
581 |
0 |
581 |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) { |
581 |
0 |
581 |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) { |
1 |
580 |
581 |
|
| 377 |
1 |
| o->rv.skip_body++; |
| 378 |
1 |
| goto verdict; |
| 379 |
|
| } |
| 380 |
580 |
| ba = (AV *)SvRV(*bd); |
| 381 |
580 |
| b0 = av_fetch(ba, 0, 0); |
| 382 |
580 |
▶ | if (!b0 || !*b0 || !SvOK(*b0) || SvROK(*b0)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!b0 || !*b0 || !SvOK(*b0) || SvROK(*b0)) { |
580 |
0 |
580 |
| if (!b0 || !*b0 || !SvOK(*b0) || SvROK(*b0)) { |
580 |
0 |
580 |
| if (!b0 || !*b0 || !SvOK(*b0) || SvROK(*b0)) { |
580 |
0 |
580 |
| if (!b0 || !*b0 || !SvOK(*b0) || SvROK(*b0)) { |
0 |
580 |
580 |
|
| 383 |
0 |
| o->rv.skip_body++; |
| 384 |
0 |
| goto verdict; |
| 385 |
|
| } |
| 386 |
|
| |
| 387 |
|
| /* 4. the status declares no schema */ |
| 388 |
580 |
| handle = oa_resp_handle(aTHX_ o, status); |
| 389 |
580 |
▶ | if (!handle) { o->rv.skip_schema++; goto verdict; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!handle) { o->rv.skip_schema++; goto verdict; } |
1 |
579 |
580 |
|
| 390 |
|
| |
| 391 |
579 |
| data = oa_body_decode(aTHX_ *b0); |
| 392 |
579 |
▶ | if (!data) { o->rv.skip_decode++; goto verdict; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!data) { o->rv.skip_decode++; goto verdict; } |
0 |
579 |
579 |
|
| 393 |
|
| |
| 394 |
579 |
| o->rv.checked++; |
| 395 |
579 |
| (void)JSF->validate(aTHX_ handle, data, errs); |
| 396 |
|
| |
| 397 |
591 |
| verdict: |
| 398 |
591 |
▶ | if (av_len(errs) < 0) return NULL; /* nothing found, body or header */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(errs) < 0) return NULL; /* nothing found, body or header */ |
553 |
38 |
591 |
|
| 399 |
|
| |
| 400 |
38 |
| o->rv.violations++; |
| 401 |
87 |
▶ | for (j = 0; j <= av_len(errs); j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j <= av_len(errs); j++) { |
49 |
38 |
87 |
|
| 402 |
49 |
| SV **e = av_fetch(errs, j, 0); |
| 403 |
49 |
▶ | HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; |
49 |
0 |
49 |
| HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; |
49 |
0 |
49 |
| HV *h = (e && *e && SvROK(*e)) ? (HV *)SvRV(*e) : NULL; |
49 |
0 |
49 |
|
| 404 |
49 |
▶ | if (h) (void)hv_stores(h, "in", newSVpvs("response")); |
Branch: 1 branch
| Branch | True | False | Total |
| if (h) (void)hv_stores(h, "in", newSVpvs("response")); |
49 |
0 |
49 |
|
| 405 |
|
| } |
| 406 |
38 |
▶ | if (out_errs) *out_errs = errs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_errs) *out_errs = errs; |
20 |
18 |
38 |
|
| 407 |
|
| |
| 408 |
38 |
▶ | if (rv->mode == OA_RV_REPORT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv->mode == OA_RV_REPORT) { |
35 |
3 |
38 |
|
| 409 |
35 |
| oa_rv_report(aTHX_ rv->report, o->op_id, status, errs, env_rv); |
| 410 |
35 |
| return NULL; /* the client's bytes are not ours to edit */ |
| 411 |
|
| } |
| 412 |
3 |
| return oa_error_response(aTHX_ 500, errs); |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| /* The coverage counters as a hash (+1 owned by the caller). */ |
| 416 |
24 |
| static HV *oa_rv_hv(pTHX_ const oa_rvstat *s) { |
| 417 |
24 |
| HV *h = newHV(); |
| 418 |
24 |
| (void)hv_stores(h, "total", newSVuv(s->total)); |
| 419 |
24 |
| (void)hv_stores(h, "sampled", newSVuv(s->sampled)); |
| 420 |
24 |
| (void)hv_stores(h, "checked", newSVuv(s->checked)); |
| 421 |
24 |
| (void)hv_stores(h, "violations", newSVuv(s->violations)); |
| 422 |
24 |
| (void)hv_stores(h, "skip_ctype", newSVuv(s->skip_ctype)); |
| 423 |
24 |
| (void)hv_stores(h, "skip_size", newSVuv(s->skip_size)); |
| 424 |
24 |
| (void)hv_stores(h, "skip_body", newSVuv(s->skip_body)); |
| 425 |
24 |
| (void)hv_stores(h, "skip_schema", newSVuv(s->skip_schema)); |
| 426 |
24 |
| (void)hv_stores(h, "skip_decode", newSVuv(s->skip_decode)); |
| 427 |
24 |
| (void)hv_stores(h, "unsampled", newSVuv(s->total - s->sampled)); |
| 428 |
24 |
| return h; |
| 429 |
|
| } |
| 430 |
|
| |
| 431 |
|
| /* The request path's version: the mode comes from its own capture slot, the |
| 432 |
|
| * report callback and the sampling settings from the options HV. */ |
| 433 |
40 |
| static void oa_rvcfg_from(pTHX_ int vresp, HV *opts, oa_rvcfg *rv) { |
| 434 |
|
| SV **v; |
| 435 |
40 |
| rv->mode = vresp; rv->report = NULL; rv->sample = 1; rv->max_body = 0; |
| 436 |
40 |
▶ | if (!opts) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opts) return; |
0 |
40 |
40 |
|
| 437 |
40 |
▶ | if ((v = hv_fetchs(opts, "response_report", 0)) && *v && SvOK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "response_report", 0)) && *v && SvOK(*v)) |
37 |
3 |
40 |
| if ((v = hv_fetchs(opts, "response_report", 0)) && *v && SvOK(*v)) |
37 |
0 |
37 |
| if ((v = hv_fetchs(opts, "response_report", 0)) && *v && SvOK(*v)) |
37 |
0 |
37 |
|
| 438 |
37 |
| rv->report = *v; |
| 439 |
40 |
▶ | if ((v = hv_fetchs(opts, "response_sample", 0)) && *v && SvOK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "response_sample", 0)) && *v && SvOK(*v)) |
12 |
28 |
40 |
| if ((v = hv_fetchs(opts, "response_sample", 0)) && *v && SvOK(*v)) |
12 |
0 |
12 |
| if ((v = hv_fetchs(opts, "response_sample", 0)) && *v && SvOK(*v)) |
12 |
0 |
12 |
|
| 440 |
12 |
| rv->sample = SvIV(*v); |
| 441 |
40 |
▶ | if ((v = hv_fetchs(opts, "response_max_body", 0)) && *v && SvOK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "response_max_body", 0)) && *v && SvOK(*v)) |
2 |
38 |
40 |
| if ((v = hv_fetchs(opts, "response_max_body", 0)) && *v && SvOK(*v)) |
2 |
0 |
2 |
| if ((v = hv_fetchs(opts, "response_max_body", 0)) && *v && SvOK(*v)) |
2 |
0 |
2 |
|
| 442 |
2 |
| rv->max_body = SvIV(*v); |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
|
| /* Check with the configured mode. NULL unless replace mode found a |
| 446 |
|
| * mismatch, in which case the 500 that replaces the response (+1). */ |
| 447 |
40 |
| static SV *oa_check_response_opts(pTHX_ oa_op *o, SV *trip, int vresp, |
| 448 |
|
| HV *opts, SV *env_rv) { |
| 449 |
|
| oa_rvcfg rv; |
| 450 |
|
| /* deliberately no `!o->nresps` shortcut: an operation whose declared |
| 451 |
|
| * statuses carry no schema still counts its responses and attributes |
| 452 |
|
| * them to skip_schema. Silence there would read as coverage. */ |
| 453 |
40 |
▶ | if (!o || !vresp) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!o || !vresp) return NULL; |
40 |
0 |
40 |
| if (!o || !vresp) return NULL; |
0 |
40 |
40 |
|
| 454 |
40 |
| oa_rvcfg_from(aTHX_ vresp, opts, &rv); |
| 455 |
40 |
| return oa_check_response_cfg(aTHX_ o, trip, &rv, env_rv, NULL); |
| 456 |
|
| } |
| 457 |
|
| |
| 458 |
|
| /* ---- response finalization (security headers, CORS, problem+json) ----------------- */ |
| 459 |
|
| |
| 460 |
|
| /* case-insensitive: does the header AV already carry `name`? */ |
| 461 |
2484 |
| static int oa_hdr_has(pTHX_ AV *ha, const char *name, STRLEN nl) { |
| 462 |
2484 |
| SSize_t i, n = av_len(ha) + 1; |
| 463 |
8893 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
6410 |
2483 |
8893 |
|
| 464 |
6410 |
| SV **k = av_fetch(ha, i, 0); |
| 465 |
6410 |
▶ | if (k && *k) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (k && *k) { |
6410 |
0 |
6410 |
| if (k && *k) { |
6410 |
0 |
6410 |
|
| 466 |
6410 |
| STRLEN kl; const char *kp = SvPV_const(*k, kl); |
| 467 |
6410 |
| STRLEN j; int eq = (kl == nl); |
| 468 |
7043 |
▶ | for (j = 0; eq && j < kl; j++) |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; eq && j < kl; j++) |
634 |
6409 |
7043 |
| for (j = 0; eq && j < kl; j++) |
633 |
1 |
634 |
|
| 469 |
633 |
▶ | if (toLOWER((U8)kp[j]) != toLOWER((U8)name[j])) eq = 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (toLOWER((U8)kp[j]) != toLOWER((U8)name[j])) eq = 0; |
621 |
12 |
633 |
| if (toLOWER((U8)kp[j]) != toLOWER((U8)name[j])) eq = 0; |
621 |
12 |
633 |
| if (toLOWER((U8)kp[j]) != toLOWER((U8)name[j])) eq = 0; |
618 |
15 |
633 |
|
| 470 |
6410 |
▶ | if (eq) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eq) return 1; |
1 |
6409 |
6410 |
|
| 471 |
|
| } |
| 472 |
|
| } |
| 473 |
2483 |
| return 0; |
| 474 |
|
| } |
| 475 |
|
| |
| 476 |
|
| /* the triplet's header AV, or NULL */ |
| 477 |
1222 |
| static AV *oa_resp_headers(pTHX_ SV *resp) { |
| 478 |
|
| AV *tv; SV **hh; |
| 479 |
1222 |
▶ | if (!oa_is_triplet(aTHX_ resp)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_is_triplet(aTHX_ resp)) return NULL; |
0 |
1222 |
1222 |
|
| 480 |
1222 |
| tv = (AV *)SvRV(resp); |
| 481 |
1222 |
| hh = av_fetch(tv, 1, 0); |
| 482 |
1222 |
▶ | return (hh && *hh && SvROK(*hh)) ? (AV *)SvRV(*hh) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (hh && *hh && SvROK(*hh)) ? (AV *)SvRV(*hh) : NULL; |
1222 |
0 |
1222 |
| return (hh && *hh && SvROK(*hh)) ? (AV *)SvRV(*hh) : NULL; |
1222 |
0 |
1222 |
| return (hh && *hh && SvROK(*hh)) ? (AV *)SvRV(*hh) : NULL; |
1222 |
0 |
1222 |
|
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
|
| /* push name => value (borrowed name, value copied) */ |
| 486 |
2489 |
| static void oa_hdr_push(pTHX_ AV *ha, const char *name, SV *val) { |
| 487 |
2489 |
| av_push(ha, newSVpv(name, 0)); |
| 488 |
2489 |
| av_push(ha, newSVsv(val)); |
| 489 |
2489 |
| } |
| 490 |
|
| |
| 491 |
|
| /* join an AV of strings with ", " (mortal SV), NULL when empty */ |
| 492 |
1 |
| static SV *oa_join_csv(pTHX_ AV *av) { |
| 493 |
1 |
▶ | SSize_t i, n = av ? av_len(av) + 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t i, n = av ? av_len(av) + 1 : 0; |
1 |
0 |
1 |
|
| 494 |
|
| SV *out; |
| 495 |
1 |
▶ | if (n <= 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) return NULL; |
0 |
1 |
1 |
|
| 496 |
1 |
| out = sv_2mortal(newSVpvs("")); |
| 497 |
3 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
2 |
1 |
3 |
|
| 498 |
2 |
| SV **e = av_fetch(av, i, 0); |
| 499 |
2 |
▶ | if (!e || !*e) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !*e) continue; |
2 |
0 |
2 |
| if (!e || !*e) continue; |
0 |
2 |
2 |
|
| 500 |
2 |
▶ | if (SvCUR(out)) sv_catpvs(out, ", "); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(out)) sv_catpvs(out, ", "); |
1 |
1 |
2 |
|
| 501 |
2 |
| sv_catsv(out, *e); |
| 502 |
|
| } |
| 503 |
1 |
| return out; |
| 504 |
|
| } |
| 505 |
|
| |
| 506 |
|
| #define OA_HAS(ha, lit) oa_hdr_has(aTHX_ (ha), "" lit, sizeof(lit) - 1) |
| 507 |
|
| |
| 508 |
|
| /* apply the configured header set (set-if-absent, so a handler or the after |
| 509 |
|
| * hook keeps the last word) to a triplet response */ |
| 510 |
619 |
| static void oa_apply_headers(pTHX_ SV *resp, HV *hdrs) { |
| 511 |
619 |
| AV *ha = oa_resp_headers(aTHX_ resp); |
| 512 |
|
| HE *he; |
| 513 |
619 |
▶ | if (!ha || !hdrs) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ha || !hdrs) return; |
619 |
0 |
619 |
| if (!ha || !hdrs) return; |
0 |
619 |
619 |
|
| 514 |
619 |
| hv_iterinit(hdrs); |
| 515 |
3095 |
▶ | while ((he = hv_iternext(hdrs))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hdrs))) { |
2476 |
619 |
3095 |
|
| 516 |
2476 |
| I32 kl; const char *k = hv_iterkey(he, &kl); |
| 517 |
2476 |
▶ | if (!oa_hdr_has(aTHX_ ha, k, (STRLEN)kl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_hdr_has(aTHX_ ha, k, (STRLEN)kl)) |
2475 |
1 |
2476 |
|
| 518 |
2475 |
| oa_hdr_push(aTHX_ ha, k, hv_iterval(hdrs, he)); |
| 519 |
|
| } |
| 520 |
|
| } |
| 521 |
|
| |
| 522 |
|
| /* Finalize every outgoing triplet: convert error envelopes to problem+json |
| 523 |
|
| * (opt-in), add CORS response headers for an allowed cross-origin request, |
| 524 |
|
| * then stamp the security header set. Called at each exit and, for async |
| 525 |
|
| * handlers, when the future resolves. opts may be NULL. */ |
| 526 |
|
| static void oa_cors_actual(pTHX_ SV *resp, SV *env_rv, HV *cors); /* below */ |
| 527 |
|
| static void oa_to_problem(pTHX_ SV *resp); /* below */ |
| 528 |
|
| |
| 529 |
619 |
| static void oa_finalize(pTHX_ SV *resp, SV *env_rv, HV *opts) { |
| 530 |
|
| SV **v; |
| 531 |
|
| SV *setcookie; |
| 532 |
619 |
▶ | if (!oa_is_triplet(aTHX_ resp)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!oa_is_triplet(aTHX_ resp)) return; |
0 |
619 |
619 |
|
| 533 |
|
| /* CSRF cookie rotation from a check callback (independent of opts, so it |
| 534 |
|
| * applies on the async path and even when no other option is set) */ |
| 535 |
619 |
▶ | if ((setcookie = oa_env_get(aTHX_ (HV *)SvRV(env_rv), "openapi.csrf.setcookie"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((setcookie = oa_env_get(aTHX_ (HV *)SvRV(env_rv), "openapi.csrf.setcookie"))) { |
4 |
615 |
619 |
|
| 536 |
4 |
| AV *ha = oa_resp_headers(aTHX_ resp); |
| 537 |
4 |
▶ | if (ha) oa_hdr_push(aTHX_ ha, "Set-Cookie", setcookie); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ha) oa_hdr_push(aTHX_ ha, "Set-Cookie", setcookie); |
4 |
0 |
4 |
|
| 538 |
|
| } |
| 539 |
619 |
▶ | if (!opts) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opts) return; |
0 |
619 |
619 |
|
| 540 |
619 |
▶ | if ((v = hv_fetchs(opts, "error_format", 0)) && *v && SvOK(*v)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "error_format", 0)) && *v && SvOK(*v)) { |
3 |
616 |
619 |
| if ((v = hv_fetchs(opts, "error_format", 0)) && *v && SvOK(*v)) { |
3 |
0 |
3 |
| if ((v = hv_fetchs(opts, "error_format", 0)) && *v && SvOK(*v)) { |
3 |
0 |
3 |
|
| 541 |
3 |
| STRLEN l; const char *p = SvPV_const(*v, l); |
| 542 |
3 |
▶ | if (l == 7 && memEQ(p, "problem", 7)) oa_to_problem(aTHX_ resp); |
Branch: 2 branches
| Branch | True | False | Total |
| if (l == 7 && memEQ(p, "problem", 7)) oa_to_problem(aTHX_ resp); |
3 |
0 |
3 |
| if (l == 7 && memEQ(p, "problem", 7)) oa_to_problem(aTHX_ resp); |
3 |
0 |
3 |
|
| 543 |
|
| } |
| 544 |
619 |
▶ | if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
5 |
614 |
619 |
| if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
5 |
0 |
5 |
| if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
5 |
0 |
5 |
|
| 545 |
5 |
| oa_cors_actual(aTHX_ resp, env_rv, (HV *)SvRV(*v)); |
| 546 |
619 |
▶ | if ((v = hv_fetchs(opts, "headers", 0)) && *v && SvROK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "headers", 0)) && *v && SvROK(*v)) |
619 |
0 |
619 |
| if ((v = hv_fetchs(opts, "headers", 0)) && *v && SvROK(*v)) |
619 |
0 |
619 |
| if ((v = hv_fetchs(opts, "headers", 0)) && *v && SvROK(*v)) |
619 |
0 |
619 |
|
| 547 |
619 |
| oa_apply_headers(aTHX_ resp, (HV *)SvRV(*v)); |
| 548 |
|
| } |
| 549 |
|
| |
| 550 |
|
| /* finalize and return - a function (not a macro) so the response expression |
| 551 |
|
| * is evaluated exactly once at each exit */ |
| 552 |
617 |
| static SV *oa_fin(pTHX_ SV *resp, SV *env_rv, HV *opts) { |
| 553 |
617 |
| oa_finalize(aTHX_ resp, env_rv, opts); |
| 554 |
617 |
| return resp; |
| 555 |
|
| } |
| 556 |
|
| |
| 557 |
|
| /* reason phrase for the statuses this layer emits */ |
| 558 |
2 |
| static const char *oa_reason(int s) { |
| 559 |
2 |
| switch (s) { |
| 560 |
1 |
| case 400: return "Bad Request"; |
| 561 |
0 |
| case 401: return "Unauthorized"; |
| 562 |
0 |
| case 403: return "Forbidden"; |
| 563 |
1 |
| case 404: return "Not Found"; |
| 564 |
0 |
| case 405: return "Method Not Allowed"; |
| 565 |
0 |
| case 406: return "Not Acceptable"; |
| 566 |
0 |
| case 413: return "Payload Too Large"; |
| 567 |
0 |
| case 415: return "Unsupported Media Type"; |
| 568 |
0 |
| case 500: return "Internal Server Error"; |
| 569 |
0 |
| case 501: return "Not Implemented"; |
| 570 |
0 |
| default: return "Error"; |
| 571 |
|
| } |
| 572 |
|
| } |
| 573 |
|
| |
| 574 |
|
| /* Rewrite our JSON error envelope { errors => [...] } into an RFC 7807 |
| 575 |
|
| * application/problem+json body. Only touches a >=400 triplet whose body is |
| 576 |
|
| * our application/json envelope; a handler's own error body is left alone. */ |
| 577 |
3 |
| static void oa_to_problem(pTHX_ SV *resp) { |
| 578 |
3 |
| AV *tv = (AV *)SvRV(resp); |
| 579 |
3 |
| SV **st = av_fetch(tv, 0, 0); |
| 580 |
3 |
| AV *ha = oa_resp_headers(aTHX_ resp); |
| 581 |
|
| SV **bd, **b0, **errs; |
| 582 |
|
| AV *ba; |
| 583 |
|
| SV *data, *enc; |
| 584 |
|
| HV *src, *prob; |
| 585 |
3 |
| int status, is_json = 0; |
| 586 |
|
| SSize_t i, n; |
| 587 |
|
| |
| 588 |
3 |
▶ | if (!st || !*st || !ha) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!st || !*st || !ha) return; |
3 |
0 |
3 |
| if (!st || !*st || !ha) return; |
3 |
0 |
3 |
| if (!st || !*st || !ha) return; |
0 |
3 |
3 |
|
| 589 |
3 |
| status = (int)SvIV(*st); |
| 590 |
3 |
▶ | if (status < 400) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (status < 400) return; |
1 |
2 |
3 |
|
| 591 |
|
| |
| 592 |
2 |
| n = av_len(ha) + 1; |
| 593 |
2 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
2 |
0 |
2 |
|
| 594 |
2 |
| SV **k = av_fetch(ha, i, 0), **v = av_fetch(ha, i + 1, 0); |
| 595 |
2 |
▶ | if (k && *k && v && *v) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (k && *k && v && *v) { |
2 |
0 |
2 |
| if (k && *k && v && *v) { |
2 |
0 |
2 |
| if (k && *k && v && *v) { |
2 |
0 |
2 |
| if (k && *k && v && *v) { |
2 |
0 |
2 |
|
| 596 |
2 |
| STRLEN kl; const char *kp = SvPV_const(*k, kl); |
| 597 |
2 |
▶ | if (oa_ci_eq(kp, kl, "content-type", 12)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_ci_eq(kp, kl, "content-type", 12)) { |
2 |
0 |
2 |
|
| 598 |
2 |
| STRLEN vl; const char *vp = SvPV_const(*v, vl); |
| 599 |
2 |
▶ | is_json = (vl >= 16 && memEQ(vp, "application/json", 16) |
Branch: 1 branch
| Branch | True | False | Total |
| is_json = (vl >= 16 && memEQ(vp, "application/json", 16) |
2 |
0 |
2 |
|
| 600 |
4 |
▶ | && (vl == 16 || vp[16] == ';')); |
Branch: 3 branches
| Branch | True | False | Total |
| && (vl == 16 || vp[16] == ';')); |
2 |
0 |
2 |
| && (vl == 16 || vp[16] == ';')); |
0 |
2 |
2 |
| && (vl == 16 || vp[16] == ';')); |
0 |
0 |
0 |
|
| 601 |
2 |
| break; |
| 602 |
|
| } |
| 603 |
|
| } |
| 604 |
|
| } |
| 605 |
2 |
▶ | if (!is_json) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_json) return; |
0 |
2 |
2 |
|
| 606 |
|
| |
| 607 |
2 |
| bd = av_fetch(tv, 2, 0); |
| 608 |
2 |
▶ | if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) return; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) return; |
2 |
0 |
2 |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) return; |
2 |
0 |
2 |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) return; |
2 |
0 |
2 |
| if (!bd || !*bd || !SvROK(*bd) || SvTYPE(SvRV(*bd)) != SVt_PVAV) return; |
0 |
2 |
2 |
|
| 609 |
2 |
| ba = (AV *)SvRV(*bd); |
| 610 |
2 |
| b0 = av_fetch(ba, 0, 0); |
| 611 |
2 |
▶ | if (!b0 || !*b0 || !SvOK(*b0)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b0 || !*b0 || !SvOK(*b0)) return; |
2 |
0 |
2 |
| if (!b0 || !*b0 || !SvOK(*b0)) return; |
2 |
0 |
2 |
| if (!b0 || !*b0 || !SvOK(*b0)) return; |
0 |
2 |
2 |
|
| 612 |
2 |
| data = oa_body_decode(aTHX_ *b0); /* mortal, or NULL */ |
| 613 |
2 |
▶ | if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return; |
2 |
0 |
2 |
| if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return; |
2 |
0 |
2 |
| if (!data || !SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) return; |
0 |
2 |
2 |
|
| 614 |
2 |
| src = (HV *)SvRV(data); |
| 615 |
2 |
| errs = hv_fetchs(src, "errors", 0); |
| 616 |
|
| /* the errors AV marks OUR envelope; a handler's own JSON error body |
| 617 |
|
| * (no errors key) is left alone, as promised above */ |
| 618 |
2 |
▶ | if (!(errs && *errs && SvROK(*errs) && SvTYPE(SvRV(*errs)) == SVt_PVAV)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(errs && *errs && SvROK(*errs) && SvTYPE(SvRV(*errs)) == SVt_PVAV)) |
2 |
0 |
2 |
| if (!(errs && *errs && SvROK(*errs) && SvTYPE(SvRV(*errs)) == SVt_PVAV)) |
2 |
0 |
2 |
| if (!(errs && *errs && SvROK(*errs) && SvTYPE(SvRV(*errs)) == SVt_PVAV)) |
2 |
0 |
2 |
| if (!(errs && *errs && SvROK(*errs) && SvTYPE(SvRV(*errs)) == SVt_PVAV)) |
0 |
2 |
2 |
|
| 619 |
0 |
| return; |
| 620 |
2 |
| prob = newHV(); |
| 621 |
2 |
| (void)hv_stores(prob, "type", newSVpvs("about:blank")); |
| 622 |
2 |
| (void)hv_stores(prob, "title", newSVpv(oa_reason(status), 0)); |
| 623 |
2 |
| (void)hv_stores(prob, "status", newSViv(status)); |
| 624 |
2 |
| (void)hv_stores(prob, "errors", newSVsv(*errs)); |
| 625 |
|
| { |
| 626 |
2 |
| AV *ea = (AV *)SvRV(*errs); |
| 627 |
2 |
| SV **e0 = av_fetch(ea, 0, 0); |
| 628 |
2 |
▶ | if (e0 && *e0 && SvROK(*e0) && SvTYPE(SvRV(*e0)) == SVt_PVHV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (e0 && *e0 && SvROK(*e0) && SvTYPE(SvRV(*e0)) == SVt_PVHV) { |
2 |
0 |
2 |
| if (e0 && *e0 && SvROK(*e0) && SvTYPE(SvRV(*e0)) == SVt_PVHV) { |
2 |
0 |
2 |
| if (e0 && *e0 && SvROK(*e0) && SvTYPE(SvRV(*e0)) == SVt_PVHV) { |
2 |
0 |
2 |
| if (e0 && *e0 && SvROK(*e0) && SvTYPE(SvRV(*e0)) == SVt_PVHV) { |
2 |
0 |
2 |
|
| 629 |
2 |
| SV **m = hv_fetchs((HV *)SvRV(*e0), "message", 0); |
| 630 |
2 |
▶ | if (m && *m && SvOK(*m)) (void)hv_stores(prob, "detail", newSVsv(*m)); |
Branch: 3 branches
| Branch | True | False | Total |
| if (m && *m && SvOK(*m)) (void)hv_stores(prob, "detail", newSVsv(*m)); |
2 |
0 |
2 |
| if (m && *m && SvOK(*m)) (void)hv_stores(prob, "detail", newSVsv(*m)); |
2 |
0 |
2 |
| if (m && *m && SvOK(*m)) (void)hv_stores(prob, "detail", newSVsv(*m)); |
2 |
0 |
2 |
|
| 631 |
|
| } |
| 632 |
|
| } |
| 633 |
2 |
| enc = oa_json_encode(aTHX_ sv_2mortal(newRV_noinc((SV *)prob))); |
| 634 |
2 |
▶ | if (!enc) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!enc) return; |
0 |
2 |
2 |
|
| 635 |
2 |
| (void)av_store(ba, 0, newSVsv(enc)); |
| 636 |
2 |
▶ | if (av_len(ba) > 0) av_fill(ba, 0); /* one chunk now */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(ba) > 0) av_fill(ba, 0); /* one chunk now */ |
0 |
2 |
2 |
|
| 637 |
2 |
▶ | for (i = 0; i + 1 < n; i += 2) { /* switch the content-type */ |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { /* switch the content-type */ |
2 |
0 |
2 |
|
| 638 |
2 |
| SV **k = av_fetch(ha, i, 0); |
| 639 |
2 |
▶ | if (k && *k) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (k && *k) { |
2 |
0 |
2 |
| if (k && *k) { |
2 |
0 |
2 |
|
| 640 |
2 |
| STRLEN kl; const char *kp = SvPV_const(*k, kl); |
| 641 |
2 |
▶ | if (oa_ci_eq(kp, kl, "content-type", 12)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_ci_eq(kp, kl, "content-type", 12)) { |
2 |
0 |
2 |
|
| 642 |
2 |
| (void)av_store(ha, i + 1, |
| 643 |
|
| newSVpvs("application/problem+json")); |
| 644 |
2 |
| break; |
| 645 |
|
| } |
| 646 |
|
| } |
| 647 |
|
| } |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
|
| /* Add CORS headers to a non-preflight response when the request carries an |
| 651 |
|
| * allowed Origin. cors config: origins ('*' or an AV of exact origins), |
| 652 |
|
| * credentials (bool), expose (AV of header names). */ |
| 653 |
5 |
| static void oa_cors_actual(pTHX_ SV *resp, SV *env_rv, HV *cors) { |
| 654 |
5 |
| HV *env = (HV *)SvRV(env_rv); |
| 655 |
5 |
| SV *origin = oa_env_get(aTHX_ env, "HTTP_ORIGIN"); |
| 656 |
5 |
| AV *ha = oa_resp_headers(aTHX_ resp); |
| 657 |
|
| SV **v, *allow_val; |
| 658 |
5 |
| int allow = 0, wildcard = 0, creds; |
| 659 |
|
| |
| 660 |
5 |
▶ | if (!origin || !ha) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!origin || !ha) return; |
5 |
0 |
5 |
| if (!origin || !ha) return; |
0 |
5 |
5 |
|
| 661 |
5 |
| v = hv_fetchs(cors, "credentials", 0); |
| 662 |
5 |
▶ | creds = (v && *v && SvTRUE(*v)); |
Branch: 3 branches
| Branch | True | False | Total |
| creds = (v && *v && SvTRUE(*v)); |
5 |
0 |
5 |
| creds = (v && *v && SvTRUE(*v)); |
5 |
0 |
5 |
| creds = (v && *v && SvTRUE(*v)); |
1 |
4 |
5 |
|
| 663 |
|
| |
| 664 |
5 |
| v = hv_fetchs(cors, "origins", 0); |
| 665 |
5 |
▶ | if (v && *v) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (v && *v) { |
5 |
0 |
5 |
| if (v && *v) { |
5 |
0 |
5 |
|
| 666 |
5 |
▶ | if (!SvROK(*v)) { /* '*', or one exact origin */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(*v)) { /* '*', or one exact origin */ |
1 |
4 |
5 |
|
| 667 |
2 |
▶ | STRLEN l; const char *p = SvPV_const(*v, l); |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN l; const char *p = SvPV_const(*v, l); |
1 |
0 |
1 |
|
| 668 |
1 |
| STRLEN ol; const char *op = SvPV_const(origin, ol); |
| 669 |
1 |
▶ | if (l == 1 && *p == '*') { allow = 1; wildcard = 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (l == 1 && *p == '*') { allow = 1; wildcard = 1; } |
1 |
0 |
1 |
| if (l == 1 && *p == '*') { allow = 1; wildcard = 1; } |
1 |
0 |
1 |
|
| 670 |
0 |
▶ | else if (l == ol && memEQ(p, op, ol)) allow = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (l == ol && memEQ(p, op, ol)) allow = 1; |
0 |
0 |
0 |
| else if (l == ol && memEQ(p, op, ol)) allow = 1; |
0 |
0 |
0 |
|
| 671 |
4 |
▶ | } else if (SvTYPE(SvRV(*v)) == SVt_PVAV) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvTYPE(SvRV(*v)) == SVt_PVAV) { |
4 |
0 |
4 |
|
| 672 |
4 |
| AV *list = (AV *)SvRV(*v); |
| 673 |
4 |
| STRLEN ol; const char *op = SvPV_const(origin, ol); |
| 674 |
4 |
| SSize_t i, n = av_len(list) + 1; |
| 675 |
5 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
4 |
1 |
5 |
|
| 676 |
4 |
| SV **e = av_fetch(list, i, 0); |
| 677 |
|
| STRLEN el; const char *ep; |
| 678 |
4 |
▶ | if (!e || !*e) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !*e) continue; |
4 |
0 |
4 |
| if (!e || !*e) continue; |
0 |
4 |
4 |
|
| 679 |
4 |
| ep = SvPV_const(*e, el); |
| 680 |
7 |
▶ | if (el == 1 && *ep == '*') { allow = 1; wildcard = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (el == 1 && *ep == '*') { allow = 1; wildcard = 1; break; } |
0 |
4 |
4 |
| if (el == 1 && *ep == '*') { allow = 1; wildcard = 1; break; } |
0 |
0 |
0 |
|
| 681 |
4 |
▶ | if (el == ol && memEQ(ep, op, ol)) { allow = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (el == ol && memEQ(ep, op, ol)) { allow = 1; break; } |
3 |
1 |
4 |
| if (el == ol && memEQ(ep, op, ol)) { allow = 1; break; } |
3 |
0 |
3 |
|
| 682 |
|
| } |
| 683 |
|
| } |
| 684 |
|
| } |
| 685 |
5 |
▶ | if (!allow) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!allow) return; |
1 |
4 |
5 |
|
| 686 |
|
| |
| 687 |
|
| /* credentials forbid a wildcard ACAO: echo the concrete origin */ |
| 688 |
4 |
▶ | allow_val = (wildcard && !creds) ? sv_2mortal(newSVpvs("*")) : origin; |
Branch: 2 branches
| Branch | True | False | Total |
| allow_val = (wildcard && !creds) ? sv_2mortal(newSVpvs("*")) : origin; |
1 |
3 |
4 |
| allow_val = (wildcard && !creds) ? sv_2mortal(newSVpvs("*")) : origin; |
1 |
0 |
1 |
|
| 689 |
4 |
▶ | if (!OA_HAS(ha, "Access-Control-Allow-Origin")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!OA_HAS(ha, "Access-Control-Allow-Origin")) |
4 |
0 |
4 |
|
| 690 |
4 |
| oa_hdr_push(aTHX_ ha, "Access-Control-Allow-Origin", allow_val); |
| 691 |
4 |
▶ | if (!wildcard && !OA_HAS(ha, "Vary")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!wildcard && !OA_HAS(ha, "Vary")) |
3 |
1 |
4 |
| if (!wildcard && !OA_HAS(ha, "Vary")) |
3 |
0 |
3 |
|
| 692 |
3 |
| oa_hdr_push(aTHX_ ha, "Vary", sv_2mortal(newSVpvs("Origin"))); |
| 693 |
4 |
▶ | if (creds && !OA_HAS(ha, "Access-Control-Allow-Credentials")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (creds && !OA_HAS(ha, "Access-Control-Allow-Credentials")) |
1 |
3 |
4 |
| if (creds && !OA_HAS(ha, "Access-Control-Allow-Credentials")) |
1 |
0 |
1 |
|
| 694 |
1 |
| oa_hdr_push(aTHX_ ha, "Access-Control-Allow-Credentials", |
| 695 |
|
| sv_2mortal(newSVpvs("true"))); |
| 696 |
4 |
| v = hv_fetchs(cors, "expose", 0); |
| 697 |
4 |
▶ | if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) { |
0 |
4 |
4 |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) { |
0 |
0 |
0 |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) { |
0 |
0 |
0 |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 698 |
0 |
| SV *joined = oa_join_csv(aTHX_ (AV *)SvRV(*v)); |
| 699 |
0 |
▶ | if (joined && !OA_HAS(ha, "Access-Control-Expose-Headers")) |
Branch: 2 branches
| Branch | True | False | Total |
| if (joined && !OA_HAS(ha, "Access-Control-Expose-Headers")) |
0 |
0 |
0 |
| if (joined && !OA_HAS(ha, "Access-Control-Expose-Headers")) |
0 |
0 |
0 |
|
| 700 |
0 |
| oa_hdr_push(aTHX_ ha, "Access-Control-Expose-Headers", joined); |
| 701 |
|
| } |
| 702 |
|
| } |
| 703 |
|
| |
| 704 |
|
| /* Build a CORS preflight (204) response for an OPTIONS request against a known |
| 705 |
|
| * path: Allow-Methods from the router's Allow list (the declared methods), |
| 706 |
|
| * Allow-Headers echoed from the request or configured, and Max-Age. The |
| 707 |
|
| * Allow-Origin / credentials headers are added by oa_cors_actual in finalize. |
| 708 |
|
| * (+1) */ |
| 709 |
1 |
| static SV *oa_cors_preflight(pTHX_ SV *env_rv, HV *cors, AV *allow) { |
| 710 |
1 |
| HV *env = (HV *)SvRV(env_rv); |
| 711 |
1 |
| AV *h = newAV(), *b = newAV(), *r = newAV(); |
| 712 |
1 |
| SV **v, *methods, *val = NULL; |
| 713 |
|
| |
| 714 |
1 |
| av_push(r, newSViv(204)); |
| 715 |
1 |
| av_push(r, newRV_noinc((SV *)h)); |
| 716 |
1 |
| av_push(b, newSVpvs("")); |
| 717 |
1 |
| av_push(r, newRV_noinc((SV *)b)); |
| 718 |
|
| |
| 719 |
|
| /* Allow-Methods: the declared methods (the router uppercases them), |
| 720 |
|
| * else echo the requested one */ |
| 721 |
1 |
| methods = oa_join_csv(aTHX_ allow); |
| 722 |
1 |
▶ | if (!methods) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!methods) |
0 |
1 |
1 |
|
| 723 |
0 |
| methods = oa_env_get(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_METHOD"); |
| 724 |
1 |
▶ | if (methods) oa_hdr_push(aTHX_ h, "Access-Control-Allow-Methods", methods); |
Branch: 1 branch
| Branch | True | False | Total |
| if (methods) oa_hdr_push(aTHX_ h, "Access-Control-Allow-Methods", methods); |
1 |
0 |
1 |
|
| 725 |
|
| |
| 726 |
|
| /* Allow-Headers: configured list, else echo the requested set */ |
| 727 |
1 |
| v = hv_fetchs(cors, "headers", 0); |
| 728 |
1 |
▶ | if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) |
0 |
1 |
1 |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) |
0 |
0 |
0 |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) |
0 |
0 |
0 |
| if (v && *v && SvROK(*v) && SvTYPE(SvRV(*v)) == SVt_PVAV) |
0 |
0 |
0 |
|
| 729 |
0 |
| val = oa_join_csv(aTHX_ (AV *)SvRV(*v)); |
| 730 |
1 |
▶ | else if (v && *v && SvOK(*v) && !SvROK(*v)) |
Branch: 4 branches
| Branch | True | False | Total |
| else if (v && *v && SvOK(*v) && !SvROK(*v)) |
0 |
1 |
1 |
| else if (v && *v && SvOK(*v) && !SvROK(*v)) |
0 |
0 |
0 |
| else if (v && *v && SvOK(*v) && !SvROK(*v)) |
0 |
0 |
0 |
| else if (v && *v && SvOK(*v) && !SvROK(*v)) |
0 |
0 |
0 |
|
| 731 |
0 |
| val = *v; |
| 732 |
1 |
▶ | if (!val) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!val) |
1 |
0 |
1 |
|
| 733 |
1 |
| val = oa_env_get(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_HEADERS"); |
| 734 |
1 |
▶ | if (val) oa_hdr_push(aTHX_ h, "Access-Control-Allow-Headers", val); |
Branch: 1 branch
| Branch | True | False | Total |
| if (val) oa_hdr_push(aTHX_ h, "Access-Control-Allow-Headers", val); |
0 |
1 |
1 |
|
| 735 |
|
| |
| 736 |
1 |
▶ | if ((v = hv_fetchs(cors, "max_age", 0)) && *v && SvOK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(cors, "max_age", 0)) && *v && SvOK(*v)) |
1 |
0 |
1 |
| if ((v = hv_fetchs(cors, "max_age", 0)) && *v && SvOK(*v)) |
1 |
0 |
1 |
| if ((v = hv_fetchs(cors, "max_age", 0)) && *v && SvOK(*v)) |
1 |
0 |
1 |
|
| 737 |
1 |
| oa_hdr_push(aTHX_ h, "Access-Control-Max-Age", *v); |
| 738 |
|
| |
| 739 |
1 |
| return newRV_noinc((SV *)r); |
| 740 |
|
| } |
| 741 |
|
| |
| 742 |
|
| /* ---- content negotiation (opt-in: 415 request type, 406 Accept) ------------------- */ |
| 743 |
|
| |
| 744 |
|
| /* compare two media types ignoring parameters, list separators and space */ |
| 745 |
1 |
| static int oa_media_eq(const char *a, STRLEN al, const char *b, STRLEN bl) { |
| 746 |
1 |
| STRLEN i = 0, j = 0, ie, je; |
| 747 |
1 |
▶ | while (i < al && (a[i] == ' ' || a[i] == '\t')) i++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < al && (a[i] == ' ' || a[i] == '\t')) i++; |
1 |
0 |
1 |
| while (i < al && (a[i] == ' ' || a[i] == '\t')) i++; |
0 |
1 |
1 |
| while (i < al && (a[i] == ' ' || a[i] == '\t')) i++; |
0 |
1 |
1 |
|
| 748 |
1 |
▶ | while (j < bl && (b[j] == ' ' || b[j] == '\t')) j++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (j < bl && (b[j] == ' ' || b[j] == '\t')) j++; |
1 |
0 |
1 |
| while (j < bl && (b[j] == ' ' || b[j] == '\t')) j++; |
0 |
1 |
1 |
| while (j < bl && (b[j] == ' ' || b[j] == '\t')) j++; |
0 |
1 |
1 |
|
| 749 |
1 |
| ie = i; |
| 750 |
1 |
| je = j; |
| 751 |
17 |
▶ | while (ie < al && a[ie] != ';' && a[ie] != ',' && a[ie] != ' ') ie++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (ie < al && a[ie] != ';' && a[ie] != ',' && a[ie] != ' ') ie++; |
16 |
1 |
17 |
| while (ie < al && a[ie] != ';' && a[ie] != ',' && a[ie] != ' ') ie++; |
16 |
0 |
16 |
| while (ie < al && a[ie] != ';' && a[ie] != ',' && a[ie] != ' ') ie++; |
16 |
0 |
16 |
| while (ie < al && a[ie] != ';' && a[ie] != ',' && a[ie] != ' ') ie++; |
16 |
0 |
16 |
|
| 752 |
17 |
▶ | while (je < bl && b[je] != ';' && b[je] != ',' && b[je] != ' ') je++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (je < bl && b[je] != ';' && b[je] != ',' && b[je] != ' ') je++; |
16 |
1 |
17 |
| while (je < bl && b[je] != ';' && b[je] != ',' && b[je] != ' ') je++; |
16 |
0 |
16 |
| while (je < bl && b[je] != ';' && b[je] != ',' && b[je] != ' ') je++; |
16 |
0 |
16 |
| while (je < bl && b[je] != ';' && b[je] != ',' && b[je] != ' ') je++; |
16 |
0 |
16 |
|
| 753 |
1 |
| return oa_ci_eq(a + i, ie - i, b + j, je - j); |
| 754 |
|
| } |
| 755 |
|
| |
| 756 |
|
| /* 415 when a request body is present but its Content-Type matches none of the |
| 757 |
|
| * operation's declared body media types. NULL to proceed, else a +1 response. */ |
| 758 |
5 |
| static SV *oa_check_request_ctype(pTHX_ oa_op *o, HV *env) { |
| 759 |
5 |
| SV *cl = oa_env_get(aTHX_ env, "CONTENT_LENGTH"); |
| 760 |
|
| SV *ct; |
| 761 |
|
| STRLEN rl; const char *rp; |
| 762 |
5 |
| int i, match = 0; |
| 763 |
5 |
▶ | if (!o->nbodies) return NULL; /* no declared body: pass */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o->nbodies) return NULL; /* no declared body: pass */ |
3 |
2 |
5 |
|
| 764 |
2 |
▶ | if (!cl || SvIV(cl) <= 0) return NULL; /* no body sent: pass */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cl || SvIV(cl) <= 0) return NULL; /* no body sent: pass */ |
2 |
0 |
2 |
| if (!cl || SvIV(cl) <= 0) return NULL; /* no body sent: pass */ |
0 |
2 |
2 |
|
| 765 |
2 |
| ct = oa_env_get(aTHX_ env, "CONTENT_TYPE"); |
| 766 |
2 |
▶ | if (!ct) return oa_error_msg(aTHX_ 415, "missing Content-Type"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ct) return oa_error_msg(aTHX_ 415, "missing Content-Type"); |
0 |
2 |
2 |
|
| 767 |
2 |
| rp = SvPV_const(ct, rl); |
| 768 |
5 |
▶ | for (i = 0; i < o->nbodies; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < o->nbodies; i++) { |
4 |
1 |
5 |
|
| 769 |
4 |
| STRLEN dl; const char *dp = SvPV_const(o->bodies[i].ctype, dl); |
| 770 |
|
| /* oa_ctype_matches, not oa_media_eq: a declared range has to admit the |
| 771 |
|
| * types it covers here too, or the 415 gate refuses a body that |
| 772 |
|
| * oa_body_for would have accepted. */ |
| 773 |
4 |
▶ | if (oa_ctype_matches(dp, dl, rp, rl)) { match = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_ctype_matches(dp, dl, rp, rl)) { match = 1; break; } |
1 |
3 |
4 |
|
| 774 |
|
| } |
| 775 |
2 |
▶ | return match ? NULL : oa_error_msg(aTHX_ 415, "unsupported media type"); |
Branch: 1 branch
| Branch | True | False | Total |
| return match ? NULL : oa_error_msg(aTHX_ 415, "unsupported media type"); |
1 |
1 |
2 |
|
| 776 |
|
| } |
| 777 |
|
| |
| 778 |
|
| /* 406 when the request has an Accept that admits none of the operation's |
| 779 |
|
| * declared response media types. Lenient: unknown (no declared types, no |
| 780 |
|
| * Accept, or Accept carries a wildcard) always passes. */ |
| 781 |
4 |
| static SV *oa_check_accept(pTHX_ oa_op *o, HV *env) { |
| 782 |
4 |
| SV *acc = oa_env_get(aTHX_ env, "HTTP_ACCEPT"); |
| 783 |
|
| STRLEN al, k; const char *ap; |
| 784 |
4 |
| int i, any = 0, ok = 0; |
| 785 |
4 |
▶ | if (!acc || !o->nresps) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!acc || !o->nresps) return NULL; |
3 |
1 |
4 |
| if (!acc || !o->nresps) return NULL; |
0 |
3 |
3 |
|
| 786 |
3 |
| ap = SvPV_const(acc, al); |
| 787 |
3 |
▶ | if (al == 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (al == 0) return NULL; |
0 |
3 |
3 |
|
| 788 |
30 |
▶ | for (k = 0; k + 2 < al; k++) /* star-slash-star: accept all */ |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k + 2 < al; k++) /* star-slash-star: accept all */ |
28 |
2 |
30 |
|
| 789 |
28 |
▶ | if (ap[k] == '*' && ap[k+1] == '/' && ap[k+2] == '*') return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (ap[k] == '*' && ap[k+1] == '/' && ap[k+2] == '*') return NULL; |
1 |
27 |
28 |
| if (ap[k] == '*' && ap[k+1] == '/' && ap[k+2] == '*') return NULL; |
1 |
0 |
1 |
| if (ap[k] == '*' && ap[k+1] == '/' && ap[k+2] == '*') return NULL; |
1 |
0 |
1 |
|
| 790 |
4 |
▶ | for (i = 0; i < o->nresps && !ok; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < o->nresps && !ok; i++) { |
2 |
2 |
4 |
| for (i = 0; i < o->nresps && !ok; i++) { |
2 |
0 |
2 |
|
| 791 |
|
| STRLEN dl; const char *dp; |
| 792 |
|
| STRLEN s; |
| 793 |
2 |
▶ | if (!o->resps[i].ctype) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o->resps[i].ctype) continue; |
0 |
2 |
2 |
|
| 794 |
2 |
| any = 1; |
| 795 |
2 |
| dp = SvPV_const(o->resps[i].ctype, dl); |
| 796 |
|
| /* acceptable if the declared type (up to ';') appears in Accept */ |
| 797 |
2 |
▶ | for (s = 0; s + dl <= al; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = 0; s + dl <= al; s++) |
1 |
1 |
2 |
|
| 798 |
1 |
▶ | if (oa_media_eq(ap + s, al - s, dp, dl)) { ok = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_media_eq(ap + s, al - s, dp, dl)) { ok = 1; break; } |
1 |
0 |
1 |
|
| 799 |
|
| } |
| 800 |
2 |
▶ | if (!any) return NULL; /* nothing declared: pass */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!any) return NULL; /* nothing declared: pass */ |
0 |
2 |
2 |
|
| 801 |
2 |
▶ | return ok ? NULL : oa_error_msg(aTHX_ 406, "not acceptable"); |
Branch: 1 branch
| Branch | True | False | Total |
| return ok ? NULL : oa_error_msg(aTHX_ 406, "not acceptable"); |
1 |
1 |
2 |
|
| 802 |
|
| } |
| 803 |
|
| |
| 804 |
|
| /* ---- hooks ------------------------------------------------------------------------ */ |
| 805 |
|
| |
| 806 |
|
| /* Resolve a hook/handler spec - a coderef, or a fully qualified sub name - |
| 807 |
|
| * to an RV-to-CV (+1). Croaks (with `what` for context) on anything else. */ |
| 808 |
46 |
| static SV *oa_cv_or_name(pTHX_ SV *v, const char *what) { |
| 809 |
46 |
▶ | if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) |
46 |
0 |
46 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) |
43 |
3 |
46 |
| if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVCV) |
43 |
0 |
43 |
|
| 810 |
43 |
| return newSVsv(v); |
| 811 |
3 |
▶ | if (v && SvOK(v) && !SvROK(v)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && SvOK(v) && !SvROK(v)) { |
3 |
0 |
3 |
| if (v && SvOK(v) && !SvROK(v)) { |
3 |
0 |
3 |
| if (v && SvOK(v) && !SvROK(v)) { |
3 |
0 |
3 |
|
| 812 |
3 |
| STRLEN nl; const char *np = SvPV_const(v, nl); |
| 813 |
3 |
| CV *cv = get_cv(np, 0); |
| 814 |
3 |
▶ | if (!cv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cv) |
1 |
2 |
3 |
|
| 815 |
1 |
| croak("Open::API->to_app: %s names no such sub '%.*s'", |
| 816 |
|
| what, (int)nl, np); |
| 817 |
2 |
| return newRV_inc((SV *)cv); |
| 818 |
|
| } |
| 819 |
0 |
| croak("Open::API->to_app: %s must be a coderef or a fully qualified " |
| 820 |
|
| "sub name", what); |
| 821 |
|
| return NULL; /* not reached */ |
| 822 |
|
| } |
| 823 |
|
| |
| 824 |
|
| /* Run the after hook on a finished response: $after->($resp, $env, $opId). |
| 825 |
|
| * The hook may mutate the triplet in place; returning a PSGI triplet |
| 826 |
|
| * replaces the response (other return values are ignored). A die becomes a |
| 827 |
|
| * 500. Replaces *resp in place, consuming the old one. */ |
| 828 |
608 |
| static void oa_run_after(pTHX_ SV **resp, SV *after, SV *env_rv, SV *opid) { |
| 829 |
608 |
| dSP; int n; SV *r = NULL; |
| 830 |
608 |
▶ | if (!after || !SvOK(after)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!after || !SvOK(after)) return; |
608 |
0 |
608 |
| if (!after || !SvOK(after)) return; |
600 |
8 |
608 |
|
| 831 |
8 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
8 |
8 |
|
| 832 |
8 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
8 |
8 |
|
| 833 |
8 |
| PUSHs(*resp); |
| 834 |
8 |
| PUSHs(env_rv); |
| 835 |
8 |
| PUSHs(opid); |
| 836 |
8 |
| PUTBACK; |
| 837 |
8 |
| n = call_sv(after, G_SCALAR | G_EVAL); |
| 838 |
8 |
| SPAGAIN; |
| 839 |
8 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
8 |
0 |
8 |
| if (SvTRUE(ERRSV)) { |
0 |
8 |
8 |
|
| 840 |
0 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 841 |
0 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 842 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 843 |
0 |
| SvREFCNT_dec(*resp); |
| 844 |
0 |
| *resp = oa_error_msg(aTHX_ 500, SvPV_nolen(e)); |
| 845 |
0 |
| SvREFCNT_dec(e); |
| 846 |
0 |
| return; |
| 847 |
|
| } |
| 848 |
8 |
▶ | if (n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { |
8 |
0 |
8 |
|
| 849 |
8 |
| SV *t = POPs; |
| 850 |
8 |
▶ | if (oa_is_triplet(aTHX_ t) && t != *resp) r = newSVsv(t); |
Branch: 2 branches
| Branch | True | False | Total |
| if (oa_is_triplet(aTHX_ t) && t != *resp) r = newSVsv(t); |
1 |
7 |
8 |
| if (oa_is_triplet(aTHX_ t) && t != *resp) r = newSVsv(t); |
1 |
0 |
1 |
|
| 851 |
|
| } |
| 852 |
8 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
8 |
0 |
8 |
|
| 853 |
8 |
▶ | if (r) { SvREFCNT_dec(*resp); *resp = r; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) { SvREFCNT_dec(*resp); *resp = r; } |
1 |
7 |
8 |
|
| 854 |
|
| } |
| 855 |
|
| |
| 856 |
|
| /* Run the before hook after routing, before validation: |
| 857 |
|
| * $before->($env, $opId). A REFERENCE return short-circuits the request and |
| 858 |
|
| * becomes the response (a PSGI triplet as-is, other data JSON-encoded); |
| 859 |
|
| * non-reference returns continue. A die becomes a 500. Returns the |
| 860 |
|
| * short-circuit response (+1) or NULL to continue. */ |
| 861 |
6 |
| static SV *oa_run_before(pTHX_ SV *before, SV *env_rv, SV *opid) { |
| 862 |
6 |
| dSP; int n; SV *out = NULL; |
| 863 |
6 |
▶ | if (!before || !SvOK(before)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!before || !SvOK(before)) return NULL; |
6 |
0 |
6 |
| if (!before || !SvOK(before)) return NULL; |
0 |
6 |
6 |
|
| 864 |
6 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
6 |
6 |
|
| 865 |
6 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
6 |
6 |
|
| 866 |
6 |
| PUSHs(env_rv); |
| 867 |
6 |
| PUSHs(opid); |
| 868 |
6 |
| PUTBACK; |
| 869 |
6 |
| n = call_sv(before, G_SCALAR | G_EVAL); |
| 870 |
6 |
| SPAGAIN; |
| 871 |
6 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
6 |
0 |
6 |
| if (SvTRUE(ERRSV)) { |
1 |
5 |
6 |
|
| 872 |
1 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
1 |
0 |
1 |
|
| 873 |
1 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
1 |
0 |
1 |
|
| 874 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 875 |
1 |
| out = oa_error_msg(aTHX_ 500, SvPV_nolen(e)); |
| 876 |
1 |
| SvREFCNT_dec(e); |
| 877 |
1 |
| return out; |
| 878 |
|
| } |
| 879 |
5 |
▶ | if (n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { |
5 |
0 |
5 |
|
| 880 |
5 |
| SV *t = POPs; |
| 881 |
5 |
▶ | if (SvROK(t)) out = oa_map_result(aTHX_ t); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(t)) out = oa_map_result(aTHX_ t); |
1 |
4 |
5 |
|
| 882 |
|
| } |
| 883 |
5 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
5 |
0 |
5 |
|
| 884 |
5 |
| return out; |
| 885 |
|
| } |
| 886 |
|
| |
| 887 |
|
| /* ---- security enforcement ---------------------------------------------------------- */ |
| 888 |
|
| |
| 889 |
|
| /* Extract the credential a scheme describes from the request. Mortal SV, or |
| 890 |
|
| * NULL when absent/malformed. Basic credentials are base64-decoded so the |
| 891 |
|
| * checker sees "user:pass". */ |
| 892 |
11 |
| static SV *oa_sec_extract(pTHX_ oa_scheme *s, HV *env) { |
| 893 |
11 |
| switch (s->type) { |
| 894 |
10 |
| case OA_SEC_APIKEY: { |
| 895 |
10 |
| STRLEN nl; const char *np = SvPV_const(s->pname, nl); |
| 896 |
10 |
▶ | if (s->loc == OA_IN_HEADER) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->loc == OA_IN_HEADER) { |
10 |
0 |
10 |
|
| 897 |
|
| char key[160]; |
| 898 |
|
| STRLEN k; |
| 899 |
|
| SV **e; |
| 900 |
10 |
▶ | if (nl + 5 >= sizeof key) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nl + 5 >= sizeof key) return NULL; |
0 |
10 |
10 |
|
| 901 |
10 |
| memcpy(key, "HTTP_", 5); |
| 902 |
52 |
▶ | for (k = 0; k < nl; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nl; k++) { |
42 |
10 |
52 |
|
| 903 |
42 |
| char c = np[k]; |
| 904 |
42 |
▶ | key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
Branch: 2 branches
| Branch | True | False | Total |
| key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
30 |
12 |
42 |
| key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
4 |
26 |
30 |
|
| 905 |
|
| } |
| 906 |
10 |
| e = hv_fetch(env, key, (I32)(5 + nl), 0); |
| 907 |
10 |
▶ | return (e && *e && SvOK(*e)) ? sv_mortalcopy(*e) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvOK(*e)) ? sv_mortalcopy(*e) : NULL; |
9 |
1 |
10 |
| return (e && *e && SvOK(*e)) ? sv_mortalcopy(*e) : NULL; |
9 |
0 |
9 |
| return (e && *e && SvOK(*e)) ? sv_mortalcopy(*e) : NULL; |
9 |
0 |
9 |
|
| 908 |
|
| } |
| 909 |
0 |
▶ | if (s->loc == OA_IN_QUERY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->loc == OA_IN_QUERY) { |
0 |
0 |
0 |
|
| 910 |
0 |
| SV *qs = oa_env_get(aTHX_ env, "QUERY_STRING"); |
| 911 |
|
| STRLEN ql; const char *qp; |
| 912 |
0 |
| STRLEN i = 0; |
| 913 |
0 |
▶ | if (!qs) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!qs) return NULL; |
0 |
0 |
0 |
|
| 914 |
0 |
| qp = SvPV_const(qs, ql); |
| 915 |
0 |
▶ | while (i < ql) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < ql) { |
0 |
0 |
0 |
|
| 916 |
0 |
| STRLEN e = i, eq; |
| 917 |
0 |
▶ | while (e < ql && qp[e] != '&') e++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (e < ql && qp[e] != '&') e++; |
0 |
0 |
0 |
| while (e < ql && qp[e] != '&') e++; |
0 |
0 |
0 |
|
| 918 |
0 |
| eq = i; |
| 919 |
0 |
▶ | while (eq < e && qp[eq] != '=') eq++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (eq < e && qp[eq] != '=') eq++; |
0 |
0 |
0 |
| while (eq < e && qp[eq] != '=') eq++; |
0 |
0 |
0 |
|
| 920 |
0 |
▶ | if (eq - i == nl && memEQ(qp + i, np, nl) && eq < e) |
Branch: 3 branches
| Branch | True | False | Total |
| if (eq - i == nl && memEQ(qp + i, np, nl) && eq < e) |
0 |
0 |
0 |
| if (eq - i == nl && memEQ(qp + i, np, nl) && eq < e) |
0 |
0 |
0 |
| if (eq - i == nl && memEQ(qp + i, np, nl) && eq < e) |
0 |
0 |
0 |
|
| 921 |
0 |
| return sv_2mortal(oa_pct_decode(aTHX_ qp + eq + 1, |
| 922 |
|
| e - eq - 1, 1)); |
| 923 |
0 |
| i = e + 1; |
| 924 |
|
| } |
| 925 |
0 |
| return NULL; |
| 926 |
|
| } |
| 927 |
0 |
▶ | if (s->loc == OA_IN_COOKIE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->loc == OA_IN_COOKIE) { |
0 |
0 |
0 |
|
| 928 |
0 |
| SV *ch = oa_env_get(aTHX_ env, "HTTP_COOKIE"); |
| 929 |
|
| HV *jar; |
| 930 |
|
| SV **v; |
| 931 |
0 |
| SV *out = NULL; |
| 932 |
|
| STRLEN cl; const char *cp; |
| 933 |
0 |
▶ | if (!ch) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ch) return NULL; |
0 |
0 |
0 |
|
| 934 |
0 |
| cp = SvPV_const(ch, cl); |
| 935 |
0 |
| jar = oa_parse_cookies(aTHX_ cp, cl); |
| 936 |
0 |
| v = hv_fetch(jar, np, (I32)nl, 0); |
| 937 |
0 |
▶ | if (v && *v && SvOK(*v)) out = sv_mortalcopy(*v); |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && *v && SvOK(*v)) out = sv_mortalcopy(*v); |
0 |
0 |
0 |
| if (v && *v && SvOK(*v)) out = sv_mortalcopy(*v); |
0 |
0 |
0 |
| if (v && *v && SvOK(*v)) out = sv_mortalcopy(*v); |
0 |
0 |
0 |
|
| 938 |
0 |
| SvREFCNT_dec((SV *)jar); |
| 939 |
0 |
| return out; |
| 940 |
|
| } |
| 941 |
0 |
| return NULL; |
| 942 |
|
| } |
| 943 |
1 |
| case OA_SEC_BEARER: |
| 944 |
|
| case OA_SEC_BASIC: { |
| 945 |
1 |
| SV *az = oa_env_get(aTHX_ env, "HTTP_AUTHORIZATION"); |
| 946 |
|
| SV *dec; |
| 947 |
|
| STRLEN al; const char *ap; |
| 948 |
1 |
▶ | const char *want = (s->type == OA_SEC_BEARER) ? "Bearer " : "Basic "; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *want = (s->type == OA_SEC_BEARER) ? "Bearer " : "Basic "; |
1 |
0 |
1 |
|
| 949 |
1 |
▶ | STRLEN wl = (s->type == OA_SEC_BEARER) ? 7 : 6; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN wl = (s->type == OA_SEC_BEARER) ? 7 : 6; |
1 |
0 |
1 |
|
| 950 |
|
| STRLEN k; |
| 951 |
1 |
▶ | if (!az) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!az) return NULL; |
0 |
1 |
1 |
|
| 952 |
1 |
| ap = SvPV_const(az, al); |
| 953 |
1 |
▶ | if (al <= wl) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (al <= wl) return NULL; |
0 |
1 |
1 |
|
| 954 |
8 |
▶ | for (k = 0; k < wl; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < wl; k++) |
7 |
1 |
8 |
|
| 955 |
7 |
▶ | if (toLOWER((U8)ap[k]) != toLOWER((U8)want[k])) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (toLOWER((U8)ap[k]) != toLOWER((U8)want[k])) return NULL; |
1 |
6 |
7 |
| if (toLOWER((U8)ap[k]) != toLOWER((U8)want[k])) return NULL; |
1 |
6 |
7 |
| if (toLOWER((U8)ap[k]) != toLOWER((U8)want[k])) return NULL; |
0 |
7 |
7 |
|
| 956 |
1 |
▶ | while (wl < al && ap[wl] == ' ') wl++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (wl < al && ap[wl] == ' ') wl++; |
1 |
0 |
1 |
| while (wl < al && ap[wl] == ' ') wl++; |
0 |
1 |
1 |
|
| 957 |
1 |
▶ | if (s->type == OA_SEC_BEARER) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->type == OA_SEC_BEARER) |
1 |
0 |
1 |
|
| 958 |
1 |
| return sv_2mortal(newSVpvn(ap + wl, al - wl)); |
| 959 |
0 |
| dec = oa_b64_decode(aTHX_ ap + wl, al - wl); |
| 960 |
0 |
▶ | return dec ? sv_2mortal(dec) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return dec ? sv_2mortal(dec) : NULL; |
0 |
0 |
0 |
|
| 961 |
|
| } |
| 962 |
0 |
| case OA_SEC_OTHER: { |
| 963 |
|
| /* A scheme this library cannot settle: hand the checker whatever the |
| 964 |
|
| * request carried and let it decide. Something truthy has to come |
| 965 |
|
| * back even when there is no header, or the alternative could never |
| 966 |
|
| * be satisfied - mutualTLS carries no credential in the request, it |
| 967 |
|
| * is settled by the transport. */ |
| 968 |
0 |
| SV *az = oa_env_get(aTHX_ env, "HTTP_AUTHORIZATION"); |
| 969 |
0 |
▶ | return (az && SvOK(az)) ? sv_mortalcopy(az) : sv_2mortal(newSViv(1)); |
Branch: 2 branches
| Branch | True | False | Total |
| return (az && SvOK(az)) ? sv_mortalcopy(az) : sv_2mortal(newSViv(1)); |
0 |
0 |
0 |
| return (az && SvOK(az)) ? sv_mortalcopy(az) : sv_2mortal(newSViv(1)); |
0 |
0 |
0 |
|
| 970 |
|
| } |
| 971 |
|
| } |
| 972 |
0 |
| return NULL; |
| 973 |
|
| } |
| 974 |
|
| |
| 975 |
|
| /* Enforce the operation's security requirements: try each alternative in |
| 976 |
|
| * order (schemes within one are ANDed); the first fully satisfied one wins |
| 977 |
|
| * and its checker results land in $env->{'openapi.auth'} = { name => ... }. |
| 978 |
|
| * Returns NULL on success, else a +1 response (401, or 500 for a checker |
| 979 |
|
| * die). Coverage of `checkers` was validated at to_app time. */ |
| 980 |
7 |
| static SV *oa_security_check(pTHX_ oa_api *a, oa_op *o, SV *env_rv, |
| 981 |
|
| HV *checkers) { |
| 982 |
7 |
| oa_ops *t = (oa_ops *)a->ops; |
| 983 |
7 |
| HV *env = (HV *)SvRV(env_rv); |
| 984 |
|
| int i; |
| 985 |
12 |
▶ | for (i = 0; i < o->nsec; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < o->nsec; i++) { |
9 |
3 |
12 |
|
| 986 |
9 |
| oa_secalt *alt = &o->sec[i]; |
| 987 |
9 |
| HV *auth = (HV *)sv_2mortal((SV *)newHV()); |
| 988 |
9 |
| int j, ok = 1; |
| 989 |
15 |
▶ | for (j = 0; j < alt->n && ok; j++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < alt->n && ok; j++) { |
11 |
4 |
15 |
| for (j = 0; j < alt->n && ok; j++) { |
11 |
0 |
11 |
|
| 990 |
11 |
| oa_scheme *s = &t->schemes[alt->items[j].scheme]; |
| 991 |
11 |
| SV *cred = oa_sec_extract(aTHX_ s, env); |
| 992 |
|
| SV **cbp; |
| 993 |
|
| STRLEN nl; const char *np; |
| 994 |
15 |
▶ | if (!cred) { ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cred) { ok = 0; break; } |
1 |
10 |
11 |
|
| 995 |
10 |
| np = SvPV_const(s->name, nl); |
| 996 |
10 |
| cbp = hv_fetch(checkers, np, (I32)nl, 0); |
| 997 |
|
| /* No checker for a scheme this alternative names: the |
| 998 |
|
| * alternative cannot be satisfied. The Plack path validates |
| 999 |
|
| * coverage at to_app time so this never fires there; it keeps |
| 1000 |
|
| * a caller that supplies an incomplete map (check_op_security) |
| 1001 |
|
| * from dereferencing NULL. */ |
| 1002 |
10 |
▶ | if (!cbp || !*cbp) { ok = 0; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cbp || !*cbp) { ok = 0; break; } |
10 |
0 |
10 |
| if (!cbp || !*cbp) { ok = 0; break; } |
0 |
10 |
10 |
|
| 1003 |
|
| { |
| 1004 |
10 |
| dSP; int n; SV *ret = NULL; |
| 1005 |
10 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
10 |
10 |
|
| 1006 |
10 |
▶ | EXTEND(SP, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 4); |
0 |
10 |
10 |
|
| 1007 |
10 |
| PUSHs(cred); |
| 1008 |
10 |
| PUSHs(env_rv); |
| 1009 |
10 |
| PUSHs(o->op_id); |
| 1010 |
10 |
▶ | PUSHs(alt->items[j].scopes ? alt->items[j].scopes |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(alt->items[j].scopes ? alt->items[j].scopes |
10 |
0 |
10 |
|
| 1011 |
|
| : &PL_sv_undef); |
| 1012 |
10 |
| PUTBACK; |
| 1013 |
10 |
| n = call_sv(*cbp, G_SCALAR | G_EVAL); |
| 1014 |
10 |
| SPAGAIN; |
| 1015 |
10 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
10 |
0 |
10 |
| if (SvTRUE(ERRSV)) { |
0 |
10 |
10 |
|
| 1016 |
0 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 1017 |
|
| SV *resp; |
| 1018 |
0 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 1019 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 1020 |
0 |
| resp = oa_error_msg(aTHX_ 500, SvPV_nolen(e)); |
| 1021 |
0 |
| SvREFCNT_dec(e); |
| 1022 |
0 |
| return resp; |
| 1023 |
|
| } |
| 1024 |
10 |
▶ | if (n > 0) { SV *r = POPs; if (SvTRUE(r)) ret = newSVsv(r); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 0) { SV *r = POPs; if (SvTRUE(r)) ret = newSVsv(r); } |
10 |
0 |
10 |
| if (n > 0) { SV *r = POPs; if (SvTRUE(r)) ret = newSVsv(r); } |
6 |
4 |
10 |
|
| 1025 |
10 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
10 |
0 |
10 |
|
| 1026 |
10 |
▶ | if (!ret) { ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret) { ok = 0; break; } |
4 |
6 |
10 |
|
| 1027 |
6 |
| (void)hv_store(auth, np, (I32)nl, ret, 0); |
| 1028 |
|
| } |
| 1029 |
|
| } |
| 1030 |
9 |
▶ | if (ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) { |
4 |
5 |
9 |
|
| 1031 |
4 |
| (void)hv_stores(env, "openapi.auth", |
| 1032 |
|
| newRV_inc((SV *)auth)); |
| 1033 |
4 |
| return NULL; |
| 1034 |
|
| } |
| 1035 |
|
| } |
| 1036 |
3 |
| return oa_error_msg(aTHX_ 401, "unauthorized"); |
| 1037 |
|
| } |
| 1038 |
|
| |
| 1039 |
|
| /* ---- CSRF -------------------------------------------------------------------------- */ |
| 1040 |
|
| |
| 1041 |
|
| /* "scheme://host:port/..." or a bare "host:port" -> the host:port slice */ |
| 1042 |
28 |
| static void oa_origin_host(const char *p, STRLEN l, const char **hp, STRLEN *hl) { |
| 1043 |
28 |
| STRLEN i = 0, s; |
| 1044 |
28 |
| const char *sep = "://"; |
| 1045 |
168 |
▶ | for (i = 0; i + 2 < l; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 2 < l; i++) |
168 |
0 |
168 |
|
| 1046 |
168 |
▶ | if (p[i] == ':' && p[i+1] == '/' && p[i+2] == '/') { i += 3; break; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (p[i] == ':' && p[i+1] == '/' && p[i+2] == '/') { i += 3; break; } |
28 |
140 |
168 |
| if (p[i] == ':' && p[i+1] == '/' && p[i+2] == '/') { i += 3; break; } |
28 |
0 |
28 |
| if (p[i] == ':' && p[i+1] == '/' && p[i+2] == '/') { i += 3; break; } |
28 |
0 |
28 |
|
| 1047 |
28 |
▶ | if (i + 2 >= l) i = 0; /* no scheme: treat the whole thing as authority */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (i + 2 >= l) i = 0; /* no scheme: treat the whole thing as authority */ |
0 |
28 |
28 |
|
| 1048 |
28 |
| s = i; |
| 1049 |
450 |
▶ | while (i < l && p[i] != '/' && p[i] != '?' && p[i] != '#') i++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (i < l && p[i] != '/' && p[i] != '?' && p[i] != '#') i++; |
424 |
26 |
450 |
| while (i < l && p[i] != '/' && p[i] != '?' && p[i] != '#') i++; |
422 |
2 |
424 |
| while (i < l && p[i] != '/' && p[i] != '?' && p[i] != '#') i++; |
422 |
0 |
422 |
| while (i < l && p[i] != '/' && p[i] != '?' && p[i] != '#') i++; |
422 |
0 |
422 |
|
| 1050 |
28 |
| *hp = p + s; |
| 1051 |
28 |
| *hl = i - s; |
| 1052 |
|
| (void)sep; |
| 1053 |
28 |
| } |
| 1054 |
|
| |
| 1055 |
|
| /* Enforce CSRF protection for state-changing methods. cfg keys (normalised at |
| 1056 |
|
| * to_app time): origins (AV of allowed origins, may be empty for same-origin), |
| 1057 |
|
| * secret (SV, present => double-submit), cookie (SV name), header (SV name). |
| 1058 |
|
| * Returns NULL when the request may proceed, else a +1 403 response. Safe |
| 1059 |
|
| * methods (GET/HEAD/OPTIONS/TRACE) always pass. */ |
| 1060 |
19 |
| static SV *oa_csrf_check(pTHX_ oa_op *o, SV *env_rv, HV *cfg) { |
| 1061 |
19 |
| HV *env = (HV *)SvRV(env_rv); |
| 1062 |
19 |
| STRLEN ml; const char *mp = SvPV_const(o->method, ml); /* lowercase */ |
| 1063 |
|
| SV *origin, *host, **sv, **chk; |
| 1064 |
|
| AV *origins; |
| 1065 |
19 |
| const char *oh = NULL; |
| 1066 |
19 |
| STRLEN ohl = 0; |
| 1067 |
19 |
| int matched = 0; |
| 1068 |
|
| |
| 1069 |
19 |
▶ | if ((ml == 3 && memEQ(mp, "get", 3)) || (ml == 4 && memEQ(mp, "head", 4)) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((ml == 3 && memEQ(mp, "get", 3)) || (ml == 4 && memEQ(mp, "head", 4)) |
2 |
17 |
19 |
| if ((ml == 3 && memEQ(mp, "get", 3)) || (ml == 4 && memEQ(mp, "head", 4)) |
0 |
2 |
2 |
| if ((ml == 3 && memEQ(mp, "get", 3)) || (ml == 4 && memEQ(mp, "head", 4)) |
0 |
17 |
17 |
| if ((ml == 3 && memEQ(mp, "get", 3)) || (ml == 4 && memEQ(mp, "head", 4)) |
0 |
0 |
0 |
|
| 1070 |
17 |
▶ | || (ml == 7 && memEQ(mp, "options", 7)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (ml == 7 && memEQ(mp, "options", 7)) |
0 |
17 |
17 |
| || (ml == 7 && memEQ(mp, "options", 7)) |
0 |
0 |
0 |
|
| 1071 |
17 |
▶ | || (ml == 5 && memEQ(mp, "trace", 5))) |
Branch: 2 branches
| Branch | True | False | Total |
| || (ml == 5 && memEQ(mp, "trace", 5))) |
0 |
17 |
17 |
| || (ml == 5 && memEQ(mp, "trace", 5))) |
0 |
0 |
0 |
|
| 1072 |
2 |
| return NULL; |
| 1073 |
|
| |
| 1074 |
|
| /* ---- Origin / Referer check ---- */ |
| 1075 |
17 |
| origin = oa_env_get(aTHX_ env, "HTTP_ORIGIN"); |
| 1076 |
17 |
▶ | if (!origin) origin = oa_env_get(aTHX_ env, "HTTP_REFERER"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!origin) origin = oa_env_get(aTHX_ env, "HTTP_REFERER"); |
2 |
15 |
17 |
|
| 1077 |
17 |
| sv = hv_fetchs(cfg, "origins", 0); |
| 1078 |
17 |
▶ | origins = (sv && *sv && SvROK(*sv)) ? (AV *)SvRV(*sv) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| origins = (sv && *sv && SvROK(*sv)) ? (AV *)SvRV(*sv) : NULL; |
12 |
5 |
17 |
| origins = (sv && *sv && SvROK(*sv)) ? (AV *)SvRV(*sv) : NULL; |
12 |
0 |
12 |
| origins = (sv && *sv && SvROK(*sv)) ? (AV *)SvRV(*sv) : NULL; |
12 |
0 |
12 |
|
| 1079 |
17 |
▶ | if (origin) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (origin) { |
16 |
1 |
17 |
|
| 1080 |
16 |
| STRLEN ol; const char *op = SvPV_const(origin, ol); |
| 1081 |
16 |
| oa_origin_host(op, ol, &oh, &ohl); |
| 1082 |
|
| } |
| 1083 |
29 |
▶ | if (origins && av_len(origins) >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (origins && av_len(origins) >= 0) { |
12 |
5 |
17 |
| if (origins && av_len(origins) >= 0) { |
12 |
0 |
12 |
|
| 1084 |
|
| SSize_t i; |
| 1085 |
13 |
▶ | if (oh) for (i = 0; i <= av_len(origins); i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (oh) for (i = 0; i <= av_len(origins); i++) { |
12 |
0 |
12 |
| if (oh) for (i = 0; i <= av_len(origins); i++) { |
12 |
1 |
13 |
|
| 1086 |
12 |
| SV **a = av_fetch(origins, i, 0); |
| 1087 |
|
| const char *ah, *ap; STRLEN al, al2; |
| 1088 |
12 |
▶ | if (!a || !*a) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a || !*a) continue; |
12 |
0 |
12 |
| if (!a || !*a) continue; |
0 |
12 |
12 |
|
| 1089 |
12 |
| ap = SvPV_const(*a, al); |
| 1090 |
12 |
| oa_origin_host(ap, al, &ah, &al2); |
| 1091 |
12 |
▶ | if (al2 == ohl && memEQ(ah, oh, ohl)) { matched = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (al2 == ohl && memEQ(ah, oh, ohl)) { matched = 1; break; } |
12 |
0 |
12 |
| if (al2 == ohl && memEQ(ah, oh, ohl)) { matched = 1; break; } |
11 |
1 |
12 |
|
| 1092 |
|
| } |
| 1093 |
|
| } else { |
| 1094 |
|
| /* same-origin default: Origin authority must equal Host */ |
| 1095 |
5 |
| host = oa_env_get(aTHX_ env, "HTTP_HOST"); |
| 1096 |
5 |
▶ | if (oh && host) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (oh && host) { |
4 |
1 |
5 |
| if (oh && host) { |
4 |
0 |
4 |
|
| 1097 |
4 |
| STRLEN hl; const char *hpp = SvPV_const(host, hl); |
| 1098 |
4 |
▶ | if (hl == ohl && memEQ(hpp, oh, ohl)) matched = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hl == ohl && memEQ(hpp, oh, ohl)) matched = 1; |
2 |
2 |
4 |
| if (hl == ohl && memEQ(hpp, oh, ohl)) matched = 1; |
2 |
0 |
2 |
|
| 1099 |
|
| } |
| 1100 |
|
| } |
| 1101 |
17 |
▶ | if (!matched) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!matched) |
4 |
13 |
17 |
|
| 1102 |
4 |
▶ | return oa_error_msg(aTHX_ 403, |
Branch: 1 branch
| Branch | True | False | Total |
| return oa_error_msg(aTHX_ 403, |
3 |
1 |
4 |
|
| 1103 |
|
| origin ? "CSRF check failed: origin not allowed" |
| 1104 |
|
| : "CSRF check failed: missing Origin/Referer"); |
| 1105 |
|
| |
| 1106 |
|
| /* ---- token: an app-owned store, via the check callback. The submitted |
| 1107 |
|
| * token comes from the configured header (a cross-site page cannot set a |
| 1108 |
|
| * custom header, so this half is what actually stops the forgery). */ |
| 1109 |
13 |
| chk = hv_fetchs(cfg, "check", 0); |
| 1110 |
13 |
▶ | if (chk && *chk && SvROK(*chk)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (chk && *chk && SvROK(*chk)) { |
9 |
4 |
13 |
| if (chk && *chk && SvROK(*chk)) { |
9 |
0 |
9 |
| if (chk && *chk && SvROK(*chk)) { |
9 |
0 |
9 |
|
| 1111 |
9 |
| SV **hn = hv_fetchs(cfg, "header", 0); |
| 1112 |
|
| STRLEN hnl, k; |
| 1113 |
9 |
| const char *hnp = SvPV_const(*hn, hnl); |
| 1114 |
|
| char key[160]; |
| 1115 |
9 |
| SV *hd = NULL; |
| 1116 |
9 |
| dSP; int n; SV *err = NULL; SV *resp, *keep = NULL; |
| 1117 |
|
| |
| 1118 |
9 |
▶ | if (hnl + 5 < sizeof key) { /* the submitted token header */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (hnl + 5 < sizeof key) { /* the submitted token header */ |
9 |
0 |
9 |
|
| 1119 |
|
| SV **v; |
| 1120 |
9 |
| memcpy(key, "HTTP_", 5); |
| 1121 |
111 |
▶ | for (k = 0; k < hnl; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < hnl; k++) { |
102 |
9 |
111 |
|
| 1122 |
102 |
| char c = hnp[k]; |
| 1123 |
102 |
▶ | key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
Branch: 2 branches
| Branch | True | False | Total |
| key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
85 |
17 |
102 |
| key[5 + k] = (c == '-') ? '_' : (char)toUPPER((U8)c); |
32 |
53 |
85 |
|
| 1124 |
|
| } |
| 1125 |
9 |
| v = hv_fetch(env, key, (I32)(5 + hnl), 0); |
| 1126 |
9 |
▶ | if (v && *v && SvOK(*v)) hd = *v; |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && *v && SvOK(*v)) hd = *v; |
7 |
2 |
9 |
| if (v && *v && SvOK(*v)) hd = *v; |
7 |
0 |
7 |
| if (v && *v && SvOK(*v)) hd = *v; |
7 |
0 |
7 |
|
| 1127 |
|
| } |
| 1128 |
|
| |
| 1129 |
|
| /* check($submitted_token, $env, $operationId) - a truthy return |
| 1130 |
|
| * passes AND is stashed as $env->{'openapi.csrf'} (mirroring the |
| 1131 |
|
| * security checkers' 'openapi.auth'), so the handler / after hook can |
| 1132 |
|
| * consume or rotate the token without re-loading the store. A die |
| 1133 |
|
| * becomes a 500. */ |
| 1134 |
9 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
9 |
9 |
|
| 1135 |
9 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
9 |
9 |
|
| 1136 |
9 |
▶ | PUSHs(hd ? hd : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(hd ? hd : &PL_sv_undef); |
7 |
2 |
9 |
|
| 1137 |
9 |
| PUSHs(env_rv); |
| 1138 |
9 |
| PUSHs(o->op_id); |
| 1139 |
9 |
| PUTBACK; |
| 1140 |
9 |
| n = call_sv(*chk, G_SCALAR | G_EVAL); |
| 1141 |
9 |
| SPAGAIN; |
| 1142 |
9 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
9 |
0 |
9 |
| if (SvTRUE(ERRSV)) { |
1 |
8 |
9 |
|
| 1143 |
1 |
▶ | err = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| err = newSVsv(ERRSV); |
1 |
0 |
1 |
|
| 1144 |
1 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
1 |
0 |
1 |
|
| 1145 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 1146 |
1 |
| resp = oa_error_msg(aTHX_ 500, SvPV_nolen(err)); |
| 1147 |
1 |
| SvREFCNT_dec(err); |
| 1148 |
4 |
| return resp; |
| 1149 |
|
| } |
| 1150 |
8 |
▶ | if (n > 0) { SV *r = POPs; if (SvTRUE(r)) keep = newSVsv(r); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 0) { SV *r = POPs; if (SvTRUE(r)) keep = newSVsv(r); } |
8 |
0 |
8 |
| if (n > 0) { SV *r = POPs; if (SvTRUE(r)) keep = newSVsv(r); } |
5 |
3 |
8 |
|
| 1151 |
8 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
8 |
0 |
8 |
|
| 1152 |
8 |
▶ | if (!keep) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!keep) |
3 |
5 |
8 |
|
| 1153 |
3 |
| return oa_error_msg(aTHX_ 403, |
| 1154 |
|
| "CSRF check failed: invalid or missing token"); |
| 1155 |
5 |
| (void)hv_stores(env, "openapi.csrf", keep); /* env owns it */ |
| 1156 |
|
| /* a returned string token rotates the cookie: the framework sets it |
| 1157 |
|
| * on the response (any status), so the client always leaves holding a |
| 1158 |
|
| * fresh, unused token. A non-string truthy return (1, a ref) just |
| 1159 |
|
| * passes - set your own cookie in the after hook. */ |
| 1160 |
5 |
▶ | if (SvPOK(keep) && SvCUR(keep) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvPOK(keep) && SvCUR(keep) > 0) { |
4 |
1 |
5 |
| if (SvPOK(keep) && SvCUR(keep) > 0) { |
4 |
0 |
4 |
|
| 1161 |
4 |
| SV **cn = hv_fetchs(cfg, "cookie", 0); |
| 1162 |
4 |
| STRLEN cnl; const char *cnp = SvPV_const(*cn, cnl); |
| 1163 |
4 |
| SV *sc = newSVpvn(cnp, cnl); |
| 1164 |
4 |
| sv_catpvs(sc, "="); |
| 1165 |
4 |
| sv_catsv(sc, keep); |
| 1166 |
4 |
| sv_catpvs(sc, "; Path=/; SameSite=Strict"); |
| 1167 |
4 |
| (void)hv_stores(env, "openapi.csrf.setcookie", sc); |
| 1168 |
|
| } |
| 1169 |
|
| } |
| 1170 |
|
| |
| 1171 |
9 |
| return NULL; |
| 1172 |
|
| } |
| 1173 |
|
| |
| 1174 |
|
| /* ---- Future completion (nonblocking servers) --------------------------------------- * |
| 1175 |
|
| * When a handler returns a Future on a nonblocking server AND there is |
| 1176 |
|
| * post-processing to do (after hook or validate_responses), the future is |
| 1177 |
|
| * chained with ->then(done, fail): done maps the resolved value and applies |
| 1178 |
|
| * response validation + the after hook; fail turns a failed future into a |
| 1179 |
|
| * 500 (also passed to after). Captures: [0]=api self SV (keeps the op table |
| 1180 |
|
| * alive), [1]=env_rv, [2]=opId SV, [3]=after or undef, [4]=vresp IV, |
| 1181 |
|
| * [5]=op pointer IV, [6]=opts HV ref or undef. */ |
| 1182 |
|
| |
| 1183 |
|
| XS_INTERNAL(oa_then_done_cb); |
| 1184 |
2 |
| XS_INTERNAL(oa_then_done_cb) { |
| 1185 |
2 |
| dXSARGS; |
| 1186 |
2 |
| AV *cap = oa_clos_cap(aTHX_ cv); |
| 1187 |
|
| SV *env_rv, *opid, *after, *resp, *optsv; |
| 1188 |
|
| oa_op *o; |
| 1189 |
|
| int vresp; |
| 1190 |
2 |
▶ | if (!cap) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) XSRETURN_EMPTY; |
0 |
2 |
2 |
|
| 1191 |
2 |
| env_rv = *av_fetch(cap, 1, 0); |
| 1192 |
2 |
| opid = *av_fetch(cap, 2, 0); |
| 1193 |
2 |
| after = *av_fetch(cap, 3, 0); |
| 1194 |
2 |
| vresp = (int)SvIV(*av_fetch(cap, 4, 0)); |
| 1195 |
2 |
| o = INT2PTR(oa_op *, SvIV(*av_fetch(cap, 5, 0))); |
| 1196 |
2 |
| optsv = *av_fetch(cap, 6, 0); |
| 1197 |
2 |
▶ | resp = oa_map_result(aTHX_ items > 0 ? ST(0) : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| resp = oa_map_result(aTHX_ items > 0 ? ST(0) : &PL_sv_undef); |
2 |
0 |
2 |
|
| 1198 |
2 |
▶ | if (vresp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (vresp) { |
1 |
1 |
2 |
|
| 1199 |
1 |
| SV *bad = oa_check_response_opts(aTHX_ o, resp, vresp, |
| 1200 |
1 |
▶ | SvROK(optsv) ? (HV *)SvRV(optsv) : NULL, env_rv); |
Branch: 1 branch
| Branch | True | False | Total |
| SvROK(optsv) ? (HV *)SvRV(optsv) : NULL, env_rv); |
1 |
0 |
1 |
|
| 1201 |
1 |
▶ | if (bad) { SvREFCNT_dec(resp); resp = bad; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (bad) { SvREFCNT_dec(resp); resp = bad; } |
1 |
0 |
1 |
|
| 1202 |
|
| } |
| 1203 |
2 |
| oa_run_after(aTHX_ &resp, after, env_rv, opid); |
| 1204 |
2 |
▶ | oa_finalize(aTHX_ resp, env_rv, SvROK(optsv) ? (HV *)SvRV(optsv) : NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| oa_finalize(aTHX_ resp, env_rv, SvROK(optsv) ? (HV *)SvRV(optsv) : NULL); |
2 |
0 |
2 |
|
| 1205 |
2 |
| ST(0) = sv_2mortal(resp); |
| 1206 |
2 |
| XSRETURN(1); |
| 1207 |
|
| } |
| 1208 |
|
| |
| 1209 |
|
| XS_INTERNAL(oa_then_fail_cb); |
| 1210 |
0 |
| XS_INTERNAL(oa_then_fail_cb) { |
| 1211 |
0 |
| dXSARGS; |
| 1212 |
0 |
| AV *cap = oa_clos_cap(aTHX_ cv); |
| 1213 |
|
| SV *env_rv, *opid, *after, *resp, *optsv; |
| 1214 |
0 |
▶ | if (!cap) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) XSRETURN_EMPTY; |
0 |
0 |
0 |
|
| 1215 |
0 |
| env_rv = *av_fetch(cap, 1, 0); |
| 1216 |
0 |
| opid = *av_fetch(cap, 2, 0); |
| 1217 |
0 |
| after = *av_fetch(cap, 3, 0); |
| 1218 |
0 |
| optsv = *av_fetch(cap, 6, 0); |
| 1219 |
0 |
▶ | resp = oa_error_msg(aTHX_ 500, |
Branch: 1 branch
| Branch | True | False | Total |
| resp = oa_error_msg(aTHX_ 500, |
0 |
0 |
0 |
|
| 1220 |
0 |
▶ | (items > 0 && SvOK(ST(0))) ? SvPV_nolen(ST(0)) : "handler future failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| (items > 0 && SvOK(ST(0))) ? SvPV_nolen(ST(0)) : "handler future failed"); |
0 |
0 |
0 |
|
| 1221 |
0 |
| oa_run_after(aTHX_ &resp, after, env_rv, opid); |
| 1222 |
0 |
▶ | oa_finalize(aTHX_ resp, env_rv, SvROK(optsv) ? (HV *)SvRV(optsv) : NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| oa_finalize(aTHX_ resp, env_rv, SvROK(optsv) ? (HV *)SvRV(optsv) : NULL); |
0 |
0 |
0 |
|
| 1223 |
0 |
| ST(0) = sv_2mortal(resp); |
| 1224 |
0 |
| XSRETURN(1); |
| 1225 |
|
| } |
| 1226 |
|
| |
| 1227 |
|
| /* one capture AV for the then callbacks (each closure needs its own) */ |
| 1228 |
4 |
| static AV *oa_then_cap(pTHX_ SV *self_sv, SV *env_rv, oa_op *o, SV *after, |
| 1229 |
|
| int vresp, SV *optsv) { |
| 1230 |
4 |
| AV *cap = newAV(); |
| 1231 |
4 |
| av_push(cap, SvREFCNT_inc(self_sv)); |
| 1232 |
4 |
| av_push(cap, SvREFCNT_inc(env_rv)); |
| 1233 |
4 |
| av_push(cap, newSVsv(o->op_id)); |
| 1234 |
4 |
▶ | av_push(cap, after && SvOK(after) ? SvREFCNT_inc(after) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(cap, after && SvOK(after) ? SvREFCNT_inc(after) : newSV(0)); |
4 |
0 |
4 |
| av_push(cap, after && SvOK(after) ? SvREFCNT_inc(after) : newSV(0)); |
2 |
2 |
4 |
|
| 1235 |
4 |
| av_push(cap, newSViv(vresp)); |
| 1236 |
4 |
| av_push(cap, newSViv(PTR2IV(o))); |
| 1237 |
4 |
▶ | av_push(cap, optsv && SvROK(optsv) ? SvREFCNT_inc(optsv) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(cap, optsv && SvROK(optsv) ? SvREFCNT_inc(optsv) : newSV(0)); |
4 |
0 |
4 |
| av_push(cap, optsv && SvROK(optsv) ? SvREFCNT_inc(optsv) : newSV(0)); |
4 |
0 |
4 |
|
| 1238 |
4 |
| return cap; |
| 1239 |
|
| } |
| 1240 |
|
| |
| 1241 |
|
| /* ---- the request path ------------------------------------------------------------ */ |
| 1242 |
|
| |
| 1243 |
|
| /* Serve one PSGI request. Returns a PSGI triplet SV, or a Future SV on a |
| 1244 |
|
| * non-blocking server (chained with the post-processing when an after hook |
| 1245 |
|
| * or response validation is active). (+1 owned by caller) */ |
| 1246 |
619 |
| static SV *oa_serve_psgi(pTHX_ SV *self_sv, HV *handlers, SV *env_rv, |
| 1247 |
|
| int vresp, SV *before, SV *after, HV *seccb, |
| 1248 |
|
| HV *csrf, HV *opts) { |
| 1249 |
619 |
| oa_api *a = (oa_api *)INT2PTR(void *, SvIV(SvRV(self_sv))); |
| 1250 |
619 |
| HV *env = (HV *)SvRV(env_rv); |
| 1251 |
619 |
| SV *msv = oa_env_get(aTHX_ env, "REQUEST_METHOD"); |
| 1252 |
619 |
| SV *psv = oa_env_get(aTHX_ env, "PATH_INFO"); |
| 1253 |
619 |
| SV *qsv = oa_env_get(aTHX_ env, "QUERY_STRING"); |
| 1254 |
619 |
| HV *caps = (HV *)sv_2mortal((SV *)newHV()); |
| 1255 |
619 |
| AV *allow = (AV *)sv_2mortal((SV *)newAV()); |
| 1256 |
|
| oa_op *o; |
| 1257 |
|
| HV *headers; |
| 1258 |
|
| SV *body, *handler; |
| 1259 |
619 |
| HV *params = NULL; |
| 1260 |
|
| AV *errs; |
| 1261 |
619 |
| int is_options = 0; |
| 1262 |
619 |
| HV *cors = NULL; |
| 1263 |
619 |
| int negotiate = 0; |
| 1264 |
619 |
| IV max_body = 0; |
| 1265 |
|
| STRLEN ml, pl, il; |
| 1266 |
|
| const char *mp, *pp, *ip; |
| 1267 |
|
| SV **h; |
| 1268 |
|
| |
| 1269 |
|
| /* every triplet exit runs through finalize (problem+json, CORS, headers) */ |
| 1270 |
|
| #define OA_FIN(r) oa_fin(aTHX_ (r), env_rv, opts) |
| 1271 |
|
| |
| 1272 |
619 |
▶ | if (opts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) { |
619 |
0 |
619 |
|
| 1273 |
|
| SV **v; |
| 1274 |
619 |
▶ | if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
5 |
614 |
619 |
| if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
5 |
0 |
5 |
| if ((v = hv_fetchs(opts, "cors", 0)) && *v && SvROK(*v)) |
5 |
0 |
5 |
|
| 1275 |
5 |
| cors = (HV *)SvRV(*v); |
| 1276 |
619 |
▶ | if ((v = hv_fetchs(opts, "negotiate", 0)) && *v) negotiate = SvTRUE(*v); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "negotiate", 0)) && *v) negotiate = SvTRUE(*v); |
5 |
614 |
619 |
| if ((v = hv_fetchs(opts, "negotiate", 0)) && *v) negotiate = SvTRUE(*v); |
5 |
0 |
5 |
|
| 1277 |
619 |
▶ | if ((v = hv_fetchs(opts, "max_body_size", 0)) && *v) max_body = SvIV(*v); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetchs(opts, "max_body_size", 0)) && *v) max_body = SvIV(*v); |
502 |
117 |
619 |
| if ((v = hv_fetchs(opts, "max_body_size", 0)) && *v) max_body = SvIV(*v); |
502 |
0 |
502 |
|
| 1278 |
|
| } |
| 1279 |
|
| |
| 1280 |
1238 |
▶ | mp = msv ? SvPV_const(msv, ml) : (ml = 3, "GET"); |
Branch: 1 branch
| Branch | True | False | Total |
| mp = msv ? SvPV_const(msv, ml) : (ml = 3, "GET"); |
619 |
0 |
619 |
|
| 1281 |
1238 |
▶ | pp = psv ? SvPV_const(psv, pl) : (pl = 1, "/"); |
Branch: 1 branch
| Branch | True | False | Total |
| pp = psv ? SvPV_const(psv, pl) : (pl = 1, "/"); |
619 |
0 |
619 |
|
| 1282 |
619 |
▶ | is_options = (ml == 7 && (mp[0]=='O'||mp[0]=='o') && oa_ci_eq(mp, ml, "options", 7)); |
Branch: 4 branches
| Branch | True | False | Total |
| is_options = (ml == 7 && (mp[0]=='O'||mp[0]=='o') && oa_ci_eq(mp, ml, "options", 7)); |
1 |
618 |
619 |
| is_options = (ml == 7 && (mp[0]=='O'||mp[0]=='o') && oa_ci_eq(mp, ml, "options", 7)); |
0 |
1 |
1 |
| is_options = (ml == 7 && (mp[0]=='O'||mp[0]=='o') && oa_ci_eq(mp, ml, "options", 7)); |
0 |
0 |
0 |
| is_options = (ml == 7 && (mp[0]=='O'||mp[0]=='o') && oa_ci_eq(mp, ml, "options", 7)); |
1 |
0 |
1 |
|
| 1283 |
619 |
| o = oa_route(aTHX_ a, mp, ml, pp, pl, caps, allow); |
| 1284 |
619 |
▶ | if (!o) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o) { |
11 |
608 |
619 |
|
| 1285 |
11 |
▶ | if (av_len(allow) >= 0) { /* path exists, method does not */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(allow) >= 0) { /* path exists, method does not */ |
2 |
9 |
11 |
|
| 1286 |
|
| SV *resp; |
| 1287 |
|
| AV *tv, *ha; |
| 1288 |
|
| SV **hh; |
| 1289 |
|
| SV *joined; |
| 1290 |
|
| SSize_t i; |
| 1291 |
|
| /* CORS preflight: an OPTIONS with Access-Control-Request-Method */ |
| 1292 |
2 |
▶ | if (cors && is_options |
Branch: 2 branches
| Branch | True | False | Total |
| if (cors && is_options |
1 |
1 |
2 |
| if (cors && is_options |
1 |
0 |
1 |
|
| 1293 |
1 |
▶ | && oa_env_get(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_METHOD")) |
Branch: 1 branch
| Branch | True | False | Total |
| && oa_env_get(aTHX_ env, "HTTP_ACCESS_CONTROL_REQUEST_METHOD")) |
1 |
0 |
1 |
|
| 1294 |
1 |
| return OA_FIN(oa_cors_preflight(aTHX_ env_rv, cors, allow)); |
| 1295 |
1 |
| resp = oa_error_msg(aTHX_ 405, "method not allowed"); |
| 1296 |
1 |
| tv = (AV *)SvRV(resp); |
| 1297 |
1 |
| hh = av_fetch(tv, 1, 0); |
| 1298 |
1 |
| ha = (AV *)SvRV(*hh); |
| 1299 |
1 |
| joined = newSVpvs(""); |
| 1300 |
3 |
▶ | for (i = 0; i <= av_len(allow); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= av_len(allow); i++) { |
2 |
1 |
3 |
|
| 1301 |
2 |
| SV **m = av_fetch(allow, i, 0); |
| 1302 |
2 |
▶ | if (i) sv_catpvs(joined, ", "); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i) sv_catpvs(joined, ", "); |
1 |
1 |
2 |
|
| 1303 |
2 |
| sv_catsv(joined, *m); |
| 1304 |
|
| } |
| 1305 |
1 |
| av_push(ha, newSVpvs("Allow")); |
| 1306 |
1 |
| av_push(ha, joined); |
| 1307 |
1 |
| return OA_FIN(resp); |
| 1308 |
|
| } |
| 1309 |
9 |
| return OA_FIN(oa_error_msg(aTHX_ 404, "not found")); |
| 1310 |
|
| } |
| 1311 |
|
| |
| 1312 |
|
| /* body size limit: reject an over-large declared Content-Length before |
| 1313 |
|
| * doing any auth or reading the body */ |
| 1314 |
608 |
▶ | if (max_body > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (max_body > 0) { |
502 |
106 |
608 |
|
| 1315 |
502 |
| SV *cl = oa_env_get(aTHX_ env, "CONTENT_LENGTH"); |
| 1316 |
502 |
▶ | if (cl && SvIV(cl) > max_body) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cl && SvIV(cl) > max_body) { |
2 |
500 |
502 |
| if (cl && SvIV(cl) > max_body) { |
1 |
1 |
2 |
|
| 1317 |
1 |
| SV *resp = oa_error_msg(aTHX_ 413, "request body too large"); |
| 1318 |
1 |
| oa_run_after(aTHX_ &resp, after, env_rv, o->op_id); |
| 1319 |
1 |
| return OA_FIN(resp); |
| 1320 |
|
| } |
| 1321 |
|
| } |
| 1322 |
|
| |
| 1323 |
|
| /* CSRF: guard state-changing methods before anything else touches the |
| 1324 |
|
| * request (independent of who is authenticated) */ |
| 1325 |
607 |
▶ | if (csrf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (csrf) { |
19 |
588 |
607 |
|
| 1326 |
19 |
| SV *denied = oa_csrf_check(aTHX_ o, env_rv, csrf); |
| 1327 |
19 |
▶ | if (denied) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (denied) { |
8 |
11 |
19 |
|
| 1328 |
8 |
| oa_run_after(aTHX_ &denied, after, env_rv, o->op_id); |
| 1329 |
8 |
| return OA_FIN(denied); |
| 1330 |
|
| } |
| 1331 |
|
| } |
| 1332 |
|
| |
| 1333 |
|
| /* spec-driven security first: extract credential, run the checker, 401 |
| 1334 |
|
| * when no requirement alternative is satisfied */ |
| 1335 |
599 |
▶ | if (o->nsec && seccb) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (o->nsec && seccb) { |
2 |
597 |
599 |
| if (o->nsec && seccb) { |
2 |
0 |
2 |
|
| 1336 |
2 |
| SV *denied = oa_security_check(aTHX_ a, o, env_rv, seccb); |
| 1337 |
2 |
▶ | if (denied) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (denied) { |
1 |
1 |
2 |
|
| 1338 |
1 |
| oa_run_after(aTHX_ &denied, after, env_rv, o->op_id); |
| 1339 |
1 |
| return OA_FIN(denied); |
| 1340 |
|
| } |
| 1341 |
|
| } |
| 1342 |
|
| |
| 1343 |
|
| /* content negotiation (opt-in): 415 on an unsupported request body type, |
| 1344 |
|
| * 406 when the client's Accept admits none of the declared responses */ |
| 1345 |
598 |
▶ | if (negotiate) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (negotiate) { |
5 |
593 |
598 |
|
| 1346 |
5 |
| SV *bad = oa_check_request_ctype(aTHX_ o, env); |
| 1347 |
5 |
▶ | if (!bad) bad = oa_check_accept(aTHX_ o, env); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bad) bad = oa_check_accept(aTHX_ o, env); |
4 |
1 |
5 |
|
| 1348 |
5 |
▶ | if (bad) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bad) { |
2 |
3 |
5 |
|
| 1349 |
2 |
| oa_run_after(aTHX_ &bad, after, env_rv, o->op_id); |
| 1350 |
2 |
| return OA_FIN(bad); |
| 1351 |
|
| } |
| 1352 |
|
| } |
| 1353 |
|
| |
| 1354 |
|
| /* before hook: post-route, pre-validation - a reference return |
| 1355 |
|
| * short-circuits (the auth case: return [401, ...]) */ |
| 1356 |
596 |
▶ | if (before && SvOK(before)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (before && SvOK(before)) { |
596 |
0 |
596 |
| if (before && SvOK(before)) { |
6 |
590 |
596 |
|
| 1357 |
6 |
| SV *early = oa_run_before(aTHX_ before, env_rv, o->op_id); |
| 1358 |
6 |
▶ | if (early) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (early) { |
2 |
4 |
6 |
|
| 1359 |
2 |
| oa_run_after(aTHX_ &early, after, env_rv, o->op_id); |
| 1360 |
2 |
| return OA_FIN(early); |
| 1361 |
|
| } |
| 1362 |
|
| } |
| 1363 |
|
| |
| 1364 |
594 |
| headers = (HV *)sv_2mortal((SV *)oa_headers_for(aTHX_ o, env)); |
| 1365 |
594 |
▶ | body = o->nbodies ? oa_read_body_env(aTHX_ env) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| body = o->nbodies ? oa_read_body_env(aTHX_ env) : NULL; |
12 |
582 |
594 |
|
| 1366 |
594 |
| errs = (AV *)sv_2mortal((SV *)newAV()); |
| 1367 |
|
| |
| 1368 |
594 |
▶ | if (!oa_validate_op(aTHX_ a, o, caps, |
Branch: 2 branches
| Branch | True | False | Total |
| if (!oa_validate_op(aTHX_ a, o, caps, |
594 |
0 |
594 |
| if (!oa_validate_op(aTHX_ a, o, caps, |
7 |
587 |
594 |
|
| 1369 |
|
| qsv ? qsv : &PL_sv_no, headers, body, |
| 1370 |
|
| ¶ms, errs)) { |
| 1371 |
7 |
| SV *resp = oa_error_response(aTHX_ 400, errs); |
| 1372 |
7 |
| oa_run_after(aTHX_ &resp, after, env_rv, o->op_id); |
| 1373 |
7 |
| return OA_FIN(resp); |
| 1374 |
|
| } |
| 1375 |
|
| |
| 1376 |
587 |
| ip = SvPV_const(o->op_id, il); |
| 1377 |
587 |
| h = hv_fetch(handlers, ip, (I32)il, 0); |
| 1378 |
587 |
▶ | if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVCV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVCV) { |
585 |
2 |
587 |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVCV) { |
585 |
0 |
585 |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVCV) { |
585 |
0 |
585 |
| if (!h || !*h || !SvROK(*h) || SvTYPE(SvRV(*h)) != SVt_PVCV) { |
0 |
585 |
585 |
|
| 1379 |
|
| SV *resp; |
| 1380 |
2 |
| SvREFCNT_dec((SV *)params); |
| 1381 |
2 |
| resp = oa_error_msg(aTHX_ 501, "no handler for this operation"); |
| 1382 |
2 |
| oa_run_after(aTHX_ &resp, after, env_rv, o->op_id); |
| 1383 |
2 |
| return OA_FIN(resp); |
| 1384 |
|
| } |
| 1385 |
585 |
| handler = *h; |
| 1386 |
|
| |
| 1387 |
|
| { /* call the handler: ($params, $env) */ |
| 1388 |
585 |
| dSP; int n; SV *res = NULL, *resp; |
| 1389 |
585 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
585 |
585 |
|
| 1390 |
585 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
585 |
585 |
|
| 1391 |
585 |
| mPUSHs(newRV_noinc((SV *)params)); |
| 1392 |
585 |
| PUSHs(env_rv); |
| 1393 |
585 |
| PUTBACK; |
| 1394 |
585 |
| n = call_sv(handler, G_SCALAR | G_EVAL); |
| 1395 |
585 |
| SPAGAIN; |
| 1396 |
585 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
585 |
0 |
585 |
| if (SvTRUE(ERRSV)) { |
2 |
583 |
585 |
|
| 1397 |
2 |
▶ | SV *e = newSVsv(ERRSV); /* keep past FREETMPS */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); /* keep past FREETMPS */ |
2 |
0 |
2 |
|
| 1398 |
2 |
▶ | if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) (void)POPs; |
2 |
0 |
2 |
|
| 1399 |
2 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
2 |
0 |
2 |
|
| 1400 |
2 |
| resp = oa_error_msg(aTHX_ 500, SvPV_nolen(e)); |
| 1401 |
2 |
| SvREFCNT_dec(e); |
| 1402 |
2 |
| oa_run_after(aTHX_ &resp, after, env_rv, o->op_id); |
| 1403 |
2 |
| return OA_FIN(resp); |
| 1404 |
|
| } |
| 1405 |
583 |
▶ | res = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| res = n > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
583 |
0 |
583 |
|
| 1406 |
583 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
583 |
0 |
583 |
|
| 1407 |
|
| |
| 1408 |
583 |
▶ | if (oa_is_future(aTHX_ res)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (oa_is_future(aTHX_ res)) { |
2 |
581 |
583 |
|
| 1409 |
2 |
| SV *nb = oa_env_get(aTHX_ env, "psgi.nonblocking"); |
| 1410 |
2 |
▶ | if (nb && SvTRUE(nb)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nb && SvTRUE(nb)) { |
2 |
0 |
2 |
| if (nb && SvTRUE(nb)) { |
2 |
0 |
2 |
|
| 1411 |
2 |
▶ | int want_after = after && SvOK(after); |
Branch: 2 branches
| Branch | True | False | Total |
| int want_after = after && SvOK(after); |
2 |
0 |
2 |
| int want_after = after && SvOK(after); |
1 |
1 |
2 |
|
| 1412 |
2 |
▶ | if (!want_after && !vresp && !opts) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!want_after && !vresp && !opts) |
1 |
1 |
2 |
| if (!want_after && !vresp && !opts) |
0 |
1 |
1 |
| if (!want_after && !vresp && !opts) |
0 |
0 |
0 |
|
| 1413 |
0 |
| return res; /* nothing to add: hand it over */ |
| 1414 |
|
| { /* chain ->then(done, fail) so mapping + response |
| 1415 |
|
| * validation + finalize + the after hook run on |
| 1416 |
|
| * resolution */ |
| 1417 |
2 |
| dSP; int n2; SV *derived = NULL; |
| 1418 |
2 |
| SV *optsv = opts ? sv_2mortal(newRV_inc((SV *)opts)) |
| 1419 |
2 |
▶ | : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| : &PL_sv_undef; |
2 |
0 |
2 |
|
| 1420 |
2 |
| SV *done = oa_closure(aTHX_ oa_then_done_cb, |
| 1421 |
|
| oa_then_cap(aTHX_ self_sv, env_rv, o, after, vresp, optsv)); |
| 1422 |
2 |
| SV *fail = oa_closure(aTHX_ oa_then_fail_cb, |
| 1423 |
|
| oa_then_cap(aTHX_ self_sv, env_rv, o, after, 0, optsv)); |
| 1424 |
2 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
2 |
2 |
|
| 1425 |
2 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
2 |
2 |
|
| 1426 |
2 |
| PUSHs(sv_2mortal(res)); |
| 1427 |
2 |
| PUSHs(sv_2mortal(done)); |
| 1428 |
2 |
| PUSHs(sv_2mortal(fail)); |
| 1429 |
2 |
| PUTBACK; |
| 1430 |
2 |
| n2 = call_method("then", G_SCALAR | G_EVAL); |
| 1431 |
2 |
| SPAGAIN; |
| 1432 |
2 |
▶ | if (!SvTRUE(ERRSV) && n2 > 0) derived = SvREFCNT_inc(POPs); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && n2 > 0) derived = SvREFCNT_inc(POPs); |
2 |
0 |
2 |
| if (!SvTRUE(ERRSV) && n2 > 0) derived = SvREFCNT_inc(POPs); |
2 |
0 |
2 |
| if (!SvTRUE(ERRSV) && n2 > 0) derived = SvREFCNT_inc(POPs); |
2 |
0 |
2 |
|
| 1433 |
0 |
▶ | else if (n2 > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n2 > 0) (void)POPs; |
0 |
0 |
0 |
|
| 1434 |
2 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
2 |
0 |
2 |
|
| 1435 |
2 |
▶ | if (derived) return derived; |
Branch: 1 branch
| Branch | True | False | Total |
| if (derived) return derived; |
2 |
0 |
2 |
|
| 1436 |
0 |
| return OA_FIN(oa_error_msg(aTHX_ 500, "could not chain future")); |
| 1437 |
|
| } |
| 1438 |
|
| } |
| 1439 |
|
| { /* blocking: await inline */ |
| 1440 |
0 |
| dSP; int n2; SV *val = NULL; |
| 1441 |
0 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
0 |
0 |
|
| 1442 |
0 |
▶ | EXTEND(SP, 1); PUSHs(sv_2mortal(res)); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(sv_2mortal(res)); |
0 |
0 |
0 |
|
| 1443 |
0 |
| PUTBACK; |
| 1444 |
0 |
| n2 = call_method("get", G_SCALAR | G_EVAL); |
| 1445 |
0 |
| SPAGAIN; |
| 1446 |
0 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
0 |
0 |
0 |
| if (SvTRUE(ERRSV)) { |
0 |
0 |
0 |
|
| 1447 |
0 |
▶ | SV *e = newSVsv(ERRSV); /* keep past FREETMPS */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); /* keep past FREETMPS */ |
0 |
0 |
0 |
|
| 1448 |
0 |
▶ | if (n2 > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n2 > 0) (void)POPs; |
0 |
0 |
0 |
|
| 1449 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 1450 |
0 |
| resp = oa_error_msg(aTHX_ 500, SvPV_nolen(e)); |
| 1451 |
0 |
| SvREFCNT_dec(e); |
| 1452 |
0 |
| oa_run_after(aTHX_ &resp, after, env_rv, o->op_id); |
| 1453 |
0 |
| return OA_FIN(resp); |
| 1454 |
|
| } |
| 1455 |
0 |
▶ | val = n2 > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| val = n2 > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
0 |
0 |
0 |
|
| 1456 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 1457 |
0 |
| res = val; |
| 1458 |
|
| } |
| 1459 |
|
| } |
| 1460 |
581 |
| resp = oa_map_result(aTHX_ res); |
| 1461 |
581 |
| SvREFCNT_dec(res); |
| 1462 |
581 |
▶ | if (vresp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (vresp) { |
39 |
542 |
581 |
|
| 1463 |
39 |
| SV *bad = oa_check_response_opts(aTHX_ o, resp, vresp, opts, env_rv); |
| 1464 |
39 |
▶ | if (bad) { SvREFCNT_dec(resp); resp = bad; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (bad) { SvREFCNT_dec(resp); resp = bad; } |
2 |
37 |
39 |
|
| 1465 |
|
| } |
| 1466 |
581 |
| oa_run_after(aTHX_ &resp, after, env_rv, o->op_id); |
| 1467 |
581 |
| return OA_FIN(resp); |
| 1468 |
|
| } |
| 1469 |
|
| #undef OA_FIN |
| 1470 |
|
| } |
| 1471 |
|
| |
| 1472 |
|
| #endif /* OA_PLACK_H */ |