Coverage Report

Created: 2024-02-25 06:14

/src/PROJ/curl/lib/altsvc.c
Line
Count
Source (jump to first uncovered line)
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 <curl/curl.h>
32
#include "urldata.h"
33
#include "altsvc.h"
34
#include "curl_get_line.h"
35
#include "strcase.h"
36
#include "parsedate.h"
37
#include "sendf.h"
38
#include "warnless.h"
39
#include "fopen.h"
40
#include "rename.h"
41
#include "strdup.h"
42
#include "inet_pton.h"
43
44
/* The last 3 #include files should be in this order */
45
#include "curl_printf.h"
46
#include "curl_memory.h"
47
#include "memdebug.h"
48
49
0
#define MAX_ALTSVC_LINE 4095
50
#define MAX_ALTSVC_DATELENSTR "64"
51
#define MAX_ALTSVC_DATELEN 64
52
#define MAX_ALTSVC_HOSTLENSTR "512"
53
0
#define MAX_ALTSVC_HOSTLEN 512
54
#define MAX_ALTSVC_ALPNLENSTR "10"
55
#define MAX_ALTSVC_ALPNLEN 10
56
57
0
#define H3VERSION "h3"
58
59
static enum alpnid alpn2alpnid(char *name)
60
0
{
61
0
  if(strcasecompare(name, "h1"))
62
0
    return ALPN_h1;
63
0
  if(strcasecompare(name, "h2"))
64
0
    return ALPN_h2;
65
0
  if(strcasecompare(name, H3VERSION))
66
0
    return ALPN_h3;
67
0
  return ALPN_none; /* unknown, probably rubbish input */
68
0
}
69
70
/* Given the ALPN ID, return the name */
71
const char *Curl_alpnid2str(enum alpnid id)
72
0
{
73
0
  switch(id) {
74
0
  case ALPN_h1:
75
0
    return "h1";
76
0
  case ALPN_h2:
77
0
    return "h2";
78
0
  case ALPN_h3:
79
0
    return H3VERSION;
80
0
  default:
81
0
    return ""; /* bad */
82
0
  }
83
0
}
84
85
86
static void altsvc_free(struct altsvc *as)
87
0
{
88
0
  free(as->src.host);
89
0
  free(as->dst.host);
90
0
  free(as);
91
0
}
92
93
static struct altsvc *altsvc_createid(const char *srchost,
94
                                      const char *dsthost,
95
                                      enum alpnid srcalpnid,
96
                                      enum alpnid dstalpnid,
97
                                      unsigned int srcport,
98
                                      unsigned int dstport)
99
0
{
100
0
  struct altsvc *as = calloc(1, sizeof(struct altsvc));
101
0
  size_t hlen;
102
0
  size_t dlen;
103
0
  if(!as)
104
0
    return NULL;
105
0
  hlen = strlen(srchost);
106
0
  dlen = strlen(dsthost);
107
0
  DEBUGASSERT(hlen);
108
0
  DEBUGASSERT(dlen);
109
0
  if(!hlen || !dlen) {
110
    /* bad input */
111
0
    free(as);
112
0
    return NULL;
113
0
  }
114
0
  if((hlen > 2) && srchost[0] == '[') {
115
    /* IPv6 address, strip off brackets */
116
0
    srchost++;
117
0
    hlen -= 2;
118
0
  }
119
0
  else if(srchost[hlen - 1] == '.')
120
    /* strip off trailing dot */
121
0
    hlen--;
122
0
  if((dlen > 2) && dsthost[0] == '[') {
123
    /* IPv6 address, strip off brackets */
124
0
    dsthost++;
125
0
    dlen -= 2;
126
0
  }
127
128
0
  as->src.host = Curl_memdup0(srchost, hlen);
129
0
  if(!as->src.host)
130
0
    goto error;
131
132
0
  as->dst.host = Curl_memdup0(dsthost, dlen);
133
0
  if(!as->dst.host)
134
0
    goto error;
135
136
0
  as->src.alpnid = srcalpnid;
137
0
  as->dst.alpnid = dstalpnid;
138
0
  as->src.port = curlx_ultous(srcport);
139
0
  as->dst.port = curlx_ultous(dstport);
140
141
0
  return as;
142
0
error:
143
0
  altsvc_free(as);
144
0
  return NULL;
145
0
}
146
147
static struct altsvc *altsvc_create(char *srchost,
148
                                    char *dsthost,
149
                                    char *srcalpn,
150
                                    char *dstalpn,
151
                                    unsigned int srcport,
152
                                    unsigned int dstport)
