Coverage Report

Created: 2026-12-01 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/dynhds.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
#include "curl_setup.h"
25
26
#include "dynhds.h"
27
#include "strcase.h"
28
#include "curlx/strparse.h"
29
30
static struct dynhds_entry *entry_new(const char *name, uint32_t namelen,
31
                                      const char *value, uint32_t valuelen,
32
                                      uint8_t opts)
33
239k
{
34
239k
  struct dynhds_entry *e;
35
239k
  char *p;
36
37
  /* namelen + valuelen <= SIZE_MAX has already been checked by caller */
38
239k
  if((SIZE_MAX - namelen - valuelen) < (sizeof(*e) + 2))
39
0
    return NULL;
40
239k
  e = curlx_calloc(1, sizeof(*e) + namelen + valuelen + 2);
41
239k
  if(!e)
42
17
    return NULL;
43
238k
  e->name = p = (char *)e + sizeof(*e);
44
238k
  memcpy(p, name, namelen);
45
238k
  e->namelen = namelen;
46
238k
  e->value = p += namelen + 1; /* leave a \0 at the end of name */
47
238k
  if(valuelen) {
48
226k
    memcpy(p, value, valuelen);
49
226k
    e->valuelen = valuelen;
50
226k
  }
51
238k
  if(opts & DYNHDS_OPT_LOWERCASE)
52
135k
    Curl_strntolower(e->name, e->name, e->namelen);
53
238k
  return e;
54
239k
}
55
56
static void entry_free(struct dynhds_entry *e)
57
238k
{
58
238k
  curlx_free(e);
59
238k
}
60
61
void Curl_dynhds_init(struct dynhds *dynhds, uint32_t max_entries,
62
                      uint32_t max_strs_size)
63
45.7k
{
64
45.7k
  DEBUGASSERT(dynhds);
65
45.7k
  dynhds->hds = NULL;
66
45.7k
  dynhds->hds_len = dynhds->hds_allc = dynhds->strs_len = 0;
67
45.7k
  dynhds->max_entries = max_entries ? max_entries : UINT32_MAX;
68
45.7k
  dynhds->max_strs_size = max_strs_size ? max_strs_size : UINT32_MAX;
69
45.7k
  dynhds->opts = 0;
70
45.7k
}
71
72
void Curl_dynhds_reset(struct dynhds *dynhds)
73
57.1k
{
74
57.1k
  DEBUGASSERT(dynhds);
75
57.1k
  if(dynhds->hds_len) {
76
22.7k
    size_t i;
77
22.7k
    DEBUGASSERT(dynhds->hds);
78
261k
    for(i = 0; i < dynhds->hds_len; ++i) {
79
238k
      entry_free(dynhds->hds[i]);
80
238k
      dynhds->hds[i] = NULL;
81
238k
    }
82
22.7k
  }
83
57.1k
  dynhds->hds_len = dynhds->strs_len = 0;
84
57.1k
}
85
86
void Curl_dynhds_cleanup(struct dynhds *dynhds)
87
45.7k
{
88
45.7k
  DEBUGASSERT(dynhds);
89
45.7k
  Curl_dynhds_reset(dynhds);
90
45.7k
  curlx_safefree(dynhds->hds);
91
45.7k
  dynhds->hds_allc = 0;
92
45.7k
}
93
94
uint32_t Curl_dynhds_count(struct dynhds *dynhds)
95
11.4k
{
96
11.4k
  return dynhds->hds_len;
97
11.4k
}
98
99
void Curl_dynhds_set_opts(struct dynhds *dynhds, uint8_t opts)
100
11.3k
{
101
11.3k
  dynhds->opts = opts;
102
11.3k
}
103
104
struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, uint32_t n)
105
102k
{
106
102k
  DEBUGASSERT(dynhds);
107
102k
  return (n < dynhds->hds_len) ? dynhds->hds[n] : NULL;
108
102k
}
109
110
struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds, const char *name,
111
                                     size_t namelen)
112
11.1k
{
113
11.1k
  size_t i;
114
12.6k
  for(i = 0; i < dynhds->hds_len; ++i) {
115
12.5k
    if(dynhds->hds[i]->namelen == namelen &&
116
11.2k
       curl_strnequal(dynhds->hds[i]->name, name, namelen)) {
117
11.0k
      return dynhds->hds[i];
118
11.0k
    }
119
12.5k
  }
120
109
  return NULL;
121
11.1k
}
122
123
struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name)
124
0
{
125
0
  return Curl_dynhds_get(dynhds, name, strlen(name));
126
0
}
127
128
CURLcode Curl_dynhds_add(struct dynhds *dynhds,
129
                         const char *name, size_t namelen,
130
                         const char *value, size_t valuelen)
