Coverage Report

Created: 2026-09-01 06:58

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
9.88k
#define MAX_ALTSVC_LINE    4095
42
0
#define MAX_ALTSVC_DATELEN 17
43
0
#define MAX_ALTSVC_HOSTLEN 2048
44
9.88k
#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
9.88k
{
170
  /* Example line:
171
     h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
172
   */
173
9.88k
  struct Curl_str srchost;
174
9.88k
  struct Curl_str dsthost;
175
9.88k
  struct Curl_str srcalpn;
176
9.88k
  struct Curl_str dstalpn;
177
9.88k
  struct Curl_str date;
178
9.88k
  curl_off_t srcport;
179
9.88k
  curl_off_t dstport;
180
9.88k
  curl_off_t persist;
181
9.88k
  curl_off_t prio;
182
183
9.88k
  if(curlx_str_word(&line, &srcalpn, MAX_ALTSVC_ALPNLEN) ||
184
9.88k
     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
9.88k
    ;
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
9.88k
  return CURLE_OK;
227
9.88k
}
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
9.88k
{
239
9.88k
  CURLcode result = CURLE_OK;
240
9.88k
  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
9.88k
  curlx_free(asi->filename);
245
9.88k
  asi->filename = curlx_strdup(file);
246
9.88k
  if(!asi->filename)
247
0
    return CURLE_OUT_OF_MEMORY;
248
249
9.88k
  fp = curlx_fopen(file, FOPEN_READTEXT);
250
9.88k
  if(fp) {
251
9.88k
    curlx_struct_stat stat;
252
9.88k
    if((curlx_fstat(fileno(fp), &stat) == -1) || !S_ISDIR(stat.st_mode)) {
253
9.88k
      bool eof = FALSE;
254
9.88k
      struct dynbuf buf;
255
9.88k
      curlx_dyn_init(&buf, MAX_ALTSVC_LINE);
256
9.88k
      do {
257
9.88k
        result = Curl_get_line(&buf, fp, &eof);
258
9.88k
        if(!result) {
259
9.88k
          const char *lineptr = curlx_dyn_ptr(&buf);
260
9.88k
          curlx_str_passblanks(&lineptr);
261
9.88k
          if(curlx_str_single(&lineptr, '#'))
262
9.88k
            altsvc_add(asi, lineptr);
263
9.88k
        }
264
9.88k
      } while(!result && !eof);
265
9.88k
      curlx_dyn_free(&buf); /* free the line buffer */
266
9.88k
    }
267
9.88k
    curlx_fclose(fp);
268
9.88k
  }
269
9.88k
  return result;
270
9.88k
}
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
0
    return result;
285
0
#ifdef USE_IPV6
286
0
  else {
287
0
    char ipv6_unused[16];
288
0
    if(curlx_inet_pton(AF_INET6, as->dst.host, ipv6_unused) == 1) {
289
0
      dst6_pre = "[";
290
0
      dst6_post = "]";
291
0
    }
292
0
    if(curlx_inet_pton(AF_INET6, as->src.host, ipv6_unused) == 1) {
293
0
      src6_pre = "[";
294
0
      src6_post = "]";
295
0
    }
296
0
  }
297
0
#endif
298
0
  curl_mfprintf(fp,
299
0
                "%s %s%s%s %u "
300
0
                "%s %s%s%s %u "
301
0
                "\"%d%02d%02d "
302
0
                "%02d:%02d:%02d\" "
303
0
                "%d 0\n", /* prio still always zero */
304
0
                Curl_alpnid2str(as->src.alpnid),
305
0
                src6_pre, as->src.host, src6_post,
306
0
                as->src.port,
307
308
0
                Curl_alpnid2str(as->dst.alpnid),
309
0
                dst6_pre, as->dst.host, dst6_post,
310
0
                as->dst.port,
311
312
0
                stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
313
0
                stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
314
0
                as->persist);
315
0
  return CURLE_OK;
316
0
}
317
318
/* ---- library-wide functions below ---- */
319
320
/*
321
 * Curl_altsvc_init() creates a new altsvc cache.
322
 * It returns the new instance or NULL if something goes wrong.
323
 */