153
0
{
154
0
  enum alpnid dstalpnid = alpn2alpnid(dstalpn);
155
0
  enum alpnid srcalpnid = alpn2alpnid(srcalpn);
156
0
  if(!srcalpnid || !dstalpnid)
157
0
    return NULL;
158
0
  return altsvc_createid(srchost, dsthost, srcalpnid, dstalpnid,
159
0
                         srcport, dstport);
160
0
}
161
162
/* only returns SERIOUS errors */
163
static CURLcode altsvc_add(struct altsvcinfo *asi, char *line)
164
0
{
165
  /* Example line:
166
     h2 example.com 443 h3 shiny.example.com 8443 "20191231 10:00:00" 1
167
   */
168
0
  char srchost[MAX_ALTSVC_HOSTLEN + 1];
169
0
  char dsthost[MAX_ALTSVC_HOSTLEN + 1];
170
0
  char srcalpn[MAX_ALTSVC_ALPNLEN + 1];
171
0
  char dstalpn[MAX_ALTSVC_ALPNLEN + 1];
172
0
  char date[MAX_ALTSVC_DATELEN + 1];
173
0
  unsigned int srcport;
174
0
  unsigned int dstport;
175
0
  unsigned int prio;
176
0
  unsigned int persist;
177
0
  int rc;
178
179
0
  rc = sscanf(line,
180
0
              "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
181
0
              "%" MAX_ALTSVC_ALPNLENSTR "s %" MAX_ALTSVC_HOSTLENSTR "s %u "
182
0
              "\"%" MAX_ALTSVC_DATELENSTR "[^\"]\" %u %u",
183
0
              srcalpn, srchost, &srcport,
184
0
              dstalpn, dsthost, &dstport,
185
0
              date, &persist, &prio);
186
0
  if(9 == rc) {
187
0
    struct altsvc *as;
188
0
    time_t expires = Curl_getdate_capped(date);
189
0
    as = altsvc_create(srchost, dsthost, srcalpn, dstalpn, srcport, dstport);
190
0
    if(as) {
191
0
      as->expires = expires;
192
0
      as->prio = prio;
193
0
      as->persist = persist ? 1 : 0;
194
0
      Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
195
0
    }
196
0
  }
197
198
0
  return CURLE_OK;
199
0
}
200
201
/*
202
 * Load alt-svc entries from the given file. The text based line-oriented file
203
 * format is documented here: https://curl.se/docs/alt-svc.html
204
 *
205
 * This function only returns error on major problems that prevent alt-svc
206
 * handling to work completely. It will ignore individual syntactical errors
207
 * etc.
208
 */
209
static CURLcode altsvc_load(struct altsvcinfo *asi, const char *file)
210
0
{
211
0
  CURLcode result = CURLE_OK;
212
0
  FILE *fp;
213
214
  /* we need a private copy of the file name so that the altsvc cache file
215
     name survives an easy handle reset */
216
0
  free(asi->filename);
217
0
  asi->filename = strdup(file);
218
0
  if(!asi->filename)
219
0
    return CURLE_OUT_OF_MEMORY;
220
221
0
  fp = fopen(file, FOPEN_READTEXT);
222
0
  if(fp) {
223
0
    struct dynbuf buf;
224
0
    Curl_dyn_init(&buf, MAX_ALTSVC_LINE);
225
0
    while(Curl_get_line(&buf, fp)) {
226
0
      char *lineptr = Curl_dyn_ptr(&buf);
227
0
      while(*lineptr && ISBLANK(*lineptr))
228
0
        lineptr++;
229
0
      if(*lineptr == '#')
230
        /* skip commented lines */
231
0
        continue;
232
233
0
      altsvc_add(asi, lineptr);
234
0
    }
235
0
    Curl_dyn_free(&buf); /* free the line buffer */
236
0
    fclose(fp);
237
0
  }
238
0
  return result;
239
0
}
240
241
/*
242
 * Write this single altsvc entry to a single output line
243
 */
