xs/request.xs

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