131
239k
{
132
239k
  struct dynhds_entry *entry = NULL;
133
239k
  CURLcode result = CURLE_OUT_OF_MEMORY;
134
239k
  uint32_t nlen, vlen;
135
136
  /* Accept NULL value with length 0, name must have length */
137
239k
  if(!dynhds || !name || !namelen || (!value && valuelen)) {
138
0
    DEBUGASSERT(0);
139
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
140
0
  }
141
239k
  if((namelen > UINT32_MAX) || (valuelen > UINT32_MAX))
142
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
143
239k
  nlen = (uint32_t)namelen;
144
239k
  vlen = (uint32_t)valuelen;
145
146
  /* Does the new header fit into the limits? */
147
239k
  if((dynhds->hds_len >= dynhds->max_entries) ||
148
239k
     (dynhds->max_strs_size - dynhds->strs_len < nlen) ||
149
239k
     (dynhds->max_strs_size - dynhds->strs_len - nlen < vlen))
150
0
    return CURLE_OUT_OF_MEMORY;
151
152
239k
  entry = entry_new(name, nlen, value, vlen, dynhds->opts);
153
239k
  if(!entry)
154
17
    goto out;
155
156
238k
  if((dynhds->hds_allc - dynhds->hds_len) < 1) {
157
25.8k
    struct dynhds_entry **nhds;
158
25.8k
    uint32_t nhds_allc;
159
160
    /* start with room for 16 headers, then double, add max 512 new ones
161
     * or what the limit still allows. */
162
25.8k
    if(dynhds->hds_allc) {
163
3.12k
      uint32_t max_grow =
164
3.12k
        CURLMIN(dynhds->max_entries - dynhds->hds_allc, 512);
165
3.12k
      nhds_allc = dynhds->hds_allc + CURLMIN(dynhds->hds_allc, max_grow);
166
3.12k
    }
167
22.7k
    else
168
22.7k
      nhds_allc = 16;
169
170
25.8k
    nhds = curlx_calloc(nhds_allc, sizeof(struct dynhds_entry *));
171
25.8k
    if(!nhds)
172
3
      goto out;
173
25.8k
    if(dynhds->hds) {
174
3.12k
      memcpy(nhds, dynhds->hds,
175
3.12k
             dynhds->hds_len * sizeof(struct dynhds_entry *));
176
3.12k
      curlx_safefree(dynhds->hds);
177
3.12k
    }
178
25.8k
    dynhds->hds = nhds;
179
25.8k
    dynhds->hds_allc = nhds_allc;
180
25.8k
  }
181
238k
  dynhds->hds[dynhds->hds_len++] = entry;
182
238k
  entry = NULL;
183
238k
  dynhds->strs_len += nlen + vlen;
184
238k
  result = CURLE_OK;
185
186
239k
out:
187
239k
  if(entry)
188
3
    entry_free(entry);
189
239k
  return result;
190
238k
}
191
192
CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
193
                          const char *name, const char *value)
194
0
{
195
0
  if(!name)
196
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
197
0
  return Curl_dynhds_add(dynhds, name, strlen(name),
198
0
                         value, value ? strlen(value) : 0);
199
0
}
200
201
CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds,
202
                                 const char *line, size_t line_len)
203
103k
{
204
103k
  const char *p;
205
103k
  const char *name;
206
103k
  size_t namelen;
207
103k
  const char *value;
208
103k
  size_t valuelen, i;
209
210
103k
  if(!line || !line_len)
211
0
    return CURLE_OK;
212
213
103k
  p = memchr(line, ':', line_len);
214
103k
  if(!p || p == line)
215
10
    return CURLE_BAD_FUNCTION_ARGUMENT;
216
103k
  name = line;
217
103k
  namelen = p - line;
218
103k
  p++; /* move past the colon */
219
195k
  for(i = namelen + 1; i < line_len; ++i, ++p) {
220
188k
    if(!ISBLANK(*p))
221
96.5k
      break;
222
188k
  }
223
103k
  value = p;
224
103k
  valuelen = line_len - i;
225
226
103k
  p = memchr(value, '\r', valuelen);
227
103k
  if(!p)
228
101k
    p = memchr(value, '\n', valuelen);
229
103k
  if(p)
230
1.02k
    valuelen = (size_t)(p - value);
231
232
103k
  return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
233
103k
}
234
235
CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line)
236
0
{
237
0
  return Curl_dynhds_h1_add_line(dynhds, line, line ? strlen(line) : 0);
238
0
}
239
240
#ifdef UNITTESTS
241
/* @unittest 2602 */
242
243
/**
244
 * Return TRUE iff one or more headers with the given name exist.
245
 */
246
UNITTEST bool dynhds_contains(struct dynhds *dynhds,
247
                              const char *name, size_t namelen);
