altsvc.c 19 KB

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