| 1 |
|
| /* punk_request.h - C request parsing (the Punk::Request hot methods). |
| 2 |
|
| * |
| 3 |
|
| * The object is a plain blessed AV whose slot layout is shared with |
| 4 |
|
| * lib/Punk/Request.pm (the pure-Perl reference tier) - keep the enum |
| 5 |
|
| * below in sync with the constants there. Parsing semantics mirror the |
| 6 |
|
| * Perl exactly: pairs split on '&'/';', keys split on the first '=', |
| 7 |
|
| * '+' and %XX percent-decoding, repeated keys promote to arrayrefs, |
| 8 |
|
| * cookie values decode with first-value-wins. body() stays Perl (I/O); |
| 9 |
|
| * form() calls it through the method interface once, then caches. |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #ifndef PUNK_REQUEST_H |
| 13 |
|
| #define PUNK_REQUEST_H |
| 14 |
|
| |
| 15 |
|
| enum { |
| 16 |
|
| PQ_ENV = 0, |
| 17 |
|
| PQ_QUERY = 1, |
| 18 |
|
| PQ_FORM = 2, |
| 19 |
|
| PQ_COOKIES = 3, |
| 20 |
|
| PQ_BODY = 4, |
| 21 |
|
| PQ_READ = 5, |
| 22 |
|
| PQ_UPLOADS = 6, |
| 23 |
|
| PQ_TEMPFILES = 7, /* spilled upload paths, removed when the request ends */ |
| 24 |
|
| PQ_XML = 8 /* the body parsed as XML, once per request */ |
| 25 |
|
| }; |
| 26 |
|
| |
| 27 |
14016 |
| static AV *punk_req_av(pTHX_ SV *self) { |
| 28 |
|
| /* |
| 29 |
|
| # uncoverable branch true |
| 30 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 31 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 32 |
14016 |
▶ | if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) |
14016 |
0 |
14016 |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) |
0 |
14016 |
14016 |
|
| 33 |
0 |
| croak("Punk::Request: not a request object"); /* # uncoverable statement */ |
| 34 |
14016 |
| return (AV *)SvRV(self); |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
13500 |
| static HV *punk_req_env(pTHX_ AV *req) { |
| 38 |
13500 |
| SV **e = av_fetch(req, PQ_ENV, 0); |
| 39 |
|
| /* |
| 40 |
|
| # uncoverable branch true |
| 41 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 42 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 43 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 44 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 45 |
13500 |
▶ | if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) |
13500 |
0 |
13500 |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) |
13500 |
0 |
13500 |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) |
13500 |
0 |
13500 |
| if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) |
0 |
13500 |
13500 |
|
| 46 |
0 |
| croak("Punk::Request: no PSGI environment"); /* # uncoverable statement */ |
| 47 |
13500 |
| return (HV *)SvRV(*e); |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| /* Every request header, as a hash keyed the way HTTP/2 and most Perl web |
| 51 |
|
| * code spell them: HTTP_X_FORWARDED_FOR -> 'x-forwarded-for', lowercased |
| 52 |
|
| * with dashes. CONTENT_TYPE is folded in because PSGI stores it (and |
| 53 |
|
| * CONTENT_LENGTH) without the HTTP_ prefix, so a caller walking HTTP_* |
| 54 |
|
| * alone would silently miss it. |
| 55 |
|
| * |
| 56 |
|
| * content-length is deliberately left to the caller: Punk::Request takes it |
| 57 |
|
| * from the environment, while the OpenAPI validation path takes the length |
| 58 |
|
| * it actually read off psgi.input, which is the one the body was framed by. |
| 59 |
|
| * |
| 60 |
|
| * Owned (+1). */ |
| 61 |
64 |
| static HV *pq_headers(pTHX_ HV *env) { |
| 62 |
64 |
| HV *out = newHV(); |
| 63 |
|
| HE *he; |
| 64 |
|
| SV **e; |
| 65 |
|
| /* |
| 66 |
|
| # uncoverable branch true |
| 67 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 68 |
64 |
▶ | if (!env) return out; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!env) return out; |
0 |
64 |
64 |
|
| 69 |
|
| |
| 70 |
64 |
| hv_iterinit(env); |
| 71 |
560 |
▶ | while ((he = hv_iternext(env))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(env))) { |
496 |
64 |
560 |
|
| 72 |
|
| I32 kl; |
| 73 |
496 |
| const char *k = hv_iterkey(he, &kl); |
| 74 |
|
| char stackbuf[64], *lname; |
| 75 |
|
| STRLEN nl, i; |
| 76 |
496 |
▶ | if (!(kl > 5 && memEQ(k, "HTTP_", 5))) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(kl > 5 && memEQ(k, "HTTP_", 5))) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
496 |
0 |
496 |
| if (!(kl > 5 && memEQ(k, "HTTP_", 5))) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
448 |
48 |
496 |
|
| 77 |
48 |
| nl = (STRLEN)(kl - 5); |
| 78 |
48 |
▶ | lname = nl < sizeof stackbuf ? stackbuf : (char *)safemalloc(nl); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| lname = nl < sizeof stackbuf ? stackbuf : (char *)safemalloc(nl); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
48 |
0 |
48 |
|
| 79 |
373 |
▶ | for (i = 0; i < nl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nl; i++) { |
325 |
48 |
373 |
|
| 80 |
325 |
| char c = k[5 + i]; |
| 81 |
325 |
▶ | lname[i] = (c == '_') ? '-' : (char)toLOWER((U8)c); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| lname[i] = (c == '_') ? '-' : (char)toLOWER((U8)c); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
309 |
16 |
325 |
| lname[i] = (c == '_') ? '-' : (char)toLOWER((U8)c); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
309 |
0 |
309 |
|
| 82 |
|
| } |
| 83 |
48 |
| (void)hv_store(out, lname, (I32)nl, |
| 84 |
|
| newSVsv(hv_iterval(env, he)), 0); |
| 85 |
|
| /* |
| 86 |
|
| # uncoverable branch true |
| 87 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 88 |
48 |
▶ | if (lname != stackbuf) safefree(lname); |
Branch: 1 branch
| Branch | True | False | Total |
| if (lname != stackbuf) safefree(lname); |
0 |
48 |
48 |
|
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
64 |
| e = hv_fetchs(env, "CONTENT_TYPE", 0); |
| 92 |
|
| /* |
| 93 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 94 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 95 |
64 |
▶ | if (e && *e && SvOK(*e)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) { |
45 |
19 |
64 |
| if (e && *e && SvOK(*e)) { |
45 |
0 |
45 |
| if (e && *e && SvOK(*e)) { |
45 |
0 |
45 |
|
| 96 |
|
| STRLEN vl; |
| 97 |
45 |
| (void)SvPV_const(*e, vl); /* not SvCUR: it may not be a PV yet */ |
| 98 |
45 |
▶ | if (vl) (void)hv_stores(out, "content-type", newSVsv(*e)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (vl) (void)hv_stores(out, "content-type", newSVsv(*e)); |
14 |
31 |
45 |
|
| 99 |
|
| } |
| 100 |
64 |
| return out; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| /* %XX / '+' decode of [s, s+len) into a fresh SV (owned). */ |
| 104 |
2407 |
| static SV *pq_decode(pTHX_ const char *s, STRLEN len) { |
| 105 |
2407 |
| SV *out = newSV(len + 1); |
| 106 |
|
| char *d; |
| 107 |
2407 |
| STRLEN i, o = 0; |
| 108 |
2407 |
| SvPOK_on(out); |
| 109 |
2407 |
| d = SvPVX(out); |
| 110 |
39622 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
37215 |
2407 |
39622 |
|
| 111 |
37215 |
| char c = s[i]; |
| 112 |
37215 |
▶ | if (c == '+') d[o++] = ' '; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '+') d[o++] = ' '; |
1 |
37214 |
37215 |
|
| 113 |
|
| /* |
| 114 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 115 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 116 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 117 |
37214 |
▶ | else if (c == '%' && i + 2 < len |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '%' && i + 2 < len |
249 |
36965 |
37214 |
| else if (c == '%' && i + 2 < len |
249 |
0 |
249 |
|
| 118 |
249 |
▶ | && isXDIGIT((U8)s[i + 1]) && isXDIGIT((U8)s[i + 2])) { |
Branch: 2 branches
| Branch | True | False | Total |
| && isXDIGIT((U8)s[i + 1]) && isXDIGIT((U8)s[i + 2])) { |
249 |
0 |
249 |
| && isXDIGIT((U8)s[i + 1]) && isXDIGIT((U8)s[i + 2])) { |
249 |
0 |
249 |
|
| 119 |
745 |
| unsigned hi = isDIGIT((U8)s[i+1]) ? (unsigned)(s[i+1] - '0') |
| 120 |
249 |
▶ | : (unsigned)((s[i+1] | 0x20) - 'a' + 10); |
Branch: 1 branch
| Branch | True | False | Total |
| : (unsigned)((s[i+1] | 0x20) - 'a' + 10); |
247 |
2 |
249 |
|
| 121 |
629 |
| unsigned lo = isDIGIT((U8)s[i+2]) ? (unsigned)(s[i+2] - '0') |
| 122 |
249 |
▶ | : (unsigned)((s[i+2] | 0x20) - 'a' + 10); |
Branch: 1 branch
| Branch | True | False | Total |
| : (unsigned)((s[i+2] | 0x20) - 'a' + 10); |
131 |
118 |
249 |
|
| 123 |
249 |
| d[o++] = (char)((hi << 4) | lo); |
| 124 |
249 |
| i += 2; |
| 125 |
|
| } |
| 126 |
36965 |
| else d[o++] = c; |
| 127 |
|
| } |
| 128 |
2407 |
| d[o] = '\0'; |
| 129 |
2407 |
| SvCUR_set(out, o); |
| 130 |
2407 |
| return out; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| /* One decoded pair into the hash, promoting repeats to an arrayref. */ |
| 134 |
1065 |
| static void pq_store_pair(pTHX_ HV *out, |
| 135 |
|
| const char *k, STRLEN kl, |
| 136 |
|
| const char *v, STRLEN vl) { |
| 137 |
1065 |
| SV *key = sv_2mortal(pq_decode(aTHX_ k, kl)); |
| 138 |
1065 |
| SV *val = pq_decode(aTHX_ v, vl); |
| 139 |
1065 |
| HE *he = hv_fetch_ent(out, key, 0, 0); |
| 140 |
1065 |
▶ | if (he) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (he) { |
3 |
1062 |
1065 |
|
| 141 |
3 |
| SV *have = HeVAL(he); |
| 142 |
|
| AV *list; |
| 143 |
|
| /* |
| 144 |
|
| # uncoverable branch true |
| 145 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 146 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 147 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 148 |
3 |
▶ | if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) |
0 |
3 |
3 |
| if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) |
0 |
0 |
0 |
|
| 149 |
0 |
| list = (AV *)SvRV(have); /* # uncoverable statement */ |
| 150 |
|
| else { |
| 151 |
3 |
| list = newAV(); |
| 152 |
3 |
| av_push(list, newSVsv(have)); |
| 153 |
3 |
| (void)hv_store_ent(out, key, newRV_noinc((SV *)list), 0); |
| 154 |
|
| } |
| 155 |
3 |
| av_push(list, val); |
| 156 |
|
| } |
| 157 |
1062 |
| else (void)hv_store_ent(out, key, val, 0); |
| 158 |
1065 |
| } |
| 159 |
|
| |
| 160 |
|
| /* application/x-www-form-urlencoded pairs -> owned HV. */ |
| 161 |
3275 |
| static HV *pq_parse_pairs(pTHX_ const char *s, STRLEN len) { |
| 162 |
3275 |
| HV *out = newHV(); |
| 163 |
3275 |
| STRLEN start = 0, i; |
| 164 |
14834 |
▶ | for (i = 0; i <= len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= len; i++) { |
11559 |
3275 |
14834 |
|
| 165 |
11559 |
▶ | if (i == len || s[i] == '&' || s[i] == ';') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (i == len || s[i] == '&' || s[i] == ';') { |
8284 |
3275 |
11559 |
| if (i == len || s[i] == '&' || s[i] == ';') { |
8219 |
65 |
8284 |
| if (i == len || s[i] == '&' || s[i] == ';') { |
1 |
8218 |
8219 |
|
| 166 |
3341 |
▶ | if (i > start) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > start) { |
1065 |
2276 |
3341 |
|
| 167 |
1065 |
| const char *pair = s + start; |
| 168 |
1065 |
| STRLEN plen = i - start, j, eq = plen; |
| 169 |
2625 |
▶ | for (j = 0; j < plen; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < plen; j++) |
2624 |
1 |
2625 |
|
| 170 |
2624 |
▶ | if (pair[j] == '=') { eq = j; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (pair[j] == '=') { eq = j; break; } |
1064 |
1560 |
2624 |
|
| 171 |
3193 |
▶ | pq_store_pair(aTHX_ out, pair, eq, |
Branch: 2 branches
| Branch | True | False | Total |
| pq_store_pair(aTHX_ out, pair, eq, |
1064 |
1 |
1065 |
| pq_store_pair(aTHX_ out, pair, eq, |
1064 |
1 |
1065 |
|
| 172 |
1064 |
| eq < plen ? pair + eq + 1 : "", |
| 173 |
1064 |
| eq < plen ? plen - eq - 1 : 0); |
| 174 |
|
| } |
| 175 |
3341 |
| start = i + 1; |
| 176 |
|
| } |
| 177 |
|
| } |
| 178 |
3275 |
| return out; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
|
| /* The Cookie header -> owned HV; first value wins, values decoded. */ |
| 182 |
260 |
| static HV *pq_parse_cookies(pTHX_ const char *s, STRLEN len) { |
| 183 |
260 |
| HV *out = newHV(); |
| 184 |
260 |
| STRLEN start = 0, i; |
| 185 |
33460 |
▶ | for (i = 0; i <= len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= len; i++) { |
33200 |
260 |
33460 |
|
| 186 |
33200 |
▶ | if (i == len || s[i] == ';') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i == len || s[i] == ';') { |
32940 |
260 |
33200 |
| if (i == len || s[i] == ';') { |
18 |
32922 |
32940 |
|
| 187 |
|
| /* |
| 188 |
|
| # uncoverable branch false |
| 189 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 190 |
278 |
▶ | if (i > start) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > start) { |
278 |
0 |
278 |
|
| 191 |
278 |
| const char *pair = s + start; |
| 192 |
278 |
| STRLEN plen = i - start, j, eq = plen; |
| 193 |
|
| /* |
| 194 |
|
| # uncoverable branch false |
| 195 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 196 |
2357 |
▶ | for (j = 0; j < plen; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < plen; j++) |
2357 |
0 |
2357 |
|
| 197 |
2357 |
▶ | if (pair[j] == '=') { eq = j; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (pair[j] == '=') { eq = j; break; } |
278 |
2079 |
2357 |
|
| 198 |
|
| /* |
| 199 |
|
| # uncoverable branch false |
| 200 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 201 |
278 |
▶ | if (eq > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eq > 0) { |
278 |
0 |
278 |
|
| 202 |
278 |
| SV *key = sv_2mortal(newSVpvn(pair, eq)); |
| 203 |
278 |
▶ | if (!hv_exists_ent(out, key, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_exists_ent(out, key, 0)) { |
277 |
1 |
278 |
|
| 204 |
277 |
| SV *val = eq < plen |
| 205 |
277 |
| ? pq_decode(aTHX_ pair + eq + 1, plen - eq - 1) |
| 206 |
277 |
▶ | : newSVpvs(""); /* # uncoverable statement class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| : newSVpvs(""); /* # uncoverable statement class:ignore_covered_err */ |
277 |
0 |
277 |
|
| 207 |
277 |
| (void)hv_store_ent(out, key, val, 0); |
| 208 |
|
| } |
| 209 |
|
| } |
| 210 |
|
| } |
| 211 |
278 |
| start = i + 1; |
| 212 |
|
| /* Perl splits on semicolon-plus-whitespace: eat the spaces */ |
| 213 |
297 |
▶ | while (start < len && isSPACE((U8)s[start])) start++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (start < len && isSPACE((U8)s[start])) start++; |
37 |
260 |
297 |
| while (start < len && isSPACE((U8)s[start])) start++; |
19 |
18 |
37 |
|
| 214 |
278 |
| i = start - 1; |
| 215 |
|
| } |
| 216 |
|
| } |
| 217 |
260 |
| return out; |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
|
| /* Cached-slot access: return the slot's hashref, building it with |
| 221 |
|
| * `build` on first use. The returned SV is the live cached ref. */ |
| 222 |
5647 |
| static SV *pq_cached(pTHX_ AV *req, I32 slot, HV *built) { |
| 223 |
5647 |
| SV *rv = newRV_noinc((SV *)built); |
| 224 |
5647 |
| (void)av_store(req, slot, rv); |
| 225 |
5647 |
| return rv; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
|
| /* The query or form table, built through the accessor of that name on |
| 229 |
|
| * first use so the parse lands in the slot exactly once. Borrowed, or |
| 230 |
|
| * NULL when the accessor gave back something that is not a hashref. */ |
| 231 |
4908 |
| static HV *pq_table(pTHX_ SV *self, AV *req, I32 slot, const char *meth) { |
| 232 |
4908 |
| SV **c = av_fetch(req, slot, 0); |
| 233 |
|
| /* |
| 234 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 235 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 236 |
4908 |
▶ | if (!(c && *c && SvROK(*c))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(c && *c && SvROK(*c))) { |
252 |
4656 |
4908 |
| if (!(c && *c && SvROK(*c))) { |
252 |
0 |
252 |
| if (!(c && *c && SvROK(*c))) { |
0 |
252 |
252 |
|
| 237 |
4656 |
| dSP; |
| 238 |
4656 |
▶ | PUSHMARK(SP); XPUSHs(self); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); XPUSHs(self); PUTBACK; |
0 |
4656 |
4656 |
| PUSHMARK(SP); XPUSHs(self); PUTBACK; |
0 |
4656 |
4656 |
|
| 239 |
4656 |
| call_method(meth, G_SCALAR); |
| 240 |
4656 |
| SPAGAIN; (void)POPs; PUTBACK; |
| 241 |
4656 |
| c = av_fetch(req, slot, 0); |
| 242 |
|
| } |
| 243 |
4908 |
▶ | return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) |
4908 |
0 |
4908 |
| return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) |
4908 |
0 |
4908 |
| return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) |
4908 |
0 |
4908 |
|
| 244 |
9816 |
▶ | ? (HV *)SvRV(*c) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(*c) : NULL; |
4908 |
0 |
4908 |
|
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| /* One parameter: query first, then form body. Borrowed, NULL when the |
| 248 |
|
| * name is in neither - which the callers keep distinct from a present |
| 249 |
|
| * undef, so params(@keys) can leave an absent key out of its hash. */ |
| 250 |
2504 |
| static SV *pq_param_get(pTHX_ SV *self, AV *req, SV *name) { |
| 251 |
2504 |
| HV *t = pq_table(aTHX_ self, req, PQ_QUERY, "query"); |
| 252 |
2504 |
▶ | HE *he = t ? hv_fetch_ent(t, name, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| HE *he = t ? hv_fetch_ent(t, name, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2504 |
0 |
2504 |
|
| 253 |
2504 |
▶ | if (!he) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!he) { |
2372 |
132 |
2504 |
|
| 254 |
2372 |
| t = pq_table(aTHX_ self, req, PQ_FORM, "form"); |
| 255 |
2372 |
▶ | he = t ? hv_fetch_ent(t, name, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| he = t ? hv_fetch_ent(t, name, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2372 |
0 |
2372 |
|
| 256 |
|
| } |
| 257 |
2504 |
▶ | return he ? HeVAL(he) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return he ? HeVAL(he) : NULL; |
247 |
2257 |
2504 |
|
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
|
| /* Copy every pair of `src` over `out`, so a caller can stack tables in |
| 261 |
|
| * precedence order. NULL sources are skipped. */ |
| 262 |
66 |
| static void pq_overlay_hv(pTHX_ HV *out, HV *src) { |
| 263 |
|
| HE *he; |
| 264 |
|
| /* |
| 265 |
|
| # uncoverable branch true |
| 266 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 267 |
66 |
▶ | if (!src) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return; |
0 |
66 |
66 |
|
| 268 |
66 |
| hv_iterinit(src); |
| 269 |
112 |
▶ | while ((he = hv_iternext(src))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) |
46 |
66 |
112 |
|
| 270 |
46 |
▶ | (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
46 |
0 |
46 |
| (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
0 |
46 |
46 |
|
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
|
| /* The same, taking whatever an accessor returned: anything that is not a |
| 274 |
|
| * hashref overlays nothing. */ |
| 275 |
34 |
| static void pq_overlay(pTHX_ HV *out, SV *src) { |
| 276 |
|
| /* |
| 277 |
|
| # uncoverable branch false |
| 278 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 279 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 280 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 281 |
34 |
▶ | if (src && SvROK(src) && SvTYPE(SvRV(src)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (src && SvROK(src) && SvTYPE(SvRV(src)) == SVt_PVHV) |
34 |
0 |
34 |
| if (src && SvROK(src) && SvTYPE(SvRV(src)) == SVt_PVHV) |
34 |
0 |
34 |
| if (src && SvROK(src) && SvTYPE(SvRV(src)) == SVt_PVHV) |
34 |
0 |
34 |
|
| 282 |
34 |
| pq_overlay_hv(aTHX_ out, (HV *)SvRV(src)); |
| 283 |
34 |
| } |
| 284 |
|
| |
| 285 |
|
| /* What has become of the body: PQ_UNREAD, PQ_SLURPED (cached whole in |
| 286 |
|
| * PQ_BODY, the ->body semantics), or PQ_STREAMED (handed to a caller a chunk |
| 287 |
|
| * at a time and gone). The third state exists so that reading the body twice |
| 288 |
|
| * is an error somebody sees rather than an empty string somebody ships. */ |
| 289 |
|
| #define PQ_UNREAD 0 |
| 290 |
|
| #define PQ_SLURPED 1 |
| 291 |
|
| #define PQ_STREAMED 2 |
| 292 |
|
| |
| 293 |
920 |
| static IV pq_read_state(pTHX_ AV *req) { |
| 294 |
920 |
| SV **flag = av_fetch(req, PQ_READ, 0); |
| 295 |
|
| /* |
| 296 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 297 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 298 |
920 |
▶ | return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD; |
Branch: 3 branches
| Branch | True | False | Total |
| return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD; |
6 |
914 |
920 |
| return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD; |
6 |
0 |
6 |
| return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD; |
6 |
0 |
6 |
|
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
|
| /* The window a body is read in when its length is not known in advance. */ |
| 302 |
|
| #define PQ_STREAM_CHUNK 65536 |
| 303 |
|
| |
| 304 |
|
| /* Does the server hold the whole body already? |
| 305 |
|
| * |
| 306 |
|
| * This is the one signal that says how much of HTTP the environment is |
| 307 |
|
| * describing. Transfer-Encoding answers "is there a body" for HTTP/1 only: |
| 308 |
|
| * HTTP/2 and HTTP/3 forbid the header outright, so a multiplexed request with |
| 309 |
|
| * a body of undeclared length carries neither it nor CONTENT_LENGTH, and |
| 310 |
|
| * reading the two headers alone concludes there is no body when there is one. |
| 311 |
|
| * |
| 312 |
|
| * psgix.input.buffered is true when psgi.input is a finite thing already in |
| 313 |
|
| * memory (or spilled to a file) rather than a live socket, which is what makes |
| 314 |
|
| * reading to EOF safe - and it is set by every server that buffers, on every |
| 315 |
|
| * protocol version, which is why the decision keys off it first. */ |
| 316 |
34 |
| static int pq_input_buffered(pTHX_ HV *env) { |
| 317 |
34 |
▶ | SV **b = env ? hv_fetchs(env, "psgix.input.buffered", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **b = env ? hv_fetchs(env, "psgix.input.buffered", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
34 |
0 |
34 |
|
| 318 |
|
| /* |
| 319 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 320 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 321 |
34 |
▶ | return (b && *b && SvTRUE(*b)) ? 1 : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| return (b && *b && SvTRUE(*b)) ? 1 : 0; |
11 |
23 |
34 |
| return (b && *b && SvTRUE(*b)) ? 1 : 0; |
11 |
0 |
11 |
| return (b && *b && SvTRUE(*b)) ? 1 : 0; |
11 |
0 |
11 |
|
| 322 |
|
| } |
| 323 |
|
| |
| 324 |
|
| /* The route's `max_body` ceiling, as published into the environment by |
| 325 |
|
| * pd_body_limit. It is there for exactly the requests that check cannot make |
| 326 |
|
| * up front - a body whose length was never declared - and it becomes the |
| 327 |
|
| * default ceiling for every read below, so the limit bites on the bytes that |
| 328 |
|
| * actually arrive rather than on a header nobody sent. 0 when there is none. */ |
| 329 |
|
| #define PQ_ENV_MAX_BODY "punk.max_body" |
| 330 |
|
| |
| 331 |
2975 |
| static IV pq_env_ceiling(pTHX_ HV *env) { |
| 332 |
2975 |
▶ | SV **e = env ? hv_fetchs(env, PQ_ENV_MAX_BODY, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV **e = env ? hv_fetchs(env, PQ_ENV_MAX_BODY, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2975 |
0 |
2975 |
|
| 333 |
|
| /* |
| 334 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 335 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 336 |
2975 |
▶ | IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
2056 |
919 |
2975 |
| IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
2056 |
0 |
2056 |
| IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; |
2056 |
0 |
2056 |
|
| 337 |
2975 |
| return n > 0 ? n : 0; |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
|
| #define PQ_OVER_CEILING(max) \ |
| 341 |
|
| croak("Punk::Request: the request body passed %" IVdf " bytes, " \ |
| 342 |
|
| "the ceiling this read was given", (IV)(max)) |
| 343 |
|
| |
| 344 |
|
| /* The raw request body: read from psgi.input via PerlIO on first call (then |
| 345 |
|
| * rewind, matching the Perl semantics), cache in the BODY slot. Returns the |
| 346 |
|
| * cached SV (borrowed) - undef SV when there is no body. sv_2io croaks on a |
| 347 |
|
| * non-handle the same way the read builtin would. |
| 348 |
|
| * |
| 349 |
|
| * How much to read is the same question pq_body_stream answers below, and it |
| 350 |
|
| * is answered the same way: CONTENT_LENGTH when there is one, otherwise to |
| 351 |
|
| * EOF if the server says its input is buffered, otherwise nothing at all |
| 352 |
|
| * because the handle is a live socket and there is no length to stop at. |
| 353 |
|
| * pq_env_ceiling bounds the undeclared case. */ |
| 354 |
901 |
| static SV *pq_body(pTHX_ AV *req) { |
| 355 |
901 |
| SV **flag = av_fetch(req, PQ_READ, 0); |
| 356 |
|
| SV **b; |
| 357 |
901 |
▶ | if (pq_read_state(aTHX_ req) == PQ_STREAMED) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pq_read_state(aTHX_ req) == PQ_STREAMED) |
3 |
898 |
901 |
|
| 358 |
3 |
| croak("Punk::Request: the body was streamed and is gone - a body " |
| 359 |
|
| "can be read once, either whole (body/json/form) or in chunks " |
| 360 |
|
| "(body_each/body_to)"); |
| 361 |
|
| /* |
| 362 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 363 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 364 |
898 |
▶ | if (!(flag && *flag && SvTRUE(*flag))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(flag && *flag && SvTRUE(*flag))) { |
1 |
897 |
898 |
| if (!(flag && *flag && SvTRUE(*flag))) { |
1 |
0 |
1 |
| if (!(flag && *flag && SvTRUE(*flag))) { |
0 |
1 |
1 |
|
| 365 |
897 |
| HV *env = punk_req_env(aTHX_ req); |
| 366 |
897 |
| SV **in = hv_fetchs(env, "psgi.input", 0); |
| 367 |
897 |
| SV **cl = hv_fetchs(env, "CONTENT_LENGTH", 0); |
| 368 |
|
| /* |
| 369 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 370 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 371 |
897 |
▶ | IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; |
Branch: 3 branches
| Branch | True | False | Total |
| IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; |
890 |
7 |
897 |
| IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; |
890 |
0 |
890 |
| IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; |
890 |
0 |
890 |
|
| 372 |
897 |
| IV max = pq_env_ceiling(aTHX_ env); |
| 373 |
897 |
▶ | if (len < 0 && !pq_input_buffered(aTHX_ env)) len = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len < 0 && !pq_input_buffered(aTHX_ env)) len = 0; |
8 |
889 |
897 |
| if (len < 0 && !pq_input_buffered(aTHX_ env)) len = 0; |
2 |
6 |
8 |
|
| 374 |
897 |
| (void)av_store(req, PQ_READ, newSViv(1)); |
| 375 |
|
| /* |
| 376 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 377 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 378 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 379 |
1775 |
▶ | if (in && *in && SvTRUE(*in) && len != 0) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (in && *in && SvTRUE(*in) && len != 0) { |
897 |
0 |
897 |
| if (in && *in && SvTRUE(*in) && len != 0) { |
897 |
0 |
897 |
| if (in && *in && SvTRUE(*in) && len != 0) { |
897 |
0 |
897 |
| if (in && *in && SvTRUE(*in) && len != 0) { |
879 |
18 |
897 |
|
| 380 |
879 |
| IO *io = sv_2io(*in); |
| 381 |
879 |
▶ | PerlIO *fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| PerlIO *fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
879 |
0 |
879 |
|
| 382 |
|
| /* |
| 383 |
|
| # uncoverable branch false |
| 384 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 385 |
879 |
▶ | if (fp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fp) { |
879 |
0 |
879 |
|
| 386 |
|
| /* into the slot before the first read: the ceiling croaks |
| 387 |
|
| * from inside the loop, and the request must still own what |
| 388 |
|
| * has been read by then rather than leak it */ |
| 389 |
|
| /* Sized for what arrives, not for what was declared. The |
| 390 |
|
| * length is a header, and reserving it before a byte has |
| 391 |
|
| * been read lets one request line ask for any amount of |
| 392 |
|
| * memory it likes. */ |
| 393 |
879 |
▶ | SV *raw = newSV((len > 0 && len < PQ_STREAM_CHUNK |
Branch: 2 branches
| Branch | True | False | Total |
| SV *raw = newSV((len > 0 && len < PQ_STREAM_CHUNK |
873 |
6 |
879 |
| SV *raw = newSV((len > 0 && len < PQ_STREAM_CHUNK |
865 |
8 |
873 |
|
| 394 |
|
| ? (STRLEN)len : PQ_STREAM_CHUNK) + 1); |
| 395 |
879 |
| IV got = 0; |
| 396 |
879 |
| SvPOK_on(raw); |
| 397 |
879 |
| SvCUR_set(raw, 0); |
| 398 |
879 |
| *SvPVX(raw) = '\0'; |
| 399 |
879 |
| (void)av_store(req, PQ_BODY, raw); |
| 400 |
914 |
| for (;;) { |
| 401 |
1793 |
| STRLEN want = (STRLEN)PQ_STREAM_CHUNK; |
| 402 |
|
| SSize_t n; |
| 403 |
|
| char *d; |
| 404 |
1793 |
▶ | if (len >= 0 && len - got < (IV)want) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 0 && len - got < (IV)want) |
1778 |
15 |
1793 |
| if (len >= 0 && len - got < (IV)want) |
1741 |
37 |
1778 |
|
| 405 |
1741 |
| want = (STRLEN)(len - got); |
| 406 |
1793 |
▶ | if (!want) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!want) break; |
871 |
922 |
1793 |
|
| 407 |
922 |
▶ | if ((STRLEN)got + want + 1 > SvLEN(raw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STRLEN)got + want + 1 > SvLEN(raw)) { |
20 |
902 |
922 |
|
| 408 |
|
| /* doubling, so a large body is not copied once per |
| 409 |
|
| * window, and never past the declared length */ |
| 410 |
20 |
| STRLEN need = (STRLEN)got + want + 1; |
| 411 |
20 |
| STRLEN grow = SvLEN(raw) * 2; |
| 412 |
20 |
▶ | if (len >= 0 && grow > (STRLEN)len + 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 0 && grow > (STRLEN)len + 1) |
13 |
7 |
20 |
| if (len >= 0 && grow > (STRLEN)len + 1) |
4 |
9 |
13 |
|
| 413 |
4 |
| grow = (STRLEN)len + 1; |
| 414 |
|
| /* |
| 415 |
|
| # uncoverable branch true |
| 416 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 417 |
20 |
▶ | if (grow < need) grow = need; |
Branch: 1 branch
| Branch | True | False | Total |
| if (grow < need) grow = need; |
0 |
20 |
20 |
|
| 418 |
20 |
▶ | (void)SvGROW(raw, grow); |
Branch: 2 branches
| Branch | True | False | Total |
| (void)SvGROW(raw, grow); |
20 |
0 |
20 |
| (void)SvGROW(raw, grow); |
20 |
0 |
20 |
|
| 419 |
|
| } |
| 420 |
922 |
| d = SvPVX(raw); |
| 421 |
922 |
| n = PerlIO_read(fp, d + got, want); |
| 422 |
922 |
▶ | if (n <= 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) break; |
7 |
915 |
922 |
|
| 423 |
915 |
| got += (IV)n; |
| 424 |
915 |
| SvCUR_set(raw, (STRLEN)got); |
| 425 |
915 |
| SvPVX(raw)[got] = '\0'; |
| 426 |
915 |
▶ | if (max && got > max) PQ_OVER_CEILING(max); |
Branch: 2 branches
| Branch | True | False | Total |
| if (max && got > max) PQ_OVER_CEILING(max); |
28 |
887 |
915 |
| if (max && got > max) PQ_OVER_CEILING(max); |
1 |
27 |
28 |
|
| 427 |
|
| } |
| 428 |
878 |
| (void)PerlIO_seek(fp, 0, SEEK_SET); |
| 429 |
|
| } |
| 430 |
0 |
| else (void)av_store(req, PQ_BODY, newSV(0)); /* # uncoverable statement */ |
| 431 |
|
| } |
| 432 |
18 |
| else (void)av_store(req, PQ_BODY, newSV(0)); |
| 433 |
|
| } |
| 434 |
897 |
| b = av_fetch(req, PQ_BODY, 0); |
| 435 |
|
| /* |
| 436 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 437 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 438 |
897 |
▶ | return b && *b ? *b : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| return b && *b ? *b : &PL_sv_undef; |
897 |
0 |
897 |
| return b && *b ? *b : &PL_sv_undef; |
897 |
0 |
897 |
|
| 439 |
|
| } |
| 440 |
|
| |
| 441 |
|
| /* ---- the body, a chunk at a time ------------------------------------------ |
| 442 |
|
| * |
| 443 |
|
| * ->body copies the whole request into one scalar. That is right for JSON and |
| 444 |
|
| * wrong for anything large: the server is already holding the bytes, and the |
| 445 |
|
| * copy doubles them for as long as the handler runs. The multipart parser has |
| 446 |
|
| * read the handle in chunks since uploads landed; this is the same window for |
| 447 |
|
| * a body that is not multipart - an import, an octet-stream PUT, an NDJSON |
| 448 |
|
| * feed - and the seam a server that drips the body in would arrive through. |
| 449 |
|
| * |
| 450 |
|
| * The sink returns 0 to carry on and non-zero to stop (which only the byte |
| 451 |
|
| * ceiling does). |
| 452 |
|
| */ |
| 453 |
|
| typedef int (*pq_sink_fn)(pTHX_ void *ud, const char *buf, STRLEN len); |
| 454 |
|
| |
| 455 |
|
| /* Feed a cached body through the sink: what happens when the body was already |
| 456 |
|
| * read whole. The bytes are here, so serving them costs nothing and the |
| 457 |
|
| * caller does not have to care which happened. */ |
| 458 |
1 |
| static IV pq_stream_cached(pTHX_ SV *body, STRLEN chunk, pq_sink_fn sink, |
| 459 |
|
| void *ud) { |
| 460 |
1 |
| STRLEN len, off = 0; |
| 461 |
|
| const char *p; |
| 462 |
|
| /* |
| 463 |
|
| # uncoverable branch true |
| 464 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 465 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 466 |
1 |
▶ | if (!body || !SvOK(body)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!body || !SvOK(body)) return 0; |
1 |
0 |
1 |
| if (!body || !SvOK(body)) return 0; |
0 |
1 |
1 |
|
| 467 |
1 |
| p = SvPV_const(body, len); |
| 468 |
353 |
▶ | while (off < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (off < len) { |
352 |
1 |
353 |
|
| 469 |
352 |
| STRLEN n = len - off; |
| 470 |
352 |
▶ | if (n > chunk) n = chunk; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > chunk) n = chunk; |
351 |
1 |
352 |
|
| 471 |
|
| /* |
| 472 |
|
| # uncoverable branch true |
| 473 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 474 |
352 |
▶ | if (sink(aTHX_ ud, p + off, n)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sink(aTHX_ ud, p + off, n)) break; |
0 |
352 |
352 |
|
| 475 |
352 |
| off += n; |
| 476 |
|
| } |
| 477 |
1 |
| return (IV)off; |
| 478 |
|
| } |
| 479 |
|
| |
| 480 |
|
| /* Read the body off psgi.input in `chunk` byte windows, handing each to the |
| 481 |
|
| * sink. Returns the number of bytes read. |
| 482 |
|
| * |
| 483 |
|
| * With a CONTENT_LENGTH exactly that many bytes are read, which is what PSGI |
| 484 |
|
| * requires of an application. |
| 485 |
|
| * |
| 486 |
|
| * Without one, what happens turns on psgix.input.buffered, because that is |
| 487 |
|
| * what decides whether reading to EOF is safe. A buffered input is a finite |
| 488 |
|
| * thing the server is already holding, so EOF arrives; the body is read to it |
| 489 |
|
| * and a request that had none costs an empty read. That is the multiplexed |
| 490 |
|
| * case: HTTP/2 and HTTP/3 forbid Transfer-Encoding, so an undeclared-length |
| 491 |
|
| * body over either arrives with no framing header of any kind, and a rule |
| 492 |
|
| * written around Transfer-Encoding drops it. |
| 493 |
|
| * |
| 494 |
|
| * On an unbuffered input - a live socket - reading to EOF is how an |
| 495 |
|
| * application hangs, so Transfer-Encoding decides instead, and it can, because |
| 496 |
|
| * an unbuffered input is HTTP/1 by construction. Neither header means no body, |
| 497 |
|
| * and this reads nothing: it does NOT go looking, which would turn an ordinary |
| 498 |
|
| * bodyless POST into an error. Chunked means a body of unknown length that |
| 499 |
|
| * cannot be read here, and refusing with a reason beats hanging. |
| 500 |
|
| * |
| 501 |
|
| * `max` stops the read and croaks; 0 falls back to the route's max_body |
| 502 |
|
| * (pq_env_ceiling), which is the only ceiling there is once no length was |
| 503 |
|
| * declared - exactly the case pd_body_limit could not check up front. |
| 504 |
|
| */ |
| 505 |
19 |
| static IV pq_body_stream(pTHX_ AV *req, STRLEN chunk, IV max, |
| 506 |
|
| pq_sink_fn sink, void *ud) { |
| 507 |
|
| HV *env; |
| 508 |
|
| SV **in, **cl; |
| 509 |
19 |
| IV len, got = 0; |
| 510 |
|
| IO *io; |
| 511 |
|
| PerlIO *fp; |
| 512 |
|
| SV *window; |
| 513 |
|
| char *w; |
| 514 |
|
| |
| 515 |
19 |
▶ | if (!chunk) chunk = PQ_STREAM_CHUNK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!chunk) chunk = PQ_STREAM_CHUNK; |
15 |
4 |
19 |
|
| 516 |
|
| |
| 517 |
19 |
| switch (pq_read_state(aTHX_ req)) { |
| 518 |
1 |
| case PQ_STREAMED: |
| 519 |
1 |
| croak("Punk::Request: the body was streamed and is gone - a body " |
| 520 |
|
| "can be read once, either whole (body/json/form) or in chunks " |
| 521 |
|
| "(body_each/body_to)"); |
| 522 |
1 |
| case PQ_SLURPED: { |
| 523 |
1 |
| SV **b = av_fetch(req, PQ_BODY, 0); |
| 524 |
|
| /* |
| 525 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 526 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 527 |
1 |
▶ | return pq_stream_cached(aTHX_ (b && *b) ? *b : NULL, chunk, sink, ud); |
Branch: 2 branches
| Branch | True | False | Total |
| return pq_stream_cached(aTHX_ (b && *b) ? *b : NULL, chunk, sink, ud); |
1 |
0 |
1 |
| return pq_stream_cached(aTHX_ (b && *b) ? *b : NULL, chunk, sink, ud); |
1 |
0 |
1 |
|
| 528 |
|
| } |
| 529 |
17 |
| default: break; |
| 530 |
|
| } |
| 531 |
|
| |
| 532 |
17 |
| env = punk_req_env(aTHX_ req); |
| 533 |
17 |
| in = hv_fetchs(env, "psgi.input", 0); |
| 534 |
17 |
| cl = hv_fetchs(env, "CONTENT_LENGTH", 0); |
| 535 |
17 |
▶ | len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
13 |
4 |
17 |
| len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
13 |
0 |
13 |
| len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
10 |
3 |
13 |
|
| 536 |
17 |
▶ | if (!max) max = pq_env_ceiling(aTHX_ env); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!max) max = pq_env_ceiling(aTHX_ env); |
16 |
1 |
17 |
|
| 537 |
|
| |
| 538 |
17 |
▶ | if (len < 0 && !pq_input_buffered(aTHX_ env)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len < 0 && !pq_input_buffered(aTHX_ env)) { |
7 |
10 |
17 |
| if (len < 0 && !pq_input_buffered(aTHX_ env)) { |
4 |
3 |
7 |
|
| 539 |
4 |
| SV **te = hv_fetchs(env, "HTTP_TRANSFER_ENCODING", 0); |
| 540 |
|
| /* |
| 541 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 542 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 543 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 544 |
4 |
▶ | if (!(te && *te && SvOK(*te) && SvCUR(*te))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(te && *te && SvOK(*te) && SvCUR(*te))) { |
2 |
2 |
4 |
| if (!(te && *te && SvOK(*te) && SvCUR(*te))) { |
2 |
0 |
2 |
| if (!(te && *te && SvOK(*te) && SvCUR(*te))) { |
2 |
0 |
2 |
| if (!(te && *te && SvOK(*te) && SvCUR(*te))) { |
0 |
2 |
2 |
|
| 545 |
|
| /* no length and no transfer coding: the request has no body */ |
| 546 |
2 |
| (void)av_store(req, PQ_READ, newSViv(PQ_STREAMED)); |
| 547 |
2 |
| return 0; |
| 548 |
|
| } |
| 549 |
2 |
| croak("Punk::Request: a chunked body with no CONTENT_LENGTH, and " |
| 550 |
|
| "this server does not declare psgix.input.buffered - there " |
| 551 |
|
| "is no length to read to and reading to EOF on a live " |
| 552 |
|
| "socket would hang"); |
| 553 |
|
| } |
| 554 |
|
| |
| 555 |
|
| /* the state changes before the first read: a sink that dies halfway |
| 556 |
|
| * through has still consumed the input, and the next reader must be told |
| 557 |
|
| * so rather than handed the remainder */ |
| 558 |
13 |
| (void)av_store(req, PQ_READ, newSViv(PQ_STREAMED)); |
| 559 |
|
| |
| 560 |
|
| /* |
| 561 |
|
| # uncoverable branch true |
| 562 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 563 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 564 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 565 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 566 |
13 |
▶ | if (!(in && *in && SvTRUE(*in)) || len == 0) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(in && *in && SvTRUE(*in)) || len == 0) return 0; |
13 |
0 |
13 |
| if (!(in && *in && SvTRUE(*in)) || len == 0) return 0; |
13 |
0 |
13 |
| if (!(in && *in && SvTRUE(*in)) || len == 0) return 0; |
13 |
0 |
13 |
| if (!(in && *in && SvTRUE(*in)) || len == 0) return 0; |
0 |
13 |
13 |
|
| 567 |
13 |
| io = sv_2io(*in); |
| 568 |
13 |
▶ | fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
13 |
0 |
13 |
|
| 569 |
|
| /* |
| 570 |
|
| # uncoverable branch true |
| 571 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 572 |
13 |
▶ | if (!fp) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fp) return 0; |
0 |
13 |
13 |
|
| 573 |
|
| |
| 574 |
13 |
| window = sv_2mortal(newSV(chunk + 1)); |
| 575 |
13 |
| SvPOK_on(window); |
| 576 |
13 |
| w = SvPVX(window); |
| 577 |
|
| |
| 578 |
168 |
▶ | while (len < 0 || got < len) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (len < 0 || got < len) { |
5 |
163 |
168 |
| while (len < 0 || got < len) { |
154 |
9 |
163 |
|
| 579 |
159 |
| STRLEN want = chunk; |
| 580 |
|
| SSize_t n; |
| 581 |
159 |
▶ | if (len >= 0 && (IV)want > len - got) want = (STRLEN)(len - got); |
Branch: 2 branches
| Branch | True | False | Total |
| if (len >= 0 && (IV)want > len - got) want = (STRLEN)(len - got); |
154 |
5 |
159 |
| if (len >= 0 && (IV)want > len - got) want = (STRLEN)(len - got); |
9 |
145 |
154 |
|
| 582 |
159 |
| n = PerlIO_read(fp, w, want); |
| 583 |
159 |
▶ | if (n <= 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) break; |
2 |
157 |
159 |
|
| 584 |
157 |
| got += (IV)n; |
| 585 |
157 |
▶ | if (max && got > max) PQ_OVER_CEILING(max); |
Branch: 2 branches
| Branch | True | False | Total |
| if (max && got > max) PQ_OVER_CEILING(max); |
7 |
150 |
157 |
| if (max && got > max) PQ_OVER_CEILING(max); |
2 |
5 |
7 |
|
| 586 |
|
| /* |
| 587 |
|
| # uncoverable branch true |
| 588 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 589 |
155 |
▶ | if (sink(aTHX_ ud, w, (STRLEN)n)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sink(aTHX_ ud, w, (STRLEN)n)) break; |
0 |
155 |
155 |
|
| 590 |
|
| } |
| 591 |
11 |
| return got; |
| 592 |
|
| } |
| 593 |
|
| |
| 594 |
|
| /* the two sinks: a Perl callback, and a handle */ |
| 595 |
|
| |
| 596 |
|
| typedef struct { SV *cb; SV *self; } pq_sink_cb_ud; |
| 597 |
|
| |
| 598 |
457 |
| static int pq_sink_cb(pTHX_ void *ud, const char *buf, STRLEN len) { |
| 599 |
457 |
| pq_sink_cb_ud *s = (pq_sink_cb_ud *)ud; |
| 600 |
457 |
| dSP; |
| 601 |
457 |
| ENTER; SAVETMPS; |
| 602 |
457 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
457 |
457 |
|
| 603 |
457 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
457 |
457 |
|
| 604 |
|
| /* a fresh scalar per chunk: a callback that keeps one must be able to, |
| 605 |
|
| * which a reused buffer would quietly break */ |
| 606 |
457 |
| PUSHs(sv_2mortal(newSVpvn(buf, len))); |
| 607 |
457 |
| PUSHs(s->self); |
| 608 |
457 |
| PUTBACK; |
| 609 |
457 |
| (void)call_sv(s->cb, G_VOID | G_DISCARD); |
| 610 |
457 |
| SPAGAIN; |
| 611 |
457 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
457 |
0 |
457 |
|
| 612 |
457 |
| return 0; |
| 613 |
|
| } |
| 614 |
|
| |
| 615 |
|
| /* body_to's handle, owned by the savestack for as long as the read runs */ |
| 616 |
|
| typedef struct { PerlIO *fp; int open; } pq_out; |
| 617 |
|
| |
| 618 |
2 |
| static void pq_out_cleanup(pTHX_ void *p) { |
| 619 |
2 |
| pq_out *o = (pq_out *)p; |
| 620 |
|
| /* |
| 621 |
|
| # uncoverable branch true |
| 622 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 623 |
2 |
▶ | if (o->open) { (void)PerlIO_close(o->fp); o->open = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->open) { (void)PerlIO_close(o->fp); o->open = 0; } |
0 |
2 |
2 |
|
| 624 |
2 |
| } |
| 625 |
|
| |
| 626 |
50 |
| static int pq_sink_io(pTHX_ void *ud, const char *buf, STRLEN len) { |
| 627 |
50 |
| PerlIO *out = (PerlIO *)ud; |
| 628 |
50 |
| STRLEN off = 0; |
| 629 |
100 |
▶ | while (off < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (off < len) { |
50 |
50 |
100 |
|
| 630 |
50 |
| SSize_t n = PerlIO_write(out, buf + off, len - off); |
| 631 |
|
| /* |
| 632 |
|
| # uncoverable branch true |
| 633 |
|
| # uncoverable branch true count:2 class:ignore_covered_err |
| 634 |
|
| # uncoverable branch false count:3 class:ignore_covered_err |
| 635 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 636 |
50 |
▶ | if (n <= 0) croak("Punk::Request: writing the body failed: %s", |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) croak("Punk::Request: writing the body failed: %s", |
0 |
50 |
50 |
|
| 637 |
|
| Strerror(errno)); /* # uncoverable statement */ |
| 638 |
50 |
| off += (STRLEN)n; |
| 639 |
|
| } |
| 640 |
50 |
| return 0; |
| 641 |
|
| } |
| 642 |
|
| |
| 643 |
|
| #endif /* PUNK_REQUEST_H */ |