248
UNITTEST bool dynhds_contains(struct dynhds *dynhds,
249
                              const char *name, size_t namelen)
250
0
{
251
0
  return !!Curl_dynhds_get(dynhds, name, namelen);
252
0
}
253
254
/* @unittest 2602 */
255
UNITTEST bool dynhds_ccontains(struct dynhds *dynhds, const char *name);
256
UNITTEST bool dynhds_ccontains(struct dynhds *dynhds, const char *name)
257
0
{
258
0
  return dynhds_contains(dynhds, name, strlen(name));
259
0
}
260
261
/**
262
 * Return how often the given name appears in `dynhds`.
263
 * Names are case-insensitive.
264
 *
265
 * @unittest 2602
266
 */
267
UNITTEST size_t dynhds_count_name(struct dynhds *dynhds,
268
                                  const char *name, size_t namelen);
269
UNITTEST size_t dynhds_count_name(struct dynhds *dynhds,
270
                                  const char *name, size_t namelen)
271
0
{
272
0
  size_t n = 0;
273
0
  if(dynhds->hds_len) {
274
0
    size_t i;
275
0
    for(i = 0; i < dynhds->hds_len; ++i) {
276
0
      if((namelen == dynhds->hds[i]->namelen) &&
277
0
         curl_strnequal(name, dynhds->hds[i]->name, namelen))
278
0
        ++n;
279
0
    }
280
0
  }
281
0
  return n;
282
0
}
283
284
/**
285
 * Return how often the given null-terminated name appears in `dynhds`.
286
 * Names are case-insensitive.
287
 *
288
 * @unittest 2602
289
 */
290
UNITTEST size_t dynhds_ccount_name(struct dynhds *dynhds,
291
                                        const char *name);
292
UNITTEST size_t dynhds_ccount_name(struct dynhds *dynhds,
293
                                        const char *name)
294
0
{
295
0
  return dynhds_count_name(dynhds, name, strlen(name));
296
0
}
297
298
/**
299
 * Remove all entries with the given name.
300
 * Returns number of entries removed.
301
 *
302
 * @unittest 2602
303
 */
304
UNITTEST size_t dynhds_remove(struct dynhds *dynhds,
305
                              const char *name, size_t namelen);
306
UNITTEST size_t dynhds_remove(struct dynhds *dynhds,
307
                              const char *name, size_t namelen)
308
0
{
309
0
  size_t n = 0;
310
0
  if(dynhds->hds_len) {
311
0
    size_t i, len;
312
0
    for(i = 0; i < dynhds->hds_len; ++i) {
313
0
      if((namelen == dynhds->hds[i]->namelen) &&
314
0
         curl_strnequal(name, dynhds->hds[i]->name, namelen)) {
315
0
        ++n;
316
0
        --dynhds->hds_len;
317
0
        dynhds->strs_len -= (dynhds->hds[i]->namelen +
318
0
                             dynhds->hds[i]->valuelen);
319
0
        entry_free(dynhds->hds[i]);
320
0
        len = dynhds->hds_len - i; /* remaining entries */
321
0
        if(len) {
322
0
          memmove(&dynhds->hds[i], &dynhds->hds[i + 1],
323
0
                  len * sizeof(dynhds->hds[i]));
324
0
        }
325
0
        --i; /* do this index again */
326
0
      }
327
0
    }
328
0
  }
329
0
  return n;
330
0
}
331
332
/**
333
 * Set the give header name and value, replacing any entries with
334
 * the same name. The header is added at the end of all (remaining)
335
 * entries.
336
 *
337
 * @unittest 2602
338
 */
339
UNITTEST CURLcode dynhds_set(struct dynhds *dynhds,
340
                             const char *name, size_t namelen,
341
                             const char *value, size_t valuelen);
342
UNITTEST CURLcode dynhds_set(struct dynhds *dynhds,
343
                             const char *name, size_t namelen,
344
                             const char *value, size_t valuelen)