244
245
static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
246
0
{
247
0
  struct tm stamp;
248
0
  const char *dst6_pre = "";
249
0
  const char *dst6_post = "";
250
0
  const char *src6_pre = "";
251
0
  const char *src6_post = "";
252
0
  CURLcode result = Curl_gmtime(as->expires, &stamp);
253
0
  if(result)
254
0
    return result;
255
0
#ifdef ENABLE_IPV6
256
0
  else {
257
0
    char ipv6_unused[16];
258
0
    if(1 == Curl_inet_pton(AF_INET6, as->dst.host, ipv6_unused)) {
259
0
      dst6_pre = "[";
260
0
      dst6_post = "]";
261
0
    }
262
0
    if(1 == Curl_inet_pton(AF_INET6, as->src.host, ipv6_unused)) {
263
0
      src6_pre = "[";
264
0
      src6_post = "]";
265
0
    }
266
0
  }
267
0
#endif
268
0
  fprintf(fp,
269
0
          "%s %s%s%s %u "
270
0
          "%s %s%s%s %u "
271
0
          "\"%d%02d%02d "
272
0
          "%02d:%02d:%02d\" "
273
0
          "%u %d\n",
274
0
          Curl_alpnid2str(as->src.alpnid),
275
0
          src6_pre, as->src.host, src6_post,
276
0
          as->src.port,
277
278
0
          Curl_alpnid2str(as->dst.alpnid),
279
0
          dst6_pre, as->dst.host, dst6_post,
280
0
          as->dst.port,
281
282
0
          stamp.tm_year + 1900, stamp.tm_mon + 1, stamp.tm_mday,
283
0
          stamp.tm_hour, stamp.tm_min, stamp.tm_sec,
284
0
          as->persist, as->prio);
285
0
  return CURLE_OK;
286
0
}
287
288
/* ---- library-wide functions below ---- */
289
290
/*
291
 * Curl_altsvc_init() creates a new altsvc cache.
292
 * It returns the new instance or NULL if something goes wrong.
293
 */
294
struct altsvcinfo *Curl_altsvc_init(void)
295
0
{
296
0
  struct altsvcinfo *asi = calloc(1, sizeof(struct altsvcinfo));
297
0
  if(!asi)
298
0
    return NULL;
299
0
  Curl_llist_init(&asi->list, NULL);
300
301
  /* set default behavior */
302
0
  asi->flags = CURLALTSVC_H1
303
#ifdef USE_HTTP2
304
    | CURLALTSVC_H2
305
#endif
306
#ifdef ENABLE_QUIC
307
    | CURLALTSVC_H3
308
#endif
309
0
    ;
310
0
  return asi;
311
0
}
312
313
/*
314
 * Curl_altsvc_load() loads alt-svc from file.
315
 */
316
CURLcode Curl_altsvc_load(struct altsvcinfo *asi, const char *file)
317
0
{
318
0
  CURLcode result;
319
0
  DEBUGASSERT(asi);
320
0
  result = altsvc_load(asi, file);
321
0
  return result;
322
0
}
323
324
/*
325
 * Curl_altsvc_ctrl() passes on the external bitmask.
326
 */
327
CURLcode Curl_altsvc_ctrl(struct altsvcinfo *asi, const long ctrl)
328
0
{
329
0
  DEBUGASSERT(asi);
330
0
  asi->flags = ctrl;
331
0
  return CURLE_OK;
332
0
}
333
334
/*
335
 * Curl_altsvc_cleanup() frees an altsvc cache instance and all associated
336
 * resources.
337
 */
338
void Curl_altsvc_cleanup(struct altsvcinfo **altsvcp)
339
0
{
340
0
  struct Curl_llist_element *e;
341
0
  struct Curl_llist_element *n;
342
0
  if(*altsvcp) {
343
0
    struct altsvcinfo *altsvc = *altsvcp;
344
0
    for(e = altsvc->list.head; e; e = n) {
345
0
      struct altsvc *as = e->ptr;
346
0
      n = e->next;
347
0
      altsvc_free(as);
348
0
    }
349
0
    free(altsvc->filename);
350
0
    free(altsvc);
351
0
    *altsvcp = NULL; /* clear the pointer */
352
0
  }
353
0
}
354
355
/*
356
 * Curl_altsvc_save() writes the altsvc cache to a file.
357
 */
358
CURLcode Curl_altsvc_save(struct Curl_easy *data,
359
                          struct altsvcinfo *altsvc, const char *file)
