Coverage Report

Created: 2026-04-12 06:56

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
29
static struct dynhds_entry *entry_new(const char *name, size_t namelen,
30
                                      const char *value, size_t valuelen,
31
                                      int opts)
32
0
{
33
0
  struct dynhds_entry *e;
34
0
  char *p;
35
36
0
  DEBUGASSERT(name);
37
0
  DEBUGASSERT(value);
38
0
  e = curlx_calloc(1, sizeof(*e) + namelen + valuelen + 2);
39
0
  if(!e)
40
0
    return NULL;
41
0
  e->name = p = (char *)e + sizeof(*e);
42
0
  memcpy(p, name, namelen);
43
0
  e->namelen = namelen;
44
0
  e->value = p += namelen + 1; /* leave a \0 at the end of name */
45
0
  memcpy(p, value, valuelen);
46
0
  e->valuelen = valuelen;
47
0
  if(opts & DYNHDS_OPT_LOWERCASE)
48
0
    Curl_strntolower(e->name, e->name, e->namelen);
49
0
  return e;
50
0
}
51
52
static void entry_free(struct dynhds_entry *e)
53
0
{
54
0
  curlx_free(e);
55
0
}
56
57
void Curl_dynhds_init(struct dynhds *dynhds, size_t max_entries,
58
                      size_t max_strs_size)
59
0
{
60
0
  DEBUGASSERT(dynhds);
61
0
  DEBUGASSERT(max_strs_size);
62
0
  dynhds->hds = NULL;
63
0
  dynhds->hds_len = dynhds->hds_allc = dynhds->strs_len = 0;
64
0
  dynhds->max_entries = max_entries;
65
0
  dynhds->max_strs_size = max_strs_size;
66
0
  dynhds->opts = 0;
67
0
}
68
69
void Curl_dynhds_free(struct dynhds *dynhds)
70
0
{
71
0
  DEBUGASSERT(dynhds);
72
0
  if(dynhds->hds && dynhds->hds_len) {
73
0
    size_t i;
74
0
    DEBUGASSERT(dynhds->hds);
75
0
    for(i = 0; i < dynhds->hds_len; ++i) {
76
0
      entry_free(dynhds->hds[i]);
77
0
    }
78
0
  }
79
0
  curlx_safefree(dynhds->hds);
80
0
  dynhds->hds_len = dynhds->hds_allc = dynhds->strs_len = 0;
81
0
}
82
83
void Curl_dynhds_reset(struct dynhds *dynhds)
84
0
{
85
0
  DEBUGASSERT(dynhds);
86
0
  if(dynhds->hds_len) {
87
0
    size_t i;
88
0
    DEBUGASSERT(dynhds->hds);
89
0
    for(i = 0; i < dynhds->hds_len; ++i) {
90
0
      entry_free(dynhds->hds[i]);
91
0
      dynhds->hds[i] = NULL;
92
0
    }
93
0
  }
94
0
  dynhds->hds_len = dynhds->strs_len = 0;
95
0
}
96
97
size_t Curl_dynhds_count(struct dynhds *dynhds)
98
0
{
99
0
  return dynhds->hds_len;
100
0
}
101
102
void Curl_dynhds_set_opts(struct dynhds *dynhds, int opts)
103
0
{
104
0
  dynhds->opts = opts;
105
0
}
106
107
struct dynhds_entry *Curl_dynhds_getn(struct dynhds *dynhds, size_t n)
108
0
{
109
0
  DEBUGASSERT(dynhds);
110
0
  return (n < dynhds->hds_len) ? dynhds->hds[n] : NULL;
111
0
}
112
113
struct dynhds_entry *Curl_dynhds_get(struct dynhds *dynhds, const char *name,
114
                                     size_t namelen)
115
0
{
116
0
  size_t i;
117
0
  for(i = 0; i < dynhds->hds_len; ++i) {
118
0
    if(dynhds->hds[i]->namelen == namelen &&
119
0
       curl_strnequal(dynhds->hds[i]->name, name, namelen)) {
120
0
      return dynhds->hds[i];
121
0
    }
122
0
  }
123
0
  return NULL;
124
0
}
125
126
struct dynhds_entry *Curl_dynhds_cget(struct dynhds *dynhds, const char *name)
127
0
{
128
0
  return Curl_dynhds_get(dynhds, name, strlen(name));
129
0
}
130
131
CURLcode Curl_dynhds_add(struct dynhds *dynhds,
132
                         const char *name, size_t namelen,
133
                         const char *value, size_t valuelen)
