Coverage Report

Created: 2026-10-22 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/altsvc.c
Line
Count
Source
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
30
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_ALTSVC)
31
#include "urldata.h"
32
#include "altsvc.h"
33
#include "curl_fopen.h"
34
#include "curl_get_line.h"
35
#include "parsedate.h"
36
#include "curl_trc.h"
37
#include "curlx/inet_pton.h"
38
#include "curlx/strparse.h"
39
#include "connect.h"
40
41
0
#define MAX_ALTSVC_LINE    4095
42
0
#define MAX_ALTSVC_DATELEN 17
43
0
#define MAX_ALTSVC_HOSTLEN 2048
44
0
#define MAX_ALTSVC_ALPNLEN 10
45
46
0
#define H3VERSION "h3"
47
48
#if defined(DEBUGBUILD) || defined(UNITTESTS)
49
/* to play well with debug builds, we can *set* a fixed time this will
50
   return */
51
static time_t altsvc_debugtime(void *unused)
52
0
{
53
0
  const char *timestr = getenv("CURL_TIME");
54
0
  (void)unused;
55
0
  if(timestr) {
56
0
    curl_off_t val;
57
0
    curlx_str_number(&timestr, &val, TIME_T_MAX);
58
0
    return (time_t)val;
59
0
  }
60
0
  return time(NULL);
61
0
}
62
#undef time
63
0
#define time(x) altsvc_debugtime(x)
64
#endif
65
66
/* Given the ALPN ID, return the name */
67
const char *Curl_alpnid2str(enum alpnid id)
68
0
{
69
0
  switch(id) {
70
0
  case ALPN_h1:
71
0
    return "h1";
72
0
  case ALPN_h2:
73
0
    return "h2";
74
0
  case ALPN_h3:
75
0
    return H3VERSION;
76
0
  default:
77
0
    return ""; /* bad */
78
0
  }
79
0
}
80
81
static enum alpnid Curl_str2alpnid(const struct Curl_str *cstr)
82
0
{
83
0
  return Curl_alpn2alpnid((const unsigned char *)curlx_str(cstr),
84
0
                          curlx_strlen(cstr));
85
0
}
86
87
0
#define altsvc_free(x) curlx_free(x)
88
89
static struct altsvc *altsvc_createid(const char *srchost,
90
                                      size_t hlen,
91
                                      const char *dsthost,
92
                                      size_t dlen, /* dsthost length */
93
                                      enum alpnid srcalpnid,
94
                                      enum alpnid dstalpnid,
95
                                      size_t srcport,
96
                                      size_t dstport)
97
0
{
98
0
  struct altsvc *as;
99
0
  if((hlen > 2) && srchost[0] == '[') {
100
    /* IPv6 address, strip off brackets */
101
0
    srchost++;
102
0
    hlen -= 2;
103
0
  }
104
0
  else if(hlen && (srchost[hlen - 1] == '.')) {
105
    /* strip off trailing dot */
106
0
    hlen--;
107
0
  }
108
0
  if((dlen > 2) && dsthost[0] == '[') {
109
    /* IPv6 address, strip off brackets */
110
0
    dsthost++;
111
0
    dlen -= 2;
112
0
  }
113
0
  if(!hlen || !dlen)
114
    /* bad input */
115
0
    return NULL;
116
  /* struct size plus both strings */
117
0
  as = curlx_calloc(1, sizeof(struct altsvc) + (hlen + 1) + (dlen + 1));
118
0
  if(!as)
119
0
    return NULL;
120
0
  as->src.host = (char *)as + sizeof(struct altsvc);
121
0
  memcpy(as->src.host, srchost, hlen);
122
  /* the null-terminator is already there */
123
124
0
  as->dst.host = (char *)as + sizeof(struct altsvc) + hlen + 1;
125
0
  memcpy(as->dst.host, dsthost, dlen);
126
  /* the null-terminator is already there */
127
128
0
  as->src.alpnid = srcalpnid;
129
0
  as->dst.alpnid = dstalpnid;
130
0
  as->src.port = (unsigned short)srcport;
131
0
  as->dst.port = (unsigned short)dstport;
132
133
0
  return as;
134
0
}
135
136
static struct altsvc *altsvc_create(struct Curl_str *srchost,
137
                                    struct Curl_str *dsthost,
138
                                    struct Curl_str *srcalpn,
139
                                    struct Curl_str *dstalpn,
140
                                    size_t srcport,
141
                                    size_t dstport)
