Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | /* |
25 | | * The Alt-Svc: header is defined in RFC 7838: |
26 | | * https://datatracker.ietf.org/doc/html/rfc7838 |
27 | | */ |
28 | | #include "curl_setup.h" |
29 | | |
30 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_ALTSVC) |
31 | | #include <curl/curl.h> |
32 | | #include "urldata.h" |
33 | | #include "altsvc.h" |
34 | | #include "curl_get_line.h" |
35 | | #include "strcase.h" |
36 | | #include "parsedate.h" |
37 | | #include "sendf.h" |
38 | | #include "warnless.h" |
39 | | #include "fopen.h" |
40 | | #include "rename.h" |
41 | | #include "strdup.h" |
42 | | #include "inet_pton.h" |
43 | | |
44 | | /* The last 3 #include files should be in this order */ |
45 | | #include "curl_printf.h" |
46 | | #include "curl_memory.h" |
47 | | #include "memdebug.h" |
48 | | |
49 | 705 | #define MAX_ALTSVC_LINE 4095 |
50 | | #define MAX_ALTSVC_DATELENSTR "64" |
51 | | #define MAX_ALTSVC_DATELEN 64 |
52 | | #define MAX_ALTSVC_HOSTLENSTR "512" |
53 | 0 | #define MAX_ALTSVC_HOSTLEN 512 |
54 | | #define MAX_ALTSVC_ALPNLENSTR "10" |
55 | | #define MAX_ALTSVC_ALPNLEN 10 |
56 | | |
57 | 0 | #define H3VERSION "h3" |
58 | | |
59 | | static enum alpnid alpn2alpnid(char *name) |
60 | 0 | { |
61 | 0 | if(strcasecompare(name, "h1")) |
62 | 0 | return ALPN_h1; |
63 | 0 | if(strcasecompare(name, "h2")) |
64 | 0 | return ALPN_h2; |
65 | 0 | if(strcasecompare(name, H3VERSION)) |
66 | 0 | return ALPN_h3; |
67 | 0 | return ALPN_none; /* unknown, probably rubbish input */ |
68 | 0 | } |
69 | | |
70 | | /* Given the ALPN ID, return the name */ |
71 | | const char *Curl_alpnid2str(enum alpnid id) |
72 | 0 | { |
73 | 0 | switch(id) { |
74 | 0 | case ALPN_h1: |
75 | 0 | return "h1"; |
76 | 0 | case ALPN_h2: |
77 | 0 | return "h2"; |
78 | 0 | case ALPN_h3: |
79 | 0 | return H3VERSION; |
80 | 0 | default: |
81 | 0 | return ""; /* bad */ |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | |
86 | | static void altsvc_free(struct altsvc *as) |
87 | 0 | { |
88 | 0 | free(as->src.host); |
89 | 0 | free(as->dst.host); |
90 | 0 | free(as); |
91 | 0 | } |
92 | | |
93 | | static struct altsvc *altsvc_createid(const char *srchost, |
94 | | const char *dsthost, |
95 | | enum alpnid srcalpnid, |
96 | | enum alpnid dstalpnid, |
97 | | unsigned int srcport, |
98 | | unsigned int dstport) |
99 | 0 | { |
100 | 0 | struct altsvc *as = calloc(1, sizeof(struct altsvc)); |
101 | 0 | size_t hlen; |
102 | 0 | size_t dlen; |
103 | 0 | if(!as) |
104 | 0 | return NULL; |
105 | 0 | hlen = strlen(srchost); |
106 | 0 | dlen = strlen(dsthost); |
107 | 0 | DEBUGASSERT(hlen); |
108 | 0 | DEBUGASSERT(dlen); |
109 | 0 | if(!hlen || !dlen) |
110 | | /* bad input */ |
111 | 0 | return NULL; |
112 | 0 | if((hlen > 2) && srchost[0] == '[') { |
113 | | /* IPv6 address, strip off brackets */ |
114 | 0 | srchost++; |
115 | 0 | hlen -= 2; |
116 | 0 | } |
117 | 0 | else if(srchost[hlen - 1] == '.') |
118 | | /* strip off trailing dot */ |
119 | 0 | hlen--; |
120 | 0 | if((dlen > 2) && dsthost[0] == '[') { |
121 | | /* IPv6 address, strip off brackets */ |
122 | 0 | dsthost++; |
123 | 0 | dlen -= 2; |
124 | 0 | } |
125 | |
|
126 | 0 | as->src.host = Curl_strndup(srchost, hlen); |
127 | 0 | if(!as->src.host) |
128 | 0 | goto error; |
129 | | |
130 | 0 | as->dst.host = Curl_strndup(dsthost, dlen); |
131 | 0 | if(!as->dst.host) |
132 | 0 | goto error; |
133 | | |
134 | 0 | as->src.alpnid = srcalpnid; |
135 | 0 | as->dst.alpnid = dstalpnid; |
136 | 0 | as->src.port = curlx_ultous(srcport); |
137 | 0 | as->dst.port = curlx_ultous(dstport); |
138 | |
|
139 | 0 | return as; |
140 | 0 | error: |
141 | 0 | altsvc_free(as); |
142 | 0 | return NULL; |
143 | 0 | } |
144 | | |
145 | | static struct altsvc *altsvc_create(char *srchost, |
146 | | char *dsthost, |
147 | | char *srcalpn, |
148 | | char *dstalpn, |
149 | | unsigned int srcport, |
150 | | unsigned int dstport) |
151 | 0 | { |
152 | 0 | enum alpnid dstalpnid = alpn2alpnid(dstalpn); |
153 | 0 | enum alpnid srcalpnid = alpn2alpnid(srcalpn); |
154 | 0 | if(!srcalpnid || !dstalpnid) |
155 | 0 | return NULL; |
156 | 0 | return altsvc_createid(srchost, dsthost, srcalpnid, dstalpnid, |
157 | 0 | srcport, dstport); |
158 | 0 | } |
159 | | |
160 | | /* only returns SERIOUS errors */ |
161 | | static CURLcode altsvc_add(struct altsvcinfo *asi, char *line) |
162 | 0 | { |
163 | | /* Example line: |
164 | | h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1 |
165 | | */ |
166 | 0 | char srchost[MAX_ALTSVC_HOSTLEN + 1]; |
167 | 0 | char dsthost[MAX_ALTSVC_HOSTLEN + 1]; |
168 | 0 | char srcalpn[MAX_ALTSVC_ALPNLEN + 1]; |
169 | 0 | char dstalpn[MAX_ALTSVC_ALPNLEN + 1]; |
170 | 0 | char date[MAX_ALTSVC_DATELEN + 1]; |
171 | 0 | unsigned int srcport; |
172 | 0 | unsigned int dstport; |
173 | 0 | unsigned int prio; |
174 | 0 | unsigned int persist; |
175 | 0 | int rc; |
176 | |
|
177 | 0 | rc = sscanf(line, |
178 | 0 | "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u " |
179 | 0 | "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u " |
180 | 0 | "\"%" MAX_ALTSVC_DATELENSTR "[^\"]\" %u %u", |
181 | 0 | srcalpn, srchost, &srcport, |
182 | 0 | dstalpn, dsthost, &dstport, |
183 | 0 | date, &persist, &prio); |
184 | 0 | if(9 == rc) { |
185 | 0 | struct altsvc *as; |
186 | 0 | time_t expires = Curl_getdate_capped(date); |
187 | 0 | as = altsvc_create(srchost, dsthost, srcalpn, dstalpn, srcport, dstport); |
188 | 0 | if(as) { |
189 | 0 | as->expires = expires; |
190 | 0 | as->prio = prio; |
191 | 0 | as->persist = persist ? 1 : 0; |
192 | 0 | Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node); |
193 | 0 | } |
194 | 0 | } |
195 | |
|
196 | 0 | return CURLE_OK; |
197 | 0 | } |
198 | | |
199 | | /* |
200 | | * Load alt-svc entries from the given file. The text based line-oriented file |
201 | | * format is documented here: https://curl.se/docs/alt-svc.html |
202 | | * |
203 | | * This function only returns error on major problems that prevent alt-svc |
204 | | * handling to work completely. It will ignore individual syntactical errors |
205 | | * etc. |
206 | | */ |
207 | | static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file) |
208 | 705 | { |
209 | 705 | CURLcode result = CURLE_OK; |
210 | 705 | char *line = NULL; |
211 | 705 | FILE *fp; |
212 | | |
213 | | /* we need a private copy of the file name so that the altsvc cache file |
214 | | name survives an easy handle reset */ |
215 | 705 | free(asi->filename); |
216 | 705 | asi->filename = strdup(file); |
217 | 705 | if(!asi->filename) |
218 | 0 | return CURLE_OUT_OF_MEMORY; |
219 | | |
220 | 705 | fp = fopen(file, FOPEN_READTEXT); |
221 | 705 | if(fp) { |
222 | 705 | line = malloc(MAX_ALTSVC_LINE); |
223 | 705 | if(!line) |
224 | 0 | goto fail; |
225 | 705 | while(Curl_get_line(line, MAX_ALTSVC_LINE, fp)) { |
226 | 0 | char *lineptr = line; |
227 | 0 | while(*lineptr && ISBLANK(*lineptr)) |
228 | 0 | lineptr++; |
229 | 0 | if(*lineptr == '#') |
230 | | /* skip commented lines */ |
231 | 0 | continue; |
232 | | |
233 | 0 | altsvc_add(asi, lineptr); |
234 | 0 | } |
235 | 705 | free(line); /* free the line buffer */ |
236 | 705 | fclose(fp); |
237 | 705 | } |
238 | 705 | return result; |
239 | | |
240 | 0 | fail: |
241 | 0 | Curl_safefree(asi->filename); |
242 | 0 | free(line); |
243 | 0 | fclose(fp); |
244 | 0 | return CURLE_OUT_OF_MEMORY; |
245 | 705 | } |
246 | | |
247 | | /* |
248 | | * Write this single altsvc entry to a single output line |
249 | | */ |
250 | | |
251 | | static CURLcode altsvc_out(struct altsvc *as, FILE *fp) |
252 | 0 | { |
253 | 0 | struct tm stamp; |
254 | 0 | const char *dst6_pre = ""; |
255 | 0 | const char *dst6_post = ""; |
256 | 0 | const char *src6_pre = ""; |
257 | 0 | const char *src6_post = ""; |
258 | 0 | CURLcode result = Curl_gmtime(as->expires, &stamp); |
259 | 0 | if(result) |
260 | 0 | return result; |
261 | 0 | #ifdef ENABLE_IPV6 |
262 | 0 | else { |
263 | 0 | char ipv6_unused[16]; |
264 | 0 | if(1 == Curl_inet_pton(AF_INET6, as->dst.host, ipv6_unused)) { |
265 | 0 | dst6_pre = "["; |
266 | 0 | dst6_post = "]"; |
267 | 0 | } |
268 | 0 | if(1 == Curl_inet_pton(AF_INET6, as->src.host, ipv6_unused)) { |
269 | 0 | src6_pre = "["; |
270 | 0 | src6_post = "]"; |
271 | 0 | } |
272 | 0 | } |
273 | 0 | #endif |
274 | 0 | fprintf(fp, |
275 | 0 | "%s %s%s%s %u " |
276 | 0 | "%s %s%s%s %u " |
277 | 0 | "\"%d%02d%02d " |
278 | 0 | "%02d:%02d:%02d\" " |
279 | 0 | "%u %d\n", |
280 | 0 | Curl_alpnid2str(as->src.alpnid), |
281 | 0 | src6_pre, as->src.host, src6_post, |
282 | 0 | as->src.port, |
283 | |
|
284 | 0 | Curl_alpnid2str(as->dst.alpnid), |
285 | 0 | dst6_pre, as->dst.host, dst6_post, |
286 | 0 | as->dst.port, |
287 | |
|
288 | 0 | stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday, |
289 | 0 | stamp.tm_hour, stamp.tm_min, stamp.tm_sec, |
290 | 0 | as->persist, as->prio); |
291 | 0 | return CURLE_OK; |
292 | 0 | } |
293 | | |
294 | | /* ---- library-wide functions below ---- */ |
295 | | |
296 | | /* |
297 | | * Curl_altsvc_init() creates a new altsvc cache. |
298 | | * It returns the new instance or NULL if something goes wrong. |
299 | | */ |
300 | | struct altsvcinfo *Curl_altsvc_init(void) |
301 | 705 | { |
302 | 705 | struct altsvcinfo *asi = calloc(1, sizeof(struct altsvcinfo)); |
303 | 705 | if(!asi) |
304 | 0 | return NULL; |
305 | 705 | Curl_llist_init(&asi->list, NULL); |
306 | | |
307 | | /* set default behavior */ |
308 | 705 | asi->flags = CURLALTSVC_H1 |
309 | 705 | #ifdef USE_HTTP2 |
310 | 705 | | CURLALTSVC_H2 |
311 | 705 | #endif |
312 | | #ifdef ENABLE_QUIC |
313 | | | CURLALTSVC_H3 |
314 | | #endif |
315 | 705 | ; |
316 | 705 | return asi; |
317 | 705 | } |
318 | | |
319 | | /* |
320 | | * Curl_altsvc_load() loads alt-svc from file. |
321 | | */ |
322 | | CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file) |
323 | 705 | { |
324 | 705 | CURLcode result; |
325 | 705 | DEBUGASSERT(asi); |
326 | 705 | result = altsvc_load(asi, file); |
327 | 705 | return result; |
328 | 705 | } |
329 | | |
330 | | /* |
331 | | * Curl_altsvc_ctrl() passes on the external bitmask. |
332 | | */ |
333 | | CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl) |
334 | 0 | { |
335 | 0 | DEBUGASSERT(asi); |
336 | 0 | if(!ctrl) |
337 | | /* unexpected */ |
338 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
339 | 0 | asi->flags = ctrl; |
340 | 0 | return CURLE_OK; |
341 | 0 | } |
342 | | |
343 | | /* |
344 | | * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated |
345 | | * resources. |
346 | | */ |
347 | | void Curl_altsvc_cleanup(struct altsvcinfo **altsvcp) |
348 | 1.17k | { |
349 | 1.17k | struct Curl_llist_element *e; |
350 | 1.17k | struct Curl_llist_element *n; |
351 | 1.17k | if(*altsvcp) { |
352 | 705 | struct altsvcinfo *altsvc = *altsvcp; |
353 | 705 | for(e = altsvc->list.head; e; e = n) { |
354 | 0 | struct altsvc *as = e->ptr; |
355 | 0 | n = e->next; |
356 | 0 | altsvc_free(as); |
357 | 0 | } |
358 | 705 | free(altsvc->filename); |
359 | 705 | free(altsvc); |
360 | 705 | *altsvcp = NULL; /* clear the pointer */ |
361 | 705 | } |
362 | 1.17k | } |
363 | | |
364 | | /* |
365 | | * Curl_altsvc_save() writes the altsvc cache to a file. |
366 | | */ |
367 | | CURLcode Curl_altsvc_save(struct Curl_easy *data, |
368 | | struct altsvcinfo *altsvc, const char *file) |
369 | 1.17k | { |
370 | 1.17k | struct Curl_llist_element *e; |
371 | 1.17k | struct Curl_llist_element *n; |
372 | 1.17k | CURLcode result = CURLE_OK; |
373 | 1.17k | FILE *out; |
374 | 1.17k | char *tempstore = NULL; |
375 | | |
376 | 1.17k | if(!altsvc) |
377 | | /* no cache activated */ |
378 | 468 | return CURLE_OK; |
379 | | |
380 | | /* if not new name is given, use the one we stored from the load */ |
381 | 705 | if(!file && altsvc->filename) |
382 | 0 | file = altsvc->filename; |
383 | | |
384 | 705 | if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file || !file[0]) |
385 | | /* marked as read-only, no file or zero length file name */ |
386 | 0 | return CURLE_OK; |
387 | | |
388 | 705 | result = Curl_fopen(data, file, &out, &tempstore); |
389 | 705 | if(!result) { |
390 | 705 | fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n" |
391 | 705 | "# This file was generated by libcurl! Edit at your own risk.\n", |
392 | 705 | out); |
393 | 705 | for(e = altsvc->list.head; e; e = n) { |
394 | 0 | struct altsvc *as = e->ptr; |
395 | 0 | n = e->next; |
396 | 0 | result = altsvc_out(as, out); |
397 | 0 | if(result) |
398 | 0 | break; |
399 | 0 | } |
400 | 705 | fclose(out); |
401 | 705 | if(!result && tempstore && Curl_rename(tempstore, file)) |
402 | 0 | result = CURLE_WRITE_ERROR; |
403 | | |
404 | 705 | if(result && tempstore) |
405 | 0 | unlink(tempstore); |
406 | 705 | } |
407 | 705 | free(tempstore); |
408 | 705 | return result; |
409 | 705 | } |
410 | | |
411 | | static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen) |
412 | 0 | { |
413 | 0 | size_t len; |
414 | 0 | const char *protop; |
415 | 0 | const char *p = *ptr; |
416 | 0 | while(*p && ISBLANK(*p)) |
417 | 0 | p++; |
418 | 0 | protop = p; |
419 | 0 | while(*p && !ISBLANK(*p) && (*p != ';') && (*p != '=')) |
420 | 0 | p++; |
421 | 0 | len = p - protop; |
422 | 0 | *ptr = p; |
423 | |
|
424 | 0 | if(!len || (len >= buflen)) |
425 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
426 | 0 | memcpy(alpnbuf, protop, len); |
427 | 0 | alpnbuf[len] = 0; |
428 | 0 | return CURLE_OK; |
429 | 0 | } |
430 | | |
431 | | /* hostcompare() returns true if 'host' matches 'check'. The first host |
432 | | * argument may have a trailing dot present that will be ignored. |
433 | | */ |
434 | | static bool hostcompare(const char *host, const char *check) |
435 | 0 | { |
436 | 0 | size_t hlen = strlen(host); |
437 | 0 | size_t clen = strlen(check); |
438 | |
|
439 | 0 | if(hlen && (host[hlen - 1] == '.')) |
440 | 0 | hlen--; |
441 | 0 | if(hlen != clen) |
442 | | /* they can't match if they have different lengths */ |
443 | 0 | return FALSE; |
444 | 0 | return strncasecompare(host, check, hlen); |
445 | 0 | } |
446 | | |
447 | | /* altsvc_flush() removes all alternatives for this source origin from the |
448 | | list */ |
449 | | static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid, |
450 | | const char *srchost, unsigned short srcport) |
451 | 0 | { |
452 | 0 | struct Curl_llist_element *e; |
453 | 0 | struct Curl_llist_element *n; |
454 | 0 | for(e = asi->list.head; e; e = n) { |
455 | 0 | struct altsvc *as = e->ptr; |
456 | 0 | n = e->next; |
457 | 0 | if((srcalpnid == as->src.alpnid) && |
458 | 0 | (srcport == as->src.port) && |
459 | 0 | hostcompare(srchost, as->src.host)) { |
460 | 0 | Curl_llist_remove(&asi->list, e, NULL); |
461 | 0 | altsvc_free(as); |
462 | 0 | } |
463 | 0 | } |
464 | 0 | } |
465 | | |
466 | | #ifdef DEBUGBUILD |
467 | | /* to play well with debug builds, we can *set* a fixed time this will |
468 | | return */ |
469 | | static time_t altsvc_debugtime(void *unused) |
470 | 0 | { |
471 | 0 | char *timestr = getenv("CURL_TIME"); |
472 | 0 | (void)unused; |
473 | 0 | if(timestr) { |
474 | 0 | unsigned long val = strtol(timestr, NULL, 10); |
475 | 0 | return (time_t)val; |
476 | 0 | } |
477 | 0 | return time(NULL); |
478 | 0 | } |
479 | | #undef time |
480 | 0 | #define time(x) altsvc_debugtime(x) |
481 | | #endif |
482 | | |
483 | 0 | #define ISNEWLINE(x) (((x) == '\n') || (x) == '\r') |
484 | | |
485 | | /* |
486 | | * Curl_altsvc_parse() takes an incoming alt-svc response header and stores |
487 | | * the data correctly in the cache. |
488 | | * |
489 | | * 'value' points to the header *value*. That's contents to the right of the |
490 | | * header name. |
491 | | * |
492 | | * Currently this function rejects invalid data without returning an error. |
493 | | * Invalid host name, port number will result in the specific alternative |
494 | | * being rejected. Unknown protocols are skipped. |
495 | | */ |
496 | | CURLcode Curl_altsvc_parse(struct Curl_easy *data, |
497 | | struct altsvcinfo *asi, const char *value, |
498 | | enum alpnid srcalpnid, const char *srchost, |
499 | | unsigned short srcport) |
500 | 0 | { |
501 | 0 | const char *p = value; |
502 | 0 | size_t len; |
503 | 0 | char namebuf[MAX_ALTSVC_HOSTLEN] = ""; |
504 | 0 | char alpnbuf[MAX_ALTSVC_ALPNLEN] = ""; |
505 | 0 | struct altsvc *as; |
506 | 0 | unsigned short dstport = srcport; /* the same by default */ |
507 | 0 | CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf)); |
508 | 0 | size_t entries = 0; |
509 | | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
510 | | (void)data; |
511 | | #endif |
512 | 0 | if(result) { |
513 | 0 | infof(data, "Excessive alt-svc header, ignoring."); |
514 | 0 | return CURLE_OK; |
515 | 0 | } |
516 | | |
517 | 0 | DEBUGASSERT(asi); |
518 | | |
519 | | /* "clear" is a magic keyword */ |
520 | 0 | if(strcasecompare(alpnbuf, "clear")) { |
521 | | /* Flush cached alternatives for this source origin */ |
522 | 0 | altsvc_flush(asi, srcalpnid, srchost, srcport); |
523 | 0 | return CURLE_OK; |
524 | 0 | } |
525 | | |
526 | 0 | do { |
527 | 0 | if(*p == '=') { |
528 | | /* [protocol]="[host][:port]" */ |
529 | 0 | enum alpnid dstalpnid = alpn2alpnid(alpnbuf); /* the same by default */ |
530 | 0 | p++; |
531 | 0 | if(*p == '\"') { |
532 | 0 | const char *dsthost = ""; |
533 | 0 | const char *value_ptr; |
534 | 0 | char option[32]; |
535 | 0 | unsigned long num; |
536 | 0 | char *end_ptr; |
537 | 0 | bool quoted = FALSE; |
538 | 0 | time_t maxage = 24 * 3600; /* default is 24 hours */ |
539 | 0 | bool persist = FALSE; |
540 | 0 | bool valid = TRUE; |
541 | 0 | p++; |
542 | 0 | if(*p != ':') { |
543 | | /* host name starts here */ |
544 | 0 | const char *hostp = p; |
545 | 0 | if(*p == '[') { |
546 | | /* pass all valid IPv6 letters - does not handle zone id */ |
547 | 0 | len = strspn(++p, "0123456789abcdefABCDEF:."); |
548 | 0 | if(p[len] != ']') |
549 | | /* invalid host syntax, bail out */ |
550 | 0 | break; |
551 | | /* we store the IPv6 numerical address *with* brackets */ |
552 | 0 | len += 2; |
553 | 0 | p = &p[len-1]; |
554 | 0 | } |
555 | 0 | else { |
556 | 0 | while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-'))) |
557 | 0 | p++; |
558 | 0 | len = p - hostp; |
559 | 0 | } |
560 | 0 | if(!len || (len >= MAX_ALTSVC_HOSTLEN)) { |
561 | 0 | infof(data, "Excessive alt-svc host name, ignoring."); |
562 | 0 | valid = FALSE; |
563 | 0 | } |
564 | 0 | else { |
565 | 0 | memcpy(namebuf, hostp, len); |
566 | 0 | namebuf[len] = 0; |
567 | 0 | dsthost = namebuf; |
568 | 0 | } |
569 | 0 | } |
570 | 0 | else { |
571 | | /* no destination name, use source host */ |
572 | 0 | dsthost = srchost; |
573 | 0 | } |
574 | 0 | if(*p == ':') { |
575 | 0 | unsigned long port = 0; |
576 | 0 | p++; |
577 | 0 | if(ISDIGIT(*p)) |
578 | | /* a port number */ |
579 | 0 | port = strtoul(p, &end_ptr, 10); |
580 | 0 | else |
581 | 0 | end_ptr = (char *)p; /* not left uninitialized */ |
582 | 0 | if(!port || port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') { |
583 | 0 | infof(data, "Unknown alt-svc port number, ignoring."); |
584 | 0 | valid = FALSE; |
585 | 0 | } |
586 | 0 | else { |
587 | 0 | dstport = curlx_ultous(port); |
588 | 0 | p = end_ptr; |
589 | 0 | } |
590 | 0 | } |
591 | 0 | if(*p++ != '\"') |
592 | 0 | break; |
593 | | /* Handle the optional 'ma' and 'persist' flags. Unknown flags |
594 | | are skipped. */ |
595 | 0 | for(;;) { |
596 | 0 | while(ISBLANK(*p)) |
597 | 0 | p++; |
598 | 0 | if(*p != ';') |
599 | 0 | break; |
600 | 0 | p++; /* pass the semicolon */ |
601 | 0 | if(!*p || ISNEWLINE(*p)) |
602 | 0 | break; |
603 | 0 | result = getalnum(&p, option, sizeof(option)); |
604 | 0 | if(result) { |
605 | | /* skip option if name is too long */ |
606 | 0 | option[0] = '\0'; |
607 | 0 | } |
608 | 0 | while(*p && ISBLANK(*p)) |
609 | 0 | p++; |
610 | 0 | if(*p != '=') |
611 | 0 | return CURLE_OK; |
612 | 0 | p++; |
613 | 0 | while(*p && ISBLANK(*p)) |
614 | 0 | p++; |
615 | 0 | if(!*p) |
616 | 0 | return CURLE_OK; |
617 | 0 | if(*p == '\"') { |
618 | | /* quoted value */ |
619 | 0 | p++; |
620 | 0 | quoted = TRUE; |
621 | 0 | } |
622 | 0 | value_ptr = p; |
623 | 0 | if(quoted) { |
624 | 0 | while(*p && *p != '\"') |
625 | 0 | p++; |
626 | 0 | if(!*p++) |
627 | 0 | return CURLE_OK; |
628 | 0 | } |
629 | 0 | else { |
630 | 0 | while(*p && !ISBLANK(*p) && *p!= ';' && *p != ',') |
631 | 0 | p++; |
632 | 0 | } |
633 | 0 | num = strtoul(value_ptr, &end_ptr, 10); |
634 | 0 | if((end_ptr != value_ptr) && (num < ULONG_MAX)) { |
635 | 0 | if(strcasecompare("ma", option)) |
636 | 0 | maxage = num; |
637 | 0 | else if(strcasecompare("persist", option) && (num == 1)) |
638 | 0 | persist = TRUE; |
639 | 0 | } |
640 | 0 | } |
641 | 0 | if(dstalpnid && valid) { |
642 | 0 | if(!entries++) |
643 | | /* Flush cached alternatives for this source origin, if any - when |
644 | | this is the first entry of the line. */ |
645 | 0 | altsvc_flush(asi, srcalpnid, srchost, srcport); |
646 | |
|
647 | 0 | as = altsvc_createid(srchost, dsthost, |
648 | 0 | srcalpnid, dstalpnid, |
649 | 0 | srcport, dstport); |
650 | 0 | if(as) { |
651 | | /* The expires time also needs to take the Age: value (if any) into |
652 | | account. [See RFC 7838 section 3.1] */ |
653 | 0 | as->expires = maxage + time(NULL); |
654 | 0 | as->persist = persist; |
655 | 0 | Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node); |
656 | 0 | infof(data, "Added alt-svc: %s:%d over %s", dsthost, dstport, |
657 | 0 | Curl_alpnid2str(dstalpnid)); |
658 | 0 | } |
659 | 0 | } |
660 | 0 | } |
661 | 0 | else |
662 | 0 | break; |
663 | | /* after the double quote there can be a comma if there's another |
664 | | string or a semicolon if no more */ |
665 | 0 | if(*p == ',') { |
666 | | /* comma means another alternative is presented */ |
667 | 0 | p++; |
668 | 0 | result = getalnum(&p, alpnbuf, sizeof(alpnbuf)); |
669 | 0 | if(result) |
670 | 0 | break; |
671 | 0 | } |
672 | 0 | } |
673 | 0 | else |
674 | 0 | break; |
675 | 0 | } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r')); |
676 | | |
677 | 0 | return CURLE_OK; |
678 | 0 | } |
679 | | |
680 | | /* |
681 | | * Return TRUE on a match |
682 | | */ |
683 | | bool Curl_altsvc_lookup(struct altsvcinfo *asi, |
684 | | enum alpnid srcalpnid, const char *srchost, |
685 | | int srcport, |
686 | | struct altsvc **dstentry, |
687 | | const int versions) /* one or more bits */ |
688 | 0 | { |
689 | 0 | struct Curl_llist_element *e; |
690 | 0 | struct Curl_llist_element *n; |
691 | 0 | time_t now = time(NULL); |
692 | 0 | DEBUGASSERT(asi); |
693 | 0 | DEBUGASSERT(srchost); |
694 | 0 | DEBUGASSERT(dstentry); |
695 | | |
696 | 0 | for(e = asi->list.head; e; e = n) { |
697 | 0 | struct altsvc *as = e->ptr; |
698 | 0 | n = e->next; |
699 | 0 | if(as->expires < now) { |
700 | | /* an expired entry, remove */ |
701 | 0 | Curl_llist_remove(&asi->list, e, NULL); |
702 | 0 | altsvc_free(as); |
703 | 0 | continue; |
704 | 0 | } |
705 | 0 | if((as->src.alpnid == srcalpnid) && |
706 | 0 | hostcompare(srchost, as->src.host) && |
707 | 0 | (as->src.port == srcport) && |
708 | 0 | (versions & as->dst.alpnid)) { |
709 | | /* match */ |
710 | 0 | *dstentry = as; |
711 | 0 | return TRUE; |
712 | 0 | } |
713 | 0 | } |
714 | 0 | return FALSE; |
715 | 0 | } |
716 | | |
717 | | #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_ALTSVC */ |