altsvc.c 19 KB

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