142
0
{
143
0
  enum alpnid dstalpnid = Curl_str2alpnid(dstalpn);
144
0
  enum alpnid srcalpnid = Curl_str2alpnid(srcalpn);
145
0
  if(!srcalpnid || !dstalpnid)
146
0
    return NULL;
147
0
  return altsvc_createid(curlx_str(srchost), curlx_strlen(srchost),
148
0
                         curlx_str(dsthost), curlx_strlen(dsthost),
149
0
                         srcalpnid, dstalpnid,
150
0
                         srcport, dstport);
151
0
}
152
153
/* append the new entry to the list after possibly removing an old entry
154
   first */
155
static void altsvc_append(struct altsvcinfo *asi, struct altsvc *as)
156
0
{
157
0
  while(Curl_llist_count(&asi->list) >= MAX_ALTSVC_ENTRIES) {
158
    /* It is full. Remove the first entry in the list */
159
0
    struct Curl_llist_node *e = Curl_llist_head(&asi->list);
160
0
    struct altsvc *oldas = Curl_node_elem(e);
161
0
    Curl_node_remove(e);
162
0
    altsvc_free(oldas);
163
0
  }
164
0
  Curl_llist_append(&asi->list, as, &as->node);
165
0
}
166
167
/* only returns SERIOUS errors */
168
static CURLcode altsvc_add(struct altsvcinfo *asi, const char *line)
169
0
{
170
  /* Example line:
171
     h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
172
   */
173
0
  struct Curl_str srchost;
174
0
  struct Curl_str dsthost;
175
0
  struct Curl_str srcalpn;
176
0
  struct Curl_str dstalpn;
177
0
  struct Curl_str date;
178
0
  curl_off_t srcport;
179
0
  curl_off_t dstport;
180
0
  curl_off_t persist;
181
0
  curl_off_t prio;
182
183
0
  if(curlx_str_word(&line, &srcalpn, MAX_ALTSVC_ALPNLEN) ||
184
0
     curlx_str_singlespace(&line) ||
185
0
     curlx_str_word(&line, &srchost, MAX_ALTSVC_HOSTLEN) ||
186
0
     curlx_str_singlespace(&line) ||
187
0
     curlx_str_number(&line, &srcport, 65535) ||
188
0
     curlx_str_singlespace(&line) ||
189
0
     curlx_str_word(&line, &dstalpn, MAX_ALTSVC_ALPNLEN) ||
190
0
     curlx_str_singlespace(&line) ||
191
0
     curlx_str_word(&line, &dsthost, MAX_ALTSVC_HOSTLEN) ||
192
0
     curlx_str_singlespace(&line) ||
193
0
     curlx_str_number(&line, &dstport, 65535) ||
194
0
     curlx_str_singlespace(&line) ||
195
0
     curlx_str_quotedword(&line, &date, MAX_ALTSVC_DATELEN) ||
196
0
     curlx_str_singlespace(&line) ||
197
0
     curlx_str_number(&line, &persist, 1) ||
198
0
     curlx_str_singlespace(&line) ||
199
0
     curlx_str_number(&line, &prio, 0) ||
200
0
     curlx_str_newline(&line))
201
0
    ;
202
0
  else {
203
0
    char dbuf[MAX_ALTSVC_DATELEN + 1];
204
0
    time_t expires = 0;
205
0
    time_t now = time(NULL);
206
207
    /* The date parser works on a null-terminated string. The maximum length
208
       is upheld by curlx_str_quotedword(). */
209
0
    memcpy(dbuf, curlx_str(&date), curlx_strlen(&date));
210
0
    dbuf[curlx_strlen(&date)] = 0;
211
0
    Curl_getdate_capped(dbuf, &expires);
212
213
0
    if(now < expires) {
214
0
      struct altsvc *as = altsvc_create(&srchost, &dsthost, &srcalpn, &dstalpn,
215
0
                                        (size_t)srcport, (size_t)dstport);
216
0
      if(as) {
217
0
        as->expires = expires;
218
0
        as->persist = persist ? 1 : 0;
219
0
        altsvc_append(asi, as);
220
0
      }
221
0
      else
222
0
        return CURLE_OUT_OF_MEMORY;
223
0
    }
224
0
  }
225
226
0
  return CURLE_OK;
227
0
}
228
229
/*
230
 * Load alt-svc entries from the given file. The text based line-oriented file
231
 * format is documented here: https://curl.se/docs/alt-svc.html
232
 *
233
 * This function only returns error on major problems that prevent alt-svc
234
 * handling to work completely. It will ignore individual syntactical errors
235
 * etc.
236
 */
