Coverage Report

Created: 2026-10-22 06:50

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
52.5k
#define MAX_ALTSVC_LINE    4095
42
0
#define MAX_ALTSVC_DATELEN 17
43
7.93k
#define MAX_ALTSVC_HOSTLEN 2048
44
24.8k
#define MAX_ALTSVC_ALPNLEN 10
45
46
18
#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
4.40k
{
53
4.40k
  const char *timestr = getenv("CURL_TIME");
54
4.40k
  (void)unused;
55
4.40k
  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
4.40k
  return time(NULL);
61
4.40k
}
62
#undef time
63
4.40k
#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
1.89k
{
69
1.89k
  switch(id) {
70
1.29k
  case ALPN_h1:
71
1.29k
    return "h1";
72
577
  case ALPN_h2:
73
577
    return "h2";
74
18
  case ALPN_h3:
75
18
    return H3VERSION;
76
0
  default:
77
0
    return ""; /* bad */
78
1.89k
  }
79
1.89k
}
80
81
static enum alpnid Curl_str2alpnid(const struct Curl_str *cstr)
82
17.9k
{
83
17.9k
  return Curl_alpn2alpnid((const unsigned char *)curlx_str(cstr),
84
17.9k
                          curlx_strlen(cstr));
85
17.9k
}
86
87
4.40k
#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
4.41k
{
98
4.41k
  struct altsvc *as;
99
4.41k
  if((hlen > 2) && srchost[0] == '[') {
100
    /* IPv6 address, strip off brackets */
101
0
    srchost++;
102
0
    hlen -= 2;
103
0
  }
104
4.41k
  else if(hlen && (srchost[hlen - 1] == '.')) {
105
    /* strip off trailing dot */
106
259
    hlen--;
107
259
  }
108
4.41k
  if((dlen > 2) && dsthost[0] == '[') {
109
    /* IPv6 address, strip off brackets */
110
1
    dsthost++;
111
1
    dlen -= 2;
112
1
  }
113
4.41k
  if(!hlen || !dlen)
114
    /* bad input */
115
0
    return NULL;
116
  /* struct size plus both strings */
117
4.41k
  as = curlx_calloc(1, sizeof(struct altsvc) + (hlen + 1) + (dlen + 1));
118
4.41k
  if(!as)
119
1
    return NULL;
120
4.40k
  as->src.host = (char *)as + sizeof(struct altsvc);
121
4.40k
  memcpy(as->src.host, srchost, hlen);
122
  /* the null-terminator is already there */
123
124
4.40k
  as->dst.host = (char *)as + sizeof(struct altsvc) + hlen + 1;
125
4.40k
  memcpy(as->dst.host, dsthost, dlen);
126
  /* the null-terminator is already there */
127
128
4.40k
  as->src.alpnid = srcalpnid;
129
4.40k
  as->dst.alpnid = dstalpnid;
130
4.40k
  as->src.port = (unsigned short)srcport;
131
4.40k
  as->dst.port = (unsigned short)dstport;
132
133
4.40k
  return as;
134
4.41k
}
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
4.40k
{
157
4.40k
  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
4.40k
  Curl_llist_append(&asi->list, as, &as->node);
165
4.40k
}
166
167
/* only returns SERIOUS errors */
168
static CURLcode altsvc_add(struct altsvcinfo *asi, const char *line)
169
24.8k
{
170
  /* Example line:
171
     h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
172
   */
173
24.8k
  struct Curl_str srchost;
174
24.8k
  struct Curl_str dsthost;
175
24.8k
  struct Curl_str srcalpn;
176
24.8k
  struct Curl_str dstalpn;
177
24.8k
  struct Curl_str date;
178
24.8k
  curl_off_t srcport;
179
24.8k
  curl_off_t dstport;
180
24.8k
  curl_off_t persist;
181
24.8k
  curl_off_t prio;
182
183
24.8k
  if(curlx_str_word(&line, &srcalpn, MAX_ALTSVC_ALPNLEN) ||
184
24.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
24.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
24.8k
  return CURLE_OK;
227
24.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
24.8k
{
239
24.8k
  CURLcode result = CURLE_OK;
240
24.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
24.8k
  curlx_free(asi->filename);
245
24.8k
  asi->filename = curlx_strdup(file);
246
24.8k
  if(!asi->filename)
247
8
    return CURLE_OUT_OF_MEMORY;
248
249
24.8k
  fp = curlx_fopen(file, FOPEN_READTEXT);
250
24.8k
  if(fp) {
251
24.8k
    curlx_struct_stat stat;
252
24.8k
    if((curlx_fstat(fileno(fp), &stat) == -1) || !S_ISDIR(stat.st_mode)) {
253
24.8k
      bool eof = FALSE;
254
24.8k
      struct dynbuf buf;
255
24.8k
      curlx_dyn_init(&buf, MAX_ALTSVC_LINE);
256
24.8k
      do {
257
24.8k
        result = Curl_get_line(&buf, fp, &eof);
258
24.8k
        if(!result) {
259
24.8k
          const char *lineptr = curlx_dyn_ptr(&buf);
260
24.8k
          curlx_str_passblanks(&lineptr);
261
24.8k
          if(curlx_str_single(&lineptr, '#'))
262
24.8k
            altsvc_add(asi, lineptr);
263
24.8k
        }
264
24.8k
      } while(!result && !eof);
265
24.8k
      curlx_dyn_free(&buf); /* free the line buffer */
266
24.8k
    }
267
24.8k
    curlx_fclose(fp);
268
24.8k
  }
269
24.8k
  return result;
270
24.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
277
{
277
277
  struct tm stamp;
278
277
  const char *dst6_pre = "";
279
277
  const char *dst6_post = "";
280
277
  const char *src6_pre = "";
281
277
  const char *src6_post = "";
282
277
  CURLcode result = curlx_gmtime(as->expires, &stamp);
283
277
  if(result)
284
    /* fail silently for date errors so that only this entry gets
285
       skipped */
286
0
    return CURLE_OK;
287
277
#ifdef USE_IPV6
288
277
  else {
289
277
    char ipv6_unused[16];
290
277
    if(curlx_inet_pton(AF_INET6, as->dst.host, ipv6_unused) == 1) {
291
169
      dst6_pre = "[";
292
169
      dst6_post = "]";
293
169
    }
294
277
    if(curlx_inet_pton(AF_INET6, as->src.host, ipv6_unused) == 1) {
295
0
      src6_pre = "[";
296
0
      src6_post = "]";
297
0
    }
298
277
  }
299
277
#endif
300
277
  curl_mfprintf(fp,
301
277
                "%s %s%s%s %u "
302
277
                "%s %s%s%s %u "
303
277
                "\"%d%02d%02d "
304
277
                "%02d:%02d:%02d\" "
305
277
                "%d 0\n", /* prio still always zero */
306
277
                Curl_alpnid2str(as->src.alpnid),
307
277
                src6_pre, as->src.host, src6_post,
308
277
                as->src.port,
309
310
277
                Curl_alpnid2str(as->dst.alpnid),
311
277
                dst6_pre, as->dst.host, dst6_post,
312
277
                as->dst.port,
313
314
277
                stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
315
277
                stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
316
277
                as->persist);
317
277
  return CURLE_OK;
318
277
}
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
24.9k
{
328
24.9k
  struct altsvcinfo *asi = curlx_calloc(1, sizeof(struct altsvcinfo));
329
24.9k
  if(!asi)
330
56
    return NULL;
331
24.8k
  Curl_llist_init(&asi->list, NULL);
332
333
  /* set default behavior */
334
24.8k
  asi->flags = CURLALTSVC_H1
335
24.8k
#ifdef USE_HTTP2
336
24.8k
    | CURLALTSVC_H2
337
24.8k
#endif
338
#ifdef USE_HTTP3
339
    | CURLALTSVC_H3
340
#endif
341
24.8k
    ;
342
24.8k
  return asi;
343
24.9k
}
344
345
/*
346
 * Curl_altsvc_load() loads alt-svc from file.
347
 */
348
CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
349
24.8k
{
350
24.8k
  DEBUGASSERT(asi);
351
24.8k
  return altsvc_load(asi, file);
352
24.8k
}
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
367
{
359
367
  DEBUGASSERT(data);
360
367
  if(!ctrl)
361
179
    return CURLE_BAD_FUNCTION_ARGUMENT;
362
363
188
  if(!data->asi) {
364
4
    data->asi = Curl_altsvc_init();
365
4
    if(!data->asi)
366
1
      return CURLE_OUT_OF_MEMORY;
367
4
  }
368
187
  data->asi->flags = ctrl;
369
187
  return CURLE_OK;
370
188
}
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
49.8k
{
378
49.8k
  if(*asi) {
379
24.8k
    struct Curl_llist_node *e;
380
24.8k
    struct Curl_llist_node *n;
381
24.8k
    struct altsvcinfo *altsvc = *asi;
382
25.1k
    for(e = Curl_llist_head(&altsvc->list); e; e = n) {
383
277
      struct altsvc *as = Curl_node_elem(e);
384
277
      n = Curl_node_next(e);
385
277
      altsvc_free(as);
386
277
    }
387
24.8k
    curlx_free(altsvc->filename);
388
24.8k
    curlx_free(altsvc);
389
24.8k
    *asi = NULL; /* clear the pointer */
390
24.8k
  }
391
49.8k
}
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
49.8k
{
399
49.8k
  CURLcode result = CURLE_OK;
400
49.8k
  FILE *out;
401
49.8k
  char *tempstore = NULL;
402
403
49.8k
  if(!asi)
404
    /* no cache activated */
405
24.9k
    return CURLE_OK;
406
407
  /* if not new name is given, use the one we stored from the load */
408
24.8k
  if(!file && asi->filename)
409
0
    file = asi->filename;
410
411
24.8k
  if((asi->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
412
    /* marked as read-only, no file or zero length filename */
413
69
    return CURLE_OK;
414
415
24.8k
  result = Curl_fopen(data, file, &out, &tempstore);
416
24.8k
  if(!result) {
417
24.8k
    struct Curl_llist_node *e;
418
24.8k
    struct Curl_llist_node *n;
419
24.8k
    fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
420
24.8k
          "# This file was generated by libcurl! Edit at your own risk.\n",
421
24.8k
          out);
422
25.0k
    for(e = Curl_llist_head(&asi->list); e; e = n) {
423
277
      struct altsvc *as = Curl_node_elem(e);
424
277
      n = Curl_node_next(e);
425
277
      result = altsvc_out(as, out);
426
277
      if(result)
427
0
        break;
428
277
    }
429
24.8k
    curlx_fclose(out);
430
24.8k
    if(!result && tempstore && curlx_rename(tempstore, file))
431
0
      result = CURLE_WRITE_ERROR;
432
433
24.8k
    if(result && tempstore)
434
0
      unlink(tempstore);
435
24.8k
  }
436
24.8k
  curlx_free(tempstore);
437
24.8k
  return result;
438
24.8k
}
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
4.81k
{
445
4.81k
  size_t hlen = strlen(host);
446
4.81k
  size_t clen = strlen(check);
447
448
4.81k
  if(hlen && (host[hlen - 1] == '.'))
449
392
    hlen--;
450
4.81k
  if(hlen != clen)
451
    /* they cannot match if they have different lengths */
452
621
    return FALSE;
453
4.18k
  return curl_strnequal(host, check, hlen);
454
4.81k
}
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
5.56k
{
462
5.56k
  struct Curl_llist_node *e;
463
5.56k
  struct Curl_llist_node *n;
464
10.3k
  for(e = Curl_llist_head(&asi->list); e; e = n) {
465
4.81k
    struct altsvc *as = Curl_node_elem(e);
466
4.81k
    n = Curl_node_next(e);
467
4.81k
    if((origin_alpnid == as->src.alpnid) &&
468
4.81k
       (origin->port == as->src.port) &&
469
4.81k
       hostcompare(origin->hostname, as->src.host)) {
470
4.13k
      Curl_node_remove(e);
471
4.13k
      altsvc_free(as);
472
4.13k
    }
473
4.81k
  }
474
5.56k
}
475
476
5.08k
#define CAP_ALTSVC_MAX_AGE (2*365*24*3600) /* two years cap */
477
478
11.7k
#define ALTSVC_MA      1
479
1.61k
#define ALTSVC_PERSIST 2
480
static void altsvc_parse_params(const char **pp,
481
                                time_t *pmaxage,
482
                                bool *ppersist)
483
6.76k
{
484
6.76k
  curlx_str_passblanks(pp);
485
6.76k
  if(curlx_str_single(pp, ';'))
486
63
    return;
487
488
8.39k
  for(;;) {
489
8.39k
    struct Curl_str name;
490
8.39k
    struct Curl_str val;
491
8.39k
    curl_off_t num;
492
8.39k
    int keyword = 0;
493
494
    /* allow some extra whitespaces around name and value */
495
8.39k
    if(curlx_str_until(pp, &name, MAX_ALTSVC_LINE, '=') ||
496
8.39k
       curlx_str_single(pp, '='))
497
43
      break;  /* skip further parameter parsing */
498
499
8.35k
    curlx_str_trimblanks(&name);
500
8.35k
    curlx_str_passblanks(pp);
501
8.35k
    if(**pp == '\"') {
502
1.37k
      if(curlx_str_quotedword(pp, &val, MAX_ALTSVC_LINE))
503
13
        break;
504
1.37k
    }
505
6.98k
    else {
506
6.98k
      if(curlx_str_cspn(pp, &val, ",;\r\n"))
507
3
        break;
508
6.98k
    }
509
8.34k
    curlx_str_trimblanks(&val);
510
511
8.34k
    if(curlx_str_casecompare(&name, "ma"))
512
5.12k
      keyword = ALTSVC_MA;
513
3.21k
    else if(curlx_str_casecompare(&name, "persist"))
514
1.61k
      keyword = ALTSVC_PERSIST;
515
516
8.34k
    if(keyword) {
517
6.73k
      const char *vp = curlx_str(&val);
518
6.73k
      const char *vend = vp + curlx_strlen(&val);
519
6.73k
      if(curlx_str_number(&vp, &num, CURL_OFF_T_MAX))
520
25
        break; /* not a number, skip further parameter parsing */
521
6.70k
      if(vp != vend)
522
37
        break; /* not entirely a number, skip further parameter parsing */
523
6.66k
      if(keyword == ALTSVC_MA) {
524
5.07k
        if(num > CAP_ALTSVC_MAX_AGE)
525
6
          num = CAP_ALTSVC_MAX_AGE;
526
5.07k
        *pmaxage = (time_t)num;
527
5.07k
      }
528
1.59k
      else if(num == 1)
529
1.57k
        *ppersist = TRUE;
530
6.66k
    }
531
532
8.27k
    curlx_str_passblanks(pp);
533
8.27k
    if(curlx_str_single(pp, ';'))
534
6.57k
      break; /* no further parameters */
535
8.27k
  }
536
6.69k
}
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
15.5k
{
544
15.5k
  curl_off_t port = 0;
545
546
15.5k
  if(curlx_str_single(pp, '\"'))
547
595
    return FALSE;
548
549
  /* quoted string, with hostname or just :port ? */
550
14.9k
  if(curlx_str_single(pp, ':')) { /* is hostname:port ? */
551
10.3k
    if(curlx_str_single(pp, '[')) { /* DNS hostname/IPv4 */
552
7.93k
      if(curlx_str_until(pp, dsthost, MAX_ALTSVC_HOSTLEN, ':')) {
553
46
        infof(data, "Bad alt-svc hostname, ignoring.");
554
46
        return FALSE;
555
46
      }
556
7.93k
    }
557
2.41k
    else { /* IPv6 hostname */
558
2.41k
      if(curlx_str_until(pp, dsthost, MAX_IPADR_LEN, ']') ||
559
2.41k
         curlx_str_single(pp, ']')) {
560
106
        infof(data, "Bad alt-svc IPv6 hostname, ignoring.");
561
106
        return FALSE;
562
106
      }
563
2.41k
    }
564
10.2k
    if(curlx_str_single(pp, ':'))
565
2.00k
      return FALSE; /* not followed by ':' */
566
10.2k
  }
567
4.61k
  else  /* is only :port, hostname is effectively origin */
568
4.61k
    curlx_str_assign(dsthost, origin->hostname,
569
4.61k
                     strlen(origin->hostname));
570
571
12.8k
  if(curlx_str_number(pp, &port, 0xffff)) {
572
5.59k
    infof(data, "Unknown alt-svc port number, ignoring.");
573
5.59k
    return FALSE;
574
5.59k
  }
575
576
7.22k
  *pdstport = (uint16_t)port;
577
7.22k
  if(curlx_str_single(pp, '\"'))
578
461
    return FALSE; /* quoted string not ending here as expected */
579
6.76k
  return TRUE;
580
7.22k
}
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
18.1k
{
598
18.1k
  struct altsvc *as;
599
18.1k
  size_t entries = 0;
600
18.1k
  struct Curl_str alpn;
601
18.1k
  const char *p;
602
603
18.1k
  DEBUGASSERT(asi);
604
18.1k
  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
18.1k
  p = value;
614
18.1k
  if(!curlx_str_cspn(&p, &alpn, ";\n\r")) {
615
18.1k
    curlx_str_trimblanks(&alpn);
616
    /* "clear" is a magic keyword */
617
18.1k
    if(curlx_str_casecompare(&alpn, "clear")) {
618
      /* Flush cached alternatives for this source origin */
619
1.74k
      altsvc_flush(asi, origin, origin_alpnid);
620
1.74k
      return CURLE_OK;
621
1.74k
    }
622
18.1k
  }
623
624
  /* Not a standalone "clear", parse from start for alpn entries */
625
17.9k
  for(p = value; *p;) {
626
17.9k
    time_t maxage = 24 * 3600; /* default is 24 hours */
627
17.9k
    bool persist = FALSE;
628
17.9k
    enum alpnid dstalpnid;
629
17.9k
    struct Curl_str dsthost;
630
17.9k
    uint16_t dstport;
631
632
17.9k
    if(curlx_str_until(&p, &alpn, MAX_ALTSVC_LINE, '='))
633
60
      break; /* not another entry, leave */
634
17.9k
    curlx_str_trimblanks(&alpn);
635
17.9k
    dstalpnid = Curl_str2alpnid(&alpn);
636
637
17.9k
    if(curlx_str_single(&p, '='))
638
2.33k
      break;
639
640
    /* Parse altsvc hostname:port */
641
15.5k
    if(!altsvc_parse_dest(&p, data, origin, &dsthost, &dstport))
642
8.80k
      break;
643
644
    /* Parse optional parameters */
645
6.76k
    altsvc_parse_params(&p, &maxage, &persist);
646
647
6.76k
    if(dstalpnid) { /* this is a known ALPN id, e.g. not ALPN_none */
648
4.41k
      if(!entries++)
649
        /* Flush cached alternatives for this source origin, if any - when
650
           this is the first entry of the line. */
651
3.82k
        altsvc_flush(asi, origin, origin_alpnid);
652
653
4.41k
      as = altsvc_createid(origin->hostname, strlen(origin->hostname),
654
4.41k
                           curlx_str(&dsthost),
655
4.41k
                           curlx_strlen(&dsthost),
656
4.41k
                           origin_alpnid, dstalpnid,
657
4.41k
                           origin->port, dstport);
658
4.41k
      if(as) {
659
4.40k
        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
4.40k
        if(maxage > (TIME_T_MAX - secs))
663
0
          as->expires = TIME_T_MAX;
664
4.40k
        else
665
4.40k
          as->expires = maxage + secs;
666
4.40k
        as->persist = persist;
667
4.40k
        altsvc_append(asi, as);
668
4.40k
        infof(data, "Added alt-svc: %.*s:%u over %s",
669
4.40k
              (int)curlx_strlen(&dsthost), curlx_str(&dsthost),
670
4.40k
              dstport, Curl_alpnid2str(dstalpnid));
671
4.40k
      }
672
1
      else
673
1
        return CURLE_OUT_OF_MEMORY;
674
4.41k
    }
675
676
    /* When this is followed by a comma, we expect another entry */
677
6.76k
    if(curlx_str_single(&p, ','))
678
5.16k
      break;
679
6.76k
  }
680
681
16.3k
  return CURLE_OK;
682
16.3k
}
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
141
{
694
141
  DEBUGASSERT(asi);
695
141
  DEBUGASSERT(origin);
696
141
  DEBUGASSERT(dstentry);
697
141
  *psame_destination = FALSE;
698
699
141
  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
141
  return FALSE;
727
141
}
728
729
#if defined(DEBUGBUILD) || defined(UNITTESTS)
730
#undef time
731
#endif
732
733
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_ALTSVC */