345
0
{
346
0
  dynhds_remove(dynhds, name, namelen);
347
0
  return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
348
0
}
349
350
/* @unittest 2602 */
351
UNITTEST size_t dynhds_cremove(struct dynhds *dynhds, const char *name);
352
UNITTEST size_t dynhds_cremove(struct dynhds *dynhds, const char *name)
353
0
{
354
0
  return dynhds_remove(dynhds, name, strlen(name));
355
0
}
356
357
#endif /* UNITTESTS */
358
359
CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf)
360
0
{
361
0
  CURLcode result = CURLE_OK;
362
0
  size_t i;
363
364
0
  if(!dynhds->hds_len)
365
0
    return result;
366
367
0
  for(i = 0; i < dynhds->hds_len; ++i) {
368
0
    result = curlx_dyn_addn(dbuf, dynhds->hds[i]->name,
369
0
                            dynhds->hds[i]->namelen);
370
0
    if(!result)
371
0
      result = curlx_dyn_addn(dbuf, STRCONST(": "));
372
0
    if(!result)
373
0
      result = curlx_dyn_addn(dbuf, dynhds->hds[i]->value,
374
0
                              dynhds->hds[i]->valuelen);
375
0
    if(!result)
376
0
      result = curlx_dyn_addn(dbuf, STRCONST("\r\n"));
377
0
    if(result)
378
0
      break;
379
0
  }
380
381
0
  return result;
382
0
}
383
384
CURLcode Curl_dynhds_add_slist(struct dynhds *dynhds,
385
                               struct curl_slist *headers,
386
                               Curl_dynhds_hd_cb *hd_cb,
387
                               void *user_data)
388
50.5k
{
389
50.5k
  CURLcode result;
390
391
136k
  for(; headers; headers = headers->next) {
392
85.6k
    struct Curl_str name, value;
393
85.6k
    const char *ptr = headers->data;
394
85.6k
    size_t hlen = strlen(headers->data);
395
396
85.6k
    value.str = NULL;
397
85.6k
    value.len = 0;
398
    /* There are 2 quirks in place for custom headers:
399
     * 1. setting only 'name:' to suppress a header from being sent
400
     * 2. setting only 'name;' to send an empty (illegal) header
401
     */
402
85.6k
    if(!curlx_str_until(&ptr, &name, hlen, ';') &&
403
72.5k
       !curlx_str_single(&ptr, ';') &&
404
35.4k
       !curlx_str_single(&ptr, '\0') &&
405
33.3k
       !memchr(curlx_str(&name), ':', curlx_strlen(&name))) {
406
      /* quirk #2, send an empty header */
407
21.5k
      value.str = "";
408
21.5k
      value.len = 0;
409
21.5k
    }
410
64.0k
    else {
411
64.0k
      ptr = headers->data;
412
64.0k
      if(!curlx_str_until(&ptr, &name, hlen, ':') &&
413
46.3k
         !curlx_str_single(&ptr, ':')) {
414
25.5k
        curlx_str_untilnl(&ptr, &value, hlen);
415
25.5k
        curlx_str_trimblanks(&value);
416
25.5k
        if(!curlx_strlen(&value))
417
          /* quirk #1, do not send this */
418
4.02k
          continue;
419
25.5k
      }
420
38.5k
      else
421
        /* no colon */
422
38.5k
        continue;
423
64.0k
    }
424
425
43.1k
    DEBUGASSERT(curlx_strlen(&name) && value.str);
426
    /* trim surrounding whitespace so a padded field name to
427
     * prevent the name form passing checks */
428
43.1k
    curlx_str_trimblanks(&name);
429
43.1k
    if(!curlx_strlen(&name))
430
72
      continue;
431
432
43.0k
    if(hd_cb) {
433
43.0k
      result = hd_cb(user_data, &name, &value, headers->data);
434
43.0k
      if(result == CURLE_AGAIN)
435
43.0k
        continue;
436
0
      else if(result)
437
0
        return result;
438
43.0k
    }
439
440
0
    if(dynhds) {
441
0
      result = Curl_dynhds_add(dynhds, curlx_str(&name), curlx_strlen(&name),
442
0
                               curlx_str(&value), curlx_strlen(&value));
443
0
      if(result)
444
0
        return result;
445
0
    }
446
0
  }
447
50.5k
  return CURLE_OK;
448
50.5k
}
449
450
#ifdef USE_NGHTTP2
451
452
nghttp2_nv *Curl_dynhds_to_nva(struct dynhds *dynhds, size_t *pcount)
453
11.3k
{
454
11.3k
  nghttp2_nv *nva = curlx_calloc(1, sizeof(nghttp2_nv) * dynhds->hds_len);
455
11.3k
  size_t i;
456
457
11.3k
  *pcount = 0;
458
11.3k
  if(!nva)
459
2
    return NULL;
460
461
146k
  for(i = 0; i < dynhds->hds_len; ++i) {
462
135k
    struct dynhds_entry *e = dynhds->hds[i];
463
135k
    DEBUGASSERT(e);
464
135k
    nva[i].name = (unsigned char *)e->name;
465
135k
    nva[i].namelen = e->namelen;
466
135k
    nva[i].value = (unsigned char *)e->value;
467
135k
    nva[i].valuelen = e->valuelen;
468
135k
    nva[i].flags = NGHTTP2_NV_FLAG_NONE;
469
135k
  }
470
11.3k
  *pcount = dynhds->hds_len;
471
11.3k
  return nva;
472
11.3k
}
473
474
#endif /* USE_NGHTTP2 */