237
static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
238
0
{
239
0
  CURLcode result = CURLE_OK;
240
0
  FILE *fp;
241
242
  /* we need a private copy of the filename so that the altsvc cache file
243
     name survives an easy handle reset */
244
0
  curlx_free(asi->filename);
245
0
  asi->filename = curlx_strdup(file);
246
0
  if(!asi->filename)
247
0
    return CURLE_OUT_OF_MEMORY;
248
249
0
  fp = curlx_fopen(file, FOPEN_READTEXT);
250
0
  if(fp) {
251
0
    curlx_struct_stat stat;
252
0
    if((curlx_fstat(fileno(fp), &stat) == -1) || !S_ISDIR(stat.st_mode)) {
253
0
      bool eof = FALSE;
254
0
      struct dynbuf buf;
255
0
      curlx_dyn_init(&buf, MAX_ALTSVC_LINE);
256
0
      do {
257
0
        result = Curl_get_line(&buf, fp, &eof);
258
0
        if(!result) {
259
0
          const char *lineptr = curlx_dyn_ptr(&buf);
260
0
          curlx_str_passblanks(&lineptr);
261
0
          if(curlx_str_single(&lineptr, '#'))
262
0
            altsvc_add(asi, lineptr);
263
0
        }
264
0
      } while(!result && !eof);
265
0
      curlx_dyn_free(&buf); /* free the line buffer */
266
0
    }
267
0
    curlx_fclose(fp);
268
0
  }
269
0
  return result;
270
0
}
271
272
/*
273
 * Write this single altsvc entry to a single output line
274
 */
275
static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
276
0
{
277
0
  struct tm stamp;
278
0
  const char *dst6_pre = "";
279
0
  const char *dst6_post = "";
280
0
  const char *src6_pre = "";
281
0
  const char *src6_post = "";
282
0
  CURLcode result = curlx_gmtime(as->expires, &stamp);
283
0
  if(result)
284
    /* fail silently for date errors so that only this entry gets
285
       skipped */
286
0
    return CURLE_OK;
287
0
#ifdef USE_IPV6
288
0
  else {
289
0
    char ipv6_unused[16];
290
0
    if(curlx_inet_pton(AF_INET6, as->dst.host, ipv6_unused) == 1) {
291
0
      dst6_pre = "[";
292
0
      dst6_post = "]";
293
0
    }
294
0
    if(curlx_inet_pton(AF_INET6, as->src.host, ipv6_unused) == 1) {
295
0
      src6_pre = "[";
296
0
      src6_post = "]";
297
0
    }
298
0
  }
299
0
#endif
300
0
  curl_mfprintf(fp,
301
0
                "%s %s%s%s %u "
302
0
                "%s %s%s%s %u "
303
0
                "\"%d%02d%02d "
304
0
                "%02d:%02d:%02d\" "
305
0
                "%d 0\n", /* prio still always zero */
306
0
                Curl_alpnid2str(as->src.alpnid),
307
0
                src6_pre, as->src.host, src6_post,
308
0
                as->src.port,
309
310
0
                Curl_alpnid2str(as->dst.alpnid),
311
0
                dst6_pre, as->dst.host, dst6_post,
312
0
                as->dst.port,
313
314
0
                stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
315
0
                stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
316
0
                as->persist);
317
0
  return CURLE_OK;
318
0
}
319
320
/* ---- library-wide functions below ---- */
321
322
/*
323
 * Curl_altsvc_init() creates a new altsvc cache.
324
 * It returns the new instance or NULL if something goes wrong.
325
 */
