Coverage Report

Created: 2025-07-11 06:33

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