include/fetch/ft_altsvc.h

filter: Statementstmt 10.5% 10 / 95 Branchbran 6.2% 8 / 128 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 8.0% 18 / 223 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_h3.h ↑ Summary xs/cookiejar.xs »
1 #ifndef FT_ALTSVC_H
2 #define FT_ALTSVC_H
3  
4 /* ft_altsvc.h - how a client ever finds out HTTP/3 exists (RFC 7838).
5 *
6 * There is no h3:// scheme and no upgrade handshake. Every https:// URL
7 * starts on TCP, and the ONLY thing that says the origin also answers over
8 * QUIC is an Alt-Svc response header on a reply that has already come back
9 * over HTTP/1.1 or HTTP/2:
10 *
11 * Alt-Svc: h3=":443"; ma=86400
12 *
13 * So without this file the h3 client in ft_h3.h is unreachable from an
14 * ordinary $ua->get - which is why the plan called it out as easy to leave
15 * untested and why it is a header parser rather than anything to do with QUIC.
16 *
17 * The cache is per PROCESS rather than per UA, because it describes the
18 * origin and not the client: two agents talking to the same server should not
19 * each have to discover it. It is small, linear and bounded - a client talks
20 * to a handful of origins, and an entry is 3 pointers and a time.
21 *
22 * NOT cached across processes. RFC 7838 permits persisting it; doing so would
23 * mean a file somewhere, a format to version, and a stale entry surviving a
24 * server that turned h3 off. A first connection per process paying one
25 * round-trip to find out is the cheaper mistake.
26 */
27  
28 #include "ft_win.h" /* ft_monotonic: Windows has no clock_gettime */
29  
30 #define FT_ALTSVC_MAX 32
31  
32 typedef struct ft_altsvc {
33 char *origin; /* "host:port" as requested */
34 char *host; /* where h3 actually is (usually the same) */
35 int port;
36 double expires; /* monotonic seconds; 0 = free slot */
37 } ft_altsvc;
38  
39 static ft_altsvc ft_altsvc_tab[FT_ALTSVC_MAX];
40  
41 #define ft_altsvc_now() ft_monotonic()
42  
43 0 static void ft_altsvc_clear_slot(ft_altsvc *e) {
44 0 free(e->origin); free(e->host);
45 0 e->origin = e->host = NULL;
46 0 e->expires = 0;
47 0 }
48  
49 /* The live entry for this origin, or NULL. An expired one is cleared as it is
50 * found: nothing else sweeps the table, and a client that stops talking to an
51 * origin should not keep its row for ever. */
52 0 static ft_altsvc *ft_altsvc_get(const char *origin) {
53 0 double now = ft_altsvc_now();
54 int i;
55 0 ▶ for (i = 0; i < FT_ALTSVC_MAX; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < FT_ALTSVC_MAX; i++) { 0 0 0
56 0 ft_altsvc *e = &ft_altsvc_tab[i];
57 0 ▶ if (!e->origin || strcmp(e->origin, origin) != 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e->origin || strcmp(e->origin, origin) != 0) continue; 0 0 0
if (!e->origin || strcmp(e->origin, origin) != 0) continue; 0 0 0
58 0 ▶ if (e->expires <= now) { ft_altsvc_clear_slot(e); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (e->expires <= now) { ft_altsvc_clear_slot(e); return NULL; } 0 0 0
59 0 return e;
60 }
61 0 return NULL;
62 }
63  
64 0 static void ft_altsvc_put(const char *origin, const char *host, int port,
65 double ma) {
66 0 int i, victim = -1;
67 0 double oldest = 0;
68 0 double now = ft_altsvc_now();
69 0 ▶ for (i = 0; i < FT_ALTSVC_MAX; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < FT_ALTSVC_MAX; i++) { 0 0 0
70 0 ft_altsvc *e = &ft_altsvc_tab[i];
71 0 ▶ if (e->origin && strcmp(e->origin, origin) == 0) { victim = i; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (e->origin && strcmp(e->origin, origin) == 0) { victim = i; break; } 0 0 0
if (e->origin && strcmp(e->origin, origin) == 0) { victim = i; break; } 0 0 0
72 0 ▶ if (!e->origin) { victim = i; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e->origin) { victim = i; break; } 0 0 0
73 0 ▶ if (oldest == 0 || e->expires < oldest) { oldest = e->expires; victim = i; }
Branch: 2 branches
BranchTrueFalseTotal
if (oldest == 0 || e->expires < oldest) { oldest = e->expires; victim = i; } 0 0 0
if (oldest == 0 || e->expires < oldest) { oldest = e->expires; victim = i; } 0 0 0
74 }
75 0 ▶ if (victim < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (victim < 0) return; 0 0 0
76 0 ft_altsvc_clear_slot(&ft_altsvc_tab[victim]);
77 0 ft_altsvc_tab[victim].origin = ft_strdup_plain(origin);
78 0 ft_altsvc_tab[victim].host = ft_strdup_plain(host);
79 0 ft_altsvc_tab[victim].port = port;
80 0 ft_altsvc_tab[victim].expires = now + ma;
81 0 ▶ if (!ft_altsvc_tab[victim].origin || !ft_altsvc_tab[victim].host)
Branch: 2 branches
BranchTrueFalseTotal
if (!ft_altsvc_tab[victim].origin || !ft_altsvc_tab[victim].host) 0 0 0
if (!ft_altsvc_tab[victim].origin || !ft_altsvc_tab[victim].host) 0 0 0
82 0 ft_altsvc_clear_slot(&ft_altsvc_tab[victim]);
83 }
84  
85 0 static void ft_altsvc_forget(const char *origin) {
86 int i;
87 0 ▶ for (i = 0; i < FT_ALTSVC_MAX; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < FT_ALTSVC_MAX; i++) 0 0 0
88 0 ▶ if (ft_altsvc_tab[i].origin
Branch: 1 branch
BranchTrueFalseTotal
if (ft_altsvc_tab[i].origin 0 0 0
89 0 ▶ && strcmp(ft_altsvc_tab[i].origin, origin) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& strcmp(ft_altsvc_tab[i].origin, origin) == 0) 0 0 0
90 0 ft_altsvc_clear_slot(&ft_altsvc_tab[i]);
91 0 }
92  
93 /* Parse one Alt-Svc field value and record what it advertises.
94 *
95 * Alt-Svc: h3=":443"; ma=86400, h3-29=":443"; ma=3600
96 * Alt-Svc: clear
97 *
98 * Only "h3" is taken. The drafts (h3-29 and friends) name wire formats this
99 * client does not implement, and treating them as h3 would mean handshaking
100 * with a server that speaks something else - the failure would look like a
101 * network fault rather than a version mismatch.
102 *
103 * An empty authority ("h3=\":443\"") means the same host on that port, which
104 * is what a server almost always sends; a full one ("h3=\"other:443\"")
105 * points elsewhere and is honoured as written. `ma` defaults to 24 hours per
106 * RFC 7838; `clear` drops what is known. */
107 0 static void ft_altsvc_parse(const char *origin, const char *host,
108 const char *val, STRLEN len) {
109 0 STRLEN i = 0;
110 0 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 0 0 0
111 STRLEN start, eq;
112 char althost[256];
113 0 int altport = 0;
114 0 double ma = 86400.0;
115 0 int is_h3 = 0;
116  
117 0 ▶ while (i < len && (val[i] == ' ' || val[i] == '\t' || val[i] == ','))
Branch: 4 branches
BranchTrueFalseTotal
while (i < len && (val[i] == ' ' || val[i] == '\t' || val[i] == ',')) 0 0 0
while (i < len && (val[i] == ' ' || val[i] == '\t' || val[i] == ',')) 0 0 0
while (i < len && (val[i] == ' ' || val[i] == '\t' || val[i] == ',')) 0 0 0
while (i < len && (val[i] == ' ' || val[i] == '\t' || val[i] == ',')) 0 0 0
118 0 i++;
119 0 ▶ if (i >= len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) break; 0 0 0
120 0 start = i;
121 0 ▶ while (i < len && val[i] != '=' && val[i] != ',' && val[i] != ';') i++;
Branch: 4 branches
BranchTrueFalseTotal
while (i < len && val[i] != '=' && val[i] != ',' && val[i] != ';') i++; 0 0 0
while (i < len && val[i] != '=' && val[i] != ',' && val[i] != ';') i++; 0 0 0
while (i < len && val[i] != '=' && val[i] != ',' && val[i] != ';') i++; 0 0 0
while (i < len && val[i] != '=' && val[i] != ',' && val[i] != ';') i++; 0 0 0
122  
123 0 ▶ if (i - start == 5 && strncasecmp(val + start, "clear", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (i - start == 5 && strncasecmp(val + start, "clear", 5) == 0) { 0 0 0
if (i - start == 5 && strncasecmp(val + start, "clear", 5) == 0) { 0 0 0
124 0 ft_altsvc_forget(origin);
125 0 return;
126 }
127 0 ▶ is_h3 = (i - start == 2 && strncasecmp(val + start, "h3", 2) == 0);
Branch: 2 branches
BranchTrueFalseTotal
is_h3 = (i - start == 2 && strncasecmp(val + start, "h3", 2) == 0); 0 0 0
is_h3 = (i - start == 2 && strncasecmp(val + start, "h3", 2) == 0); 0 0 0
128 0 ▶ if (i >= len || val[i] != '=') { /* no value: skip the entry */
Branch: 2 branches
BranchTrueFalseTotal
if (i >= len || val[i] != '=') { /* no value: skip the entry */ 0 0 0
if (i >= len || val[i] != '=') { /* no value: skip the entry */ 0 0 0
129 0 ▶ while (i < len && val[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && val[i] != ',') i++; 0 0 0
while (i < len && val[i] != ',') i++; 0 0 0
130 0 continue;
131 }
132 0 eq = ++i;
133 /* the quoted alt-authority: [host]:port */
134 {
135 0 STRLEN a = eq, aend;
136 0 ▶ if (a < len && val[a] == '"') a++;
Branch: 2 branches
BranchTrueFalseTotal
if (a < len && val[a] == '"') a++; 0 0 0
if (a < len && val[a] == '"') a++; 0 0 0
137 0 aend = a;
138 0 ▶ while (aend < len && val[aend] != '"') aend++;
Branch: 2 branches
BranchTrueFalseTotal
while (aend < len && val[aend] != '"') aend++; 0 0 0
while (aend < len && val[aend] != '"') aend++; 0 0 0
139 {
140 0 STRLEN colon = aend;
141 STRLEN k;
142 0 ▶ for (k = a; k < aend; k++) if (val[k] == ':') colon = k;
Branch: 2 branches
BranchTrueFalseTotal
for (k = a; k < aend; k++) if (val[k] == ':') colon = k; 0 0 0
for (k = a; k < aend; k++) if (val[k] == ':') colon = k; 0 0 0
143 0 ▶ if (colon > a && colon - a < sizeof althost) {
Branch: 2 branches
BranchTrueFalseTotal
if (colon > a && colon - a < sizeof althost) { 0 0 0
if (colon > a && colon - a < sizeof althost) { 0 0 0
144 0 memcpy(althost, val + a, colon - a);
145 0 althost[colon - a] = '\0';
146 } else {
147 /* ":443" - the same host, a different port */
148 0 size_t hl = strlen(host);
149 0 ▶ if (hl >= sizeof althost) hl = sizeof althost - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (hl >= sizeof althost) hl = sizeof althost - 1; 0 0 0
150 0 memcpy(althost, host, hl);
151 0 althost[hl] = '\0';
152 }
153 0 ▶ if (colon < aend) altport = (int)strtol(val + colon + 1, NULL, 10);
Branch: 1 branch
BranchTrueFalseTotal
if (colon < aend) altport = (int)strtol(val + colon + 1, NULL, 10); 0 0 0
154 }
155 0 ▶ i = aend < len ? aend + 1 : len;
Branch: 1 branch
BranchTrueFalseTotal
i = aend < len ? aend + 1 : len; 0 0 0
156 }
157 /* the parameters, of which only ma is read */
158 0 ▶ while (i < len && val[i] != ',') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < len && val[i] != ',') { 0 0 0
while (i < len && val[i] != ',') { 0 0 0
159 0 ▶ if (val[i] == ';') {
Branch: 1 branch
BranchTrueFalseTotal
if (val[i] == ';') { 0 0 0
160 0 STRLEN p = ++i;
161 0 ▶ while (p < len && (val[p] == ' ' || val[p] == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < len && (val[p] == ' ' || val[p] == '\t')) p++; 0 0 0
while (p < len && (val[p] == ' ' || val[p] == '\t')) p++; 0 0 0
while (p < len && (val[p] == ' ' || val[p] == '\t')) p++; 0 0 0
162 0 ▶ if (p + 3 <= len && strncasecmp(val + p, "ma=", 3) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (p + 3 <= len && strncasecmp(val + p, "ma=", 3) == 0) 0 0 0
if (p + 3 <= len && strncasecmp(val + p, "ma=", 3) == 0) 0 0 0
163 0 ma = strtod(val + p + 3, NULL);
164 0 i = p;
165 }
166 0 i++;
167 }
168 0 ▶ if (is_h3 && altport > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_h3 && altport > 0) { 0 0 0
if (is_h3 && altport > 0) { 0 0 0
169 /* ma=0 is an explicit "stop using it" and not a fresh entry */
170 0 ▶ if (ma <= 0) ft_altsvc_forget(origin);
Branch: 1 branch
BranchTrueFalseTotal
if (ma <= 0) ft_altsvc_forget(origin); 0 0 0
171 0 else ft_altsvc_put(origin, althost, altport, ma);
172 0 return;
173 }
174 }
175 }
176  
177 /* Look for Alt-Svc in a response header list and record what it says. Called
178 * for every response that came back over TCP, which is where the header can
179 * appear - a server has no reason to advertise QUIC on a QUIC connection. */
180 6 static void ft_altsvc_note(pTHX_ const char *host, int port, AV *headers) {
181 char origin[300];
182 SSize_t i, n;
183 6 ▶ if (!headers) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!headers) return; 0 6 6
184 6 n = av_len(headers) + 1;
185 6 snprintf(origin, sizeof origin, "%s:%d", host, port);
186 24 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 18 6 24
187 18 SV **k = av_fetch(headers, i, 0);
188 18 SV **v = av_fetch(headers, i + 1, 0);
189 STRLEN kl, vl;
190 const char *ks, *vs;
191 36 ▶ if (!(k && *k && v && *v)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(k && *k && v && *v)) continue; 18 0 18
if (!(k && *k && v && *v)) continue; 18 0 18
if (!(k && *k && v && *v)) continue; 18 0 18
if (!(k && *k && v && *v)) continue; 0 18 18
192 18 ks = SvPV_const(*k, kl);
193 18 ▶ if (kl != 7 || strncasecmp(ks, "alt-svc", 7) != 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kl != 7 || strncasecmp(ks, "alt-svc", 7) != 0) continue; 0 18 18
if (kl != 7 || strncasecmp(ks, "alt-svc", 7) != 0) continue; 0 0 0
194 0 vs = SvPV_const(*v, vl);
195 0 ft_altsvc_parse(origin, host, vs, vl);
196 0 return;
197 }
198 }
199  
200 #endif /* FT_ALTSVC_H */
« include/fetch/ft_h3.h ↑ Summary xs/cookiejar.xs »