360
0
{
361
0
  struct Curl_llist_element *e;
362
0
  struct Curl_llist_element *n;
363
0
  CURLcode result = CURLE_OK;
364
0
  FILE *out;
365
0
  char *tempstore = NULL;
366
367
0
  if(!altsvc)
368
    /* no cache activated */
369
0
    return CURLE_OK;
370
371
  /* if not new name is given, use the one we stored from the load */
372
0
  if(!file && altsvc->filename)
373
0
    file = altsvc->filename;
374
375
0
  if((altsvc->flags & CURLALTSVC_READONLYFILE) || !file || !file[0])
376
    /* marked as read-only, no file or zero length file name */
377
0
    return CURLE_OK;
378
379
0
  result = Curl_fopen(data, file, &out, &tempstore);
380
0
  if(!result) {
381
0
    fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
382
0
          "# This file was generated by libcurl! Edit at your own risk.\n",
383
0
          out);
384
0
    for(e = altsvc->list.head; e; e = n) {
385
0
      struct altsvc *as = e->ptr;
386
0
      n = e->next;
387
0
      result = altsvc_out(as, out);
388
0
      if(result)
389
0
        break;
390
0
    }
391
0
    fclose(out);
392
0
    if(!result && tempstore && Curl_rename(tempstore, file))
393
0
      result = CURLE_WRITE_ERROR;
394
395
0
    if(result && tempstore)
396
0
      unlink(tempstore);
397
0
  }
398
0
  free(tempstore);
399
0
  return result;
400
0
}
401
402
static CURLcode getalnum(const char **ptr, char *alpnbuf, size_t buflen)
403
0
{
404
0
  size_t len;
405
0
  const char *protop;
406
0
  const char *p = *ptr;
407
0
  while(*p && ISBLANK(*p))
408
0
    p++;
409
0
  protop = p;
410
0
  while(*p && !ISBLANK(*p) && (*p != ';') && (*p != '='))
411
0
    p++;
412
0
  len = p - protop;
413
0
  *ptr = p;
414
415
0
  if(!len || (len >= buflen))
416
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
417
0
  memcpy(alpnbuf, protop, len);
418
0
  alpnbuf[len] = 0;
419
0
  return CURLE_OK;
420
0
}
421
422
/* hostcompare() returns true if 'host' matches 'check'. The first host
423
 * argument may have a trailing dot present that will be ignored.
424
 */
425
static bool hostcompare(const char *host, const char *check)
426
0
{
427
0
  size_t hlen = strlen(host);
428
0
  size_t clen = strlen(check);
429
430
0
  if(hlen && (host[hlen - 1] == '.'))
431
0
    hlen--;
432
0
  if(hlen != clen)
433
    /* they can't match if they have different lengths */
434
0
    return FALSE;
435
0
  return strncasecompare(host, check, hlen);
436
0
}
437
438
/* altsvc_flush() removes all alternatives for this source origin from the
439
   list */
440
static void altsvc_flush(struct altsvcinfo *asi, enum alpnid srcalpnid,
441
                         const char *srchost, unsigned short srcport)
442
0
{
443
0
  struct Curl_llist_element *e;
444
0
  struct Curl_llist_element *n;
445
0
  for(e = asi->list.head; e; e = n) {
446
0
    struct altsvc *as = e->ptr;
447
0
    n = e->next;
448
0
    if((srcalpnid == as->src.alpnid) &&
449
0
       (srcport == as->src.port) &&
450
0
       hostcompare(srchost, as->src.host)) {
451
0
      Curl_llist_remove(&asi->list, e, NULL);
452
0
      altsvc_free(as);
453
0
    }
454
0
  }
455
0
}
456
457
#ifdef DEBUGBUILD
458
/* to play well with debug builds, we can *set* a fixed time this will
459
   return */
460
static time_t altsvc_debugtime(void *unused)
461
{
462
  char *timestr = getenv("CURL_TIME");
463
  (void)unused;
464
  if(timestr) {
465
    unsigned long val = strtol(timestr, NULL, 10);
466
    return (time_t)val;
467
  }
468
  return time(NULL);
469
}
470
#undef time
471
#define time(x) altsvc_debugtime(x)
472
#endif
473
474
0
#define ISNEWLINE(x) (((x) == '\n') || (x) == '\r')
475
476
/*
477
 * Curl_altsvc_parse() takes an incoming alt-svc response header and stores
478
 * the data correctly in the cache.
479
 *
480
 * 'value' points to the header *value*. That's contents to the right of the
481
 * header name.
482
 *
483
 * Currently this function rejects invalid data without returning an error.
484
 * Invalid host name, port number will result in the specific alternative
485
 * being rejected. Unknown protocols are skipped.
486
 */