326
struct altsvcinfo *Curl_altsvc_init(void)
327
0
{
328
0
  struct altsvcinfo *asi = curlx_calloc(1, sizeof(struct altsvcinfo));
329
0
  if(!asi)
330
0
    return NULL;
331
0
  Curl_llist_init(&asi->list, NULL);
332
333
  /* set default behavior */
334
0
  asi->flags = CURLALTSVC_H1
335
0
#ifdef USE_HTTP2
336
0
    | CURLALTSVC_H2
337
0
#endif
338
#ifdef USE_HTTP3
339
    | CURLALTSVC_H3
340
#endif
341
0
    ;
342
0
  return asi;
343
0
}
344
345
/*
346
 * Curl_altsvc_load() loads alt-svc from file.
347
 */
348
CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
349
0
{
350
0
  DEBUGASSERT(asi);
351
0
  return altsvc_load(asi, file);
352
0
}
353
354
/*
355
 * Curl_altsvc_ctrl() passes on the external bitmask.
356
 */
357
CURLcode Curl_altsvc_ctrl(struct Curl_easy *data, const long ctrl)
358
0
{
359
0
  DEBUGASSERT(data);
360
0
  if(!ctrl)
361
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
362
363
0
  if(!data->asi) {
364
0
    data->asi = Curl_altsvc_init();
365
0
    if(!data->asi)
366
0
      return CURLE_OUT_OF_MEMORY;
367
0
  }
368
0
  data->asi->flags = ctrl;
369
0
  return CURLE_OK;
370
0
}
371
372
/*
373
 * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
374
 * resources.
375
 */
376
void Curl_altsvc_cleanup(struct altsvcinfo **asi)
377
0
{
378
0
  if(*asi) {
379
0
    struct Curl_llist_node *e;
380
0
    struct Curl_llist_node *n;
381
0
    struct altsvcinfo *altsvc = *asi;
382
0
    for(e = Curl_llist_head(&altsvc->list); e; e = n) {
383
0
      struct altsvc *as = Curl_node_elem(e);
384
0
      n = Curl_node_next(e);
385
0
      altsvc_free(as);
386
0
    }
387
0
    curlx_free(altsvc->filename);
388
0
    curlx_free(altsvc);
389
0
    *asi = NULL; /* clear the pointer */
390
0
  }
391
0
}
392
393
/*
394
 * Curl_altsvc_save() writes the altsvc cache to a file.
395
 */
396
CURLcode Curl_altsvc_save(struct Curl_easy *data,
397
                          struct altsvcinfo *asi, const char *file)
