include/frx/frx_uri.h

filter: Statementstmt 97.3% 108 / 111 Branchbran 81.1% 146 / 180 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.2% 254 / 291 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_arena.h ↑ Summary include/frx/frx_reader.h »
1 #ifndef FRX_URI_H
2 #define FRX_URI_H
3  
4 /* URI references: the parts, remove-dot-segments and the join of RFC 3986
5 * section 5.2, in two forms that share one body.
6 *
7 * The plain form (c14n 0) is what resolves a system identifier against
8 * the base of the entity that declared it (XML 1.0 section 4.2.2): a
9 * relative reference joined to an absolute base, dot segments removed as
10 * section 5.2.4 says, so a resolver sees one absolute identifier per
11 * distinct target and the fetch cache keys on it.
12 *
13 * The Canonical XML 1.1 form (c14n 1) is what section 2.4 of that
14 * Recommendation specifies for xml:base, which is the RFC's join with two
15 * stated deviations: a base ending in ".." is treated as "../", and a
16 * ".." that has nothing to remove in a relative path is kept rather than
17 * dropped. Those two live behind the flag and nowhere else, so lifting
18 * the code out of the canonicaliser moved no byte of its output; t/17's
19 * xml:base cases are the guard.
20 *
21 * Needs frx_buf.h. */
22  
23 /* the parts of a URI reference: scheme "x:", authority "//y", path, query */
24 typedef struct frx_uri {
25 const char *scheme; size_t slen; /* without the colon; NULL when absent */
26 const char *auth; size_t alen; /* without the //; NULL when absent */
27 const char *path; size_t plen;
28 const char *query; size_t qlen; /* without the ?; NULL when absent */
29 } frx_uri;
30  
31 static void
32 3778 frx_uri_parse(const char *p, size_t n, frx_uri *u)
33 {
34 3778 size_t i = 0;
35 3778 memset(u, 0, sizeof *u);
36 /* scheme: ALPHA *( ALPHA / DIGIT / + / - / . ) ":" */
37 3778 ▶ if (n && ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) {
Branch: 5 branches
BranchTrueFalseTotal
if (n && ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) { 3719 59 3778
if (n && ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) { 2514 1205 3719
if (n && ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) { 2508 6 2514
if (n && ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) { 2508 1205 3713
if (n && ((p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= 'a' && p[0] <= 'z'))) { 2508 0 2508
38 2514 size_t j = 1;
39 16175 ▶ while (j < n && ((p[j] >= 'A' && p[j] <= 'Z') || (p[j] >= 'a' && p[j] <= 'z')
Branch: 5 branches
BranchTrueFalseTotal
while (j < n && ((p[j] >= 'A' && p[j] <= 'Z') || (p[j] >= 'a' && p[j] <= 'z') 14857 1318 16175
while (j < n && ((p[j] >= 'A' && p[j] <= 'Z') || (p[j] >= 'a' && p[j] <= 'z') 10707 4150 14857
while (j < n && ((p[j] >= 'A' && p[j] <= 'Z') || (p[j] >= 'a' && p[j] <= 'z') 0 10707 10707
while (j < n && ((p[j] >= 'A' && p[j] <= 'Z') || (p[j] >= 'a' && p[j] <= 'z') 10701 4156 14857
while (j < n && ((p[j] >= 'A' && p[j] <= 'Z') || (p[j] >= 'a' && p[j] <= 'z') 10701 0 10701
40 14857 ▶ || (p[j] >= '0' && p[j] <= '9') || p[j] == '+' || p[j] == '-' || p[j] == '.')) j++;
Branch: 5 branches
BranchTrueFalseTotal
|| (p[j] >= '0' && p[j] <= '9') || p[j] == '+' || p[j] == '-' || p[j] == '.')) j++; 2822 1334 4156
|| (p[j] >= '0' && p[j] <= '9') || p[j] == '+' || p[j] == '-' || p[j] == '.')) j++; 1646 1176 2822
|| (p[j] >= '0' && p[j] <= '9') || p[j] == '+' || p[j] == '-' || p[j] == '.')) j++; 0 2510 2510
|| (p[j] >= '0' && p[j] <= '9') || p[j] == '+' || p[j] == '-' || p[j] == '.')) j++; 7 2503 2510
|| (p[j] >= '0' && p[j] <= '9') || p[j] == '+' || p[j] == '-' || p[j] == '.')) j++; 1307 1196 2503
41 2514 ▶ if (j < n && p[j] == ':') { u->scheme = p; u->slen = j; i = j + 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (j < n && p[j] == ':') { u->scheme = p; u->slen = j; i = j + 1; } 1196 1318 2514
if (j < n && p[j] == ':') { u->scheme = p; u->slen = j; i = j + 1; } 1170 26 1196
42 }
43 3778 ▶ if (n - i >= 2 && p[i] == '/' && p[i + 1] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (n - i >= 2 && p[i] == '/' && p[i + 1] == '/') { 3715 63 3778
if (n - i >= 2 && p[i] == '/' && p[i + 1] == '/') { 2045 1670 3715
if (n - i >= 2 && p[i] == '/' && p[i + 1] == '/') { 1170 875 2045
44 1170 size_t j = i + 2;
45 7060 ▶ while (j < n && p[j] != '/' && p[j] != '?' && p[j] != '#') j++;
Branch: 4 branches
BranchTrueFalseTotal
while (j < n && p[j] != '/' && p[j] != '?' && p[j] != '#') j++; 7059 1 7060
while (j < n && p[j] != '/' && p[j] != '?' && p[j] != '#') j++; 5890 1169 7059
while (j < n && p[j] != '/' && p[j] != '?' && p[j] != '#') j++; 5890 0 5890
while (j < n && p[j] != '/' && p[j] != '?' && p[j] != '#') j++; 5890 0 5890
46 1170 u->auth = p + i + 2; u->alen = j - (i + 2); i = j;
47 }
48 {
49 3778 size_t j = i;
50 108761 ▶ while (j < n && p[j] != '?' && p[j] != '#') j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j < n && p[j] != '?' && p[j] != '#') j++; 104994 3767 108761
while (j < n && p[j] != '?' && p[j] != '#') j++; 104985 9 104994
while (j < n && p[j] != '?' && p[j] != '#') j++; 104983 2 104985
51 3778 u->path = p + i; u->plen = j - i; i = j;
52 }
53 3778 ▶ if (i < n && p[i] == '?') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < n && p[i] == '?') { 11 3767 3778
if (i < n && p[i] == '?') { 9 2 11
54 9 size_t j = i + 1;
55 18 ▶ while (j < n && p[j] != '#') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < n && p[j] != '#') j++; 9 9 18
while (j < n && p[j] != '#') j++; 9 0 9
56 9 u->query = p + i + 1; u->qlen = j - (i + 1);
57 }
58 /* the fragment is dropped: it never reaches a resolver, and Canonical
59 * XML 1.1 section 2.4 drops it too */
60 3778 }
61  
62 /* remove-dot-segments (RFC 3986 section 5.2.4; as Canonical XML 1.1
63 * section 2.4 modifies it when c14n is set) over a path already merged;
64 * writes into out (which is reset) */
65 static int
66 1887 frx_uri_remove_dots(frx_buf *out, const char *p, size_t n, int c14n)
67 {
68 /* segments as (start, len) pairs in a growable array */
69 struct seg { const char *s; size_t n; };
70 1887 struct seg *segs = NULL;
71 1887 int nseg = 0, cap = 0;
72 1887 ▶ int absolute = n > 0 && p[0] == '/';
Branch: 2 branches
BranchTrueFalseTotal
int absolute = n > 0 && p[0] == '/'; 1886 1 1887
int absolute = n > 0 && p[0] == '/'; 1809 77 1886
73 1887 int trailing = 0;
74 1887 size_t i = absolute ? 1 : 0;
75 int k;
76  
77 1887 out->len = 0;
78 14046 ▶ while (i <= n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i <= n) { 12159 1887 14046
79 12159 size_t start = i;
80 88860 ▶ while (i < n && p[i] != '/') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && p[i] != '/') i++; 86973 1887 88860
while (i < n && p[i] != '/') i++; 76701 10272 86973
81 {
82 12159 size_t len = i - start;
83 12159 int at_end = i >= n;
84 /* a run of slashes is one slash; an empty middle segment is skipped */
85 12159 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 15 12144 12159
86 15 ▶ if (at_end && nseg) trailing = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (at_end && nseg) trailing = 1; 14 1 15
if (at_end && nseg) trailing = 1; 11 3 14
87 12144 ▶ } else if (len == 1 && p[start] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 1 && p[start] == '.') { 1802 10342 12144
} else if (len == 1 && p[start] == '.') { 0 1802 1802
88 0 ▶ if (at_end) trailing = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (at_end) trailing = 1; 0 0 0
89 12144 ▶ } else if (len == 2 && p[start] == '.' && p[start + 1] == '.') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (len == 2 && p[start] == '.' && p[start + 1] == '.') { 71 12073 12144
} else if (len == 2 && p[start] == '.' && p[start + 1] == '.') { 62 9 71
} else if (len == 2 && p[start] == '.' && p[start + 1] == '.') { 62 0 62
90 62 ▶ if (nseg && !(segs[nseg - 1].n == 2 && segs[nseg - 1].s[0] == '.' && segs[nseg - 1].s[1] == '.')) {
Branch: 4 branches
BranchTrueFalseTotal
if (nseg && !(segs[nseg - 1].n == 2 && segs[nseg - 1].s[0] == '.' && segs[nseg - 1].s[1] == '.')) { 58 4 62
if (nseg && !(segs[nseg - 1].n == 2 && segs[nseg - 1].s[0] == '.' && segs[nseg - 1].s[1] == '.')) { 3 55 58
if (nseg && !(segs[nseg - 1].n == 2 && segs[nseg - 1].s[0] == '.' && segs[nseg - 1].s[1] == '.')) { 3 0 3
if (nseg && !(segs[nseg - 1].n == 2 && segs[nseg - 1].s[0] == '.' && segs[nseg - 1].s[1] == '.')) { 0 3 3
91 55 nseg--;
92 55 ▶ if (at_end) trailing = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (at_end) trailing = 1; 0 55 55
93 7 ▶ } else if (absolute || !c14n) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (absolute || !c14n) { 7 0 7
} else if (absolute || !c14n) { 0 7 7
94 /* "/.." at the root, or a leading "../" in the RFC's
95 * form: nothing to pop, nothing kept */
96 0 ▶ if (at_end) trailing = absolute ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (at_end) trailing = absolute ? 1 : 0; 0 0 0
97 } else {
98 7 ▶ if (nseg == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (nseg == cap) { 4 3 7
99 4 ▶ int ncap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = cap ? cap * 2 : 8; 0 4 4
100 4 struct seg *ns = (struct seg *)realloc(segs, (size_t)ncap * sizeof *ns);
101 4 ▶ if (!ns) { free(segs); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) { free(segs); return 0; } 0 4 4
102 4 segs = ns; cap = ncap;
103 }
104 7 segs[nseg].s = p + start; segs[nseg].n = 2; nseg++;
105 7 ▶ if (at_end) trailing = 1; /* a trailing ".." gets "/" */
Branch: 1 branch
BranchTrueFalseTotal
if (at_end) trailing = 1; /* a trailing ".." gets "/" */ 1 6 7
106 }
107 } else {
108 12082 ▶ if (nseg == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (nseg == cap) { 2739 9343 12082
109 2739 ▶ int ncap = cap ? cap * 2 : 8;
Branch: 1 branch
BranchTrueFalseTotal
int ncap = cap ? cap * 2 : 8; 857 1882 2739
110 2739 struct seg *ns = (struct seg *)realloc(segs, (size_t)ncap * sizeof *ns);
111 2739 ▶ if (!ns) { free(segs); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) { free(segs); return 0; } 0 2739 2739
112 2739 segs = ns; cap = ncap;
113 }
114 12082 segs[nseg].s = p + start; segs[nseg].n = len; nseg++;
115 12082 trailing = 0;
116 }
117 }
118 12159 i++;
119 }
120 1887 ▶ if (absolute) frx_buf_append_ch(out, '/');
Branch: 1 branch
BranchTrueFalseTotal
if (absolute) frx_buf_append_ch(out, '/'); 1809 78 1887
121 13921 ▶ for (k = 0; k < nseg; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nseg; k++) { 12034 1887 13921
122 12034 ▶ if (k) frx_buf_append_ch(out, '/');
Branch: 1 branch
BranchTrueFalseTotal
if (k) frx_buf_append_ch(out, '/'); 10150 1884 12034
123 12034 frx_buf_append_n(out, segs[k].s, segs[k].n);
124 }
125 1887 ▶ if (trailing && nseg) frx_buf_append_ch(out, '/');
Branch: 2 branches
BranchTrueFalseTotal
if (trailing && nseg) frx_buf_append_ch(out, '/'); 12 1875 1887
if (trailing && nseg) frx_buf_append_ch(out, '/'); 12 0 12
126 1887 free(segs);
127 1887 return !out->failed;
128 }
129  
130 /* the join of section 5.2 into out; 0 on allocation failure */
131 static int
132 1889 frx_uri_join(frx_buf *out, const char *base, size_t blen, const char *ref, size_t rlen, int c14n)
133 {
134 frx_uri b, r, t;
135 frx_buf merged, path;
136 frx_buf bfix;
137 1889 int ok = 1;
138  
139 1889 frx_buf_init(&merged);
140 1889 frx_buf_init(&path);
141 1889 frx_buf_init(&bfix);
142 1889 out->len = 0;
143  
144 /* Canonical XML 1.1's 5.2.1: a trailing ".." on the base becomes "../" */
145 1889 frx_buf_append_n(&bfix, base, blen);
146 1889 ▶ if (c14n && blen >= 2 && base[blen - 1] == '.' && base[blen - 2] == '.'
Branch: 4 branches
BranchTrueFalseTotal
if (c14n && blen >= 2 && base[blen - 1] == '.' && base[blen - 2] == '.' 19 1870 1889
if (c14n && blen >= 2 && base[blen - 1] == '.' && base[blen - 2] == '.' 19 0 19
if (c14n && blen >= 2 && base[blen - 1] == '.' && base[blen - 2] == '.' 3 16 19
if (c14n && blen >= 2 && base[blen - 1] == '.' && base[blen - 2] == '.' 3 0 3
147 3 ▶ && (blen == 2 || base[blen - 3] == '/'))
Branch: 2 branches
BranchTrueFalseTotal
&& (blen == 2 || base[blen - 3] == '/')) 0 3 3
&& (blen == 2 || base[blen - 3] == '/')) 0 0 0
148 3 frx_buf_append_ch(&bfix, '/');
149 1889 ▶ frx_uri_parse(bfix.p ? bfix.p : "", bfix.len, &b);
Branch: 1 branch
BranchTrueFalseTotal
frx_uri_parse(bfix.p ? bfix.p : "", bfix.len, &b); 1889 0 1889
150 1889 frx_uri_parse(ref, rlen, &r);
151 1889 memset(&t, 0, sizeof t);
152  
153 /* 5.2.2 */
154 1889 ▶ if (r.scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.scheme) { 224 1665 1889
155 224 t = r;
156 224 ok = frx_uri_remove_dots(&path, r.path, r.plen, c14n);
157 } else {
158 1665 t.scheme = b.scheme; t.slen = b.slen;
159 1665 ▶ if (r.auth) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.auth) { 1 1664 1665
160 1 t.auth = r.auth; t.alen = r.alen;
161 1 t.query = r.query; t.qlen = r.qlen;
162 1 ok = frx_uri_remove_dots(&path, r.path, r.plen, c14n);
163 } else {
164 1664 t.auth = b.auth; t.alen = b.alen;
165 1664 ▶ if (r.plen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r.plen == 0) { 2 1662 1664
166 2 frx_buf_append_n(&path, b.path, b.plen);
167 2 ▶ if (r.query) { t.query = r.query; t.qlen = r.qlen; }
Branch: 1 branch
BranchTrueFalseTotal
if (r.query) { t.query = r.query; t.qlen = r.qlen; } 1 1 2
168 1 else { t.query = b.query; t.qlen = b.qlen; }
169 } else {
170 1662 t.query = r.query; t.qlen = r.qlen;
171 1662 ▶ if (r.path[0] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (r.path[0] == '/') { 8 1654 1662
172 8 ok = frx_uri_remove_dots(&path, r.path, r.plen, c14n);
173 } else {
174 /* 5.2.3 merge */
175 1654 ▶ if (b.auth && b.plen == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (b.auth && b.plen == 0) { 715 939 1654
if (b.auth && b.plen == 0) { 0 715 715
176 0 frx_buf_append_ch(&merged, '/');
177 } else {
178 1654 size_t k = b.plen;
179 15616 ▶ while (k > 0 && b.path[k - 1] != '/') k--;
Branch: 2 branches
BranchTrueFalseTotal
while (k > 0 && b.path[k - 1] != '/') k--; 15551 65 15616
while (k > 0 && b.path[k - 1] != '/') k--; 13962 1589 15551
180 1654 frx_buf_append_n(&merged, b.path, k);
181 }
182 1654 frx_buf_append_n(&merged, r.path, r.plen);
183 3308 ▶ ok = ok && !merged.failed
Branch: 1 branch
BranchTrueFalseTotal
ok = ok && !merged.failed 1654 0 1654
184 3308 ▶ && frx_uri_remove_dots(&path, merged.p ? merged.p : "", merged.len, c14n);
Branch: 3 branches
BranchTrueFalseTotal
&& frx_uri_remove_dots(&path, merged.p ? merged.p : "", merged.len, c14n); 1654 0 1654
&& frx_uri_remove_dots(&path, merged.p ? merged.p : "", merged.len, c14n); 1654 0 1654
&& frx_uri_remove_dots(&path, merged.p ? merged.p : "", merged.len, c14n); 1654 0 1654
185 }
186 }
187 }
188 }
189  
190 1889 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 1889 0 1889
191 1889 ▶ if (t.scheme) { frx_buf_append_n(out, t.scheme, t.slen); frx_buf_append_ch(out, ':'); }
Branch: 1 branch
BranchTrueFalseTotal
if (t.scheme) { frx_buf_append_n(out, t.scheme, t.slen); frx_buf_append_ch(out, ':'); } 949 940 1889
192 1889 ▶ if (t.auth) { frx_buf_append(out, "//"); frx_buf_append_n(out, t.auth, t.alen); }
Branch: 1 branch
BranchTrueFalseTotal
if (t.auth) { frx_buf_append(out, "//"); frx_buf_append_n(out, t.auth, t.alen); } 948 941 1889
193 1889 ▶ frx_buf_append_n(out, path.p ? path.p : "", path.len);
Branch: 1 branch
BranchTrueFalseTotal
frx_buf_append_n(out, path.p ? path.p : "", path.len); 1886 3 1889
194 1889 ▶ if (t.query) { frx_buf_append_ch(out, '?'); frx_buf_append_n(out, t.query, t.qlen); }
Branch: 1 branch
BranchTrueFalseTotal
if (t.query) { frx_buf_append_ch(out, '?'); frx_buf_append_n(out, t.query, t.qlen); } 2 1887 1889
195 1889 ok = !out->failed;
196 }
197 1889 frx_buf_free(&merged);
198 1889 frx_buf_free(&path);
199 1889 frx_buf_free(&bfix);
200 1889 return ok;
201 }
202  
203 #endif /* FRX_URI_H */
« include/frx/frx_arena.h ↑ Summary include/frx/frx_reader.h »