324
struct altsvcinfo *Curl_altsvc_init(void)
325
9.89k
{
326
9.89k
  struct altsvcinfo *asi = curlx_calloc(1, sizeof(struct altsvcinfo));
327
9.89k
  if(!asi)
328
0
    return NULL;
329
9.89k
  Curl_llist_init(&asi->list, NULL);
330
331
  /* set default behavior */
332
9.89k
  asi->flags = CURLALTSVC_H1
333
9.89k
#ifdef USE_HTTP2
334
9.89k
    | CURLALTSVC_H2
335
9.89k
#endif
336
#ifdef USE_HTTP3
337
    | CURLALTSVC_H3
338
#endif
339
9.89k
    ;
340
9.89k
  return asi;
341
9.89k
}
342
343
/*
344
 * Curl_altsvc_load() loads alt-svc from file.
345
 */
346
CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
347
9.88k
{
348
9.88k
  DEBUGASSERT(asi);
349
9.88k
  return altsvc_load(asi, file);
350
9.88k
}
351
352
/*
353
 * Curl_altsvc_ctrl() passes on the external bitmask.
354
 */
355
CURLcode Curl_altsvc_ctrl(struct Curl_easy *data, const long ctrl)
356
51
{
357
51
  DEBUGASSERT(data);
358
51
  if(!ctrl)
359
1
    return CURLE_BAD_FUNCTION_ARGUMENT;
360
361
50
  if(!data->asi) {
362
50
    data->asi = Curl_altsvc_init();
363
50
    if(!data->asi)
364
0
      return CURLE_OUT_OF_MEMORY;
365
50
  }
366
50
  data->asi->flags = ctrl;
367
50
  return CURLE_OK;
368
50
}
369
370
/*
371
 * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
372
 * resources.
373
 */
374
void Curl_altsvc_cleanup(struct altsvcinfo **asi)
375
21.9k
{
376
21.9k
  if(*asi) {
377
9.89k
    struct Curl_llist_node *e;
378
9.89k
    struct Curl_llist_node *n;
379
9.89k
    struct altsvcinfo *altsvc = *asi;
380
9.89k
    for(e = Curl_llist_head(&altsvc->list); e; e = n) {
381
0
      struct altsvc *as = Curl_node_elem(e);
382
0
      n = Curl_node_next(e);
383
0
      altsvc_free(as);
384
0
    }
385
9.89k
    curlx_free(altsvc->filename);
386
9.89k
    curlx_free(altsvc);
387
9.89k
    *asi = NULL; /* clear the pointer */
388
9.89k
  }
389
21.9k
}
390
391
/*
392
 * Curl_altsvc_save() writes the altsvc cache to a file.
393
 */
394
CURLcode Curl_altsvc_save(struct Curl_easy *data,
395
                          struct altsvcinfo *asi, const char *file)
