2
0

altsvc.c 19 KB

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