134
0
{
135
0
  struct dynhds_entry *entry = NULL;
136
0
  CURLcode result = CURLE_OUT_OF_MEMORY;
137
138
0
  DEBUGASSERT(dynhds);
139
0
  if(dynhds->max_entries && dynhds->hds_len >= dynhds->max_entries)
140
0
    return CURLE_OUT_OF_MEMORY;
141
0
  if(dynhds->strs_len + namelen + valuelen > dynhds->max_strs_size)
142
0
    return CURLE_OUT_OF_MEMORY;
143
144
0
  entry = entry_new(name, namelen, value, valuelen, dynhds->opts);
145
0
  if(!entry)
146
0
    goto out;
147
148
0
  if(dynhds->hds_len + 1 >= dynhds->hds_allc) {
149
0
    size_t nallc = dynhds->hds_len + 16;
150
0
    struct dynhds_entry **nhds;
151
152
0
    if(dynhds->max_entries && nallc > dynhds->max_entries)
153
0
      nallc = dynhds->max_entries;
154
155
0
    nhds = curlx_calloc(nallc, sizeof(struct dynhds_entry *));
156
0
    if(!nhds)
157
0
      goto out;
158
0
    if(dynhds->hds) {
159
0
      memcpy(nhds, dynhds->hds,
160
0
             dynhds->hds_len * sizeof(struct dynhds_entry *));
161
0
      curlx_safefree(dynhds->hds);
162
0
    }
163
0
    dynhds->hds = nhds;
164
0
    dynhds->hds_allc = nallc;
165
0
  }
166
0
  dynhds->hds[dynhds->hds_len++] = entry;
167
0
  entry = NULL;
168
0
  dynhds->strs_len += namelen + valuelen;
169
0
  result = CURLE_OK;
170
171
0
out:
172
0
  if(entry)
173
0
    entry_free(entry);
174
0
  return result;
175
0
}
176
177
CURLcode Curl_dynhds_cadd(struct dynhds *dynhds,
178
                          const char *name, const char *value)
179
0
{
180
0
  return Curl_dynhds_add(dynhds, name, strlen(name), value, strlen(value));
181
0
}
182
183
CURLcode Curl_dynhds_h1_add_line(struct dynhds *dynhds,
184
                                 const char *line, size_t line_len)
185
0
{
186
0
  const char *p;
187
0
  const char *name;
188
0
  size_t namelen;
189
0
  const char *value;
190
0
  size_t valuelen, i;
191
192
0
  if(!line || !line_len)
193
0
    return CURLE_OK;
194
195
0
  p = memchr(line, ':', line_len);
196
0
  if(!p)
197
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
198
0
  name = line;
199
0
  namelen = p - line;
200
0
  p++; /* move past the colon */
201
0
  for(i = namelen + 1; i < line_len; ++i, ++p) {
202
0
    if(!ISBLANK(*p))
203
0
      break;
204
0
  }
205
0
  value = p;
206
0
  valuelen = line_len - i;
207
208
0
  p = memchr(value, '\r', valuelen);
209
0
  if(!p)
210
0
    p = memchr(value, '\n', valuelen);
211
0
  if(p)
212
0
    valuelen = (size_t)(p - value);
213
214
0
  return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
215
0
}
216
217
CURLcode Curl_dynhds_h1_cadd_line(struct dynhds *dynhds, const char *line)
218
0
{
219
0
  return Curl_dynhds_h1_add_line(dynhds, line, line ? strlen(line) : 0);
220
0
}
221
222
#ifdef UNITTESTS
223
/* used by unit2602.c */
224
225
/**
226
 * Return TRUE iff one or more headers with the given name exist.
227
 */
228
UNITTEST bool Curl_dynhds_contains(struct dynhds *dynhds,
229
                                   const char *name, size_t namelen);
230
UNITTEST bool Curl_dynhds_contains(struct dynhds *dynhds,
231
                                   const char *name, size_t namelen)
232
{
233
  return !!Curl_dynhds_get(dynhds, name, namelen);
234
}
235
236
UNITTEST bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name);
237
UNITTEST bool Curl_dynhds_ccontains(struct dynhds *dynhds, const char *name)
238
{
239
  return Curl_dynhds_contains(dynhds, name, strlen(name));
240
}
241
242
/**
243
 * Return how often the given name appears in `dynhds`.
244
 * Names are case-insensitive.
245
 */
246
UNITTEST size_t Curl_dynhds_count_name(struct dynhds *dynhds,
247
                                       const char *name, size_t namelen);
248
UNITTEST size_t Curl_dynhds_count_name(struct dynhds *dynhds,
249
                                       const char *name, size_t namelen)
250
{
251
  size_t n = 0;
252
  if(dynhds->hds_len) {
253
    size_t i;
254
    for(i = 0; i < dynhds->hds_len; ++i) {
255
      if((namelen == dynhds->hds[i]->namelen) &&
256
         curl_strnequal(name, dynhds->hds[i]->name, namelen))
257
        ++n;
258
    }
259
  }
260
  return n;
261
}
262
263
/**
264
 * Return how often the given null-terminated name appears in `dynhds`.
265
 * Names are case-insensitive.
266
 */