396
21.9k
{
397
21.9k
  CURLcode result = CURLE_OK;
398
21.9k
  FILE *out;
399
21.9k
  char *tempstore = NULL;
400
401
21.9k
  if(!asi)
402
    /* no cache activated */
403
12.0k
    return CURLE_OK;
404
405
  /* if not new name is given, use the one we stored from the load */
406
9.89k
  if(!file && asi->filename)
407
0
    file = asi->filename;
408
409
9.89k
  if((asi->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
410
    /* marked as read-only, no file or zero length filename */
411
37
    return CURLE_OK;
412
413
9.85k
  result = Curl_fopen(data, file, &out, &tempstore);
414
9.85k
  if(!result) {
415
9.85k
    struct Curl_llist_node *e;
416
9.85k
    struct Curl_llist_node *n;
417
9.85k
    fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
418
9.85k
          "# This file was generated by libcurl! Edit at your own risk.\n",
419
9.85k
          out);
420
9.85k
    for(e = Curl_llist_head(&asi->list); e; e = n) {
421
0
      struct altsvc *as = Curl_node_elem(e);
422
0
      n = Curl_node_next(e);
423
0
      result = altsvc_out(as, out);
424
0
      if(result)
425
0
        break;
426
0
    }
427
9.85k
    curlx_fclose(out);
428
9.85k
    if(!result && tempstore && curlx_rename(tempstore, file))
429
0
      result = CURLE_WRITE_ERROR;
430
431
9.85k
    if(result && tempstore)
432
0
      unlink(tempstore);
433
9.85k
  }
434
9.85k
  curlx_free(tempstore);
435
9.85k
  return result;
436
9.89k
}
437
438
/* hostcompare() returns true if 'host' matches 'check'. The first host
439
 * argument may have a trailing dot present that will be ignored.
440
 */
441
static bool hostcompare(const char *host, const char *check)
442
0
{
443
0
  size_t hlen = strlen(host);
444
0
  size_t clen = strlen(check);
445
446
0
  if(hlen && (host[hlen - 1] == '.'))
447
0
    hlen--;
448
0
  if(hlen != clen)
449
    /* they cannot match if they have different lengths */
450
0
    return FALSE;
451
0
  return curl_strnequal(host, check, hlen);
452
0
}
453
454
/* altsvc_flush() removes all alternatives for this source origin from the
455
   list */
456
static void altsvc_flush(struct altsvcinfo *asi,
457
                         struct Curl_peer *origin,
458
                         enum alpnid origin_alpnid)
459
0
{
460
0
  struct Curl_llist_node *e;
461
0
  struct Curl_llist_node *n;
462
0
  for(e = Curl_llist_head(&asi->list); e; e = n) {
463
0
    struct altsvc *as = Curl_node_elem(e);
464
0
    n = Curl_node_next(e);
465
0
    if((origin_alpnid == as->src.alpnid) &&
466
0
       (origin->port == as->src.port) &&
467
0
       hostcompare(origin->hostname, as->src.host)) {
468
0
      Curl_node_remove(e);
469
0
      altsvc_free(as);
470
0
    }
471
0
  }
472
0
}
473
474
0
#define ALTSVC_MA      1
475
0
#define ALTSVC_PERSIST 2
476
static void altsvc_parse_params(const char **pp,
477
                                time_t *pmaxage,
478
                                bool *ppersist)
479
0
{
480
0
  curlx_str_passblanks(pp);
481
0
  if(curlx_str_single(pp, ';'))
482
0
    return;
483
484
0
  for(;;) {
485
0
    struct Curl_str name;
486
0
    struct Curl_str val;
487
0
    curl_off_t num;
488
0
    int keyword = 0;
489
490
    /* allow some extra whitespaces around name and value */
491
0
    if(curlx_str_until(pp, &name, MAX_ALTSVC_LINE, '=') ||
492
0
       curlx_str_single(pp, '='))
493
0
      break;  /* skip further parameter parsing */
494
495
0
    curlx_str_trimblanks(&name);
496
0
    curlx_str_passblanks(pp);
497
0
    if(**pp == '\"') {
498
0
      if(curlx_str_quotedword(pp, &val, MAX_ALTSVC_LINE))
499
0
        break;
500
0
    }
501
0
    else {
502
0
      if(curlx_str_cspn(pp, &val, ",;\r\n"))
503
0
        break;
504
0
    }
505
0
    curlx_str_trimblanks(&val);
506
507
0
    if(curlx_str_casecompare(&name, "ma"))
508
0
      keyword = ALTSVC_MA;
509
0
    else if(curlx_str_casecompare(&name, "persist"))
510
0
      keyword = ALTSVC_PERSIST;
511
512
0
    if(keyword) {
513
0
      const char *vp = curlx_str(&val);
514
0
      const char *vend = vp + curlx_strlen(&val);
515
0
      if(curlx_str_number(&vp, &num, TIME_T_MAX))
516
0
        break; /* not a number, skip further parameter parsing */
517
0
      if(vp != vend)
518
0
        break; /* not entirely a number, skip further parameter parsing */
519
0
      if(keyword == ALTSVC_MA)
520
0
        *pmaxage = (time_t)num;
521
0
      else if(num == 1)
522
0
        *ppersist = TRUE;
523
0
    }
524
525
0
    curlx_str_passblanks(pp);
526
0
    if(curlx_str_single(pp, ';'))
527
0
      break; /* no further parameters */
528
0
  }
529
0
}
530
531
static bool altsvc_parse_dest(const char **pp,
532
                              struct Curl_easy *data,
533
                              struct Curl_peer *origin,
534
                              struct Curl_str *dsthost,
535
                              uint16_t *pdstport)
536
0
{
537
0
  curl_off_t port = 0;
538
539
0
  if(curlx_str_single(pp, '\"'))
540
0
    return FALSE;
541
542
  /* quoted string, with hostname or just :port ? */
543
0
  if(curlx_str_single(pp, ':')) { /* is hostname:port ? */
544
0
    if(curlx_str_single(pp, '[')) { /* DNS hostname/IPv4 */
545
0
      if(curlx_str_until(pp, dsthost, MAX_ALTSVC_HOSTLEN, ':')) {
546
0
        infof(data, "Bad alt-svc hostname, ignoring.");
547
0
        return FALSE;
548
0
      }
549
0
    }
550
0
    else { /* IPv6 hostname */
551
0
      if(curlx_str_until(pp, dsthost, MAX_IPADR_LEN, ']') ||
552
0
         curlx_str_single(pp, ']')) {
553
0
        infof(data, "Bad alt-svc IPv6 hostname, ignoring.");
554
0
        return FALSE;
555
0
      }
556
0
    }
557
0
    if(curlx_str_single(pp, ':'))
558
0
      return FALSE; /* not followed by ':' */
559
0
  }
560
0
  else  /* is only :port, hostname is effectively origin */
561
0
    curlx_str_assign(dsthost, origin->hostname,
562
0
                     strlen(origin->hostname));
563
564
0
  if(curlx_str_number(pp, &port, 0xffff)) {
565
0
    infof(data, "Unknown alt-svc port number, ignoring.");
566
0
    return FALSE;
567
0
  }
568
569
0
  *pdstport = (uint16_t)port;
570
0
  if(curlx_str_single(pp, '\"'))
571
0
    return FALSE; /* quoted string not ending here as expected */
572
0
  return TRUE;
573
0
}
574
575
/*
576
 * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
577
 * the data correctly in the cache.
578
 *
579
 * 'value' points to the header *value*. That is contents to the right of the
580
 * header name.
581
 *
582
 * Currently this function rejects invalid data without returning an error.
583
 * Invalid hostname, port number will result in the specific alternative
584
 * being rejected. Unknown protocols are skipped.
585
 */
586
CURLcode Curl_altsvc_parse(struct Curl_easy *data,
587
                           struct altsvcinfo *asi, const char *value,
588
                           struct Curl_peer *origin,
589
                           enum alpnid origin_alpnid)
590
0
{
591
0
  struct altsvc *as;
592
0
  size_t entries = 0;
593
0
  struct Curl_str alpn;
594
0
  const char *p;
595
596
0
  DEBUGASSERT(asi);
597
0
  DEBUGASSERT(origin);
598
  /* RFC 7838, The "Alt-Svc" header field value is basically
599
   * Alt-Svc: (clear|alpn="(host)?:port"\s*(;\s*parameter=value)*)
600
   * This can be repeated, comma-separated.
601
   *
602
   * We parse "best effort", ignoring values we do not recognize.
603
   */
604
605
  /* Try to catch a standalone "clear" */
606
0
  p = value;
607
0
  if(!curlx_str_cspn(&p, &alpn, ";\n\r")) {
608
0
    curlx_str_trimblanks(&alpn);
609
    /* "clear" is a magic keyword */
610
0
    if(curlx_str_casecompare(&alpn, "clear")) {
611
      /* Flush cached alternatives for this source origin */
612
0
      altsvc_flush(asi, origin, origin_alpnid);
613
0
      return CURLE_OK;
614
0
    }
615
0
  }
616
617
  /* Not a standalone "clear", parse from start for alpn entries */
618
0
  for(p = value; *p;) {
619
0
    time_t maxage = 24 * 3600; /* default is 24 hours */
620
0
    bool persist = FALSE;
621
0
    enum alpnid dstalpnid;
622
0
    struct Curl_str dsthost;
623
0
    uint16_t dstport;
624
625
0
    if(curlx_str_until(&p, &alpn, MAX_ALTSVC_LINE, '='))
626
0
      break; /* not another entry, leave */
627
0
    curlx_str_trimblanks(&alpn);
628
0
    dstalpnid = Curl_str2alpnid(&alpn);
629
630
0
    if(curlx_str_single(&p, '='))
631
0
      break;
632
633
    /* Parse altsvc hostname:port */
634
0
    if(!altsvc_parse_dest(&p, data, origin, &dsthost, &dstport))
635
0
      break;
636
637
    /* Parse optional parameters */
638
0
    altsvc_parse_params(&p, &maxage, &persist);
639
640
0
    if(dstalpnid) { /* this is a known ALPN id, e.g. not ALPN_none */
641
0
      if(!entries++)
642
        /* Flush cached alternatives for this source origin, if any - when
643
           this is the first entry of the line. */
644
0
        altsvc_flush(asi, origin, origin_alpnid);
645
646
0
      as = altsvc_createid(origin->hostname, strlen(origin->hostname),
647
0
                           curlx_str(&dsthost),
648
0
                           curlx_strlen(&dsthost),
649
0
                           origin_alpnid, dstalpnid,
650
0
                           origin->port, dstport);
651
0
      if(as) {
652
0
        time_t secs = time(NULL);
653
        /* The expires time also needs to take the Age: value (if any)
654
           into account. [See RFC 7838 section 3.1] */
655
0
        if(maxage > (TIME_T_MAX - secs))
656
0
          as->expires = TIME_T_MAX;
657
0
        else
658
0
          as->expires = maxage + secs;
659
0
        as->persist = persist;
660
0
        altsvc_append(asi, as);
661
0
        infof(data, "Added alt-svc: %.*s:%u over %s",
662
0
              (int)curlx_strlen(&dsthost), curlx_str(&dsthost),
663
0
              dstport, Curl_alpnid2str(dstalpnid));
664
0
      }
665
0
      else
666
0
        return CURLE_OUT_OF_MEMORY;
667
0
    }
668
669
    /* When this is followed by a comma, we expect another entry */
670
0
    if(curlx_str_single(&p, ','))
671
0
      break;
672
0
  }
673
674
0
  return CURLE_OK;
675
0
}
676
677
/*
678
 * Return TRUE on a match
679
 */
680
bool Curl_altsvc_lookup(struct altsvcinfo *asi,
681
                        struct Curl_peer *origin,
682
                        enum alpnid origin_alpnid,
683
                        struct altsvc **dstentry,
684
                        const int versions, /* one or more bits */
685
                        bool *psame_destination)
686
0
{
687
0
  DEBUGASSERT(asi);
688
0
  DEBUGASSERT(origin);
689
0
  DEBUGASSERT(dstentry);
690
0
  *psame_destination = FALSE;
691
692
0
  if(Curl_llist_count(&asi->list)) {
693
0
    struct Curl_llist_node *e;
694
0
    struct Curl_llist_node *n;
695
0
    time_t now = time(NULL);
696
697
0
    for(e = Curl_llist_head(&asi->list); e; e = n) {
698
0
      struct altsvc *as = Curl_node_elem(e);
699
0
      n = Curl_node_next(e);
700
0
      if(as->expires < now) {
701
        /* an expired entry, remove */
702
0
        Curl_node_remove(e);
703
0
        altsvc_free(as);
704
0
        continue;
705
0
      }
706
0
      if((origin_alpnid == as->src.alpnid) &&
707
0
         (versions & (int)as->dst.alpnid) &&
708
0
         (origin->port == as->src.port) &&
709
0
         hostcompare(origin->hostname, as->src.host)) {
710
        /* match */
711
0
        *dstentry = as;
712
        /* alt-svc on the same host+port or another one? */
713
0
        *psame_destination = (origin->port == as->dst.port) &&
714
0
                             hostcompare(origin->hostname, as->dst.host);
715
0
        return TRUE;
716
0
      }
717
0
    }
718
0
  }
719
0
  return FALSE;
720
0
}
721
722
#if defined(DEBUGBUILD) || defined(UNITTESTS)
723
#undef time
724
#endif
725
726
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_ALTSVC */