altsvc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2019, 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.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 "cookie.h" /* for Curl_get_line() */
  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. /* TODO: adjust when known */
  233. | CURLALTSVC_H3
  234. #endif
  235. ;
  236. return asi;
  237. }
  238. /*
  239. * Curl_altsvc_load() loads alt-svc from file.
  240. */
  241. CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
  242. {
  243. CURLcode result;
  244. DEBUGASSERT(asi);
  245. result = altsvc_load(asi, file);
  246. return result;
  247. }
  248. /*
  249. * Curl_altsvc_ctrl() passes on the external bitmask.
  250. */
  251. CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl)
  252. {
  253. DEBUGASSERT(asi);
  254. if(!ctrl)
  255. /* unexpected */
  256. return CURLE_BAD_FUNCTION_ARGUMENT;
  257. asi->flags = ctrl;
  258. return CURLE_OK;
  259. }
  260. /*
  261. * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
  262. * resources.
  263. */
  264. void Curl_altsvc_cleanup(struct altsvcinfo *altsvc)
  265. {
  266. struct curl_llist_element *e;
  267. struct curl_llist_element *n;
  268. if(altsvc) {
  269. for(e = altsvc->list.head; e; e = n) {
  270. struct altsvc *as = e->ptr;
  271. n = e->next;
  272. altsvc_free(as);
  273. }
  274. free(altsvc);
  275. }
  276. }
  277. /*
  278. * Curl_altsvc_save() writes the altsvc cache to a file.
  279. */
  280. CURLcode Curl_altsvc_save(struct altsvcinfo *altsvc, const char *file)
  281. {
  282. struct curl_llist_element *e;
  283. struct curl_llist_element *n;
  284. CURLcode result = CURLE_OK;
  285. FILE *out;
  286. if(!altsvc)
  287. /* no cache activated */
  288. return CURLE_OK;
  289. if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file[0])
  290. /* marked as read-only or zero length file name */
  291. return CURLE_OK;
  292. out = fopen(file, FOPEN_WRITETEXT);
  293. if(!out)
  294. return CURLE_WRITE_ERROR;
  295. fputs("# Your alt-svc cache. https://curl.haxx.se/docs/alt-svc.html\n"
  296. "# This file was generated by libcurl! Edit at your own risk.\n",
  297. out);
  298. for(e = altsvc->list.head; e; e = n) {
  299. struct altsvc *as = e->ptr;
  300. n = e->next;
  301. result = altsvc_out(as, out);
  302. if(result)
  303. break;
  304. }
  305. fclose(out);
  306. return result;
  307. }
  308. static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen)
  309. {
  310. size_t len;
  311. const char *protop;
  312. const char *p = *ptr;
  313. while(*p && ISBLANK(*p))
  314. p++;
  315. protop = p;
  316. while(*p && ISALNUM(*p))
  317. p++;
  318. len = p - protop;
  319. if(!len || (len >= buflen))
  320. return CURLE_BAD_FUNCTION_ARGUMENT; /* TODO: improve error code */
  321. memcpy(alpnbuf, protop, len);
  322. alpnbuf[len] = 0;
  323. *ptr = p;
  324. return CURLE_OK;
  325. }
  326. /* altsvc_flush() removes all alternatives for this source origin from the
  327. list */
  328. static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
  329. const char *srchost, unsigned short srcport)
  330. {
  331. struct curl_llist_element *e;
  332. struct curl_llist_element *n;
  333. for(e = asi->list.head; e; e = n) {
  334. struct altsvc *as = e->ptr;
  335. n = e->next;
  336. if((srcalpnid == as->srcalpnid) &&
  337. (srcport == as->srcport) &&
  338. strcasecompare(srchost, as->srchost)) {
  339. Curl_llist_remove(&asi->list, e, NULL);
  340. altsvc_free(as);
  341. asi->num--;
  342. }
  343. }
  344. }
  345. #ifdef DEBUGBUILD
  346. /* to play well with debug builds, we can *set* a fixed time this will
  347. return */
  348. static time_t debugtime(void *unused)
  349. {
  350. char *timestr = getenv("CURL_TIME");
  351. (void)unused;
  352. if(timestr) {
  353. unsigned long val = strtol(timestr, NULL, 10);
  354. return (time_t)val;
  355. }
  356. return time(NULL);
  357. }
  358. #define time(x) debugtime(x)
  359. #endif
  360. /*
  361. * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
  362. * the data correctly in the cache.
  363. *
  364. * 'value' points to the header *value*. That's contents to the right of the
  365. * header name.
  366. */
  367. CURLcode Curl_altsvc_parse(struct Curl_easy *data,
  368. struct altsvcinfo *asi, const char *value,
  369. enum alpnid srcalpnid, const char *srchost,
  370. unsigned short srcport)
  371. {
  372. const char *p = value;
  373. size_t len;
  374. enum alpnid dstalpnid = srcalpnid; /* the same by default */
  375. char namebuf[MAX_ALTSVC_HOSTLEN] = "";
  376. char alpnbuf[MAX_ALTSVC_ALPNLEN] = "";
  377. struct altsvc *as;
  378. unsigned short dstport = srcport; /* the same by default */
  379. const char *semip;
  380. time_t maxage = 24 * 3600; /* default is 24 hours */
  381. bool persist = FALSE;
  382. CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  383. if(result)
  384. return result;
  385. DEBUGASSERT(asi);
  386. /* Flush all cached alternatives for this source origin, if any */
  387. altsvc_flush(asi, srcalpnid, srchost, srcport);
  388. /* "clear" is a magic keyword */
  389. if(strcasecompare(alpnbuf, "clear")) {
  390. /* TODO: clear whatever it is it should clear */
  391. return CURLE_OK;
  392. }
  393. /* The 'ma' and 'persist' flags are annoyingly meant for all alternatives
  394. but are set after the list on the line. Scan for the semicolons and get
  395. those fields first! */
  396. semip = p;
  397. do {
  398. semip = strchr(semip, ';');
  399. if(semip) {
  400. char option[32];
  401. unsigned long num;
  402. char *end_ptr;
  403. semip++; /* pass the semicolon */
  404. result = getalnum(&semip, option, sizeof(option));
  405. if(result)
  406. break;
  407. while(*semip && ISBLANK(*semip))
  408. semip++;
  409. if(*semip != '=')
  410. continue;
  411. semip++;
  412. num = strtoul(semip, &end_ptr, 10);
  413. if(num < ULONG_MAX) {
  414. if(strcasecompare("ma", option))
  415. maxage = num;
  416. else if(strcasecompare("persist", option) && (num == 1))
  417. persist = TRUE;
  418. }
  419. semip = end_ptr;
  420. }
  421. } while(semip);
  422. do {
  423. if(*p == '=') {
  424. /* [protocol]="[host][:port]" */
  425. dstalpnid = alpn2alpnid(alpnbuf);
  426. if(!dstalpnid) {
  427. infof(data, "Unknown alt-svc protocol \"%s\", ignoring...\n", alpnbuf);
  428. return CURLE_OK;
  429. }
  430. p++;
  431. if(*p == '\"') {
  432. const char *dsthost;
  433. p++;
  434. if(*p != ':') {
  435. /* host name starts here */
  436. const char *hostp = p;
  437. while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
  438. p++;
  439. len = p - hostp;
  440. if(!len || (len >= MAX_ALTSVC_HOSTLEN))
  441. return CURLE_BAD_FUNCTION_ARGUMENT; /* TODO: improve error code */
  442. memcpy(namebuf, hostp, len);
  443. namebuf[len] = 0;
  444. dsthost = namebuf;
  445. }
  446. else {
  447. /* no destination name, use source host */
  448. dsthost = srchost;
  449. }
  450. if(*p == ':') {
  451. /* a port number */
  452. char *end_ptr;
  453. unsigned long port = strtoul(++p, &end_ptr, 10);
  454. if(port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
  455. infof(data, "Unknown alt-svc port number, ignoring...\n");
  456. return CURLE_OK;
  457. }
  458. p = end_ptr;
  459. dstport = curlx_ultous(port);
  460. }
  461. if(*p++ != '\"')
  462. return CURLE_BAD_FUNCTION_ARGUMENT;
  463. as = altsvc_createid(srchost, dsthost,
  464. srcalpnid, dstalpnid,
  465. srcport, dstport);
  466. if(as) {
  467. /* TODO: the expires time also needs to take the Age: value (if any)
  468. into account. [See RFC 7838 section 3.1] */
  469. as->expires = maxage + time(NULL);
  470. as->persist = persist;
  471. Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
  472. asi->num++; /* one more entry */
  473. infof(data, "Added alt-svc: %s:%d over %s\n", dsthost, dstport,
  474. Curl_alpnid2str(dstalpnid));
  475. }
  476. }
  477. /* after the double quote there can be a comma if there's another
  478. string or a semicolon if no more */
  479. if(*p == ',') {
  480. /* comma means another alternative is presented */
  481. p++;
  482. result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
  483. if(result)
  484. /* failed to parse, but since we alredy did at least one host we
  485. return OK */
  486. return CURLE_OK;
  487. }
  488. }
  489. } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r'));
  490. return CURLE_OK;
  491. }
  492. /*
  493. * Return TRUE on a match
  494. */
  495. bool Curl_altsvc_lookup(struct altsvcinfo *asi,
  496. enum alpnid srcalpnid, const char *srchost,
  497. int srcport,
  498. enum alpnid *dstalpnid, const char **dsthost,
  499. int *dstport)
  500. {
  501. struct curl_llist_element *e;
  502. struct curl_llist_element *n;
  503. time_t now = time(NULL);
  504. DEBUGASSERT(asi);
  505. DEBUGASSERT(srchost);
  506. DEBUGASSERT(dsthost);
  507. for(e = asi->list.head; e; e = n) {
  508. struct altsvc *as = e->ptr;
  509. n = e->next;
  510. if(as->expires < now) {
  511. /* an expired entry, remove */
  512. altsvc_free(as);
  513. continue;
  514. }
  515. if((as->srcalpnid == srcalpnid) &&
  516. strcasecompare(as->srchost, srchost) &&
  517. as->srcport == srcport) {
  518. /* match */
  519. *dstalpnid = as->dstalpnid;
  520. *dsthost = as->dsthost;
  521. *dstport = as->dstport;
  522. return TRUE;
  523. }
  524. }
  525. return FALSE;
  526. }
  527. #endif /* CURL_DISABLE_HTTP || USE_ALTSVC */