487
CURLcode Curl_altsvc_parse(struct Curl_easy *data,
488
                           struct altsvcinfo *asi, const char *value,
489
                           enum alpnid srcalpnid, const char *srchost,
490
                           unsigned short srcport)
491
0
{
492
0
  const char *p = value;
493
0
  size_t len;
494
0
  char namebuf[MAX_ALTSVC_HOSTLEN] = "";
495
0
  char alpnbuf[MAX_ALTSVC_ALPNLEN] = "";
496
0
  struct altsvc *as;
497
0
  unsigned short dstport = srcport; /* the same by default */
498
0
  CURLcode result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
499
0
  size_t entries = 0;
500
#ifdef CURL_DISABLE_VERBOSE_STRINGS
501
  (void)data;
502
#endif
503
0
  if(result) {
504
0
    infof(data, "Excessive alt-svc header, ignoring.");
505
0
    return CURLE_OK;
506
0
  }
507
508
0
  DEBUGASSERT(asi);
509
510
  /* "clear" is a magic keyword */
511
0
  if(strcasecompare(alpnbuf, "clear")) {
512
    /* Flush cached alternatives for this source origin */
513
0
    altsvc_flush(asi, srcalpnid, srchost, srcport);
514
0
    return CURLE_OK;
515
0
  }
516
517
0
  do {
518
0
    if(*p == '=') {
519
      /* [protocol]="[host][:port]" */
520
0
      enum alpnid dstalpnid = alpn2alpnid(alpnbuf); /* the same by default */
521
0
      p++;
522
0
      if(*p == '\"') {
523
0
        const char *dsthost = "";
524
0
        const char *value_ptr;
525
0
        char option[32];
526
0
        unsigned long num;
527
0
        char *end_ptr;
528
0
        bool quoted = FALSE;
529
0
        time_t maxage = 24 * 3600; /* default is 24 hours */
530
0
        bool persist = FALSE;
531
0
        bool valid = TRUE;
532
0
        p++;
533
0
        if(*p != ':') {
534
          /* host name starts here */
535
0
          const char *hostp = p;
536
0
          if(*p == '[') {
537
            /* pass all valid IPv6 letters - does not handle zone id */
538
0
            len = strspn(++p, "0123456789abcdefABCDEF:.");
539
0
            if(p[len] != ']')
540
              /* invalid host syntax, bail out */
541
0
              break;
542
            /* we store the IPv6 numerical address *with* brackets */
543
0
            len += 2;
544
0
            p = &p[len-1];
545
0
          }
546
0
          else {
547
0
            while(*p && (ISALNUM(*p) || (*p == '.') || (*p == '-')))
548
0
              p++;
549
0
            len = p - hostp;
550
0
          }
551
0
          if(!len || (len >= MAX_ALTSVC_HOSTLEN)) {
552
0
            infof(data, "Excessive alt-svc host name, ignoring.");
553
0
            valid = FALSE;
554
0
          }
555
0
          else {
556
0
            memcpy(namebuf, hostp, len);
557
0
            namebuf[len] = 0;
558
0
            dsthost = namebuf;
559
0
          }
560
0
        }
561
0
        else {
562
          /* no destination name, use source host */
563
0
          dsthost = srchost;
564
0
        }
565
0
        if(*p == ':') {
566
0
          unsigned long port = 0;
567
0
          p++;
568
0
          if(ISDIGIT(*p))
569
            /* a port number */
570
0
            port = strtoul(p, &end_ptr, 10);
571
0
          else
572
0
            end_ptr = (char *)p; /* not left uninitialized */
573
0
          if(!port || port > USHRT_MAX || end_ptr == p || *end_ptr != '\"') {
574
0
            infof(data, "Unknown alt-svc port number, ignoring.");
575
0
            valid = FALSE;
576
0
          }
577
0
          else {
578
0
            dstport = curlx_ultous(port);
579
0
            p = end_ptr;
580
0
          }
581
0
        }
582
0
        if(*p++ != '\"')
583
0
          break;
584
        /* Handle the optional 'ma' and 'persist' flags. Unknown flags
585
           are skipped. */
586
0
        for(;;) {
587
0
          while(ISBLANK(*p))
588
0
            p++;
589
0
          if(*p != ';')
590
0
            break;
591
0
          p++; /* pass the semicolon */
592
0
          if(!*p || ISNEWLINE(*p))
593
0
            break;
594
0
          result = getalnum(&p, option, sizeof(option));
595
0
          if(result) {
596
            /* skip option if name is too long */
597
0
            option[0] = '\0';
598
0
          }
599
0
          while(*p && ISBLANK(*p))
600
0
            p++;
601
0
          if(*p != '=')
602
0
            return CURLE_OK;
603
0
          p++;
604
0
          while(*p && ISBLANK(*p))
605
0
            p++;
606
0
          if(!*p)
607
0
            return CURLE_OK;
608
0
          if(*p == '\"') {
609
            /* quoted value */
610
0
            p++;
611
0
            quoted = TRUE;
612
0
          }
613
0
          value_ptr = p;
614
0
          if(quoted) {
615
0
            while(*p && *p != '\"')
616
0
              p++;
617
0
            if(!*p++)
618
0
              return CURLE_OK;
619
0
          }
620
0
          else {
621
0
            while(*p && !ISBLANK(*p) && *p!= ';' && *p != ',')
622
0
              p++;
623
0
          }
624
0
          num = strtoul(value_ptr, &end_ptr, 10);
625
0
          if((end_ptr != value_ptr) && (num < ULONG_MAX)) {
626
0
            if(strcasecompare("ma", option))
627
0
              maxage = num;
628
0
            else if(strcasecompare("persist", option) && (num == 1))
629
0
              persist = TRUE;
630
0
          }
631
0
        }
632
0
        if(dstalpnid && valid) {
633
0
          if(!entries++)
634
            /* Flush cached alternatives for this source origin, if any - when
635
               this is the first entry of the line. */
636
0
            altsvc_flush(asi, srcalpnid, srchost, srcport);
637
638
0
          as = altsvc_createid(srchost, dsthost,
639
0
                               srcalpnid, dstalpnid,
640
0
                               srcport, dstport);
641
0
          if(as) {
642
            /* The expires time also needs to take the Age: value (if any) into
643
               account. [See RFC 7838 section 3.1] */
644
0
            as->expires = maxage + time(NULL);
645
0
            as->persist = persist;
646
0
            Curl_llist_insert_next(&asi->list, asi->list.tail, as, &as->node);
647
0
            infof(data, "Added alt-svc: %s:%d over %s", dsthost, dstport,
648
0
                  Curl_alpnid2str(dstalpnid));
649
0
          }
650
0
        }
651
0
      }
652
0
      else
653
0
        break;
654
      /* after the double quote there can be a comma if there's another
655
         string or a semicolon if no more */
656
0
      if(*p == ',') {
657
        /* comma means another alternative is presented */
658
0
        p++;
659
0
        result = getalnum(&p, alpnbuf, sizeof(alpnbuf));
660
0
        if(result)
661
0
          break;
662
0
      }
663
0
    }
664
0
    else
665
0
      break;
666
0
  } while(*p && (*p != ';') && (*p != '\n') && (*p != '\r'));
667
668
0
  return CURLE_OK;
669
0
}
670
671
/*
672
 * Return TRUE on a match
673
 */
