include/fetch/ft_headers.h

filter: Statementstmt 96.8% 93 / 96 Branchbran 67.0% 59 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.6% 152 / 184 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/loop.xs ↑ Summary include/fetch/ft_obs.h »
1 #ifndef FT_HEADERS_H
2 #define FT_HEADERS_H
3  
4 /* Fetch::Headers - an ordered, case-insensitive, multi-valued set of HTTP
5 * header fields, stored as a blessed [k, v, k, v, ...] arrayref so it still
6 * behaves as the flat pair list older code dereferences (@$h) while offering
7 * get/get_all/set/add/remove/names/merge on top. Duplicate field names are
8 * kept in order (Set-Cookie et al). All operations are these C helpers driven
9 * from xs/headers.xs; the same helpers back Fetch::Response's header lookup. */
10  
11 #include <ctype.h>
12 #include <string.h>
13  
14 /* case-insensitive byte compare of two field names */
15 348 static int ft_ci_eq(const char *a, STRLEN al, const char *b, STRLEN bl) {
16 STRLEN i;
17 348 ▶ if (al != bl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (al != bl) return 0; 287 61 348
18 299 ▶ for (i = 0; i < al; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < al; i++) 264 35 299
19 264 ▶ if (tolower((unsigned char)a[i]) != tolower((unsigned char)b[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (tolower((unsigned char)a[i]) != tolower((unsigned char)b[i])) 26 238 264
20 26 return 0;
21 35 return 1;
22 }
23  
24 /* index of the key SV of the first pair whose name matches case-insensitively,
25 * or -1. The value sits at the following slot. */
26 349 static SSize_t ft_hdr_find(pTHX_ AV *av, const char *name, STRLEN nl) {
27 349 SSize_t n = av_len(av) + 1, i;
28 602 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 274 328 602
29 274 SV **k = av_fetch(av, i, 0);
30 274 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 274 0 274
if (k && *k) { 274 0 274
31 STRLEN kl;
32 274 const char *ks = SvPV_const(*k, kl);
33 274 ▶ if (ft_ci_eq(ks, kl, name, nl)) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (ft_ci_eq(ks, kl, name, nl)) return i; 21 253 274
34 }
35 }
36 328 return -1;
37 }
38  
39 /* sort helper for the hashref constructor path (matches Perl's `sort keys`) */
40 typedef struct { const char *k; STRLEN kl; SV *v; } ft_kv;
41 5 static int ft_kv_cmp(const void *a, const void *b) {
42 5 const ft_kv *x = (const ft_kv *)a, *y = (const ft_kv *)b;
43 5 STRLEN m = x->kl < y->kl ? x->kl : y->kl;
44 5 ▶ int c = m ? memcmp(x->k, y->k, m) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int c = m ? memcmp(x->k, y->k, m) : 0; 5 0 5
45 5 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 5 0 5
46 0 return (x->kl > y->kl) - (x->kl < y->kl);
47 }
48  
49 7 static void ft_hdr_from_hv(pTHX_ AV *dst, HV *h) {
50 7 ▶ SSize_t n = (SSize_t)HvUSEDKEYS(h), i = 0, j;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = (SSize_t)HvUSEDKEYS(h), i = 0, j; 0 7 7
51 ft_kv *kv;
52 HE *he;
53 7 ▶ if (n <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return; 0 7 7
54 7 ▶ Newx(kv, n, ft_kv);
Branch: 1 branch
BranchTrueFalseTotal
Newx(kv, n, ft_kv); 0 7 7
55 7 hv_iterinit(h);
56 19 ▶ while ((he = hv_iternext(h)) && i < n) {
Branch: 2 branches
BranchTrueFalseTotal
while ((he = hv_iternext(h)) && i < n) { 12 7 19
while ((he = hv_iternext(h)) && i < n) { 12 0 12
57 I32 klen;
58 12 kv[i].k = hv_iterkey(he, &klen);
59 12 ▶ kv[i].kl = (STRLEN)(klen < 0 ? -klen : klen);
Branch: 1 branch
BranchTrueFalseTotal
kv[i].kl = (STRLEN)(klen < 0 ? -klen : klen); 0 12 12
60 12 kv[i].v = hv_iterval(h, he);
61 12 i++;
62 }
63 7 qsort(kv, (size_t)i, sizeof(ft_kv), ft_kv_cmp);
64 19 ▶ for (j = 0; j < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) { 12 7 19
65 12 av_push(dst, newSVpvn(kv[j].k, kv[j].kl));
66 12 av_push(dst, newSVsv(kv[j].v));
67 }
68 7 Safefree(kv);
69 }
70  
71 /* is $name present (case-insensitive)? */
72 327 static int ft_hdr_exists(pTHX_ AV *av, const char *name, STRLEN nl) {
73 327 return ft_hdr_find(aTHX_ av, name, nl) >= 0 ? 1 : 0;
74 }
75  
76 /* Append the flat name => value list from $src to $dst: a Fetch::Headers or a
77 * plain arrayref is copied element-wise; a hashref becomes sorted key/value
78 * pairs; undef contributes nothing. */
79 168 static void ft_hdr_pairs_into(pTHX_ AV *dst, SV *src) {
80 SV *rv;
81 168 ▶ if (!src || !SvOK(src)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!src || !SvOK(src)) return; 168 0 168
if (!src || !SvOK(src)) return; 0 168 168
82 168 ▶ if (!SvROK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(src)) 0 168 168
83 0 croak("Fetch::Headers: cannot build headers from 'scalar'");
84 168 rv = SvRV(src);
85 168 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 161 7 168
86 161 AV *s = (AV *)rv;
87 161 SSize_t n = av_len(s) + 1, i;
88 171 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 161 171
89 10 SV **e = av_fetch(s, i, 0);
90 10 ▶ av_push(dst, (e && *e) ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, (e && *e) ? newSVsv(*e) : newSV(0)); 10 0 10
av_push(dst, (e && *e) ? newSVsv(*e) : newSV(0)); 10 0 10
91 }
92 7 ▶ } else if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(rv) == SVt_PVHV) { 7 0 7
93 7 ft_hdr_from_hv(aTHX_ dst, (HV *)rv);
94 } else {
95 0 croak("Fetch::Headers: cannot build headers from '%s'",
96 sv_reftype(rv, 0));
97 }
98 }
99  
100 /* lowercase a name into a freshly malloc'd buffer (caller frees) */
101 210 static char *ft_lc_dup(const char *s, STRLEN n) {
102 210 char *r = (char *)malloc(n + 1); /* +1: NUL-terminate so callers that
103 * treat the result as a C string (the
104 * URL scheme) do not read past it */
105 STRLEN i;
106 210 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 210 0 210
107 1144 ▶ for (i = 0; i < n; i++) r[i] = (char)tolower((unsigned char)s[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) r[i] = (char)tolower((unsigned char)s[i]); 934 210 1144
108 210 r[n] = '\0';
109 }
110 210 return r;
111 }
112  
113 /* Overlay $other on the flat [k,v,...] list $av in place: each field name it
114 * carries replaces all of $av's values for that name; names present only in
115 * $av are kept, in order; the overlay is appended. */
116 8 static void ft_hdr_merge(pTHX_ AV *av, SV *other) {
117 AV *ov, *kept;
118 HV *repl;
119 SSize_t on, n, i;
120 8 ▶ if (!SvOK(other)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(other)) return; 0 8 8
121 8 ov = newAV();
122 8 repl = newHV();
123 8 kept = newAV();
124 8 sv_2mortal((SV *)ov);
125 8 sv_2mortal((SV *)repl);
126 8 ft_hdr_pairs_into(aTHX_ ov, other);
127 8 on = av_len(ov) + 1;
128 23 ▶ for (i = 0; i + 1 < on; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < on; i += 2) { 15 8 23
129 15 SV **k = av_fetch(ov, i, 0);
130 15 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 15 0 15
if (k && *k) { 15 0 15
131 STRLEN kl;
132 15 const char *ks = SvPV_const(*k, kl);
133 15 char *lc = ft_lc_dup(ks, kl);
134 15 (void)hv_store(repl, lc, (I32)kl, &PL_sv_yes, 0);
135 15 free(lc);
136 }
137 }
138 8 n = av_len(av) + 1;
139 10 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 2 8 10
140 2 SV **k = av_fetch(av, i, 0);
141 2 int drop = 0;
142 2 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 2 0 2
if (k && *k) { 2 0 2
143 STRLEN kl;
144 2 const char *ks = SvPV_const(*k, kl);
145 2 char *lc = ft_lc_dup(ks, kl);
146 2 drop = hv_exists(repl, lc, (I32)kl);
147 2 free(lc);
148 }
149 2 ▶ if (!drop) {
Branch: 1 branch
BranchTrueFalseTotal
if (!drop) { 1 1 2
150 1 SV **v = av_fetch(av, i + 1, 0);
151 1 ▶ av_push(kept, (k && *k) ? newSVsv(*k) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(kept, (k && *k) ? newSVsv(*k) : newSV(0)); 1 0 1
av_push(kept, (k && *k) ? newSVsv(*k) : newSV(0)); 1 0 1
152 1 ▶ av_push(kept, (v && *v) ? newSVsv(*v) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(kept, (v && *v) ? newSVsv(*v) : newSV(0)); 1 0 1
av_push(kept, (v && *v) ? newSVsv(*v) : newSV(0)); 1 0 1
153 }
154 }
155 8 on = av_len(ov) + 1;
156 38 ▶ for (i = 0; i < on; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < on; i++) { 30 8 38
157 30 SV **e = av_fetch(ov, i, 0);
158 30 ▶ av_push(kept, (e && *e) ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(kept, (e && *e) ? newSVsv(*e) : newSV(0)); 30 0 30
av_push(kept, (e && *e) ? newSVsv(*e) : newSV(0)); 30 0 30
159 }
160 8 av_clear(av);
161 8 n = av_len(kept) + 1;
162 40 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 32 8 40
163 32 SV **e = av_fetch(kept, i, 0);
164 32 ▶ av_push(av, (e && *e) ? SvREFCNT_inc(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(av, (e && *e) ? SvREFCNT_inc(*e) : newSV(0)); 32 0 32
av_push(av, (e && *e) ? SvREFCNT_inc(*e) : newSV(0)); 32 0 32
165 }
166 8 SvREFCNT_dec((SV *)kept);
167 }
168  
169 #endif /* FT_HEADERS_H */
« xs/loop.xs ↑ Summary include/fetch/ft_obs.h »