Coverage Report

Created: 2026-09-04 07:15

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
33.6k
#define MAX_ALTSVC_LINE    4095
42
0
#define MAX_ALTSVC_DATELEN 17
43
4.60k
#define MAX_ALTSVC_HOSTLEN 2048
44
20.8k
#define MAX_ALTSVC_ALPNLEN 10
45
46
461
#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
5.26k
{
53
5.26k
  const char *timestr = getenv("CURL_TIME");
54
5.26k
  (void)unused;
55
5.26k
  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
5.26k
  return time(NULL);
61
5.26k
}
62
#undef time
63
5.26k
#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
4.64k
{
69
4.64k
  switch(id) {
70
3.50k
  case ALPN_h1:
71
3.50k
    return "h1";
72
679
  case ALPN_h2:
73
679
    return "h2";
74
461
  case ALPN_h3:
75
461
    return H3VERSION;
76
0
  default:
77
0
    return ""; /* bad */
78
4.64k
  }
79
4.64k
}
80
81
static enum alpnid Curl_str2alpnid(const struct Curl_str *cstr)
82
10.6k
{
83
10.6k
  return Curl_alpn2alpnid((const unsigned char *)curlx_str(cstr),
84
10.6k
                          curlx_strlen(cstr));
85
10.6k
}
86
87
5.26k
#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
5.26k
{
98
5.26k
  struct altsvc *as;
99
5.26k
  if((hlen > 2) && srchost[0] == '[') {
100
    /* IPv6 address, strip off brackets */
101
0
    srchost++;
102
0
    hlen -= 2;
103
0
  }
104
5.26k
  else if(hlen && (srchost[hlen - 1] == '.')) {
105
    /* strip off trailing dot */
106
420
    hlen--;
107
420
  }
108
5.26k
  if((dlen > 2) && dsthost[0] == '[') {
109
    /* IPv6 address, strip off brackets */
110
151
    dsthost++;
111
151
    dlen -= 2;
112
151
  }
113
5.26k
  if(!hlen || !dlen)
114
    /* bad input */
115
0
    return NULL;
116
  /* struct size plus both strings */
117
5.26k
  as = curlx_calloc(1, sizeof(struct altsvc) + (hlen + 1) + (dlen + 1));
118
5.26k
  if(!as)
119
0
    return NULL;
120
5.26k
  as->src.host = (char *)as + sizeof(struct altsvc);
121
5.26k
  memcpy(as->src.host, srchost, hlen);
122
  /* the null-terminator is already there */
123
124
5.26k
  as->dst.host = (char *)as + sizeof(struct altsvc) + hlen + 1;
125
5.26k
  memcpy(as->dst.host, dsthost, dlen);
126
  /* the null-terminator is already there */
127
128
5.26k
  as->src.alpnid = srcalpnid;
129
5.26k
  as->dst.alpnid = dstalpnid;
130
5.26k
  as->src.port = (unsigned short)srcport;
131
5.26k
  as->dst.port = (unsigned short)dstport;
132
133
5.26k
  return as;
134
5.26k
}
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
5.26k
{
157
5.26k
  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
5.26k
  Curl_llist_append(&asi->list, as, &as->node);
165
5.26k
}
166
167
/* only returns SERIOUS errors */
168
static CURLcode altsvc_add(struct altsvcinfo *asi, const char *line)
169
20.8k
{
170
  /* Example line:
171
     h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
172
   */
173
20.8k
  struct Curl_str srchost;
174
20.8k
  struct Curl_str dsthost;
175
20.8k
  struct Curl_str srcalpn;
176
20.8k
  struct Curl_str dstalpn;
177
20.8k
  struct Curl_str date;
178
20.8k
  curl_off_t srcport;
179
20.8k
  curl_off_t dstport;
180
20.8k
  curl_off_t persist;
181
20.8k
  curl_off_t prio;
182
183
20.8k
  if(curlx_str_word(&line, &srcalpn, MAX_ALTSVC_ALPNLEN) ||
184
20.8k
     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
20.8k
    ;
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
20.8k
  return CURLE_OK;
227
20.8k
}
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
20.8k
{
239
20.8k
  CURLcode result = CURLE_OK;
240
20.8k
  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
20.8k
  curlx_free(asi->filename);
245
20.8k
  asi->filename = curlx_strdup(file);
246
20.8k
  if(!asi->filename)
247
0
    return CURLE_OUT_OF_MEMORY;
248
249
20.8k
  fp = curlx_fopen(file, FOPEN_READTEXT);
250
20.8k
  if(fp) {
251
20.8k
    curlx_struct_stat stat;
252
20.8k
    if((curlx_fstat(fileno(fp), &stat) == -1) || !S_ISDIR(stat.st_mode)) {
253
20.8k
      bool eof = FALSE;
254
20.8k
      struct dynbuf buf;
255
20.8k
      curlx_dyn_init(&buf, MAX_ALTSVC_LINE);
256
20.8k
      do {
257
20.8k
        result = Curl_get_line(&buf, fp, &eof);
258
20.8k
        if(!result) {
259
20.8k
          const char *lineptr = curlx_dyn_ptr(&buf);
260
20.8k
          curlx_str_passblanks(&lineptr);
261
20.8k
          if(curlx_str_single(&lineptr, '#'))
262
20.8k
            altsvc_add(asi, lineptr);
263
20.8k
        }
264
20.8k
      } while(!result && !eof);
265
20.8k
      curlx_dyn_free(&buf); /* free the line buffer */
266
20.8k
    }
267
20.8k
    curlx_fclose(fp);
268
20.8k
  }
269
20.8k
  return result;
270
20.8k
}
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
2.32k
{
277
2.32k
  struct tm stamp;
278
2.32k
  const char *dst6_pre = "";
279
2.32k
  const char *dst6_post = "";
280
2.32k
  const char *src6_pre = "";
281
2.32k
  const char *src6_post = "";
282
2.32k
  CURLcode result = curlx_gmtime(as->expires, &stamp);
283
2.32k
  if(result)
284
1
    return result;
285
2.32k
#ifdef USE_IPV6
286
2.32k
  else {
287
2.32k
    char ipv6_unused[16];
288
2.32k
    if(curlx_inet_pton(AF_INET6, as->dst.host, ipv6_unused) == 1) {
289
120
      dst6_pre = "[";
290
120
      dst6_post = "]";
291
120
    }
292
2.32k
    if(curlx_inet_pton(AF_INET6, as->src.host, ipv6_unused) == 1) {
293
241
      src6_pre = "[";
294
241
      src6_post = "]";
295
241
    }
296
2.32k
  }
297
2.32k
#endif
298
2.32k
  curl_mfprintf(fp,
299
2.32k
                "%s %s%s%s %u "
300
2.32k
                "%s %s%s%s %u "
301
2.32k
                "\"%d%02d%02d "
302
2.32k
                "%02d:%02d:%02d\" "
303
2.32k
                "%d 0\n", /* prio still always zero */
304
2.32k
                Curl_alpnid2str(as->src.alpnid),
305
2.32k
                src6_pre, as->src.host, src6_post,
306
2.32k
                as->src.port,
307
308
2.32k
                Curl_alpnid2str(as->dst.alpnid),
309
2.32k
                dst6_pre, as->dst.host, dst6_post,
310
2.32k
                as->dst.port,
311
312
2.32k
                stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
313
2.32k
                stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
314
2.32k
                as->persist);
315
2.32k
  return CURLE_OK;
316
2.32k
}
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
20.8k
{
326
20.8k
  struct altsvcinfo *asi = curlx_calloc(1, sizeof(struct altsvcinfo));
327
20.8k
  if(!asi)
328
0
    return NULL;
329
20.8k
  Curl_llist_init(&asi->list, NULL);
330
331
  /* set default behavior */
332
20.8k
  asi->flags = CURLALTSVC_H1
333
20.8k
#ifdef USE_HTTP2
334
20.8k
    | CURLALTSVC_H2
335
20.8k
#endif
336
#ifdef USE_HTTP3
337
    | CURLALTSVC_H3
338
#endif
339
20.8k
    ;
340
20.8k
  return asi;
341
20.8k
}
342
343
/*
344
 * Curl_altsvc_load() loads alt-svc from file.
345
 */
346
CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
347
20.8k
{
348
20.8k
  DEBUGASSERT(asi);
349
20.8k
  return altsvc_load(asi, file);
350
20.8k
}
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
72
{
357
72
  DEBUGASSERT(data);
358
72
  if(!ctrl)
359
1
    return CURLE_BAD_FUNCTION_ARGUMENT;
360
361
71
  if(!data->asi) {
362
71
    data->asi = Curl_altsvc_init();
363
71
    if(!data->asi)
364
0
      return CURLE_OUT_OF_MEMORY;
365
71
  }
366
71
  data->asi->flags = ctrl;
367
71
  return CURLE_OK;
368
71
}
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
43.1k
{
376
43.1k
  if(*asi) {
377
20.8k
    struct Curl_llist_node *e;
378
20.8k
    struct Curl_llist_node *n;
379
20.8k
    struct altsvcinfo *altsvc = *asi;
380
23.2k
    for(e = Curl_llist_head(&altsvc->list); e; e = n) {
381
2.32k
      struct altsvc *as = Curl_node_elem(e);
382
2.32k
      n = Curl_node_next(e);
383
2.32k
      altsvc_free(as);
384
2.32k
    }
385
20.8k
    curlx_free(altsvc->filename);
386
20.8k
    curlx_free(altsvc);
387
20.8k
    *asi = NULL; /* clear the pointer */
388
20.8k
  }
389
43.1k
}
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
43.1k
{
397
43.1k
  CURLcode result = CURLE_OK;
398
43.1k
  FILE *out;
399
43.1k
  char *tempstore = NULL;
400
401
43.1k
  if(!asi)
402
    /* no cache activated */
403
22.3k
    return CURLE_OK;
404
405
  /* if not new name is given, use the one we stored from the load */
406
20.8k
  if(!file && asi->filename)
407
0
    file = asi->filename;
408
409
20.8k
  if((asi->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
410
    /* marked as read-only, no file or zero length filename */
411
41
    return CURLE_OK;
412
413
20.8k
  result = Curl_fopen(data, file, &out, &tempstore);
414
20.8k
  if(!result) {
415
20.8k
    struct Curl_llist_node *e;
416
20.8k
    struct Curl_llist_node *n;
417
20.8k
    fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
418
20.8k
          "# This file was generated by libcurl! Edit at your own risk.\n",
419
20.8k
          out);
420
23.1k
    for(e = Curl_llist_head(&asi->list); e; e = n) {
421
2.32k
      struct altsvc *as = Curl_node_elem(e);
422
2.32k
      n = Curl_node_next(e);
423
2.32k
      result = altsvc_out(as, out);
424
2.32k
      if(result)
425
1
        break;
426
2.32k
    }
427
20.8k
    curlx_fclose(out);
428
20.8k
    if(!result && tempstore && curlx_rename(tempstore, file))
429
0
      result = CURLE_WRITE_ERROR;
430
431
20.8k
    if(result && tempstore)
432
0
      unlink(tempstore);
433
20.8k
  }
434
20.8k
  curlx_free(tempstore);
435
20.8k
  return result;
436
20.8k
}
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
2.94k
{
443
2.94k
  size_t hlen = strlen(host);
444
2.94k
  size_t clen = strlen(check);
445
446
2.94k
  if(hlen && (host[hlen - 1] == '.'))
447
203
    hlen--;
448
2.94k
  if(hlen != clen)
449
    /* they cannot match if they have different lengths */
450
0
    return FALSE;
451
2.94k
  return curl_strnequal(host, check, hlen);
452
2.94k
}
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
1.37k
{
460
1.37k
  struct Curl_llist_node *e;
461
1.37k
  struct Curl_llist_node *n;
462
4.31k
  for(e = Curl_llist_head(&asi->list); e; e = n) {
463
2.94k
    struct altsvc *as = Curl_node_elem(e);
464
2.94k
    n = Curl_node_next(e);
465
2.94k
    if((origin_alpnid == as->src.alpnid) &&
466
2.94k
       (origin->port == as->src.port) &&
467
2.94k
       hostcompare(origin->hostname, as->src.host)) {
468
2.94k
      Curl_node_remove(e);
469
2.94k
      altsvc_free(as);
470
2.94k
    }
471
2.94k
  }
472
1.37k
}
473
474
514
#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
7.96k
{
480
7.96k
  curlx_str_passblanks(pp);
481
7.96k
  if(curlx_str_single(pp, ';'))
482
6.94k
    return;
483
484
1.89k
  for(;;) {
485
1.89k
    struct Curl_str name;
486
1.89k
    struct Curl_str val;
487
1.89k
    curl_off_t num;
488
1.89k
    int keyword = 0;
489
490
    /* allow some extra whitespaces around name and value */
491
1.89k
    if(curlx_str_until(pp, &name, MAX_ALTSVC_LINE, '=') ||
492
1.87k
       curlx_str_single(pp, '='))
493
425
      break;  /* skip further parameter parsing */
494
495
1.46k
    curlx_str_trimblanks(&name);
496
1.46k
    curlx_str_passblanks(pp);
497
1.46k
    if(**pp == '\"') {
498
239
      if(curlx_str_quotedword(pp, &val, MAX_ALTSVC_LINE))
499
92
        break;
500
239
    }
501
1.23k
    else {
502
1.23k
      if(curlx_str_cspn(pp, &val, ",;\r\n"))
503
115
        break;
504
1.23k
    }
505
1.26k
    curlx_str_trimblanks(&val);
506
507
1.26k
    if(curlx_str_casecompare(&name, "ma"))
508
312
      keyword = ALTSVC_MA;
509
950
    else if(curlx_str_casecompare(&name, "persist"))
510
0
      keyword = ALTSVC_PERSIST;
511
512
1.26k
    if(keyword) {
513
312
      const char *vp = curlx_str(&val);
514
312
      const char *vend = vp + curlx_strlen(&val);
515
312
      if(curlx_str_number(&vp, &num, TIME_T_MAX))
516
36
        break; /* not a number, skip further parameter parsing */
517
276
      if(vp != vend)
518
74
        break; /* not entirely a number, skip further parameter parsing */
519
202
      if(keyword == ALTSVC_MA)
520
202
        *pmaxage = (time_t)num;
521
0
      else if(num == 1)
522
0
        *ppersist = TRUE;
523
202
    }
524
525
1.15k
    curlx_str_passblanks(pp);
526
1.15k
    if(curlx_str_single(pp, ';'))
527
286
      break; /* no further parameters */
528
1.15k
  }
529
1.02k
}
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
9.66k
{
537
9.66k
  curl_off_t port = 0;
538
539
9.66k
  if(curlx_str_single(pp, '\"'))
540
575
    return FALSE;
541
542
  /* quoted string, with hostname or just :port ? */
543
9.09k
  if(curlx_str_single(pp, ':')) { /* is hostname:port ? */
544
5.22k
    if(curlx_str_single(pp, '[')) { /* DNS hostname/IPv4 */
545
4.60k
      if(curlx_str_until(pp, dsthost, MAX_ALTSVC_HOSTLEN, ':')) {
546
1
        infof(data, "Bad alt-svc hostname, ignoring.");
547
1
        return FALSE;
548
1
      }
549
4.60k
    }
550
622
    else { /* IPv6 hostname */
551
622
      if(curlx_str_until(pp, dsthost, MAX_IPADR_LEN, ']') ||
552
549
         curlx_str_single(pp, ']')) {
553
233
        infof(data, "Bad alt-svc IPv6 hostname, ignoring.");
554
233
        return FALSE;
555
233
      }
556
622
    }
557
4.98k
    if(curlx_str_single(pp, ':'))
558
239
      return FALSE; /* not followed by ':' */
559
4.98k
  }
560
3.87k
  else  /* is only :port, hostname is effectively origin */
561
3.87k
    curlx_str_assign(dsthost, origin->hostname,
562
3.87k
                     strlen(origin->hostname));
563
564
8.62k
  if(curlx_str_number(pp, &port, 0xffff)) {
565
404
    infof(data, "Unknown alt-svc port number, ignoring.");
566
404
    return FALSE;
567
404
  }
568
569
8.21k
  *pdstport = (uint16_t)port;
570
8.21k
  if(curlx_str_single(pp, '\"'))
571
249
    return FALSE; /* quoted string not ending here as expected */
572
7.96k
  return TRUE;
573
8.21k
}
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
3.79k
{
591
3.79k
  struct altsvc *as;
592
3.79k
  size_t entries = 0;
593
3.79k
  struct Curl_str alpn;
594
3.79k
  const char *p;
595
596
3.79k
  DEBUGASSERT(asi);
597
3.79k
  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
3.79k
  p = value;
607
3.79k
  if(!curlx_str_cspn(&p, &alpn, ";\n\r")) {
608
3.44k
    curlx_str_trimblanks(&alpn);
609
    /* "clear" is a magic keyword */
610
3.44k
    if(curlx_str_casecompare(&alpn, "clear")) {
611
      /* Flush cached alternatives for this source origin */
612
18
      altsvc_flush(asi, origin, origin_alpnid);
613
18
      return CURLE_OK;
614
18
    }
615
3.44k
  }
616
617
  /* Not a standalone "clear", parse from start for alpn entries */
618
10.6k
  for(p = value; *p;) {
619
10.6k
    time_t maxage = 24 * 3600; /* default is 24 hours */
620
10.6k
    bool persist = FALSE;
621
10.6k
    enum alpnid dstalpnid;
622
10.6k
    struct Curl_str dsthost;
623
10.6k
    uint16_t dstport;
624
625
10.6k
    if(curlx_str_until(&p, &alpn, MAX_ALTSVC_LINE, '='))
626
43
      break; /* not another entry, leave */
627
10.6k
    curlx_str_trimblanks(&alpn);
628
10.6k
    dstalpnid = Curl_str2alpnid(&alpn);
629
630
10.6k
    if(curlx_str_single(&p, '='))
631
979
      break;
632
633
    /* Parse altsvc hostname:port */
634
9.66k
    if(!altsvc_parse_dest(&p, data, origin, &dsthost, &dstport))
635
1.70k
      break;
636
637
    /* Parse optional parameters */
638
7.96k
    altsvc_parse_params(&p, &maxage, &persist);
639
640
7.96k
    if(dstalpnid) { /* this is a known ALPN id, e.g. not ALPN_none */
641
5.26k
      if(!entries++)
642
        /* Flush cached alternatives for this source origin, if any - when
643
           this is the first entry of the line. */
644
1.36k
        altsvc_flush(asi, origin, origin_alpnid);
645
646
5.26k
      as = altsvc_createid(origin->hostname, strlen(origin->hostname),
647
5.26k
                           curlx_str(&dsthost),
648
5.26k
                           curlx_strlen(&dsthost),
649
5.26k
                           origin_alpnid, dstalpnid,
650
5.26k
                           origin->port, dstport);
651
5.26k
      if(as) {
652
5.26k
        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
5.26k
        if(maxage > (TIME_T_MAX - secs))
656
0
          as->expires = TIME_T_MAX;
657
5.26k
        else
658
5.26k
          as->expires = maxage + secs;
659
5.26k
        as->persist = persist;
660
5.26k
        altsvc_append(asi, as);
661
5.26k
        infof(data, "Added alt-svc: %.*s:%u over %s",
662
5.26k
              (int)curlx_strlen(&dsthost), curlx_str(&dsthost),
663
5.26k
              dstport, Curl_alpnid2str(dstalpnid));
664
5.26k
      }
665
0
      else
666
0
        return CURLE_OUT_OF_MEMORY;
667
5.26k
    }
668
669
    /* When this is followed by a comma, we expect another entry */
670
7.96k
    if(curlx_str_single(&p, ','))
671
1.05k
      break;
672
7.96k
  }
673
674
3.77k
  return CURLE_OK;
675
3.77k
}
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 */