altsvc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2019, Daniel Stenberg, <[email protected]>, 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.haxx.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. ***************************************************************************/
  22. /*
  23. * The Alt-Svc: header is defined in RFC 7838:
  24. * https://tools.ietf.org/html/rfc7838
  25. */
  26. #include "curl_setup.h"
  27. #if !defined(CURL_DISABLE_HTTP) && defined(USE_ALTSVC)
  28. #include <curl/curl.h>
  29. #include "urldata.h"
  30. #include "altsvc.h"
  31. #include "curl_get_line.h"
  32. #include "strcase.h"
  33. #include "parsedate.h"
  34. #include "sendf.h"
  35. #include "warnless.h"
  36. /* The last 3 #include files should be in this order */
  37. #include "curl_printf.h"
  38. #include "curl_memory.h"
  39. #include "memdebug.h"
  40. #define MAX_ALTSVC_LINE 4095
  41. #define MAX_ALTSVC_DATELENSTR "64"
  42. #define MAX_ALTSVC_DATELEN 64
  43. #define MAX_ALTSVC_HOSTLENSTR "512"
  44. #define MAX_ALTSVC_HOSTLEN 512
  45. #define MAX_ALTSVC_ALPNLENSTR "10"
  46. #define MAX_ALTSVC_ALPNLEN 10
  47. static enum alpnid alpn2alpnid(char *name)
  48. {
  49. if(strcasecompare(name, "h1"))
  50. return ALPN_h1;
  51. if(strcasecompare(name, "h2"))
  52. return ALPN_h2;
  53. if(strcasecompare(name, "h2c"))
  54. return ALPN_h2c;
  55. if(strcasecompare(name, "h3"))
  56. return ALPN_h3;
  57. return ALPN_none; /* unknown, probably rubbish input */
  58. }
  59. /* Given the ALPN ID, return the name */
  60. const char *Curl_alpnid2str(enum alpnid id)
  61. {
  62. switch(id) {
  63. case ALPN_h1:
  64. return "h1";
  65. case ALPN_h2:
  66. return "h2";
  67. case ALPN_h2c:
  68. return "h2c";
  69. case ALPN_h3:
  70. return "h3";
  71. default:
  72. return ""; /* bad */
  73. }
  74. }
  75. static void altsvc_free(struct altsvc *as)
  76. {
  77. free(as->srchost);
  78. free(as->dsthost);
  79. free(as);
  80. }
  81. static struct altsvc *altsvc_createid(const char *srchost,
  82. const char *dsthost,
  83. enum alpnid srcalpnid,
  84. enum alpnid dstalpnid,
  85. unsigned int srcport,
  86. unsigned int dstport)
  87. {
  88. struct altsvc *as = calloc(sizeof(struct altsvc), 1);
  89. if(!as)
  90. return NULL;
  91. as->srchost = strdup(srchost);
  92. if(!as->srchost)
  93. goto error;
  94. as->dsthost = strdup(dsthost);
  95. if(!as->dsthost)
  96. goto error;
  97. as->srcalpnid = srcalpnid;
  98. as->dstalpnid = dstalpnid;
  99. as->srcport = curlx_ultous(srcport);
  100. as->dstport = curlx_ultous(dstport);
  101. return as;
  102. error:
  103. altsvc_free(as);
  104. return NULL;
  105. }
  106. static struct altsvc *altsvc_create(char *srchost,
  107. char *dsthost,
  108. char *srcalpn,
  109. char *dstalpn,
  110. unsigned int srcport,
  111. unsigned int dstport)
  112. {
  113. enum alpnid dstalpnid = alpn2alpnid(dstalpn);
  114. enum alpnid srcalpnid = alpn2alpnid(srcalpn);
  115. if(!srcalpnid || !dstalpnid)
  116. return NULL;
  117. return altsvc_createid(srchost, dsthost, srcalpnid, dstalpnid,
  118. srcport, dstport);
  119. }
  120. /* only returns SERIOUS errors */
  121. static CURLcode altsvc_add(struct altsvcinfo *asi, char *line)
  122. {
  123. /* Example line:
  124. h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
  125. */
  126. char srchost[MAX_ALTSVC_HOSTLEN + 1];
  127. char dsthost[MAX_ALTSVC_HOSTLEN + 1];
  128. char srcalpn[MAX_ALTSVC_ALPNLEN + 1];
  129. char dstalpn[MAX_ALTSVC_ALPNLEN + 1];
  130. char date[MAX_ALTSVC_DATELEN + 1];
  131. unsigned int srcport;
  132. unsigned int dstport;
  133. unsigned int prio;
  134. unsigned int persist;
  135. int rc;
  136. rc = sscanf(line,
  137. "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
  138. "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
  139. "\"%" MAX_ALTSVC_DATELENSTR "[^\"]\" %u %u",
  140. srcalpn, srchost, &srcport,
  141. dstalpn, dsthost, &dstport,
  142. date, &persist, &prio);
  143. if(9 == rc) {
  144. struct altsvc *as;
  145. time_t expires = curl_getdate(date, NULL);
  146. as = altsvc_create(srchost, dsthost, srcalpn, dstalpn, srcport, dstport);
  147. if(as) {
  148. as->expires = expires;
  149. as->prio = prio;
  150. as->persist = persist ? 1 : 0;
  151. Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
  152. asi->num++; /* one more entry */
  153. }
  154. }
  155. return CURLE_OK;
  156. }
  157. /*
  158. * Load alt-svc entries from the given file. The text based line-oriented file
  159. * format is documented here:
  160. * https://github.com/curl/curl/wiki/QUIC-implementation
  161. *
  162. * This function only returns error on major problems that prevents alt-svc
  163. * handling to work completely. It will ignore individual syntactical errors
  164. * etc.
  165. */
  166. static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
  167. {
  168. CURLcode result = CURLE_OK;
  169. char *line = NULL;
  170. FILE *fp = fopen(file, FOPEN_READTEXT);
  171. if(fp) {
  172. line = malloc(MAX_ALTSVC_LINE);
  173. if(!line)
  174. goto fail;
  175. while(Curl_get_line(line, MAX_ALTSVC_LINE, fp)) {
  176. char *lineptr = line;
  177. while(*lineptr && ISBLANK(*lineptr))
  178. lineptr++;
  179. if(*lineptr == '#')
  180. /* skip commented lines */
  181. continue;
  182. altsvc_add(asi, lineptr);
  183. }
  184. free(line); /* free the line buffer */
  185. fclose(fp);
  186. }
  187. return result;
  188. fail:
  189. free(line);
  190. fclose(fp);
  191. return CURLE_OUT_OF_MEMORY;
  192. }
  193. /*
  194. * Write this single altsvc entry to a single output line
  195. */
  196. static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
  197. {
  198. struct tm stamp;
  199. CURLcode result = Curl_gmtime(as->expires, &stamp);
  200. if(result)
  201. return result;
  202. fprintf(fp,
  203. "%s %s %u "
  204. "%s %s %u "
  205. "\"%d%02d%02d "
  206. "%02d:%02d:%02d\" "
  207. "%u %d\n",
  208. Curl_alpnid2str(as->srcalpnid), as->srchost, as->srcport,
  209. Curl_alpnid2str(as->dstalpnid), as->dsthost, as->dstport,
  210. stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
  211. stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
  212. as->persist, as->prio);
  213. return CURLE_OK;
  214. }
  215. /* ---- library-wide functions below ---- */
  216. /*
  217. * Curl_altsvc_init() creates a new altsvc cache.
  218. * It returns the new instance or NULL if something goes wrong.
  219. */
  220. struct altsvcinfo *Curl_altsvc_init(void)
  221. {
  222. struct altsvcinfo *asi = calloc(sizeof(struct altsvcinfo), 1);
  223. if(!asi)
  224. return NULL;
  225. Curl_llist_init(&asi->list, NULL);
  226. /* set default behavior */
  227. asi->flags = CURLALTSVC_H1
  228. #ifdef USE_NGHTTP2
  229. | CURLALTSVC_H2
  230. #endif
  231. #ifdef USE_HTTP3
  232. | CURLALTSVC_H3
  233. #endif
  234. ;
  235. return asi;
  236. }
  237. /*
  238. * Curl_altsvc_load() loads alt-svc from file.
  239. */
  240. CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
  241. {
  242. CURLcode result;
  243. DEBUGASSERT(asi);
  244. result = altsvc_load(asi, file);
  245. return result;
  246. }
  247. /*
  248. * Curl_altsvc_ctrl() passes on the external bitmask.
  249. */
  250. CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl)
  251. {
  252. DEBUGASSERT(asi);
  253. if(!ctrl)
  254. /* unexpected */
  255. return CURLE_BAD_FUNCTION_ARGUMENT;
  256. asi->flags = ctrl;
  257. return CURLE_OK;
  258. }
  259. /*
  260. * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
  261. * resources.
  262. */
  263. void Curl_altsvc_cleanup(struct altsvcinfo *altsvc)
  264. {
  265. struct curl_llist_element *e;
  266. struct curl_llist_element *n;
  267. if(altsvc) {
  268. for(e = altsvc->list.head; e; e = n) {
  269. struct altsvc *as = e->ptr;
  270. n = e->next;
  271. altsvc_free(as);
  272. }
  273. free(altsvc);
  274. }
  275. }
  276. /*
  277. * Curl_altsvc_save() writes the altsvc cache to a file.
  278. */
  279. CURLcode Curl_altsvc_save(struct altsvcinfo *altsvc, const char *file)
  280. {
  281. struct curl_llist_element *e;
  282. struct curl_llist_element *n;
  283. CURLcode result = CURLE_OK;
  284. FILE *out;
  285. if(!altsvc)
  286. /* no cache activated */
  287. return CURLE_OK;
  288. if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file[0])
  289. /* marked as read-only or zero length file name */
  290. return CURLE_OK;
  291. out = fopen(file, FOPEN_WRITETEXT);
  292. if(!out)
  293. return CURLE_WRITE_ERROR;
  294. fputs("# Your alt-svc cache. https://curl.haxx.se/docs/alt-svc.html\n"
  295. "# This file was generated by libcurl! Edit at your own risk.\n",
  296. out);
  297. for(e = altsvc->list.head; e; e = n) {
  298. struct altsvc *as = e->ptr;
  299. n = e->next;
  300. result = altsvc_out(as, out);
  301. if(result)
  302. break;
  303. }
  304. fclose(out);
  305. return result;
  306. }
  307. static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen)
  308. {
  309. size_t len;
  310. const char *protop;
  311. const char *p = *ptr;
  312. while(*p && ISBLANK(*p))
  313. p++;
  314. protop = p;
  315. while(*p && ISALNUM(*p))
  316. p++;
  317. len = p - protop;
  318. if(!len || (len >= buflen))
  319. return CURLE_BAD_FUNCTION_ARGUMENT;
  320. memcpy(alpnbuf, protop, len);
  321. alpnbuf[len] = 0;
  322. *ptr = p;
  323. return CURLE_OK;
  324. }
  325. /* altsvc_flush() removes all alternatives for this source origin from the
  326. list */
  327. static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
  328. const char *srchost, unsigned short srcport)
  329. {
  330. struct curl_llist_element *e;
  331. struct curl_llist_element *n;
  332. for(e = asi->list.head; e; e = n) {
  333. struct altsvc *as = e->ptr;
  334. n = e->next;
  335. if((srcalpnid == as->srcalpnid) &&
  336. (srcport == as->srcport) &&
  337. strcasecompare(srchost, as->srchost)) {
  338. Curl_llist_remove(&asi->list, e, NULL);
  339. altsvc_free(as);
  340. asi->num--;
  341. }
  342. }
  343. }
  344. #ifdef DEBUGBUILD
  345. /* to play well with debug builds, we can *set* a fixed time this will
  346. return */
  347. static time_t debugtime(void *unused)
  348. {
  349. char *timestr = getenv("CURL_TIME");
  350. (void)unused;
  351. if(timestr) {
  352. unsigned long val = strtol(timestr, NULL, 10);
  353. return (time_t)val;
  354. }
  355. return time(NULL);
  356. }
  357. #define time(x) debugtime(x)
  358. #endif
  359. /*
  360. * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
  361. * the data correctly in the cache.
  362. *
  363. * 'value' points to the header *value*. That's contents to the right of the
  364. * header name.
  365. */
  366. CURLcode Curl_altsvc_parse(struct Curl_easy *data,
  367. struct altsvcinfo *asi, const char *value,
  368. enum alpnid srcalpnid, const char *srchost,
  369. unsigned short srcport)
  370. {
  371. const char *p = value;
  372. size_t len;
  373. enum alpnid dstalpnid = srcalpnid; /* the same by default */
  374. char namebuf[MAX_ALTSVC_HOSTLEN] = "";
  375. char alpnbuf[MAX_ALTSVC_ALPNLEN] = "";
  376. struct altsvc *as;
  377. unsigned short dstport = srcport; /* the same by default */
  378. const char *semip;
  379. time_t maxage = 24 * 3600; /* default is 24 hours */
  380. bool persist = FALSE;
  381. CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  382. if(result)
  383. return result;
  384. DEBUGASSERT(asi);
  385. /* Flush all cached alternatives for this source origin, if any */
  386. altsvc_flush(asi, srcalpnid, srchost, srcport);
  387. /* "clear" is a magic keyword */
  388. if(strcasecompare(alpnbuf, "clear")) {
  389. return CURLE_OK;
  390. }
  391. /* The 'ma' and 'persist' flags are annoyingly meant for all alternatives
  392. but are set after the list on the line. Scan for the semicolons and get
  393. those fields first! */
  394. semip = p;
  395. do {
  396. semip = strchr(semip, ';');
  397. if(semip) {
  398. char option[32];
  399. unsigned long num;
  400. char *end_ptr;
  401. semip++; /* pass the semicolon */
  402. result = getalnum(&semip, option, sizeof(option));
  403. if(result)
  404. break;
  405. while(*semip && ISBLANK(*semip))
  406. semip++;
  407. if(*semip != '=')
  408. continue;
  409. semip++;
  410. num = strtoul(semip, &end_ptr, 10);
  411. if(num < ULONG_MAX) {
  412. if(strcasecompare("ma", option))
  413. maxage = num;
  414. else if(strcasecompare("persist", option) && (num == 1))
  415. persist = TRUE;
  416. }
  417. semip = end_ptr;
  418. }
  419. } while(semip);
  420. do {
  421. if(*p == '=') {
  422. /* [protocol]="[host][:port]" */
  423. dstalpnid = alpn2alpnid(alpnbuf);
  424. if(!dstalpnid) {
  425. infof(data, "Unknown alt-svc protocol \"%s\", ignoring...\n", alpnbuf);
  426. return CURLE_OK;
  427. }
  428. p++;
  429. if(*p == '\"') {
  430. const char *dsthost;
  431. p++;
  432. if(*p != ':') {
  433. /* host name starts here */
  434. const char *hostp = p;
  435. while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
  436. p++;
  437. len = p - hostp;
  438. if(!len || (len >= MAX_ALTSVC_HOSTLEN))
  439. return CURLE_BAD_FUNCTION_ARGUMENT;
  440. memcpy(namebuf, hostp, len);
  441. namebuf[len] = 0;
  442. dsthost = namebuf;
  443. }
  444. else {
  445. /* no destination name, use source host */
  446. dsthost = srchost;
  447. }
  448. if(*p == ':') {
  449. /* a port number */
  450. char *end_ptr;
  451. unsigned long port = strtoul(++p, &end_ptr, 10);
  452. if(port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
  453. infof(data, "Unknown alt-svc port number, ignoring...\n");
  454. return CURLE_OK;
  455. }
  456. p = end_ptr;
  457. dstport = curlx_ultous(port);
  458. }
  459. if(*p++ != '\"')
  460. return CURLE_BAD_FUNCTION_ARGUMENT;
  461. as = altsvc_createid(srchost, dsthost,
  462. srcalpnid, dstalpnid,
  463. srcport, dstport);
  464. if(as) {
  465. /* The expires time also needs to take the Age: value (if any) into
  466. account. [See RFC 7838 section 3.1] */
  467. as->expires = maxage + time(NULL);
  468. as->persist = persist;
  469. Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
  470. asi->num++; /* one more entry */
  471. infof(data, "Added alt-svc: %s:%d over %s\n", dsthost, dstport,
  472. Curl_alpnid2str(dstalpnid));
  473. }
  474. }
  475. /* after the double quote there can be a comma if there's another
  476. string or a semicolon if no more */
  477. if(*p == ',') {
  478. /* comma means another alternative is presented */
  479. p++;
  480. result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  481. if(result)
  482. /* failed to parse, but since we already did at least one host we
  483. return OK */
  484. return CURLE_OK;
  485. }
  486. }
  487. } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r'));
  488. return CURLE_OK;
  489. }
  490. /*
  491. * Return TRUE on a match
  492. */
  493. bool Curl_altsvc_lookup(struct altsvcinfo *asi,
  494. enum alpnid srcalpnid, const char *srchost,
  495. int srcport,
  496. enum alpnid *dstalpnid, const char **dsthost,
  497. int *dstport)
  498. {
  499. struct curl_llist_element *e;
  500. struct curl_llist_element *n;
  501. time_t now = time(NULL);
  502. DEBUGASSERT(asi);
  503. DEBUGASSERT(srchost);
  504. DEBUGASSERT(dsthost);
  505. for(e = asi->list.head; e; e = n) {
  506. struct altsvc *as = e->ptr;
  507. n = e->next;
  508. if(as->expires < now) {
  509. /* an expired entry, remove */
  510. altsvc_free(as);
  511. continue;
  512. }
  513. if((as->srcalpnid == srcalpnid) &&
  514. strcasecompare(as->srchost, srchost) &&
  515. as->srcport == srcport) {
  516. /* match */
  517. *dstalpnid = as->dstalpnid;
  518. *dsthost = as->dsthost;
  519. *dstport = as->dstport;
  520. return TRUE;
  521. }
  522. }
  523. return FALSE;
  524. }
  525. #endif /* CURL_DISABLE_HTTP || USE_ALTSVC */