2
0

altsvc.c 20 KB

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