398
0
{
399
0
  CURLcode result = CURLE_OK;
400
0
  FILE *out;
401
0
  char *tempstore = NULL;
402
403
0
  if(!asi)
404
    /* no cache activated */
405
0
    return CURLE_OK;
406
407
  /* if not new name is given, use the one we stored from the load */
408
0
  if(!file && asi->filename)
409
0
    file = asi->filename;
410
411
0
  if((asi->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
412
    /* marked as read-only, no file or zero length filename */
413
0
    return CURLE_OK;
414
415
0
  result = Curl_fopen(data, file, &out, &tempstore);
416
0
  if(!result) {
417
0
    struct Curl_llist_node *e;
418
0
    struct Curl_llist_node *n;
419
0
    fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
420
0
          "# This file was generated by libcurl! Edit at your own risk.\n",
421
0
          out);
422
0
    for(e = Curl_llist_head(&asi->list); e; e = n) {
423
0
      struct altsvc *as = Curl_node_elem(e);
424
0
      n = Curl_node_next(e);
425
0
      result = altsvc_out(as, out);
426
0
      if(result)
427
0
        break;
428
0
    }
429
0
    curlx_fclose(out);
430
0
    if(!result && tempstore && curlx_rename(tempstore, file))
431
0
      result = CURLE_WRITE_ERROR;
432
433
0
    if(result && tempstore)
434
0
      unlink(tempstore);
435
0
  }
436
0
  curlx_free(tempstore);
437
0
  return result;
438
0
}
439
440
/* hostcompare() returns true if 'host' matches 'check'. The first host
441
 * argument may have a trailing dot present that will be ignored.
442
 */
443
static bool hostcompare(const char *host, const char *check)
444
0
{
445
0
  size_t hlen = strlen(host);
446
0
  size_t clen = strlen(check);
447
448
0
  if(hlen && (host[hlen - 1] == '.'))
449
0
    hlen--;
450
0
  if(hlen != clen)
451
    /* they cannot match if they have different lengths */
452
0
    return FALSE;
453
0
  return curl_strnequal(host, check, hlen);
454
0
}
455
456
/* altsvc_flush() removes all alternatives for this source origin from the
457
   list */
458
static void altsvc_flush(struct altsvcinfo *asi,
459
                         struct Curl_peer *origin,
460
                         enum alpnid origin_alpnid)
461
0
{
462
0
  struct Curl_llist_node *e;
463
0
  struct Curl_llist_node *n;
464
0
  for(e = Curl_llist_head(&asi->list); e; e = n) {
465
0
    struct altsvc *as = Curl_node_elem(e);
466
0
    n = Curl_node_next(e);
467
0
    if((origin_alpnid == as->src.alpnid) &&
468
0
       (origin->port == as->src.port) &&
469
0
       hostcompare(origin->hostname, as->src.host)) {
470
0
      Curl_node_remove(e);
471
0
      altsvc_free(as);
472
0
    }
473
0
  }
474
0
}
475
476
0
#define CAP_ALTSVC_MAX_AGE (2*365*24*3600) /* two years cap */
477
478
0
#define ALTSVC_MA      1
479
0
#define ALTSVC_PERSIST 2
480
static void altsvc_parse_params(const char **pp,
481
                                time_t *pmaxage,
482
                                bool *ppersist)
483
0
{
484
0
  curlx_str_passblanks(pp);
485
0
  if(curlx_str_single(pp, ';'))
486
0
    return;
487
488
0
  for(;;) {
489
0
    struct Curl_str name;
490
0
    struct Curl_str val;
491
0
    curl_off_t num;
492
0
    int keyword = 0;
493
494
    /* allow some extra whitespaces around name and value */
495
0
    if(curlx_str_until(pp, &name, MAX_ALTSVC_LINE, '=') ||
496
0
       curlx_str_single(pp, '='))
497
0
      break;  /* skip further parameter parsing */
498
499
0
    curlx_str_trimblanks(&name);
500
0
    curlx_str_passblanks(pp);
501
0
    if(**pp == '\"') {
502
0
      if(curlx_str_quotedword(pp, &val, MAX_ALTSVC_LINE))
503
0
        break;
504
0
    }
505
0
    else {
506
0
      if(curlx_str_cspn(pp, &val, ",;\r\n"))
507
0
        break;
508
0
    }
509
0
    curlx_str_trimblanks(&val);
510
511
0
    if(curlx_str_casecompare(&name, "ma"))
512
0
      keyword = ALTSVC_MA;
513
0
    else if(curlx_str_casecompare(&name, "persist"))
514
0
      keyword = ALTSVC_PERSIST;
515
516
0
    if(keyword) {
517
0
      const char *vp = curlx_str(&val);
518
0
      const char *vend = vp + curlx_strlen(&val);
519
0
      if(curlx_str_number(&vp, &num, CURL_OFF_T_MAX))
520
0
        break; /* not a number, skip further parameter parsing */
521
0
      if(vp != vend)
522
0
        break; /* not entirely a number, skip further parameter parsing */
523
0
      if(keyword == ALTSVC_MA) {
524
0
        if(num > CAP_ALTSVC_MAX_AGE)
525
0
          num = CAP_ALTSVC_MAX_AGE;
526
0
        *pmaxage = (time_t)num;
527
0
      }
528
0
      else if(num == 1)
529
0
        *ppersist = TRUE;
530
0
    }
531
532
0
    curlx_str_passblanks(pp);
533
0
    if(curlx_str_single(pp, ';'))
534
0
      break; /* no further parameters */
535
0
  }
536
0
}
537
538
static bool altsvc_parse_dest(const char **pp,
539
                              struct Curl_easy *data,
540
                              struct Curl_peer *origin,
541
                              struct Curl_str *dsthost,
542
                              uint16_t *pdstport)
543
0
{
544
0
  curl_off_t port = 0;
545
546
0
  if(curlx_str_single(pp, '\"'))
547
0
    return FALSE;
548
549
  /* quoted string, with hostname or just :port ? */
550
0
  if(curlx_str_single(pp, ':')) { /* is hostname:port ? */
551
0
    if(curlx_str_single(pp, '[')) { /* DNS hostname/IPv4 */
552
0
      if(curlx_str_until(pp, dsthost, MAX_ALTSVC_HOSTLEN, ':')) {
553
0
        infof(data, "Bad alt-svc hostname, ignoring.");
554
0
        return FALSE;
555
0
      }
556
0
    }
557
0
    else { /* IPv6 hostname */
558
0
      if(curlx_str_until(pp, dsthost, MAX_IPADR_LEN, ']') ||
559
0
         curlx_str_single(pp, ']')) {
560
0
        infof(data, "Bad alt-svc IPv6 hostname, ignoring.");
561
0
        return FALSE;
562
0
      }
563
0
    }
564
0
    if(curlx_str_single(pp, ':'))
565
0
      return FALSE; /* not followed by ':' */
566
0
  }
567
0
  else  /* is only :port, hostname is effectively origin */
568
0
    curlx_str_assign(dsthost, origin->hostname,
569
0
                     strlen(origin->hostname));
570
571
0
  if(curlx_str_number(pp, &port, 0xffff)) {
572
0
    infof(data, "Unknown alt-svc port number, ignoring.");
573
0
    return FALSE;
574
0
  }
575
576
0
  *pdstport = (uint16_t)port;
577
0
  if(curlx_str_single(pp, '\"'))
578
0
    return FALSE; /* quoted string not ending here as expected */
579
0
  return TRUE;
580
0
}
581
582
/*
583
 * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
584
 * the data correctly in the cache.
585
 *
586
 * 'value' points to the header *value*. That is contents to the right of the
587
 * header name.
588
 *
589
 * Currently this function rejects invalid data without returning an error.
590
 * Invalid hostname, port number will result in the specific alternative
591
 * being rejected. Unknown protocols are skipped.
592
 */
593
CURLcode Curl_altsvc_parse(struct Curl_easy *data,
594
                           struct altsvcinfo *asi, const char *value,
595
                           struct Curl_peer *origin,
596
                           enum alpnid origin_alpnid)
597
0
{
598
0
  struct altsvc *as;
599
0
  size_t entries = 0;
600
0
  struct Curl_str alpn;
601
0
  const char *p;
602
603
0
  DEBUGASSERT(asi);
604
0
  DEBUGASSERT(origin);
605
  /* RFC 7838, The "Alt-Svc" header field value is basically
606
   * Alt-Svc: (clear|alpn="(host)?:port"\s*(;\s*parameter=value)*)
607
   * This can be repeated, comma-separated.
608
   *
609
   * We parse "best effort", ignoring values we do not recognize.
610
   */
611
612
  /* Try to catch a standalone "clear" */
613
0
  p = value;
614
0
  if(!curlx_str_cspn(&p, &alpn, ";\n\r")) {
615
0
    curlx_str_trimblanks(&alpn);
616
    /* "clear" is a magic keyword */
617
0
    if(curlx_str_casecompare(&alpn, "clear")) {
618
      /* Flush cached alternatives for this source origin */
619
0
      altsvc_flush(asi, origin, origin_alpnid);
620
0
      return CURLE_OK;
621
0
    }
622
0
  }
623
624
  /* Not a standalone "clear", parse from start for alpn entries */
625
0
  for(p = value; *p;) {
626
0
    time_t maxage = 24 * 3600; /* default is 24 hours */
627
0
    bool persist = FALSE;
628
0
    enum alpnid dstalpnid;
629
0
    struct Curl_str dsthost;
630
0
    uint16_t dstport;
631
632
0
    if(curlx_str_until(&p, &alpn, MAX_ALTSVC_LINE, '='))
633
0
      break; /* not another entry, leave */
634
0
    curlx_str_trimblanks(&alpn);
635
0
    dstalpnid = Curl_str2alpnid(&alpn);
636
637
0
    if(curlx_str_single(&p, '='))
638
0
      break;
639
640
    /* Parse altsvc hostname:port */
641
0
    if(!altsvc_parse_dest(&p, data, origin, &dsthost, &dstport))
642
0
      break;
643
644
    /* Parse optional parameters */
645
0
    altsvc_parse_params(&p, &maxage, &persist);
646
647
0
    if(dstalpnid) { /* this is a known ALPN id, e.g. not ALPN_none */
648
0
      if(!entries++)
649
        /* Flush cached alternatives for this source origin, if any - when
650
           this is the first entry of the line. */
651
0
        altsvc_flush(asi, origin, origin_alpnid);
652
653
0
      as = altsvc_createid(origin->hostname, strlen(origin->hostname),
654
0
                           curlx_str(&dsthost),
655
0
                           curlx_strlen(&dsthost),
656
0
                           origin_alpnid, dstalpnid,
657
0
                           origin->port, dstport);
658
0
      if(as) {
659
0
        time_t secs = time(NULL);
660
        /* The expires time also needs to take the Age: value (if any)
661
           into account. [See RFC 7838 section 3.1] */
662
0
        if(maxage > (TIME_T_MAX - secs))
663
0
          as->expires = TIME_T_MAX;
664
0
        else
665
0
          as->expires = maxage + secs;
666
0
        as->persist = persist;
667
0
        altsvc_append(asi, as);
668
0
        infof(data, "Added alt-svc: %.*s:%u over %s",
669
0
              (int)curlx_strlen(&dsthost), curlx_str(&dsthost),
670
0
              dstport, Curl_alpnid2str(dstalpnid));
671
0
      }
672
0
      else
673
0
        return CURLE_OUT_OF_MEMORY;
674
0
    }
675
676
    /* When this is followed by a comma, we expect another entry */
677
0
    if(curlx_str_single(&p, ','))
678
0
      break;
679
0
  }
680
681
0
  return CURLE_OK;
682
0
}
683
684
/*
685
 * Return TRUE on a match
686
 */
687
bool Curl_altsvc_lookup(struct altsvcinfo *asi,
688
                        struct Curl_peer *origin,
689
                        enum alpnid origin_alpnid,
690
                        struct altsvc **dstentry,
691
                        const int versions, /* one or more bits */
692
                        bool *psame_destination)
693
0
{
694
0
  DEBUGASSERT(asi);
695
0
  DEBUGASSERT(origin);
696
0
  DEBUGASSERT(dstentry);
697
0
  *psame_destination = FALSE;
698
699
0
  if(Curl_llist_count(&asi->list)) {
700
0
    struct Curl_llist_node *e;
701
0
    struct Curl_llist_node *n;
702
0
    time_t now = time(NULL);
703
704
0
    for(e = Curl_llist_head(&asi->list); e; e = n) {
705
0
      struct altsvc *as = Curl_node_elem(e);
706
0
      n = Curl_node_next(e);
707
0
      if(as->expires < now) {
708
        /* an expired entry, remove */
709
0
        Curl_node_remove(e);
710
0
        altsvc_free(as);
711
0
        continue;
712
0
      }
713
0
      if((origin_alpnid == as->src.alpnid) &&
714
0
         (versions & (int)as->dst.alpnid) &&
715
0
         (origin->port == as->src.port) &&
716
0
         hostcompare(origin->hostname, as->src.host)) {
717
        /* match */
718
0
        *dstentry = as;
719
        /* alt-svc on the same host+port or another one? */
720
0
        *psame_destination = (origin->port == as->dst.port) &&
721
0
                             hostcompare(origin->hostname, as->dst.host);
722
0
        return TRUE;
723
0
      }
724
0
    }
725
0
  }
726
0
  return FALSE;
727
0
}
728
729
#if defined(DEBUGBUILD) || defined(UNITTESTS)
730
#undef time
731
#endif
732
733
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_ALTSVC */