include/fetch/ft_headers.h

filter: Statementstmt 84.3% 97 / 115 Branchbran 50.8% 61 / 120 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.2% 158 / 235 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_win.h ↑ Summary xs/abi.xs »
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 337 static int ft_ci_eq(const char *a, STRLEN al, const char *b, STRLEN bl) {
16 STRLEN i;
17 337 ▶ if (al != bl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (al != bl) return 0; 281 56 337
18 306 ▶ for (i = 0; i < al; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < al; i++) 269 37 306
19 269 ▶ 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])) 19 250 269
20 19 return 0;
21 37 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 367 static SSize_t ft_hdr_find(pTHX_ AV *av, const char *name, STRLEN nl) {
27 367 SSize_t n = av_len(av) + 1, i;
28 628 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 284 344 628
29 284 SV **k = av_fetch(av, i, 0);
30 284 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 284 0 284
if (k && *k) { 284 0 284
31 STRLEN kl;
32 284 const char *ks = SvPV_const(*k, kl);
33 284 ▶ if (ft_ci_eq(ks, kl, name, nl)) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (ft_ci_eq(ks, kl, name, nl)) return i; 23 261 284
34 }
35 }
36 344 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 343 static int ft_hdr_exists(pTHX_ AV *av, const char *name, STRLEN nl) {
73 343 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 176 static void ft_hdr_pairs_into(pTHX_ AV *dst, SV *src) {
80 SV *rv;
81 176 ▶ if (!src || !SvOK(src)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!src || !SvOK(src)) return; 176 0 176
if (!src || !SvOK(src)) return; 0 176 176
82 176 ▶ if (!SvROK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(src)) 0 176 176
83 0 croak("Fetch::Headers: cannot build headers from 'scalar'");
84 176 rv = SvRV(src);
85 176 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 169 7 176
86 169 AV *s = (AV *)rv;
87 169 SSize_t n = av_len(s) + 1, i;
88 179 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 169 179
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 /* Is "trailers" one of the t-codings in a TE value? Each comma-separated
101 * member is a token with optional ;parameters and surrounding blanks, so the
102 * token is compared whole and without regard to case. A longer token that
103 * happens to contain the word does not count. */
104 0 static int ft_te_lists_trailers(const char *v, STRLEN vl) {
105 0 STRLEN i = 0;
106 0 ▶ while (i < vl) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < vl) { 0 0 0
107 STRLEN start;
108 0 ▶ while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
while (i < vl && (v[i] == ' ' || v[i] == '\t' || v[i] == ',')) i++; 0 0 0
109 0 start = i;
110 0 ▶ while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++;
Branch: 5 branches
BranchTrueFalseTotal
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
while (i < vl && v[i] != ',' && v[i] != ';' && v[i] != ' ' && v[i] != '\t') i++; 0 0 0
111 0 ▶ if (i - start == 8 && strncasecmp(v + start, "trailers", 8) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i - start == 8 && strncasecmp(v + start, "trailers", 8) == 0) return 1; 0 0 0
if (i - start == 8 && strncasecmp(v + start, "trailers", 8) == 0) return 1; 0 0 0
112 0 ▶ while (i < vl && v[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && v[i] != ',') i++; 0 0 0
while (i < vl && v[i] != ',') i++; 0 0 0
113 }
114 0 return 0;
115 }
116  
117 /* Request header names that never travel over HTTP/2 or HTTP/3 (RFC 9113
118 * 8.2.2, RFC 9114 4.2): the connection-specific fields, which describe a
119 * connection the request does not own there, plus host and content-length,
120 * which :authority and the framing carry instead. TE is handled on its own
121 * by the caller, since it is allowed with one value. */
122 7 static int ft_h2_omits_header(const char *k, STRLEN kl) {
123 7 switch (kl) {
124 0 case 4: return strncasecmp(k, "host", 4) == 0;
125 0 case 7: return strncasecmp(k, "upgrade", 7) == 0;
126 7 case 10: return strncasecmp(k, "connection", 10) == 0
127 7 ▶ || strncasecmp(k, "keep-alive", 10) == 0;
Branch: 2 branches
BranchTrueFalseTotal
|| strncasecmp(k, "keep-alive", 10) == 0; 7 0 7
|| strncasecmp(k, "keep-alive", 10) == 0; 0 7 7
128 0 case 14: return strncasecmp(k, "content-length", 14) == 0;
129 0 case 16: return strncasecmp(k, "proxy-connection", 16) == 0;
130 0 case 17: return strncasecmp(k, "transfer-encoding", 17) == 0;
131 0 default: return 0;
132 }
133 }
134  
135 /* lowercase a name into a freshly malloc'd buffer (caller frees) */
136 218 static char *ft_lc_dup(const char *s, STRLEN n) {
137 218 char *r = (char *)malloc(n + 1); /* +1: NUL-terminate so callers that
138 * treat the result as a C string (the
139 * URL scheme) do not read past it */
140 STRLEN i;
141 218 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 218 0 218
142 1184 ▶ 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]); 966 218 1184
143 218 r[n] = '\0';
144 }
145 218 return r;
146 }
147  
148 /* Overlay $other on the flat [k,v,...] list $av in place: each field name it
149 * carries replaces all of $av's values for that name; names present only in
150 * $av are kept, in order; the overlay is appended. */
151 8 static void ft_hdr_merge(pTHX_ AV *av, SV *other) {
152 AV *ov, *kept;
153 HV *repl;
154 SSize_t on, n, i;
155 8 ▶ if (!SvOK(other)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(other)) return; 0 8 8
156 8 ov = newAV();
157 8 repl = newHV();
158 8 kept = newAV();
159 8 sv_2mortal((SV *)ov);
160 8 sv_2mortal((SV *)repl);
161 8 ft_hdr_pairs_into(aTHX_ ov, other);
162 8 on = av_len(ov) + 1;
163 23 ▶ for (i = 0; i + 1 < on; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < on; i += 2) { 15 8 23
164 15 SV **k = av_fetch(ov, i, 0);
165 15 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 15 0 15
if (k && *k) { 15 0 15
166 STRLEN kl;
167 15 const char *ks = SvPV_const(*k, kl);
168 15 char *lc = ft_lc_dup(ks, kl);
169 15 (void)hv_store(repl, lc, (I32)kl, &PL_sv_yes, 0);
170 15 free(lc);
171 }
172 }
173 8 n = av_len(av) + 1;
174 10 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 2 8 10
175 2 SV **k = av_fetch(av, i, 0);
176 2 int drop = 0;
177 2 ▶ if (k && *k) {
Branch: 2 branches
BranchTrueFalseTotal
if (k && *k) { 2 0 2
if (k && *k) { 2 0 2
178 STRLEN kl;
179 2 const char *ks = SvPV_const(*k, kl);
180 2 char *lc = ft_lc_dup(ks, kl);
181 2 drop = hv_exists(repl, lc, (I32)kl);
182 2 free(lc);
183 }
184 2 ▶ if (!drop) {
Branch: 1 branch
BranchTrueFalseTotal
if (!drop) { 1 1 2
185 1 SV **v = av_fetch(av, i + 1, 0);
186 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
187 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
188 }
189 }
190 8 on = av_len(ov) + 1;
191 38 ▶ for (i = 0; i < on; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < on; i++) { 30 8 38
192 30 SV **e = av_fetch(ov, i, 0);
193 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
194 }
195 8 av_clear(av);
196 8 n = av_len(kept) + 1;
197 40 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 32 8 40
198 32 SV **e = av_fetch(kept, i, 0);
199 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
200 }
201 8 SvREFCNT_dec((SV *)kept);
202 }
203  
204 #endif /* FT_HEADERS_H */
« include/fetch/ft_win.h ↑ Summary xs/abi.xs »