include/punk/punk_request.h

filter: Statementstmt 98.4% 253 / 257 Branchbran 72.6% 218 / 300 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.5% 471 / 557 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_dbq.h ↑ Summary xs/metrics.xs »
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 10809 static AV *punk_req_av(pTHX_ SV *self) {
28 10809 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 10809 0 10809
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 0 10809 10809
29 0 croak("Punk::Request: not a request object");
30 10809 return (AV *)SvRV(self);
31 }
32  
33 8810 static HV *punk_req_env(pTHX_ AV *req) {
34 8810 SV **e = av_fetch(req, PQ_ENV, 0);
35 8810 ▶ if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) 8810 0 8810
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) 8810 0 8810
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) 8810 0 8810
if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) 0 8810 8810
36 0 croak("Punk::Request: no PSGI environment");
37 8810 return (HV *)SvRV(*e);
38 }
39  
40 /* Every request header, as a hash keyed the way HTTP/2 and most Perl web
41 * code spell them: HTTP_X_FORWARDED_FOR -> 'x-forwarded-for', lowercased
42 * with dashes. CONTENT_TYPE is folded in because PSGI stores it (and
43 * CONTENT_LENGTH) without the HTTP_ prefix, so a caller walking HTTP_*
44 * alone would silently miss it.
45 *
46 * content-length is deliberately left to the caller: Punk::Request takes it
47 * from the environment, while the OpenAPI validation path takes the length
48 * it actually read off psgi.input, which is the one the body was framed by.
49 *
50 * Owned (+1). */
51 64 static HV *pq_headers(pTHX_ HV *env) {
52 64 HV *out = newHV();
53 HE *he;
54 SV **e;
55 64 ▶ if (!env) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!env) return out; 0 64 64
56  
57 64 hv_iterinit(env);
58 560 ▶ while ((he = hv_iternext(env))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(env))) { 496 64 560
59 I32 kl;
60 496 const char *k = hv_iterkey(he, &kl);
61 char stackbuf[64], *lname;
62 STRLEN nl, i;
63 496 ▶ if (!(kl > 5 && memEQ(k, "HTTP_", 5))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(kl > 5 && memEQ(k, "HTTP_", 5))) continue; 496 0 496
if (!(kl > 5 && memEQ(k, "HTTP_", 5))) continue; 448 48 496
64 48 nl = (STRLEN)(kl - 5);
65 48 ▶ lname = nl < sizeof stackbuf ? stackbuf : (char *)safemalloc(nl);
Branch: 1 branch
BranchTrueFalseTotal
lname = nl < sizeof stackbuf ? stackbuf : (char *)safemalloc(nl); 48 0 48
66 373 ▶ for (i = 0; i < nl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nl; i++) { 325 48 373
67 325 char c = k[5 + i];
68 325 ▶ lname[i] = (c == '_') ? '-' : (char)toLOWER((U8)c);
Branch: 2 branches
BranchTrueFalseTotal
lname[i] = (c == '_') ? '-' : (char)toLOWER((U8)c); 309 16 325
lname[i] = (c == '_') ? '-' : (char)toLOWER((U8)c); 309 0 309
69 }
70 48 (void)hv_store(out, lname, (I32)nl,
71 newSVsv(hv_iterval(env, he)), 0);
72 48 ▶ if (lname != stackbuf) safefree(lname);
Branch: 1 branch
BranchTrueFalseTotal
if (lname != stackbuf) safefree(lname); 0 48 48
73 }
74  
75 64 e = hv_fetchs(env, "CONTENT_TYPE", 0);
76 64 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 45 19 64
if (e && *e && SvOK(*e)) { 45 0 45
if (e && *e && SvOK(*e)) { 45 0 45
77 STRLEN vl;
78 45 (void)SvPV_const(*e, vl); /* not SvCUR: it may not be a PV yet */
79 45 ▶ if (vl) (void)hv_stores(out, "content-type", newSVsv(*e));
Branch: 1 branch
BranchTrueFalseTotal
if (vl) (void)hv_stores(out, "content-type", newSVsv(*e)); 14 31 45
80 }
81 64 return out;
82 }
83  
84 /* %XX / '+' decode of [s, s+len) into a fresh SV (owned). */
85 2356 static SV *pq_decode(pTHX_ const char *s, STRLEN len) {
86 2356 SV *out = newSV(len + 1);
87 char *d;
88 2356 STRLEN i, o = 0;
89 2356 SvPOK_on(out);
90 2356 d = SvPVX(out);
91 37478 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 35122 2356 37478
92 35122 char c = s[i];
93 35122 ▶ if (c == '+') d[o++] = ' ';
Branch: 1 branch
BranchTrueFalseTotal
if (c == '+') d[o++] = ' '; 1 35121 35122
94 35121 ▶ else if (c == '%' && i + 2 < len
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '%' && i + 2 < len 245 34876 35121
else if (c == '%' && i + 2 < len 245 0 245
95 245 ▶ && isXDIGIT((U8)s[i + 1]) && isXDIGIT((U8)s[i + 2])) {
Branch: 2 branches
BranchTrueFalseTotal
&& isXDIGIT((U8)s[i + 1]) && isXDIGIT((U8)s[i + 2])) { 245 0 245
&& isXDIGIT((U8)s[i + 1]) && isXDIGIT((U8)s[i + 2])) { 245 0 245
96 733 unsigned hi = isDIGIT((U8)s[i+1]) ? (unsigned)(s[i+1] - '0')
97 245 ▶ : (unsigned)((s[i+1] | 0x20) - 'a' + 10);
Branch: 1 branch
BranchTrueFalseTotal
: (unsigned)((s[i+1] | 0x20) - 'a' + 10); 243 2 245
98 618 unsigned lo = isDIGIT((U8)s[i+2]) ? (unsigned)(s[i+2] - '0')
99 245 ▶ : (unsigned)((s[i+2] | 0x20) - 'a' + 10);
Branch: 1 branch
BranchTrueFalseTotal
: (unsigned)((s[i+2] | 0x20) - 'a' + 10); 128 117 245
100 245 d[o++] = (char)((hi << 4) | lo);
101 245 i += 2;
102 }
103 34876 else d[o++] = c;
104 }
105 2356 d[o] = '\0';
106 2356 SvCUR_set(out, o);
107 2356 return out;
108 }
109  
110 /* One decoded pair into the hash, promoting repeats to an arrayref. */
111 1048 static void pq_store_pair(pTHX_ HV *out,
112 const char *k, STRLEN kl,
113 const char *v, STRLEN vl) {
114 1048 SV *key = sv_2mortal(pq_decode(aTHX_ k, kl));
115 1048 SV *val = pq_decode(aTHX_ v, vl);
116 1048 HE *he = hv_fetch_ent(out, key, 0, 0);
117 1048 ▶ if (he) {
Branch: 1 branch
BranchTrueFalseTotal
if (he) { 3 1045 1048
118 3 SV *have = HeVAL(he);
119 AV *list;
120 3 ▶ if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) 0 3 3
if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) 0 0 0
121 0 list = (AV *)SvRV(have);
122 else {
123 3 list = newAV();
124 3 av_push(list, newSVsv(have));
125 3 (void)hv_store_ent(out, key, newRV_noinc((SV *)list), 0);
126 }
127 3 av_push(list, val);
128 }
129 1045 else (void)hv_store_ent(out, key, val, 0);
130 1048 }
131  
132 /* application/x-www-form-urlencoded pairs -> owned HV. */
133 3217 static HV *pq_parse_pairs(pTHX_ const char *s, STRLEN len) {
134 3217 HV *out = newHV();
135 3217 STRLEN start = 0, i;
136 14541 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 11324 3217 14541
137 11324 ▶ if (i == len || s[i] == '&' || s[i] == ';') {
Branch: 3 branches
BranchTrueFalseTotal
if (i == len || s[i] == '&' || s[i] == ';') { 8107 3217 11324
if (i == len || s[i] == '&' || s[i] == ';') { 8045 62 8107
if (i == len || s[i] == '&' || s[i] == ';') { 1 8044 8045
138 3280 ▶ if (i > start) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) { 1048 2232 3280
139 1048 const char *pair = s + start;
140 1048 STRLEN plen = i - start, j, eq = plen;
141 2552 ▶ for (j = 0; j < plen; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < plen; j++) 2551 1 2552
142 2551 ▶ if (pair[j] == '=') { eq = j; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (pair[j] == '=') { eq = j; break; } 1047 1504 2551
143 3142 ▶ pq_store_pair(aTHX_ out, pair, eq,
Branch: 2 branches
BranchTrueFalseTotal
pq_store_pair(aTHX_ out, pair, eq, 1047 1 1048
pq_store_pair(aTHX_ out, pair, eq, 1047 1 1048
144 1047 eq < plen ? pair + eq + 1 : "",
145 1047 eq < plen ? plen - eq - 1 : 0);
146 }
147 3280 start = i + 1;
148 }
149 }
150 3217 return out;
151 }
152  
153 /* The Cookie header -> owned HV; first value wins, values decoded. */
154 243 static HV *pq_parse_cookies(pTHX_ const char *s, STRLEN len) {
155 243 HV *out = newHV();
156 243 STRLEN start = 0, i;
157 31329 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 31086 243 31329
158 31086 ▶ if (i == len || s[i] == ';') {
Branch: 2 branches
BranchTrueFalseTotal
if (i == len || s[i] == ';') { 30843 243 31086
if (i == len || s[i] == ';') { 18 30825 30843
159 261 ▶ if (i > start) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) { 261 0 261
160 261 const char *pair = s + start;
161 261 STRLEN plen = i - start, j, eq = plen;
162 2204 ▶ for (j = 0; j < plen; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < plen; j++) 2204 0 2204
163 2204 ▶ if (pair[j] == '=') { eq = j; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (pair[j] == '=') { eq = j; break; } 261 1943 2204
164 261 ▶ if (eq > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (eq > 0) { 261 0 261
165 261 SV *key = sv_2mortal(newSVpvn(pair, eq));
166 261 ▶ if (!hv_exists_ent(out, key, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists_ent(out, key, 0)) { 260 1 261
167 260 SV *val = eq < plen
168 260 ? pq_decode(aTHX_ pair + eq + 1, plen - eq - 1)
169 260 ▶ : newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
: newSVpvs(""); 260 0 260
170 260 (void)hv_store_ent(out, key, val, 0);
171 }
172 }
173 }
174 261 start = i + 1;
175 /* Perl splits on semicolon-plus-whitespace: eat the spaces */
176 280 ▶ while (start < len && isSPACE((U8)s[start])) start++;
Branch: 2 branches
BranchTrueFalseTotal
while (start < len && isSPACE((U8)s[start])) start++; 37 243 280
while (start < len && isSPACE((U8)s[start])) start++; 19 18 37
177 261 i = start - 1;
178 }
179 }
180 243 return out;
181 }
182  
183 /* Cached-slot access: return the slot's hashref, building it with
184 * `build` on first use. The returned SV is the live cached ref. */
185 5538 static SV *pq_cached(pTHX_ AV *req, I32 slot, HV *built) {
186 5538 SV *rv = newRV_noinc((SV *)built);
187 5538 (void)av_store(req, slot, rv);
188 5538 return rv;
189 }
190  
191 /* The query or form table, built through the accessor of that name on
192 * first use so the parse lands in the slot exactly once. Borrowed, or
193 * NULL when the accessor gave back something that is not a hashref. */
194 4808 static HV *pq_table(pTHX_ SV *self, AV *req, I32 slot, const char *meth) {
195 4808 SV **c = av_fetch(req, slot, 0);
196 4808 ▶ if (!(c && *c && SvROK(*c))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(c && *c && SvROK(*c))) { 243 4565 4808
if (!(c && *c && SvROK(*c))) { 243 0 243
if (!(c && *c && SvROK(*c))) { 0 243 243
197 4565 dSP;
198 4565 ▶ PUSHMARK(SP); XPUSHs(self); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); XPUSHs(self); PUTBACK; 0 4565 4565
PUSHMARK(SP); XPUSHs(self); PUTBACK; 0 4565 4565
199 4565 call_method(meth, G_SCALAR);
200 4565 SPAGAIN; (void)POPs; PUTBACK;
201 4565 c = av_fetch(req, slot, 0);
202 }
203 4808 ▶ return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) 4808 0 4808
return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) 4808 0 4808
return (c && *c && SvROK(*c) && SvTYPE(SvRV(*c)) == SVt_PVHV) 4808 0 4808
204 9616 ▶ ? (HV *)SvRV(*c) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*c) : NULL; 4808 0 4808
205 }
206  
207 /* One parameter: query first, then form body. Borrowed, NULL when the
208 * name is in neither - which the callers keep distinct from a present
209 * undef, so params(@keys) can leave an absent key out of its hash. */
210 2451 static SV *pq_param_get(pTHX_ SV *self, AV *req, SV *name) {
211 2451 HV *t = pq_table(aTHX_ self, req, PQ_QUERY, "query");
212 2451 ▶ HE *he = t ? hv_fetch_ent(t, name, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = t ? hv_fetch_ent(t, name, 0, 0) : NULL; 2451 0 2451
213 2451 ▶ if (!he) {
Branch: 1 branch
BranchTrueFalseTotal
if (!he) { 2325 126 2451
214 2325 t = pq_table(aTHX_ self, req, PQ_FORM, "form");
215 2325 ▶ he = t ? hv_fetch_ent(t, name, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
he = t ? hv_fetch_ent(t, name, 0, 0) : NULL; 2325 0 2325
216 }
217 2451 ▶ return he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return he ? HeVAL(he) : NULL; 222 2229 2451
218 }
219  
220 /* Copy every pair of `src` over `out`, so a caller can stack tables in
221 * precedence order. NULL sources are skipped. */
222 66 static void pq_overlay_hv(pTHX_ HV *out, HV *src) {
223 HE *he;
224 66 ▶ if (!src) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return; 0 66 66
225 66 hv_iterinit(src);
226 112 ▶ while ((he = hv_iternext(src)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) 46 66 112
227 46 ▶ (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0);
Branch: 2 branches
BranchTrueFalseTotal
(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
228 }
229  
230 /* The same, taking whatever an accessor returned: anything that is not a
231 * hashref overlays nothing. */
232 34 static void pq_overlay(pTHX_ HV *out, SV *src) {
233 34 ▶ if (src && SvROK(src) && SvTYPE(SvRV(src)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
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
234 34 pq_overlay_hv(aTHX_ out, (HV *)SvRV(src));
235 34 }
236  
237 /* What has become of the body: PQ_UNREAD, PQ_SLURPED (cached whole in
238 * PQ_BODY, the ->body semantics), or PQ_STREAMED (handed to a caller a chunk
239 * at a time and gone). The third state exists so that reading the body twice
240 * is an error somebody sees rather than an empty string somebody ships. */
241 #define PQ_UNREAD 0
242 #define PQ_SLURPED 1
243 #define PQ_STREAMED 2
244  
245 900 static IV pq_read_state(pTHX_ AV *req) {
246 900 SV **flag = av_fetch(req, PQ_READ, 0);
247 900 ▶ return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD;
Branch: 3 branches
BranchTrueFalseTotal
return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD; 6 894 900
return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD; 6 0 6
return (flag && *flag && SvOK(*flag)) ? SvIV(*flag) : PQ_UNREAD; 6 0 6
248 }
249  
250 /* The window a body is read in when its length is not known in advance. */
251 #define PQ_STREAM_CHUNK 65536
252  
253 /* Does the server hold the whole body already?
254 *
255 * This is the one signal that says how much of HTTP the environment is
256 * describing. Transfer-Encoding answers "is there a body" for HTTP/1 only:
257 * HTTP/2 and HTTP/3 forbid the header outright, so a multiplexed request with
258 * a body of undeclared length carries neither it nor CONTENT_LENGTH, and
259 * reading the two headers alone concludes there is no body when there is one.
260 *
261 * psgix.input.buffered is true when psgi.input is a finite thing already in
262 * memory (or spilled to a file) rather than a live socket, which is what makes
263 * reading to EOF safe - and it is set by every server that buffers, on every
264 * protocol version, which is why the decision keys off it first. */
265 33 static int pq_input_buffered(pTHX_ HV *env) {
266 33 ▶ SV **b = env ? hv_fetchs(env, "psgix.input.buffered", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **b = env ? hv_fetchs(env, "psgix.input.buffered", 0) : NULL; 33 0 33
267 33 ▶ return (b && *b && SvTRUE(*b)) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
return (b && *b && SvTRUE(*b)) ? 1 : 0; 10 23 33
return (b && *b && SvTRUE(*b)) ? 1 : 0; 10 0 10
return (b && *b && SvTRUE(*b)) ? 1 : 0; 10 0 10
268 }
269  
270 /* The route's `max_body` ceiling, as published into the environment by
271 * pd_body_limit. It is there for exactly the requests that check cannot make
272 * up front - a body whose length was never declared - and it becomes the
273 * default ceiling for every read below, so the limit bites on the bytes that
274 * actually arrive rather than on a header nobody sent. 0 when there is none. */
275 #define PQ_ENV_MAX_BODY "punk.max_body"
276  
277 1429 static IV pq_env_ceiling(pTHX_ HV *env) {
278 1429 ▶ SV **e = env ? hv_fetchs(env, PQ_ENV_MAX_BODY, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = env ? hv_fetchs(env, PQ_ENV_MAX_BODY, 0) : NULL; 1429 0 1429
279 1429 ▶ IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0;
Branch: 3 branches
BranchTrueFalseTotal
IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; 535 894 1429
IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; 535 0 535
IV n = (e && *e && SvOK(*e)) ? SvIV(*e) : 0; 535 0 535
280 1429 return n > 0 ? n : 0;
281 }
282  
283 #define PQ_OVER_CEILING(max) \
284 croak("Punk::Request: the request body passed %" IVdf " bytes, " \
285 "the ceiling this read was given", (IV)(max))
286  
287 /* The raw request body: read from psgi.input via PerlIO on first call (then
288 * rewind, matching the Perl semantics), cache in the BODY slot. Returns the
289 * cached SV (borrowed) - undef SV when there is no body. sv_2io croaks on a
290 * non-handle the same way the read builtin would.
291 *
292 * How much to read is the same question pq_body_stream answers below, and it
293 * is answered the same way: CONTENT_LENGTH when there is one, otherwise to
294 * EOF if the server says its input is buffered, otherwise nothing at all
295 * because the handle is a live socket and there is no length to stop at.
296 * pq_env_ceiling bounds the undeclared case. */
297 881 static SV *pq_body(pTHX_ AV *req) {
298 881 SV **flag = av_fetch(req, PQ_READ, 0);
299 SV **b;
300 881 ▶ if (pq_read_state(aTHX_ req) == PQ_STREAMED)
Branch: 1 branch
BranchTrueFalseTotal
if (pq_read_state(aTHX_ req) == PQ_STREAMED) 3 878 881
301 3 croak("Punk::Request: the body was streamed and is gone - a body "
302 "can be read once, either whole (body/json/form) or in chunks "
303 "(body_each/body_to)");
304 878 ▶ if (!(flag && *flag && SvTRUE(*flag))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(flag && *flag && SvTRUE(*flag))) { 1 877 878
if (!(flag && *flag && SvTRUE(*flag))) { 1 0 1
if (!(flag && *flag && SvTRUE(*flag))) { 0 1 1
305 877 HV *env = punk_req_env(aTHX_ req);
306 877 SV **in = hv_fetchs(env, "psgi.input", 0);
307 877 SV **cl = hv_fetchs(env, "CONTENT_LENGTH", 0);
308 877 ▶ IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1;
Branch: 3 branches
BranchTrueFalseTotal
IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; 870 7 877
IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; 870 0 870
IV len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; 870 0 870
309 877 IV max = pq_env_ceiling(aTHX_ env);
310 877 ▶ if (len < 0 && !pq_input_buffered(aTHX_ env)) len = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (len < 0 && !pq_input_buffered(aTHX_ env)) len = 0; 7 870 877
if (len < 0 && !pq_input_buffered(aTHX_ env)) len = 0; 2 5 7
311 877 (void)av_store(req, PQ_READ, newSViv(1));
312 1735 ▶ if (in && *in && SvTRUE(*in) && len != 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (in && *in && SvTRUE(*in) && len != 0) { 877 0 877
if (in && *in && SvTRUE(*in) && len != 0) { 877 0 877
if (in && *in && SvTRUE(*in) && len != 0) { 877 0 877
if (in && *in && SvTRUE(*in) && len != 0) { 859 18 877
313 859 IO *io = sv_2io(*in);
314 859 ▶ PerlIO *fp = io ? IoIFP(io) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
PerlIO *fp = io ? IoIFP(io) : NULL; 859 0 859
315 859 ▶ if (fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (fp) { 859 0 859
316 /* into the slot before the first read: the ceiling croaks
317 * from inside the loop, and the request must still own what
318 * has been read by then rather than leak it */
319 /* Sized for what arrives, not for what was declared. The
320 * length is a header, and reserving it before a byte has
321 * been read lets one request line ask for any amount of
322 * memory it likes. */
323 859 ▶ SV *raw = newSV((len > 0 && len < PQ_STREAM_CHUNK
Branch: 2 branches
BranchTrueFalseTotal
SV *raw = newSV((len > 0 && len < PQ_STREAM_CHUNK 854 5 859
SV *raw = newSV((len > 0 && len < PQ_STREAM_CHUNK 848 6 854
324 ? (STRLEN)len : PQ_STREAM_CHUNK) + 1);
325 859 IV got = 0;
326 859 SvPOK_on(raw);
327 859 SvCUR_set(raw, 0);
328 859 *SvPVX(raw) = '\0';
329 859 (void)av_store(req, PQ_BODY, raw);
330 894 for (;;) {
331 1753 STRLEN want = (STRLEN)PQ_STREAM_CHUNK;
332 SSize_t n;
333 char *d;
334 1753 ▶ if (len >= 0 && len - got < (IV)want)
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 0 && len - got < (IV)want) 1740 13 1753
if (len >= 0 && len - got < (IV)want) 1707 33 1740
335 1707 want = (STRLEN)(len - got);
336 1753 ▶ if (!want) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!want) break; 854 899 1753
337 899 ▶ if ((STRLEN)got + want + 1 > SvLEN(raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)got + want + 1 > SvLEN(raw)) { 17 882 899
338 /* doubling, so a large body is not copied once per
339 * window, and never past the declared length */
340 17 STRLEN need = (STRLEN)got + want + 1;
341 17 STRLEN grow = SvLEN(raw) * 2;
342 17 ▶ if (len >= 0 && grow > (STRLEN)len + 1)
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 0 && grow > (STRLEN)len + 1) 11 6 17
if (len >= 0 && grow > (STRLEN)len + 1) 4 7 11
343 4 grow = (STRLEN)len + 1;
344 17 ▶ if (grow < need) grow = need;
Branch: 1 branch
BranchTrueFalseTotal
if (grow < need) grow = need; 0 17 17
345 17 ▶ (void)SvGROW(raw, grow);
Branch: 2 branches
BranchTrueFalseTotal
(void)SvGROW(raw, grow); 17 0 17
(void)SvGROW(raw, grow); 17 0 17
346 }
347 899 d = SvPVX(raw);
348 899 n = PerlIO_read(fp, d + got, want);
349 899 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 4 895 899
350 895 got += (IV)n;
351 895 SvCUR_set(raw, (STRLEN)got);
352 895 SvPVX(raw)[got] = '\0';
353 895 ▶ if (max && got > max) PQ_OVER_CEILING(max);
Branch: 2 branches
BranchTrueFalseTotal
if (max && got > max) PQ_OVER_CEILING(max); 25 870 895
if (max && got > max) PQ_OVER_CEILING(max); 1 24 25
354 }
355 858 (void)PerlIO_seek(fp, 0, SEEK_SET);
356 }
357 0 else (void)av_store(req, PQ_BODY, newSV(0));
358 }
359 18 else (void)av_store(req, PQ_BODY, newSV(0));
360 }
361 877 b = av_fetch(req, PQ_BODY, 0);
362 877 ▶ return b && *b ? *b : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
return b && *b ? *b : &PL_sv_undef; 877 0 877
return b && *b ? *b : &PL_sv_undef; 877 0 877
363 }
364  
365 /* ---- the body, a chunk at a time ------------------------------------------
366 *
367 * ->body copies the whole request into one scalar. That is right for JSON and
368 * wrong for anything large: the server is already holding the bytes, and the
369 * copy doubles them for as long as the handler runs. The multipart parser has
370 * read the handle in chunks since uploads landed; this is the same window for
371 * a body that is not multipart - an import, an octet-stream PUT, an NDJSON
372 * feed - and the seam a server that drips the body in would arrive through.
373 *
374 * The sink returns 0 to carry on and non-zero to stop (which only the byte
375 * ceiling does).
376 */
377 typedef int (*pq_sink_fn)(pTHX_ void *ud, const char *buf, STRLEN len);
378  
379 /* Feed a cached body through the sink: what happens when the body was already
380 * read whole. The bytes are here, so serving them costs nothing and the
381 * caller does not have to care which happened. */
382 1 static IV pq_stream_cached(pTHX_ SV *body, STRLEN chunk, pq_sink_fn sink,
383 void *ud) {
384 1 STRLEN len, off = 0;
385 const char *p;
386 1 ▶ if (!body || !SvOK(body)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!body || !SvOK(body)) return 0; 1 0 1
if (!body || !SvOK(body)) return 0; 0 1 1
387 1 p = SvPV_const(body, len);
388 353 ▶ while (off < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < len) { 352 1 353
389 352 STRLEN n = len - off;
390 352 ▶ if (n > chunk) n = chunk;
Branch: 1 branch
BranchTrueFalseTotal
if (n > chunk) n = chunk; 351 1 352
391 352 ▶ if (sink(aTHX_ ud, p + off, n)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (sink(aTHX_ ud, p + off, n)) break; 0 352 352
392 352 off += n;
393 }
394 1 return (IV)off;
395 }
396  
397 /* Read the body off psgi.input in `chunk` byte windows, handing each to the
398 * sink. Returns the number of bytes read.
399 *
400 * With a CONTENT_LENGTH exactly that many bytes are read, which is what PSGI
401 * requires of an application.
402 *
403 * Without one, what happens turns on psgix.input.buffered, because that is
404 * what decides whether reading to EOF is safe. A buffered input is a finite
405 * thing the server is already holding, so EOF arrives; the body is read to it
406 * and a request that had none costs an empty read. That is the multiplexed
407 * case: HTTP/2 and HTTP/3 forbid Transfer-Encoding, so an undeclared-length
408 * body over either arrives with no framing header of any kind, and a rule
409 * written around Transfer-Encoding drops it.
410 *
411 * On an unbuffered input - a live socket - reading to EOF is how an
412 * application hangs, so Transfer-Encoding decides instead, and it can, because
413 * an unbuffered input is HTTP/1 by construction. Neither header means no body,
414 * and this reads nothing: it does NOT go looking, which would turn an ordinary
415 * bodyless POST into an error. Chunked means a body of unknown length that
416 * cannot be read here, and refusing with a reason beats hanging.
417 *
418 * `max` stops the read and croaks; 0 falls back to the route's max_body
419 * (pq_env_ceiling), which is the only ceiling there is once no length was
420 * declared - exactly the case pd_body_limit could not check up front.
421 */
422 19 static IV pq_body_stream(pTHX_ AV *req, STRLEN chunk, IV max,
423 pq_sink_fn sink, void *ud) {
424 HV *env;
425 SV **in, **cl;
426 19 IV len, got = 0;
427 IO *io;
428 PerlIO *fp;
429 SV *window;
430 char *w;
431  
432 19 ▶ if (!chunk) chunk = PQ_STREAM_CHUNK;
Branch: 1 branch
BranchTrueFalseTotal
if (!chunk) chunk = PQ_STREAM_CHUNK; 15 4 19
433  
434 19 switch (pq_read_state(aTHX_ req)) {
435 1 case PQ_STREAMED:
436 1 croak("Punk::Request: the body was streamed and is gone - a body "
437 "can be read once, either whole (body/json/form) or in chunks "
438 "(body_each/body_to)");
439 1 case PQ_SLURPED: {
440 1 SV **b = av_fetch(req, PQ_BODY, 0);
441 1 ▶ return pq_stream_cached(aTHX_ (b && *b) ? *b : NULL, chunk, sink, ud);
Branch: 2 branches
BranchTrueFalseTotal
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
442 }
443 17 default: break;
444 }
445  
446 17 env = punk_req_env(aTHX_ req);
447 17 in = hv_fetchs(env, "psgi.input", 0);
448 17 cl = hv_fetchs(env, "CONTENT_LENGTH", 0);
449 17 ▶ len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1;
Branch: 3 branches
BranchTrueFalseTotal
len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; 13 4 17
len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; 13 0 13
len = (cl && *cl && SvOK(*cl)) ? SvIV(*cl) : -1; 10 3 13
450 17 ▶ if (!max) max = pq_env_ceiling(aTHX_ env);
Branch: 1 branch
BranchTrueFalseTotal
if (!max) max = pq_env_ceiling(aTHX_ env); 16 1 17
451  
452 17 ▶ if (len < 0 && !pq_input_buffered(aTHX_ env)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len < 0 && !pq_input_buffered(aTHX_ env)) { 7 10 17
if (len < 0 && !pq_input_buffered(aTHX_ env)) { 4 3 7
453 4 SV **te = hv_fetchs(env, "HTTP_TRANSFER_ENCODING", 0);
454 4 ▶ if (!(te && *te && SvOK(*te) && SvCUR(*te))) {
Branch: 4 branches
BranchTrueFalseTotal
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
455 /* no length and no transfer coding: the request has no body */
456 2 (void)av_store(req, PQ_READ, newSViv(PQ_STREAMED));
457 2 return 0;
458 }
459 2 croak("Punk::Request: a chunked body with no CONTENT_LENGTH, and "
460 "this server does not declare psgix.input.buffered - there "
461 "is no length to read to and reading to EOF on a live "
462 "socket would hang");
463 }
464  
465 /* the state changes before the first read: a sink that dies halfway
466 * through has still consumed the input, and the next reader must be told
467 * so rather than handed the remainder */
468 13 (void)av_store(req, PQ_READ, newSViv(PQ_STREAMED));
469  
470 13 ▶ if (!(in && *in && SvTRUE(*in)) || len == 0) return 0;
Branch: 4 branches
BranchTrueFalseTotal
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
471 13 io = sv_2io(*in);
472 13 ▶ fp = io ? IoIFP(io) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
fp = io ? IoIFP(io) : NULL; 13 0 13
473 13 ▶ if (!fp) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return 0; 0 13 13
474  
475 13 window = sv_2mortal(newSV(chunk + 1));
476 13 SvPOK_on(window);
477 13 w = SvPVX(window);
478  
479 168 ▶ while (len < 0 || got < len) {
Branch: 2 branches
BranchTrueFalseTotal
while (len < 0 || got < len) { 5 163 168
while (len < 0 || got < len) { 154 9 163
480 159 STRLEN want = chunk;
481 SSize_t n;
482 159 ▶ if (len >= 0 && (IV)want > len - got) want = (STRLEN)(len - got);
Branch: 2 branches
BranchTrueFalseTotal
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
483 159 n = PerlIO_read(fp, w, want);
484 159 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 2 157 159
485 157 got += (IV)n;
486 157 ▶ if (max && got > max) PQ_OVER_CEILING(max);
Branch: 2 branches
BranchTrueFalseTotal
if (max && got > max) PQ_OVER_CEILING(max); 7 150 157
if (max && got > max) PQ_OVER_CEILING(max); 2 5 7
487 155 ▶ if (sink(aTHX_ ud, w, (STRLEN)n)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (sink(aTHX_ ud, w, (STRLEN)n)) break; 0 155 155
488 }
489 11 return got;
490 }
491  
492 /* the two sinks: a Perl callback, and a handle */
493  
494 typedef struct { SV *cb; SV *self; } pq_sink_cb_ud;
495  
496 457 static int pq_sink_cb(pTHX_ void *ud, const char *buf, STRLEN len) {
497 457 pq_sink_cb_ud *s = (pq_sink_cb_ud *)ud;
498 457 dSP;
499 457 ENTER; SAVETMPS;
500 457 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 457 457
501 457 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 457 457
502 /* a fresh scalar per chunk: a callback that keeps one must be able to,
503 * which a reused buffer would quietly break */
504 457 PUSHs(sv_2mortal(newSVpvn(buf, len)));
505 457 PUSHs(s->self);
506 457 PUTBACK;
507 457 (void)call_sv(s->cb, G_VOID | G_DISCARD);
508 457 SPAGAIN;
509 457 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 457 0 457
510 457 return 0;
511 }
512  
513 /* body_to's handle, owned by the savestack for as long as the read runs */
514 typedef struct { PerlIO *fp; int open; } pq_out;
515  
516 2 static void pq_out_cleanup(pTHX_ void *p) {
517 2 pq_out *o = (pq_out *)p;
518 2 ▶ if (o->open) { (void)PerlIO_close(o->fp); o->open = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (o->open) { (void)PerlIO_close(o->fp); o->open = 0; } 0 2 2
519 2 }
520  
521 50 static int pq_sink_io(pTHX_ void *ud, const char *buf, STRLEN len) {
522 50 PerlIO *out = (PerlIO *)ud;
523 50 STRLEN off = 0;
524 100 ▶ while (off < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < len) { 50 50 100
525 50 SSize_t n = PerlIO_write(out, buf + off, len - off);
526 50 ▶ if (n <= 0) croak("Punk::Request: writing the body failed: %s",
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) croak("Punk::Request: writing the body failed: %s", 0 50 50
527 Strerror(errno));
528 50 off += (STRLEN)n;
529 }
530 50 return 0;
531 }
532  
533 #endif /* PUNK_REQUEST_H */
« include/punk/punk_dbq.h ↑ Summary xs/metrics.xs »