| 1 |
|
| /* punk_multipart.h - multipart/form-data parsing, in C. |
| 2 |
|
| * |
| 3 |
|
| * Punk::Request->form() gains a multipart branch beside its urlencoded one: |
| 4 |
|
| * field parts merge into the form hash (so $c->param sees them), file parts |
| 5 |
|
| * become Punk::Upload objects collected in PQ_UPLOADS. Boundary and line |
| 6 |
|
| * scanning use Perl's ninstr (portable substring search). Upload content is |
| 7 |
|
| * held in memory this cut. |
| 8 |
|
| * |
| 9 |
|
| * Must be included after punk_request.h (the PQ_* slots). |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #ifndef PUNK_MULTIPART_H |
| 13 |
|
| #define PUNK_MULTIPART_H |
| 14 |
|
| |
| 15 |
|
| /* add a value under a key, promoting a repeat to an arrayref (as the |
| 16 |
|
| * urlencoded parser does), without decoding */ |
| 17 |
281 |
| static void pq_hv_add(pTHX_ HV *out, const char *k, STRLEN kl, SV *val) { |
| 18 |
281 |
| SV *key = sv_2mortal(newSVpvn(k, kl)); |
| 19 |
281 |
| HE *he = hv_fetch_ent(out, key, 0, 0); |
| 20 |
281 |
▶ | if (he) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (he) { |
11 |
270 |
281 |
|
| 21 |
11 |
| SV *have = HeVAL(he); |
| 22 |
|
| AV *list; |
| 23 |
11 |
▶ | if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) list = (AV *)SvRV(have); |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) list = (AV *)SvRV(have); |
11 |
0 |
11 |
| if (SvROK(have) && SvTYPE(SvRV(have)) == SVt_PVAV) list = (AV *)SvRV(have); |
8 |
3 |
11 |
|
| 24 |
|
| else { |
| 25 |
3 |
| list = newAV(); |
| 26 |
3 |
| av_push(list, newSVsv(have)); |
| 27 |
3 |
| (void)hv_store_ent(out, key, newRV_noinc((SV *)list), 0); |
| 28 |
|
| } |
| 29 |
11 |
| av_push(list, val); |
| 30 |
|
| } |
| 31 |
270 |
| else (void)hv_store_ent(out, key, val, 0); |
| 32 |
281 |
| } |
| 33 |
|
| |
| 34 |
|
| /* the value of a header parameter: name=value or name="value". Returns a |
| 35 |
|
| * pointer into s (with *vl set) or NULL. */ |
| 36 |
2108 |
| static const char *pq_hdr_param(const char *s, STRLEN sl, const char *p, |
| 37 |
|
| STRLEN *vl) { |
| 38 |
2108 |
| STRLEN pl = strlen(p), i; |
| 39 |
35473 |
▶ | for (i = 0; i + pl + 1 <= sl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + pl + 1 <= sl; i++) { |
35005 |
468 |
35473 |
|
| 40 |
35005 |
▶ | if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') |
Branch: 4 branches
| Branch | True | False | Total |
| if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') |
32897 |
2108 |
35005 |
| if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') |
29362 |
3535 |
32897 |
| if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') |
26936 |
2426 |
29362 |
| if ((i == 0 || s[i-1] == ' ' || s[i-1] == ';' || s[i-1] == '\t') |
0 |
26936 |
26936 |
|
| 41 |
8069 |
▶ | && memEQ(s + i, p, pl) && s[i + pl] == '=') { |
Branch: 2 branches
| Branch | True | False | Total |
| && memEQ(s + i, p, pl) && s[i + pl] == '=') { |
1640 |
6429 |
8069 |
| && memEQ(s + i, p, pl) && s[i + pl] == '=') { |
1640 |
0 |
1640 |
|
| 42 |
1640 |
| const char *v = s + i + pl + 1; |
| 43 |
|
| const char *e; |
| 44 |
1640 |
▶ | if (v < s + sl && *v == '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (v < s + sl && *v == '"') { |
1640 |
0 |
1640 |
| if (v < s + sl && *v == '"') { |
1104 |
536 |
1640 |
|
| 45 |
1104 |
| v++; e = v; |
| 46 |
3619 |
▶ | while (e < s + sl && *e != '"') e++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (e < s + sl && *e != '"') e++; |
3619 |
0 |
3619 |
| while (e < s + sl && *e != '"') e++; |
2515 |
1104 |
3619 |
|
| 47 |
|
| } |
| 48 |
5534 |
▶ | else { e = v; while (e < s + sl && *e != ';') e++; } |
Branch: 2 branches
| Branch | True | False | Total |
| else { e = v; while (e < s + sl && *e != ';') e++; } |
4998 |
536 |
5534 |
| else { e = v; while (e < s + sl && *e != ';') e++; } |
4998 |
0 |
4998 |
|
| 49 |
1640 |
| *vl = (STRLEN)(e - v); |
| 50 |
1640 |
| return v; |
| 51 |
|
| } |
| 52 |
|
| } |
| 53 |
468 |
| return NULL; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| /* The whole-body parser that used to live here had no callers: every |
| 57 |
|
| * multipart body goes through pq_parse_multipart_stream (punk_mpstream.h), |
| 58 |
|
| * which spills large parts to disk and bounds what it holds. */ |
| 59 |
|
| |
| 60 |
|
| #endif /* PUNK_MULTIPART_H */ |