267
UNITTEST size_t Curl_dynhds_ccount_name(struct dynhds *dynhds,
268
                                        const char *name);
269
UNITTEST size_t Curl_dynhds_ccount_name(struct dynhds *dynhds,
270
                                        const char *name)
271
{
272
  return Curl_dynhds_count_name(dynhds, name, strlen(name));
273
}
274
275
/**
276
 * Remove all entries with the given name.
277
 * Returns number of entries removed.
278
 */
279
UNITTEST size_t Curl_dynhds_remove(struct dynhds *dynhds,
280
                                   const char *name, size_t namelen);
281
UNITTEST size_t Curl_dynhds_remove(struct dynhds *dynhds,
282
                                   const char *name, size_t namelen)
283
{
284
  size_t n = 0;
285
  if(dynhds->hds_len) {
286
    size_t i, len;
287
    for(i = 0; i < dynhds->hds_len; ++i) {
288
      if((namelen == dynhds->hds[i]->namelen) &&
289
         curl_strnequal(name, dynhds->hds[i]->name, namelen)) {
290
        ++n;
291
        --dynhds->hds_len;
292
        dynhds->strs_len -= (dynhds->hds[i]->namelen +
293
                             dynhds->hds[i]->valuelen);
294
        entry_free(dynhds->hds[i]);
295
        len = dynhds->hds_len - i; /* remaining entries */
296
        if(len) {
297
          memmove(&dynhds->hds[i], &dynhds->hds[i + 1],
298
                  len * sizeof(dynhds->hds[i]));
299
        }
300
        --i; /* do this index again */
301
      }
302
    }
303
  }
304
  return n;
305
}
306
307
/**
308
 * Set the give header name and value, replacing any entries with
309
 * the same name. The header is added at the end of all (remaining)
310
 * entries.
311
 */
312
UNITTEST CURLcode Curl_dynhds_set(struct dynhds *dynhds,
313
                                  const char *name, size_t namelen,
314
                                  const char *value, size_t valuelen);
315
UNITTEST CURLcode Curl_dynhds_set(struct dynhds *dynhds,
316
                                  const char *name, size_t namelen,
317
                                  const char *value, size_t valuelen)
318
{
319
  Curl_dynhds_remove(dynhds, name, namelen);
320
  return Curl_dynhds_add(dynhds, name, namelen, value, valuelen);
321
}
322
323
UNITTEST size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name);
324
UNITTEST size_t Curl_dynhds_cremove(struct dynhds *dynhds, const char *name)
325
{
326
  return Curl_dynhds_remove(dynhds, name, strlen(name));
327
}
328
329
#endif /* UNITTESTS */
330
331
CURLcode Curl_dynhds_h1_dprint(struct dynhds *dynhds, struct dynbuf *dbuf)
332
0
{
333
0
  CURLcode result = CURLE_OK;
334
0
  size_t i;
335
336
0
  if(!dynhds->hds_len)
337
0
    return result;
338
339
0
  for(i = 0; i < dynhds->hds_len; ++i) {
340
0
    result = curlx_dyn_addf(dbuf, "%.*s: %.*s\r\n",
341
0
                            (int)dynhds->hds[i]->namelen, dynhds->hds[i]->name,
342
0
                            (int)dynhds->hds[i]->valuelen,
343
0
                            dynhds->hds[i]->value);
344
0
    if(result)
345
0
      break;
346
0
  }
347
348
0
  return result;
349
0
}
350
351
#ifdef USE_NGHTTP2
352
353
nghttp2_nv *Curl_dynhds_to_nva(struct dynhds *dynhds, size_t *pcount)
354
0
{
355
0
  nghttp2_nv *nva = curlx_calloc(1, sizeof(nghttp2_nv) * dynhds->hds_len);
356
0
  size_t i;
357
358
0
  *pcount = 0;
359
0
  if(!nva)
360
0
    return NULL;
361
362
0
  for(i = 0; i < dynhds->hds_len; ++i) {
363
0
    struct dynhds_entry *e = dynhds->hds[i];
364
0
    DEBUGASSERT(e);
365
0
    nva[i].name = (unsigned char *)e->name;
366
0
    nva[i].namelen = e->namelen;
367
0
    nva[i].value = (unsigned char *)e->value;
368
0
    nva[i].valuelen = e->valuelen;
369
0
    nva[i].flags = NGHTTP2_NV_FLAG_NONE;
370
0
  }
371
0
  *pcount = dynhds->hds_len;
372
0
  return nva;
373
0
}
374
375
#endif /* USE_NGHTTP2 */