1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2019 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * The Alt-Svc: header is defined in RFC 7838:
24 * https://tools.ietf.org/html/rfc7838
26 #include "curl_setup.h"
28 #if !defined(CURL_DISABLE_HTTP) && defined(USE_ALTSVC)
29 #include <curl/curl.h>
32 #include "curl_get_line.h"
34 #include "parsedate.h"
40 /* The last 3 #include files should be in this order */
41 #include "curl_printf.h"
42 #include "curl_memory.h"
45 #define MAX_ALTSVC_LINE 4095
46 #define MAX_ALTSVC_DATELENSTR "64"
47 #define MAX_ALTSVC_DATELEN 64
48 #define MAX_ALTSVC_HOSTLENSTR "512"
49 #define MAX_ALTSVC_HOSTLEN 512
50 #define MAX_ALTSVC_ALPNLENSTR "10"
51 #define MAX_ALTSVC_ALPNLEN 10
53 #if defined(USE_QUICHE) && !defined(UNITTESTS)
54 #define H3VERSION "h3-29"
55 #elif defined(USE_NGTCP2) && !defined(UNITTESTS)
56 #define H3VERSION "h3-29"
58 #define H3VERSION "h3"
61 static enum alpnid alpn2alpnid(char *name)
63 if(strcasecompare(name, "h1"))
65 if(strcasecompare(name, "h2"))
67 if(strcasecompare(name, H3VERSION))
69 return ALPN_none; /* unknown, probably rubbish input */
72 /* Given the ALPN ID, return the name */
73 const char *Curl_alpnid2str(enum alpnid id)
88 static void altsvc_free(struct altsvc *as)
95 static struct altsvc *altsvc_createid(const char *srchost,
97 enum alpnid srcalpnid,
98 enum alpnid dstalpnid,
100 unsigned int dstport)
102 struct altsvc *as = calloc(sizeof(struct altsvc), 1);
106 as->src.host = strdup(srchost);
109 as->dst.host = strdup(dsthost);
113 as->src.alpnid = srcalpnid;
114 as->dst.alpnid = dstalpnid;
115 as->src.port = curlx_ultous(srcport);
116 as->dst.port = curlx_ultous(dstport);
124 static struct altsvc *altsvc_create(char *srchost,
128 unsigned int srcport,
129 unsigned int dstport)
131 enum alpnid dstalpnid = alpn2alpnid(dstalpn);
132 enum alpnid srcalpnid = alpn2alpnid(srcalpn);
133 if(!srcalpnid || !dstalpnid)
135 return altsvc_createid(srchost, dsthost, srcalpnid, dstalpnid,
139 /* only returns SERIOUS errors */
140 static CURLcode altsvc_add(struct altsvcinfo *asi, char *line)
143 h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
145 char srchost[MAX_ALTSVC_HOSTLEN + 1];
146 char dsthost[MAX_ALTSVC_HOSTLEN + 1];
147 char srcalpn[MAX_ALTSVC_ALPNLEN + 1];
148 char dstalpn[MAX_ALTSVC_ALPNLEN + 1];
149 char date[MAX_ALTSVC_DATELEN + 1];
150 unsigned int srcport;
151 unsigned int dstport;
153 unsigned int persist;
157 "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
158 "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
159 "\"%" MAX_ALTSVC_DATELENSTR "[^\"]\" %u %u",
160 srcalpn, srchost, &srcport,
161 dstalpn, dsthost, &dstport,
162 date, &persist, &prio);
165 time_t expires = Curl_getdate_capped(date);
166 as = altsvc_create(srchost, dsthost, srcalpn, dstalpn, srcport, dstport);
168 as->expires = expires;
170 as->persist = persist ? 1 : 0;
171 Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
179 * Load alt-svc entries from the given file. The text based line-oriented file
180 * format is documented here:
181 * https://github.com/curl/curl/wiki/QUIC-implementation
183 * This function only returns error on major problems that prevents alt-svc
184 * handling to work completely. It will ignore individual syntactical errors
187 static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
189 CURLcode result = CURLE_OK;
193 /* we need a private copy of the file name so that the altsvc cache file
194 name survives an easy handle reset */
196 asi->filename = strdup(file);
198 return CURLE_OUT_OF_MEMORY;
200 fp = fopen(file, FOPEN_READTEXT);
202 line = malloc(MAX_ALTSVC_LINE);
205 while(Curl_get_line(line, MAX_ALTSVC_LINE, fp)) {
206 char *lineptr = line;
207 while(*lineptr && ISBLANK(*lineptr))
210 /* skip commented lines */
213 altsvc_add(asi, lineptr);
215 free(line); /* free the line buffer */
221 Curl_safefree(asi->filename);
224 return CURLE_OUT_OF_MEMORY;
228 * Write this single altsvc entry to a single output line
231 static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
234 CURLcode result = Curl_gmtime(as->expires, &stamp);
244 Curl_alpnid2str(as->src.alpnid), as->src.host, as->src.port,
245 Curl_alpnid2str(as->dst.alpnid), as->dst.host, as->dst.port,
246 stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
247 stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
248 as->persist, as->prio);
252 /* ---- library-wide functions below ---- */
255 * Curl_altsvc_init() creates a new altsvc cache.
256 * It returns the new instance or NULL if something goes wrong.
258 struct altsvcinfo *Curl_altsvc_init(void)
260 struct altsvcinfo *asi = calloc(sizeof(struct altsvcinfo), 1);
263 Curl_llist_init(&asi->list, NULL);
265 /* set default behavior */
266 asi->flags = CURLALTSVC_H1
278 * Curl_altsvc_load() loads alt-svc from file.
280 CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
284 result = altsvc_load(asi, file);
289 * Curl_altsvc_ctrl() passes on the external bitmask.
291 CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl)
296 return CURLE_BAD_FUNCTION_ARGUMENT;
302 * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
305 void Curl_altsvc_cleanup(struct altsvcinfo **altsvcp)
307 struct Curl_llist_element *e;
308 struct Curl_llist_element *n;
310 struct altsvcinfo *altsvc = *altsvcp;
311 for(e = altsvc->list.head; e; e = n) {
312 struct altsvc *as = e->ptr;
316 free(altsvc->filename);
318 *altsvcp = NULL; /* clear the pointer */
323 * Curl_altsvc_save() writes the altsvc cache to a file.
325 CURLcode Curl_altsvc_save(struct Curl_easy *data,
326 struct altsvcinfo *altsvc, const char *file)
328 struct Curl_llist_element *e;
329 struct Curl_llist_element *n;
330 CURLcode result = CURLE_OK;
333 unsigned char randsuffix[9];
336 /* no cache activated */
339 /* if not new name is given, use the one we stored from the load */
340 if(!file && altsvc->filename)
341 file = altsvc->filename;
343 if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
344 /* marked as read-only, no file or zero length file name */
347 if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix)))
348 return CURLE_FAILED_INIT;
350 tempstore = aprintf("%s.%s.tmp", file, randsuffix);
352 return CURLE_OUT_OF_MEMORY;
354 out = fopen(tempstore, FOPEN_WRITETEXT);
356 result = CURLE_WRITE_ERROR;
358 fputs("# Your alt-svc cache. https://curl.haxx.se/docs/alt-svc.html\n"
359 "# This file was generated by libcurl! Edit at your own risk.\n",
361 for(e = altsvc->list.head; e; e = n) {
362 struct altsvc *as = e->ptr;
364 result = altsvc_out(as, out);
369 if(!result && Curl_rename(tempstore, file))
370 result = CURLE_WRITE_ERROR;
379 static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen)
383 const char *p = *ptr;
384 while(*p && ISBLANK(*p))
387 while(*p && !ISBLANK(*p) && (*p != ';') && (*p != '='))
392 if(!len || (len >= buflen))
393 return CURLE_BAD_FUNCTION_ARGUMENT;
394 memcpy(alpnbuf, protop, len);
399 /* altsvc_flush() removes all alternatives for this source origin from the
401 static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
402 const char *srchost, unsigned short srcport)
404 struct Curl_llist_element *e;
405 struct Curl_llist_element *n;
406 for(e = asi->list.head; e; e = n) {
407 struct altsvc *as = e->ptr;
409 if((srcalpnid == as->src.alpnid) &&
410 (srcport == as->src.port) &&
411 strcasecompare(srchost, as->src.host)) {
412 Curl_llist_remove(&asi->list, e, NULL);
419 /* to play well with debug builds, we can *set* a fixed time this will
421 static time_t debugtime(void *unused)
423 char *timestr = getenv("CURL_TIME");
426 unsigned long val = strtol(timestr, NULL, 10);
431 #define time(x) debugtime(x)
434 #define ISNEWLINE(x) (((x) == '\n') || (x) == '\r')
437 * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
438 * the data correctly in the cache.
440 * 'value' points to the header *value*. That's contents to the right of the
443 * Currently this function rejects invalid data without returning an error.
444 * Invalid host name, port number will result in the specific alternative
445 * being rejected. Unknown protocols are skipped.
447 CURLcode Curl_altsvc_parse(struct Curl_easy *data,
448 struct altsvcinfo *asi, const char *value,
449 enum alpnid srcalpnid, const char *srchost,
450 unsigned short srcport)
452 const char *p = value;
454 enum alpnid dstalpnid = srcalpnid; /* the same by default */
455 char namebuf[MAX_ALTSVC_HOSTLEN] = "";
456 char alpnbuf[MAX_ALTSVC_ALPNLEN] = "";
458 unsigned short dstport = srcport; /* the same by default */
459 CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
461 infof(data, "Excessive alt-svc header, ignoring...\n");
467 /* Flush all cached alternatives for this source origin, if any */
468 altsvc_flush(asi, srcalpnid, srchost, srcport);
470 /* "clear" is a magic keyword */
471 if(strcasecompare(alpnbuf, "clear")) {
477 /* [protocol]="[host][:port]" */
478 dstalpnid = alpn2alpnid(alpnbuf);
481 const char *dsthost = "";
482 const char *value_ptr;
487 time_t maxage = 24 * 3600; /* default is 24 hours */
488 bool persist = FALSE;
491 /* host name starts here */
492 const char *hostp = p;
493 while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
496 if(!len || (len >= MAX_ALTSVC_HOSTLEN)) {
497 infof(data, "Excessive alt-svc host name, ignoring...\n");
498 dstalpnid = ALPN_none;
501 memcpy(namebuf, hostp, len);
507 /* no destination name, use source host */
512 unsigned long port = strtoul(++p, &end_ptr, 10);
513 if(port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
514 infof(data, "Unknown alt-svc port number, ignoring...\n");
515 dstalpnid = ALPN_none;
518 dstport = curlx_ultous(port);
522 /* Handle the optional 'ma' and 'persist' flags. Unknown flags
529 p++; /* pass the semicolon */
530 if(!*p || ISNEWLINE(*p))
532 result = getalnum(&p, option, sizeof(option));
534 /* skip option if name is too long */
537 while(*p && ISBLANK(*p))
542 while(*p && ISBLANK(*p))
553 while(*p && *p != '\"')
559 while(*p && !ISBLANK(*p) && *p!= ';' && *p != ',')
562 num = strtoul(value_ptr, &end_ptr, 10);
563 if((end_ptr != value_ptr) && (num < ULONG_MAX)) {
564 if(strcasecompare("ma", option))
566 else if(strcasecompare("persist", option) && (num == 1))
571 as = altsvc_createid(srchost, dsthost,
572 srcalpnid, dstalpnid,
575 /* The expires time also needs to take the Age: value (if any) into
576 account. [See RFC 7838 section 3.1] */
577 as->expires = maxage + time(NULL);
578 as->persist = persist;
579 Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
580 infof(data, "Added alt-svc: %s:%d over %s\n", dsthost, dstport,
581 Curl_alpnid2str(dstalpnid));
585 infof(data, "Unknown alt-svc protocol \"%s\", skipping...\n",
591 /* after the double quote there can be a comma if there's another
592 string or a semicolon if no more */
594 /* comma means another alternative is presented */
596 result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
603 } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r'));
609 * Return TRUE on a match
611 bool Curl_altsvc_lookup(struct altsvcinfo *asi,
612 enum alpnid srcalpnid, const char *srchost,
614 struct altsvc **dstentry,
615 const int versions) /* one or more bits */
617 struct Curl_llist_element *e;
618 struct Curl_llist_element *n;
619 time_t now = time(NULL);
621 DEBUGASSERT(srchost);
622 DEBUGASSERT(dstentry);
624 for(e = asi->list.head; e; e = n) {
625 struct altsvc *as = e->ptr;
627 if(as->expires < now) {
628 /* an expired entry, remove */
629 Curl_llist_remove(&asi->list, e, NULL);
633 if((as->src.alpnid == srcalpnid) &&
634 strcasecompare(as->src.host, srchost) &&
635 (as->src.port == srcport) &&
636 (versions & as->dst.alpnid)) {
645 #endif /* CURL_DISABLE_HTTP || USE_ALTSVC */