xs/request.xs

filter: Statementstmt 94.5% 224 / 237 Branchbran 60.4% 164 / 268 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.4% 388 / 505 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_txn.h ↑ Summary xs/cache.xs »
1 MODULE = Punk PACKAGE = Punk::Request
2  
3 PROTOTYPES: DISABLE
4  
5 SV *
6 new(class, env)
7 SV *class
8 SV *env
9 CODE:
10 {
11 5541 AV *req = newAV();
12 5541 av_extend(req, PQ_READ);
13 5541 (void)av_store(req, PQ_ENV, newSVsv(env));
14 5541 RETVAL = sv_bless(newRV_noinc((SV *)req),
15 gv_stashsv(class, GV_ADD));
16 }
17 OUTPUT:
18 RETVAL
19  
20 SV *
21 env(self)
22 SV *self
23 CODE:
24 {
25 8 SV **e = av_fetch(punk_req_av(aTHX_ self), PQ_ENV, 0);
26 8 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 8 0 8
# uncoverable condition right count:1 class:ignore_covered_err 8 0 8
27 # uncoverable condition right count:2 class:ignore_covered_err
28 RETVAL = e && *e ? newSVsv(*e) : newSV(0);
29 }
30 OUTPUT:
31 RETVAL
32  
33 # The raw body bytes, read once from psgi.input and cached; the cached
34 # SV itself is returned, matching the Perl aliasing semantics.
35 SV *
36 body(self)
37 SV *self
38 CODE:
39 {
40 98 SV *body = pq_body(aTHX_ punk_req_av(aTHX_ self));
41 96 RETVAL = SvREFCNT_inc_simple_NN(body);
42 }
43 OUTPUT:
44 RETVAL
45  
46 # The body decoded through File::Raw::JSON's C ABI.
47 SV *
48 json(self)
49 SV *self
50 CODE:
51 {
52 743 SV *body = pq_body(aTHX_ punk_req_av(aTHX_ self));
53 1477 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 742 0 742
# uncoverable branch false 742 0 742
54 742 # uncoverable condition right count:1 class:ignore_covered_err
55 # uncoverable condition right count:2 class:ignore_covered_err
56 742 if (SvOK(body) && SvCUR(body)) {
57 742 const frj_abi *J = punk_frj(aTHX);
58 STRLEN bl;
59 0 const char *b = SvPV_const(body, bl);
60 RETVAL = J->decode(aTHX_ b, bl, NULL);
61 }
62 else RETVAL = newSV(0); /* # uncoverable statement */
63 }
64 OUTPUT:
65 RETVAL
66  
67 # The body parsed through File::Raw::XML's C ABI, strict: a document type
68 # declaration is refused wherever it stands, so nothing here expands an entity
69 # or reaches outside the document. Takes no options, and must not grow any -
70 # a profile or a resolver reachable from a request is the switch that would
71 # give XXE back.
72 #
73 # Unlike ->json, which decodes afresh every call, the document is kept for the
74 # rest of the request: an XML parse costs far more than a JSON decode, node
75 # identity has to hold (a node's ->doc and a by_id result must name one
76 # document), and every other derived view of the request - query, form,
77 # cookies, body - is already cached. The tree it hands back is editable, so
78 # two callers share one document, the way ->body hands back the one cached SV.
79 SV *
80 xml(self)
81 SV *self
82 CODE:
83 {
84 9 AV *req = punk_req_av(aTHX_ self);
85 9 SV **c = av_fetch(req, PQ_XML, 0);
86 9 ▶ if (c && *c) RETVAL = SvREFCNT_inc_simple_NN(*c); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c && *c) RETVAL = SvREFCNT_inc_simple_NN(*c); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 8 9
if (c && *c) RETVAL = SvREFCNT_inc_simple_NN(*c); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
87 else {
88 8 SV *body = pq_body(aTHX_ req);
89 6 ▶ SV *doc = (SvOK(body) && SvCUR(body))
Branch: 1 branch
BranchTrueFalseTotal
SV *doc = (SvOK(body) && SvCUR(body)) 6 0 6
90 6 ? punk_xml_parse(aTHX_ body)
91 7 ▶ : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
: newSV(0); 6 1 7
92 /* stored even when it is undef: the slot's presence is what says
93 * the body has been parsed, so an empty body is not re-parsed on
94 * every call */
95 5 (void)av_store(req, PQ_XML, doc);
96 5 RETVAL = SvREFCNT_inc_simple_NN(doc);
97 }
98 }
99 OUTPUT:
100 RETVAL
101  
102 SV *
103 method(self)
104 SV *self
105 CODE:
106 {
107 4 HV *env = punk_req_env(aTHX_ punk_req_av(aTHX_ self));
108 4 SV **e = hv_fetchs(env, "REQUEST_METHOD", 0);
109 4 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 4 0 4
# uncoverable condition right count:1 class:ignore_covered_err 4 0 4
110 # uncoverable condition right count:2 class:ignore_covered_err
111 RETVAL = e && *e ? newSVsv(*e) : newSV(0);
112 }
113 OUTPUT:
114 RETVAL
115  
116 SV *
117 path(self)
118 SV *self
119 CODE:
120 {
121 23 HV *env = punk_req_env(aTHX_ punk_req_av(aTHX_ self));
122 23 SV **e = hv_fetchs(env, "PATH_INFO", 0);
123 46 ▶ RETVAL = (e && *e && SvOK(*e) && SvCUR(*e))
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = (e && *e && SvOK(*e) && SvCUR(*e)) 23 0 23
RETVAL = (e && *e && SvOK(*e) && SvCUR(*e)) 23 0 23
RETVAL = (e && *e && SvOK(*e) && SvCUR(*e)) 22 1 23
124 46 ▶ ? newSVsv(*e) : newSVpvs("/");
Branch: 1 branch
BranchTrueFalseTotal
? newSVsv(*e) : newSVpvs("/"); 23 0 23
125 }
126 OUTPUT:
127 RETVAL
128  
129 # The client's address. This is REMOTE_ADDR, which is the socket peer on a
130 # directly-exposed application and the resolved client when the `proxy`
131 # keyword is in force (punk_proxy.h rewrites the env key, so there is
132 # nothing to read differently here). The connecting address behind a proxy
133 # is env->{'punk.peer_addr'}.
134 SV *
135 address(self)
136 SV *self
137 CODE:
138 {
139 28 HV *env = punk_req_env(aTHX_ punk_req_av(aTHX_ self));
140 28 SV **e = hv_fetchs(env, "REMOTE_ADDR", 0);
141 28 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 28 0 28
# uncoverable condition right count:1 class:ignore_covered_err 28 0 28
142 # uncoverable condition right count:2 class:ignore_covered_err
143 RETVAL = e && *e ? newSVsv(*e) : newSV(0);
144 }
145 OUTPUT:
146 RETVAL
147  
148 SV *
149 header(self, name)
150 SV *self
151 SV *name
152 CODE:
153 {
154 799 HV *env = punk_req_env(aTHX_ punk_req_av(aTHX_ self));
155 STRLEN nl, i;
156 799 const char *n = SvPV_const(name, nl);
157 799 char buf[256] = "HTTP_";
158 SV **e;
159 799 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 799 799
160 10121 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 9322 799 10121
161 9322 if (nl > 240) croak("Punk::Request: header name too long");
162 9322 ▶ for (i = 0; i < nl; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nl; i++) { 8547 775 9322
for (i = 0; i < nl; i++) { 8499 48 8547
163 char c = n[i];
164 799 buf[5 + i] = c == '-' ? '_' : (char)toUPPER((U8)c);
165 799 ▶ }
Branch: 3 branches
BranchTrueFalseTotal
} 44 755 799
} 44 0 44
} 0 44 44
166 755 ▶ e = hv_fetch(env, buf, (I32)(nl + 5), 0);
Branch: 2 branches
BranchTrueFalseTotal
e = hv_fetch(env, buf, (I32)(nl + 5), 0); 734 21 755
e = hv_fetch(env, buf, (I32)(nl + 5), 0); 734 0 734
167 734 # uncoverable condition left count:2 class:ignore_covered_err
168 21 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 0 21 21
# uncoverable condition left count:3 class:ignore_covered_err 0 0 0
169 0 if (!(e && *e && SvOK(*e))) {
170 21 if (nl == 12 && memEQ(buf + 5, "CONTENT_TYPE", 12)) /* # uncoverable condition right count:2 class:ignore_covered_err */
171 e = hv_fetchs(env, "CONTENT_TYPE", 0);
172 799 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 767 32 799
# uncoverable branch true 767 0 767
173 # uncoverable condition left count:1 class:ignore_covered_err
174 # uncoverable condition left count:2 class:ignore_covered_err
175 # uncoverable condition right count:2 class:ignore_covered_err
176 else if (nl == 14 && memEQ(buf + 5, "CONTENT_LENGTH", 14))
177 e = hv_fetchs(env, "CONTENT_LENGTH", 0); /* # uncoverable statement */
178 else e = NULL;
179 }
180 RETVAL = e && *e ? newSVsv(*e) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */
181 }
182 OUTPUT:
183 RETVAL
184  
185 # headers: every header at once, as a hashref keyed lowercase and
186 # dash-separated ('x-forwarded-for'). That is the spelling HTTP/2 uses and
187 # the one header() already accepts, but note the asymmetry: header() folds
188 # case for you, a plain hash cannot, so $req->headers->{'X-Foo'} misses where
189 # $req->header('X-Foo') hits. CONTENT_TYPE and CONTENT_LENGTH appear as
190 # 'content-type' and 'content-length' - PSGI keeps them out of HTTP_*, and
191 # header() special-cases them for the same reason.
192 #
193 # A fresh hash per call, so a caller may keep or mutate it freely.
194 SV *
195 headers(self)
196 SV *self
197 CODE:
198 {
199 6 HV *env = punk_req_env(aTHX_ punk_req_av(aTHX_ self));
200 6 HV *h = pq_headers(aTHX_ env);
201 6 SV **e = hv_fetchs(env, "CONTENT_LENGTH", 0);
202 6 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 4 2 6
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
203 # uncoverable condition right count:3 class:ignore_covered_err
204 4 if (e && *e && SvOK(*e)) {
205 4 ▶ STRLEN vl;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN vl; 4 0 4
206 (void)SvPV_const(*e, vl); /* may be an IV; do not read SvCUR */
207 6 # uncoverable branch false
208 # uncoverable condition right count:1 class:ignore_covered_err
209 if (vl) (void)hv_stores(h, "content-length", newSVsv(*e));
210 }
211 RETVAL = newRV_noinc((SV *)h);
212 }
213 OUTPUT:
214 RETVAL
215  
216 SV *
217 query(self)
218 SV *self
219 CODE:
220 {
221 3193 AV *req = punk_req_av(aTHX_ self);
222 3193 SV **c = av_fetch(req, PQ_QUERY, 0);
223 3193 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 9 3184 3193
# uncoverable condition right count:2 class:ignore_covered_err 9 0 9
# uncoverable condition right count:2 class:ignore_covered_err 9 0 9
224 9 # uncoverable condition right count:3 class:ignore_covered_err
225 if (c && *c && SvROK(*c))
226 3184 RETVAL = newSVsv(*c);
227 3184 else {
228 3184 HV *env = punk_req_env(aTHX_ req);
229 3184 ▶ SV **qs = hv_fetchs(env, "QUERY_STRING", 0);
Branch: 2 branches
BranchTrueFalseTotal
SV **qs = hv_fetchs(env, "QUERY_STRING", 0); 3184 0 3184
SV **qs = hv_fetchs(env, "QUERY_STRING", 0); 3184 0 3184
230 9552 ▶ STRLEN ql = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN ql = 0; 3184 0 3184
231 3184 const char *q = qs && *qs && SvOK(*qs)
232 ? SvPV_const(*qs, ql) : "";
233 RETVAL = newSVsv(pq_cached(aTHX_ req, PQ_QUERY,
234 pq_parse_pairs(aTHX_ q, ql)));
235 }
236 }
237 OUTPUT:
238 RETVAL
239  
240 SV *
241 form(self)
242 SV *self
243 CODE:
244 {
245 4410 AV *req = punk_req_av(aTHX_ self);
246 4410 SV **c = av_fetch(req, PQ_FORM, 0);
247 4410 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 10 4400 4410
# uncoverable condition right count:2 class:ignore_covered_err 10 0 10
# uncoverable condition right count:2 class:ignore_covered_err 10 0 10
248 10 # uncoverable condition right count:3 class:ignore_covered_err
249 if (c && *c && SvROK(*c))
250 4400 RETVAL = newSVsv(*c);
251 4400 else {
252 4400 HV *env = punk_req_env(aTHX_ req);
253 2154 ▶ SV **ct = hv_fetchs(env, "CONTENT_TYPE", 0);
Branch: 2 branches
BranchTrueFalseTotal
SV **ct = hv_fetchs(env, "CONTENT_TYPE", 0); 2154 0 2154
SV **ct = hv_fetchs(env, "CONTENT_TYPE", 0); 2154 0 2154
254 8708 ▶ STRLEN cl = 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN cl = 0; 2154 2246 4400
255 const char *cts = ct && *ct && SvOK(*ct)
256 4400 ▶ ? SvPV_const(*ct, cl) : "";
Branch: 1 branch
BranchTrueFalseTotal
? SvPV_const(*ct, cl) : ""; 2139 2261 4400
257 2217 ▶ HV *built;
Branch: 1 branch
BranchTrueFalseTotal
HV *built; 78 2061 2139
258 if (cl >= 33
259 && memEQ(cts, "application/x-www-form-urlencoded", 33)) {
260 78 /* the body read is I/O and stays Perl - one method
261 * call, after which the parse is cached here */
262 dSP;
263 78 SV *body;
264 78 ▶ int count;
Branch: 1 branch
BranchTrueFalseTotal
int count; 0 78 78
265 78 ▶ ENTER; SAVETMPS;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; 0 78 78
266 78 PUSHMARK(SP);
267 78 XPUSHs(self);
268 78 PUTBACK;
269 78 ▶ count = call_method("body", G_SCALAR);
Branch: 1 branch
BranchTrueFalseTotal
count = call_method("body", G_SCALAR); 78 0 78
270 78 ▶ SPAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; 78 0 78
271 body = count > 0 ? POPs : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
272 78 # uncoverable branch false
273 78 # uncoverable condition right count:1 class:ignore_covered_err
274 if (SvOK(body)) {
275 0 STRLEN bl;
276 78 ▶ const char *b = SvPV_const(body, bl);
Branch: 1 branch
BranchTrueFalseTotal
const char *b = SvPV_const(body, bl); 78 0 78
277 built = pq_parse_pairs(aTHX_ b, bl);
278 4364 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 2062 2260 4322
} 2062 0 2062
279 else built = newHV(); /* # uncoverable statement */
280 2062 PUTBACK; FREETMPS; LEAVE;
281 2062 }
282 2062 else if (cl >= 19 && memEQ(cts, "multipart/form-data", 19)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
283 2062 /* field parts -> the form hash, file parts -> PQ_UPLOADS */
284 STRLEN boundl = 0;
285 const char *boundary = pq_hdr_param(cts, cl, "boundary", &boundl);
286 2062 HV *uploads = newHV();
287 2062 built = newHV();
288 2062 ▶ /* mortal across the walk, which croaks at the ceiling: the
Branch: 2 branches
BranchTrueFalseTotal
/* mortal across the walk, which croaks at the ceiling: the 2062 0 2062
/* mortal across the walk, which croaks at the ceiling: the 2062 0 2062
289 * parts read by then would otherwise be nobody's */
290 sv_2mortal((SV *)uploads);
291 sv_2mortal((SV *)built);
292 # uncoverable branch false class:ignore_covered_err
293 # uncoverable condition right count:1 class:ignore_covered_err
294 # uncoverable condition right count:2 class:ignore_covered_err
295 if (boundary && boundl) {
296 /* Streamed off psgi.input rather than slurped through
297 * ->body.
298 *
299 * ->body caches the whole request in one scalar, which is
300 * right for JSON and wrong for a file: measured at 2.02x
301 2062 * the file size in RSS for a 64 MiB upload, on top of
302 2062 * whatever the server is already holding. Reading the
303 2062 * handle in chunks costs a window instead.
304 2062 ▶ *
Branch: 3 branches
BranchTrueFalseTotal
* 38 2024 2062
* 38 0 38
* 38 0 38
305 * NOT conditioned on psgix.input.buffered: Starman and
306 * Hyperman both set it TRUE while holding the body in
307 2062 * memory, so it says "seekable", not "cheap". */
308 2062 ▶ HV *env = punk_req_env(aTHX_ req);
Branch: 3 branches
BranchTrueFalseTotal
HV *env = punk_req_env(aTHX_ req); 2062 0 2062
HV *env = punk_req_env(aTHX_ req); 2062 0 2062
HV *env = punk_req_env(aTHX_ req); 2062 0 2062
309 2062 ▶ SV **in = hv_fetchs(env, "psgi.input", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **in = hv_fetchs(env, "psgi.input", 0); 2062 0 2062
310 2062 ▶ SV **cl2 = hv_fetchs(env, "CONTENT_LENGTH", 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **cl2 = hv_fetchs(env, "CONTENT_LENGTH", 0); 2062 0 2062
311 2062 # uncoverable condition right count:2 class:ignore_covered_err
312 2062 # uncoverable condition right count:3 class:ignore_covered_err
313 2062 IV len = (cl2 && *cl2 && SvOK(*cl2)) ? SvIV(*cl2) : -1;
314 /* no declared length is the h2/h3 upload, and the walk
315 * runs to EOF: max_body is then the only bound there is */
316 IV max = pq_env_ceiling(aTHX_ env);
317 2062 # uncoverable condition right count:1 class:ignore_covered_err
318 # uncoverable condition right count:2 class:ignore_covered_err
319 2062 # uncoverable condition right count:3 class:ignore_covered_err
320 IO *io = (in && *in && SvTRUE(*in)) ? sv_2io(*in) : NULL;
321 42 PerlIO *fp = io ? IoIFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
322 # uncoverable branch false
323 # uncoverable condition right count:1 class:ignore_covered_err
324 42 if (fp) {
325 42 SV *dir = pq_upload_dir(aTHX_ req);
326 42 AV *tmps = newAV();
327 pq_tmp_own(aTHX_ tmps); /* unlinked when freed */
328 2260 /* handed to the request BEFORE the walk: the ceiling
329 2380 * croaks from inside it, and the parts spilled by
330 * then must still be unlinked when the request goes */
331 (void)av_store(req, PQ_TEMPFILES,
332 newRV_noinc((SV *)tmps));
333 (void)pq_parse_multipart_stream(aTHX_ fp, len, max,
334 boundary, boundl, built, uploads, dir, tmps);
335 (void)PerlIO_seek(fp, 0, SEEK_SET);
336 }
337 }
338 SvREFCNT_inc_simple_void_NN((SV *)uploads);
339 SvREFCNT_inc_simple_void_NN((SV *)built);
340 (void)pq_cached(aTHX_ req, PQ_UPLOADS, uploads);
341 }
342 else built = newHV();
343 RETVAL = newSVsv(pq_cached(aTHX_ req, PQ_FORM, built));
344 }
345 }
346 OUTPUT:
347 RETVAL
348  
349 # upload($name) -> the Punk::Upload for that field (the first if several);
350 # uploads -> the { name => upload|[uploads] } hash. Both parse the body once.
351 SV *
352 upload(self, name)
353 SV *self
354 SV *name
355 CODE:
356 {
357 2106 AV *req = punk_req_av(aTHX_ self);
358 SV **u;
359 2106 STRLEN nl; const char *n = SvPV_const(name, nl);
360 SV **got;
361 /* ensure the body is parsed (populates PQ_UPLOADS) */
362 2106 ▶ { dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(self); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
{ dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(self); PUTBACK; 0 2106 2106
{ dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(self); PUTBACK; 0 2106 2106
363 2106 (void)call_method("form", G_SCALAR | G_DISCARD); SPAGAIN;
364 86 ▶ PUTBACK; FREETMPS; LEAVE; }
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; } 86 0 86
365 86 u = av_fetch(req, PQ_UPLOADS, 0);
366 86 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 32 54 86
# uncoverable condition right count:2 class:ignore_covered_err 32 0 32
# uncoverable condition right count:2 class:ignore_covered_err 32 0 32
367 86 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 4 branches
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 31 55 86
# uncoverable condition right count:3 class:ignore_covered_err 31 0 31
# uncoverable condition right count:3 class:ignore_covered_err 31 0 31
# uncoverable condition right count:3 class:ignore_covered_err 0 31 31
368 0 got = (u && *u && SvROK(*u)) ? hv_fetch((HV *)SvRV(*u), n, (I32)nl, 0) : NULL;
369 0 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 0 0 0
# uncoverable branch true 0 0 0
370 # uncoverable condition left count:1 class:ignore_covered_err
371 86 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 31 55 86
# uncoverable condition left count:2 class:ignore_covered_err 31 0 31
372 # uncoverable condition right count:2 class:ignore_covered_err
373 # uncoverable condition left count:3 class:ignore_covered_err
374 # uncoverable condition right count:3 class:ignore_covered_err
375 # uncoverable condition left count:4 class:ignore_covered_err
376 if (got && *got && SvROK(*got) && SvTYPE(SvRV(*got)) == SVt_PVAV) {
377 SV **first = av_fetch((AV *)SvRV(*got), 0, 0); /* first of several */ /* # uncoverable statement */
378 # uncoverable statement
379 # uncoverable condition left count:1
380 # uncoverable condition right count:1
381 # uncoverable condition left count:2
382 # uncoverable condition right count:2
383 RETVAL = (first && *first) ? newSVsv(*first) : newSV(0);
384 }
385 else RETVAL = (got && *got) ? newSVsv(*got) : newSV(0); /* # uncoverable condition right count:2 class:ignore_covered_err */
386 }
387 OUTPUT:
388 RETVAL
389  
390 SV *
391 uploads(self)
392 SV *self
393 CODE:
394 {
395 14 AV *req = punk_req_av(aTHX_ self);
396 SV **u;
397 14 ▶ { dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(self); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
{ dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(self); PUTBACK; 0 14 14
{ dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(self); PUTBACK; 0 14 14
398 14 (void)call_method("form", G_SCALAR | G_DISCARD); SPAGAIN;
399 14 ▶ PUTBACK; FREETMPS; LEAVE; }
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; } 14 0 14
400 14 u = av_fetch(req, PQ_UPLOADS, 0);
401 28 ▶ RETVAL = (u && *u && SvROK(*u)) ? newSVsv(*u)
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (u && *u && SvROK(*u)) ? newSVsv(*u) 14 0 14
RETVAL = (u && *u && SvROK(*u)) ? newSVsv(*u) 14 0 14
402 14 ▶ : newRV_noinc((SV *)newHV()); /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
: newRV_noinc((SV *)newHV()); /* # uncoverable statement class:ignore_covered_err */ 14 0 14
403 }
404 OUTPUT:
405 RETVAL
406  
407 SV *
408 param(self, name)
409 SV *self
410 SV *name
411 CODE:
412 {
413 2494 SV *v = pq_param_get(aTHX_ self, punk_req_av(aTHX_ self), name);
414 2494 ▶ RETVAL = v ? newSVsv(v) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v ? newSVsv(v) : newSV(0); 240 2254 2494
415 }
416 OUTPUT:
417 RETVAL
418  
419 # params -> every parameter merged, query winning over form
420 # params(@k) -> just those, as a list of values in list context (undef
421 # for a name neither table has) or a hashref of the ones
422 # that are there in scalar context.
423 #
424 # The results are written back over the argument slots, one behind the
425 # name being read, so no key is overwritten before it has been looked up.
426 void
427 params(self, ...)
428 SV *self
429 PPCODE:
430 {
431 21 AV *req = punk_req_av(aTHX_ self);
432 int i;
433 21 ▶ if (items < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) { 16 5 21
434 16 HV *merged = newHV();
435 16 pq_overlay_hv(aTHX_ merged,
436 pq_table(aTHX_ self, req, PQ_FORM, "form"));
437 16 pq_overlay_hv(aTHX_ merged,
438 pq_table(aTHX_ self, req, PQ_QUERY, "query"));
439 16 ST(0) = sv_2mortal(newRV_noinc((SV *)merged));
440 16 XSRETURN(1);
441 }
442 5 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 2 3 5
443 6 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 4 2 6
444 4 SV *v = pq_param_get(aTHX_ self, req, ST(i));
445 4 ▶ ST(i - 1) = sv_2mortal(v ? newSVsv(v) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
ST(i - 1) = sv_2mortal(v ? newSVsv(v) : newSV(0)); 3 1 4
446 }
447 2 XSRETURN(items - 1);
448 }
449 else {
450 3 HV *out = newHV();
451 9 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 6 3 9
452 6 SV *v = pq_param_get(aTHX_ self, req, ST(i));
453 6 ▶ if (v) (void)hv_store_ent(out, ST(i), newSVsv(v), 0);
Branch: 1 branch
BranchTrueFalseTotal
if (v) (void)hv_store_ent(out, ST(i), newSVsv(v), 0); 4 2 6
454 }
455 3 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
456 3 XSRETURN(1);
457 }
458 }
459  
460 SV *
461 cookies(self)
462 SV *self
463 CODE:
464 {
465 18 AV *req = punk_req_av(aTHX_ self);
466 18 SV **c = av_fetch(req, PQ_COOKIES, 0);
467 18 ▶ # uncoverable branch true
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch true 0 18 18
# uncoverable branch true 0 0 0
# uncoverable branch true 0 0 0
468 0 # uncoverable condition left count:1 class:ignore_covered_err
469 # uncoverable condition left count:2 class:ignore_covered_err
470 18 # uncoverable condition right count:2 class:ignore_covered_err
471 18 # uncoverable condition left count:3 class:ignore_covered_err
472 18 # uncoverable condition right count:3 class:ignore_covered_err
473 18 ▶ if (c && *c && SvROK(*c))
Branch: 2 branches
BranchTrueFalseTotal
if (c && *c && SvROK(*c)) 18 0 18
if (c && *c && SvROK(*c)) 18 0 18
474 54 ▶ RETVAL = newSVsv(*c); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVsv(*c); /* # uncoverable statement */ 18 0 18
475 18 else {
476 HV *env = punk_req_env(aTHX_ req);
477 SV **h = hv_fetchs(env, "HTTP_COOKIE", 0);
478 STRLEN hl = 0;
479 const char *hs = h && *h && SvOK(*h)
480 ? SvPV_const(*h, hl) : "";
481 RETVAL = newSVsv(pq_cached(aTHX_ req, PQ_COOKIES,
482 pq_parse_cookies(aTHX_ hs, hl)));
483 }
484 }
485 OUTPUT:
486 RETVAL
487  
488 SV *
489 cookie(self, name)
490 SV *self
491 SV *name
492 CODE:
493 {
494 18 AV *req = punk_req_av(aTHX_ self);
495 18 SV **c = av_fetch(req, PQ_COOKIES, 0);
496 18 HE *he = NULL;
497 18 dSP;
498 18 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 1 17 18
# uncoverable condition left count:2 class:ignore_covered_err 1 0 1
# uncoverable condition left count:2 class:ignore_covered_err 0 1 1
499 17 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 0 17 17
# uncoverable condition left count:3 class:ignore_covered_err 0 17 17
500 17 if (!(c && *c && SvROK(*c))) {
501 17 PUSHMARK(SP); XPUSHs(self); PUTBACK;
502 17 call_method("cookies", G_SCALAR);
503 SPAGAIN; (void)POPs; PUTBACK;
504 18 ▶ c = av_fetch(req, PQ_COOKIES, 0);
Branch: 3 branches
BranchTrueFalseTotal
c = av_fetch(req, PQ_COOKIES, 0); 18 0 18
c = av_fetch(req, PQ_COOKIES, 0); 18 0 18
c = av_fetch(req, PQ_COOKIES, 0); 18 0 18
505 18 }
506 18 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 18 0 18
507 # uncoverable condition right count:1 class:ignore_covered_err
508 # uncoverable condition right count:2 class:ignore_covered_err
509 # uncoverable condition right count:3 class:ignore_covered_err
510 if (c && *c && SvROK(*c))
511 he = hv_fetch_ent((HV *)SvRV(*c), name, 0, 0);
512 RETVAL = he ? newSVsv(HeVAL(he)) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
513 }
514 OUTPUT:
515 RETVAL
516  
517 # body_each(\&cb, %opts): the request body in chunks, each handed to the
518 # callback as ($chunk, $req). Options: `chunk` (window size, default 64KB) and
519 # `max` (a ceiling in bytes; 0, the default, is none). Returns the byte count.
520 #
521 # The body is read once: after this, ->body and ->json croak rather than
522 # answer with nothing. A body already read whole is replayed from the copy, so
523 # the order the two are called in is not a trap.
524 IV
525 body_each(self, cb, ...)
526 SV *self
527 SV *cb
528 CODE:
529 {
530 21 AV *req = punk_req_av(aTHX_ self);
531 pq_sink_cb_ud ud;
532 21 STRLEN chunk = 0;
533 21 IV max = 0;
534 int i;
535 21 ▶ if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 20 1 21
if (!(SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 20 20
536 1 croak("Punk::Request: body_each takes a code reference");
537 20 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 1 19 20
538 1 croak("Punk::Request: body_each takes key => value options");
539 23 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 6 17 23
540 6 const char *k = SvPV_nolen(ST(i));
541 6 SV *v = ST(i + 1);
542 6 ▶ if (strEQ(k, "chunk")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "chunk")) { 4 2 6
543 4 ▶ IV n = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
IV n = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
544 4 ▶ if (n <= 0) croak("Punk::Request: body_each chunk is a "
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) croak("Punk::Request: body_each chunk is a " 1 3 4
545 "positive number of bytes");
546 3 chunk = (STRLEN)n;
547 }
548 2 ▶ else if (strEQ(k, "max")) max = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "max")) max = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
else if (strEQ(k, "max")) max = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
549 1 else croak("Punk::Request: unknown body_each option '%s'", k);
550 }
551 17 ud.cb = cb;
552 17 ud.self = self;
553 17 RETVAL = pq_body_stream(aTHX_ req, chunk, max, pq_sink_cb, &ud);
554 }
555 OUTPUT:
556 RETVAL
557  
558 # body_to($dest, %opts): spool the body straight to a file, without a copy of
559 # it ever existing in the application. $dest is a path or an open handle; the
560 # same `chunk` and `max` options apply. Returns the byte count.
561 IV
562 body_to(self, dest, ...)
563 SV *self
564 SV *dest
565 CODE:
566 {
567 3 AV *req = punk_req_av(aTHX_ self);
568 3 STRLEN chunk = 0;
569 3 IV max = 0;
570 3 int i, opened = 0;
571 3 PerlIO *out = NULL;
572 3 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 3 3
573 0 # uncoverable condition left count:1 class:ignore_covered_err
574 4 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 1 3 4
575 1 croak("Punk::Request: body_to takes key => value options"); /* # uncoverable statement */
576 1 for (i = 2; i + 1 < items; i += 2) {
577 1 ▶ const char *k = SvPV_nolen(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
const char *k = SvPV_nolen(ST(i)); 1 0 1
578 1 ▶ SV *v = ST(i + 1);
Branch: 1 branch
BranchTrueFalseTotal
SV *v = ST(i + 1); 1 0 1
579 1 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 0 1 1
580 # uncoverable condition right count:1 class:ignore_covered_err
581 1 if (strEQ(k, "chunk")) {
582 IV n = SvOK(v) ? SvIV(v) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
583 0 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 0 0 0
# uncoverable branch true 0 0 0
584 0 # uncoverable condition left count:1 class:ignore_covered_err
585 if (n <= 0) croak("Punk::Request: body_to chunk is a "
586 3 ▶ "positive number of bytes");
Branch: 1 branch
BranchTrueFalseTotal
"positive number of bytes"); 1 2 3
587 1 chunk = (STRLEN)n;
588 3 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 1 1 2
} 0 1 1
589 1 # uncoverable statement
590 1 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 1 0 1
591 1 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 0 1 1
# uncoverable branch false 0 0 0
592 1 ▶ # uncoverable condition left count:1
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 0 1 1
593 0 # uncoverable condition right count:1
594 # uncoverable condition left count:2
595 # uncoverable condition right count:2
596 else if (strEQ(k, "max")) max = SvOK(v) ? SvIV(v) : 0;
597 else croak("Punk::Request: unknown body_to option '%s'", k); /* # uncoverable statement */
598 1 }
599 1 ▶ if (!SvOK(dest))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(dest)) 0 1 1
600 0 croak("Punk::Request: body_to takes a path or an open handle");
601 1 if (SvROK(dest) || SvTYPE(dest) == SVt_PVGV) { /* # uncoverable condition left count:2 class:ignore_covered_err */
602 1 ▶ IO *io = sv_2io(dest);
Branch: 1 branch
BranchTrueFalseTotal
IO *io = sv_2io(dest); 0 1 1
603 0 out = io ? IoOFP(io) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
604 # uncoverable branch true
605 1 # uncoverable condition left count:1 class:ignore_covered_err
606 # uncoverable condition left count:2 class:ignore_covered_err
607 # uncoverable condition right count:2 class:ignore_covered_err
608 if (!out && io) out = IoIFP(io);
609 # uncoverable branch true
610 # uncoverable condition left count:1 class:ignore_covered_err
611 if (!out)
612 croak("Punk::Request: body_to was given a handle that is " /* # uncoverable statement */
613 "not open for writing");
614 2 }
615 2 else {
616 2 STRLEN plen;
617 2 const char *path = SvPV(dest, plen);
618 2 # uncoverable branch true
619 2 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 1 1 2
620 1 if (memchr(path, 0, plen))
621 1 ▶ croak("Punk::Request: body_to path contains a NUL byte"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Request: body_to path contains a NUL byte"); /* # uncoverable statement */ 0 1 1
622 0 out = PerlIO_open(path, "wb");
623 # uncoverable branch true
624 # uncoverable condition left count:1 class:ignore_covered_err
625 2 if (!out)
626 2 ▶ croak("Punk::Request: cannot write '%s': %s", path, /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Request: cannot write '%s': %s", path, /* # uncoverable statement */ 0 2 2
627 Strerror(errno)); /* # uncoverable statement */
628 opened = 1;
629 }
630 { /* A die mid-read must still close what this opened, and the
631 * ordinary path must still be able to report a close that failed
632 * - a flush error on the last block is how a spooled file ends up
633 * short. So: the savestack owns the handle for the die path, and
634 * the success path closes it first and clears the flag. */
635 pq_out o;
636 IV got;
637 o.fp = out;
638 o.open = opened;
639 ENTER;
640 SAVEDESTRUCTOR_X(pq_out_cleanup, &o);
641 got = pq_body_stream(aTHX_ req, chunk, max, pq_sink_io, out);
642 if (o.open) {
643 o.open = 0;
644 # uncoverable branch true
645 # uncoverable condition left count:1 class:ignore_covered_err
646 if (PerlIO_close(out) != 0)
647 croak("Punk::Request: closing the file failed: %s", /* # uncoverable statement */
648 Strerror(errno)); /* # uncoverable statement */
649 }
650 LEAVE;
651 RETVAL = got;
652 }
653 }
654 OUTPUT:
655 RETVAL
« include/punk/punk_txn.h ↑ Summary xs/cache.xs »