674
bool Curl_altsvc_lookup(struct altsvcinfo *asi,
675
                        enum alpnid srcalpnid, const char *srchost,
676
                        int srcport,
677
                        struct altsvc **dstentry,
678
                        const int versions) /* one or more bits */
679
0
{
680
0
  struct Curl_llist_element *e;
681
0
  struct Curl_llist_element *n;
682
0
  time_t now = time(NULL);
683
0
  DEBUGASSERT(asi);
684
0
  DEBUGASSERT(srchost);
685
0
  DEBUGASSERT(dstentry);
686
687
0
  for(e = asi->list.head; e; e = n) {
688
0
    struct altsvc *as = e->ptr;
689
0
    n = e->next;
690
0
    if(as->expires < now) {
691
      /* an expired entry, remove */
692
0
      Curl_llist_remove(&asi->list, e, NULL);
693
0
      altsvc_free(as);
694
0
      continue;
695
0
    }
696
0
    if((as->src.alpnid == srcalpnid) &&
697
0
       hostcompare(srchost, as->src.host) &&
698
0
       (as->src.port == srcport) &&
699
0
       (versions & as->dst.alpnid)) {
700
      /* match */
701
0
      *dstentry = as;
702
0
      return TRUE;
703
0
    }
704
0
  }
705
0
  return FALSE;
706
0
}
707
708
#endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_ALTSVC */