Coverage Report

Created: 2026-10-22 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/headers.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 "urldata.h"
27
#include "sendf.h"
28
#include "curl_trc.h"
29
#include "headers.h"
30
#include "strcase.h"
31
32
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HEADERS_API)
33
34
/* Generate the curl_header struct for the user. This function MUST assign all
35
   struct fields in the output struct. */
36
static void copy_header_external(struct Curl_header_store *hs,
37
                                 size_t index,
38
                                 size_t amount,
39
                                 struct Curl_llist_node *e,
40
                                 struct curl_header *hout)
41
358k
{
42
358k
  struct curl_header *h = hout;
43
358k
  h->name = hs->name;
44
358k
  h->value = hs->value;
45
358k
  h->amount = amount;
46
358k
  h->index = index;
47
  /* this will randomly OR a reserved bit for the sole purpose of making it
48
     impossible for applications to do == comparisons, as that would otherwise
49
     be tempting and then lead to the reserved bits not being reserved
50
     anymore. */
51
358k
  h->origin = (unsigned int)(hs->type | (1 << 27));
52
358k
  h->anchor = e;
53
358k
}
54
55
/* public API */
56
CURLHcode curl_easy_header(CURL *curl,
57
                           const char *name,
58
                           size_t nameindex,
59
                           unsigned int origin,
60
                           int request,
61
                           struct curl_header **hout)
62
305k
{
63
305k
  struct Curl_eapi_guard guard;
64
305k
  CURLHcode hresult = CURLHE_OK;
65
305k
  CURLcode result;
66
67
305k
  if(CURL_EAPI_ENTER(&guard, curl, easy_header, &result)) {
68
305k
    struct Curl_easy *data = curl;
69
305k
    struct Curl_llist_node *e;
70
305k
    struct Curl_llist_node *e_pick = NULL;
71
305k
    size_t match = 0;
72
305k
    size_t amount = 0;
73
305k
    struct Curl_header_store *hs = NULL;
74
305k
    struct Curl_header_store *pick = NULL;
75
305k
    if(!name || !hout || !data ||
76
305k
       (origin > (CURLH_HEADER | CURLH_TRAILER | CURLH_CONNECT | CURLH_1XX |
77
305k
                  CURLH_PSEUDO)) || !origin || (request < -1)) {
78
0
      hresult = CURLHE_BAD_ARGUMENT;
79
0
      goto out;
80
0
    }
81
305k
    if(!Curl_llist_count(&data->state.httphdrs)) {
82
186k
      hresult = CURLHE_NOHEADERS; /* no headers available */
83
186k
      goto out;
84
186k
    }
85
119k
    if(request > data->state.requests) {
86
0
      hresult = CURLHE_NOREQUEST;
87
0
      goto out;
88
0
    }
89
119k
    if(request == -1)
90
119k
      request = data->state.requests;
91
92
    /* we need a first round to count amount of this header */
93
1.75M
    for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) {
94
1.63M
      hs = Curl_node_elem(e);
95
1.63M
      if(curl_strequal(hs->name, name) &&
96
106k
         (hs->type & origin) &&
97
106k
         (hs->request == request)) {
98
102k
        amount++;
99
102k
        pick = hs;
100
102k
        e_pick = e;
101
102k
      }
102
1.63M
    }
103
119k
    if(!amount)
104
115k
      hresult = CURLHE_MISSING;
105
4.06k
    else if(nameindex >= amount)
106
0
      hresult = CURLHE_BADINDEX;
107
119k
    if(hresult)
108
115k
      goto out;
109
110
4.06k
    if(nameindex == amount - 1)
111
      /* if the last or only occurrence is what's asked for, then we know it */
112
3.08k
      hs = pick;
113
976
    else {
114
13.0k
      for(e = Curl_llist_head(&data->state.httphdrs); e;
115
13.0k
          e = Curl_node_next(e)) {
116
13.0k
        hs = Curl_node_elem(e);
117
13.0k
        if(curl_strequal(hs->name, name) &&
118
1.49k
           (hs->type & origin) &&
119
1.49k
           (hs->request == request) &&
120
976
           (match++ == nameindex)) {
121
976
          e_pick = e;
122
976
          break;
123
976
        }
124
13.0k
      }
125
976
      if(!e) { /* this should not happen */
126
0
        hresult = CURLHE_MISSING;
127
0
        goto out;
128
0
      }
129
976
    }
130
    /* this is the name we want */
131
4.06k
    copy_header_external(hs, nameindex, amount, e_pick,
132
4.06k
                         &data->state.headerout[0]);
133
4.06k
    *hout = &data->state.headerout[0];
134
4.06k
    hresult = CURLHE_OK;
135
4.06k
  }
136
305k
out:
137
305k
  CURL_EAPI_LEAVE(&guard);
138
305k
  if(result)
139
0
    hresult = Curl_eapi_hcode(result);
140
305k
  return hresult;
141
305k
}
142
143
struct nextheader_cache_entry {
144
  struct Curl_header_store *header;
145
  size_t order;
146
};
147
148
static int nextheader_compare(const void *p1, const void *p2)
149
3.97M
{
150
3.97M
  const struct nextheader_cache_entry *e1 = p1;
151
3.97M
  const struct nextheader_cache_entry *e2 = p2;
152
3.97M
  const struct Curl_header_store *h1 = e1->header;
153
3.97M
  const struct Curl_header_store *h2 = e2->header;
154
3.97M
  const char *n1 = h1->name;
155
3.97M
  const char *n2 = h2->name;
156
157
34.1M
  while(*n1 && *n2) {
158
31.3M
    unsigned char c1 = (unsigned char)Curl_raw_toupper(*n1++);
159
31.3M
    unsigned char c2 = (unsigned char)Curl_raw_toupper(*n2++);
160
31.3M
    if(c1 != c2)
161
1.24M
      return c1 > c2 ? 1 : -1;
162
31.3M
  }
163
2.72M
  if(*n1)
164
135k
    return 1;
165
2.59M
  if(*n2)
166
15.2k
    return -1;
167
2.57M
  if(e1->order != e2->order)
168
2.57M
    return e1->order > e2->order ? 1 : -1;
169
0
  return 0;
170
2.57M
}
171
172
static bool nextheader_cache_build(struct Curl_easy *data,
173
                                   unsigned int origin,
174
                                   int request, size_t count)
175
90.6k
{
176
90.6k
  struct nextheader_cache_entry *headers;
177
90.6k
  struct Curl_llist_node *e;
178
90.6k
  size_t i = 0;
179
90.6k
  size_t first;
180
181
90.6k
  headers = curlx_malloc(sizeof(*headers) * count);
182
90.6k
  if(!headers)
183
176
    return FALSE;
184
185
1.14M
  for(e = Curl_llist_head(&data->state.httphdrs); e;
186
1.05M
      e = Curl_node_next(e)) {
187
1.05M
    struct Curl_header_store *hs = Curl_node_elem(e);
188
1.05M
    if((hs->type & origin) && (hs->request == request)) {
189
994k
      headers[i].header = hs;
190
994k
      headers[i].order = i;
191
994k
      i++;
192
994k
    }
193
1.05M
  }
194
195
90.4k
  qsort(headers, i, sizeof(*headers), nextheader_compare);
196
307k
  for(first = 0; first < i;) {
197
216k
    size_t last = first + 1;
198
216k
    size_t index;
199
994k
    while((last < i) &&
200
903k
          curl_strequal(headers[first].header->name,
201
903k
                        headers[last].header->name))
202
777k
      last++;
203
1.21M
    for(index = first; index < last; index++) {
204
994k
      headers[index].header->nh_amount = last - first;
205
994k
      headers[index].header->nh_index = index - first;
206
994k
    }
207
216k
    first = last;
208
216k
  }
209
90.4k
  curlx_free(headers);
210
211
90.4k
  data->state.nh_origin = origin;
212
90.4k
  data->state.nh_request = request;
213
90.4k
  data->state.nh_count = count;
214
90.4k
  return TRUE;
215
90.6k
}
216
217
/* public API */
218
struct curl_header *curl_easy_nextheader(CURL *curl,
219
                                         unsigned int origin,
220
                                         int request,
221
                                         struct curl_header *prev)
222
650k
{
223
650k
  struct Curl_eapi_guard guard;
224
650k
  struct curl_header *hd = NULL;
225
650k
  CURLcode result;
226
227
650k
  if(CURL_EAPI_ENTER(&guard, curl, easy_nextheader, &result)) {
228
650k
    struct Curl_easy *data = curl;
229
650k
    struct Curl_llist_node *pick;
230
650k
    struct Curl_llist_node *e;
231
650k
    struct Curl_header_store *hs;
232
650k
    size_t amount = 0;
233
650k
    size_t index = 0;
234
650k
    size_t count;
235
236
650k
    if(request > data->state.requests)
237
0
      goto out;
238
650k
    if(request == -1)
239
650k
      request = data->state.requests;
240
241
650k
    if(prev) {
242
345k
      pick = prev->anchor;
243
345k
      if(!pick)
244
        /* something is wrong */
245
0
        goto out;
246
345k
      pick = Curl_node_next(pick);
247
345k
    }
248
305k
    else
249
305k
      pick = Curl_llist_head(&data->state.httphdrs);
250
251
650k
    if(pick) {
252
      /* make sure it is the next header of the desired type */
253
990k
      do {
254
990k
        hs = Curl_node_elem(pick);
255
990k
        if((hs->type & origin) && (hs->request == request))
256
354k
          break;
257
636k
        pick = Curl_node_next(pick);
258
636k
      } while(pick);
259
382k
    }
260
261
650k
    if(!pick)
262
      /* no more headers available */
263
296k
      goto out;
264
265
354k
    hs = Curl_node_elem(pick);
266
354k
    count = Curl_llist_count(&data->state.httphdrs);
267
268
354k
    if(((data->state.nh_count == count) &&
269
263k
        (data->state.nh_origin == origin) &&
270
263k
        (data->state.nh_request == request)) ||
271
354k
       nextheader_cache_build(data, origin, request, count)) {
272
354k
      amount = hs->nh_amount;
273
354k
      index = hs->nh_index;
274
354k
    }
275
176
    else {
276
      /* count number of occurrences of this name within the mask and figure
277
         out the index for the currently selected entry */
278
7.26k
      for(e = Curl_llist_head(&data->state.httphdrs); e;
279
7.09k
          e = Curl_node_next(e)) {
280
7.09k
        struct Curl_header_store *check = Curl_node_elem(e);
281
7.09k
        if(curl_strequal(hs->name, check->name) &&
282
899
           (check->request == request) &&
283
862
           (check->type & origin))
284
862
          amount++;
285
7.09k
        if(e == pick)
286
176
          index = amount - 1;
287
7.09k
      }
288
176
    }
289
354k
    copy_header_external(hs, index, amount, pick,
290
354k
                         &data->state.headerout[1]);
291
354k
    hd = &data->state.headerout[1];
292
354k
  }
293
650k
out:
294
650k
  CURL_EAPI_LEAVE(&guard);
295
650k
  return hd;
296
650k
}
297
298
static CURLcode namevalue(char *header, size_t hlen, unsigned int type,
299
                          char **name, char **value)
300
2.27M
{
301
2.27M
  char *end = header + hlen - 1; /* point to the last byte */
302
2.27M
  DEBUGASSERT(hlen);
303
2.27M
  *name = header;
304
305
2.27M
  if(type == CURLH_PSEUDO) {
306
78.6k
    if(*header != ':')
307
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
308
78.6k
    header++;
309
78.6k
  }
310
311
  /* Find the end of the header name */
312
28.0M
  while(*header && (*header != ':'))
313
25.8M
    ++header;
314
315
2.27M
  if(*header)
316
    /* Skip over colon, null it */
317
2.26M
    *header++ = 0;
318
4.67k
  else
319
4.67k
    return CURLE_BAD_FUNCTION_ARGUMENT;
320
321
  /* skip all leading blank letters */
322
4.27M
  while(ISBLANK(*header))
323
2.01M
    header++;
324
325
2.26M
  *value = header;
326
327
  /* skip all trailing space letters */
328
2.32M
  while((end > header) && ISBLANK(*end))
329
62.0k
    *end-- = 0; /* null-terminate */
330
2.26M
  return CURLE_OK;
331
2.27M
}
332
333
/*
334
 * Curl_headers_push() gets passed a full HTTP header to store. It gets called
335
 * immediately before the header callback. The header is CRLF, CR or LF
336
 * terminated.
337
 */
338
CURLcode Curl_headers_push(struct Curl_easy *data, const char *header,
339
                           size_t hlen, /* length of header */
340
                           unsigned char type)
341
2.62M
{
342
2.62M
  char *value = NULL;
343
2.62M
  char *name = NULL;
344
2.62M
  struct Curl_header_store *hs;
345
2.62M
  CURLcode result = CURLE_OUT_OF_MEMORY;
346
2.62M
  const size_t ilen = hlen;
347
348
2.62M
  if((header[0] == '\r') || (header[0] == '\n'))
349
    /* ignore the body separator */
350
327k
    return CURLE_OK;
351
352
  /* trim off newline characters */
353
2.30M
  if(hlen && (header[hlen - 1] == '\n'))
354
2.19M
    hlen--;
355
2.30M
  if(hlen && (header[hlen - 1] == '\r'))
356
1.98M
    hlen--;
357
2.30M
  if(hlen == ilen)
358
    /* neither CR nor LF as terminator is not a valid header */
359
28.0k
    return CURLE_WEIRD_SERVER_REPLY;
360
361
2.27M
  if(ISBLANK(header[0])) {
362
    /* pass leading blanks */
363
14.8k
    while(hlen && ISBLANK(*header)) {
364
11.3k
      header++;
365
11.3k
      hlen--;
366
11.3k
    }
367
3.49k
    if(!hlen)
368
248
      return CURLE_WEIRD_SERVER_REPLY;
369
3.49k
  }
370
2.27M
  if(Curl_llist_count(&data->state.httphdrs) >= MAX_HTTP_RESP_HEADER_COUNT) {
371
16
    failf(data, "Too many response headers, %d is max",
372
16
          MAX_HTTP_RESP_HEADER_COUNT);
373
16
    return CURLE_TOO_LARGE;
374
16
  }
375
376
2.27M
  hs = curlx_calloc(1, sizeof(*hs) + hlen);
377
2.27M
  if(!hs)
378
254
    return CURLE_OUT_OF_MEMORY;
379
2.27M
  memcpy(hs->buffer, header, hlen);
380
2.27M
  hs->buffer[hlen] = 0; /* null-terminate */
381
382
2.27M
  result = namevalue(hs->buffer, hlen, type, &name, &value);
383
2.27M
  if(!result) {
384
2.26M
    hs->name = name;
385
2.26M
    hs->value = value;
386
2.26M
    hs->type = type;
387
2.26M
    hs->request = data->state.requests;
388
389
    /* insert this node into the list of headers */
390
2.26M
    Curl_llist_append(&data->state.httphdrs, hs, &hs->node);
391
2.26M
  }
392
4.67k
  else {
393
4.67k
    failf(data, "Invalid response header");
394
4.67k
    curlx_free(hs);
395
4.67k
  }
396
2.27M
  return result;
397
2.27M
}
398
399
/*
400
 * Curl_headers_reset(). Reset the headers subsystem.
401
 */
402
static void headers_reset(struct Curl_easy *data)
403
1.89M
{
404
1.89M
  Curl_llist_init(&data->state.httphdrs, NULL);
405
1.89M
  data->state.nh_count = 0;
406
1.89M
}
407
408
struct hds_cw_collect_ctx {
409
  struct Curl_cwriter super;
410
};
411
412
static CURLcode hds_cw_collect_write(struct Curl_easy *data,
413
                                     struct Curl_cwriter *writer, int type,
414
                                     const char *buf, size_t blen)
415
3.44M
{
416
3.44M
  if((type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS)) {
417
2.54M
    unsigned char htype = (unsigned char)
418
2.54M
      (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
419
2.54M
       (type & CLIENTWRITE_1XX ? CURLH_1XX :
420
2.50M
        (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
421
2.32M
         CURLH_HEADER)));
422
2.54M
    CURLcode result = Curl_headers_push(data, buf, blen, htype);
423
2.54M
    CURL_TRC_WRITE(data, "header_collect pushed(type=%x, len=%zu) -> %d",
424
2.54M
                   htype, blen, (int)result);
425
2.54M
    if(result)
426
33.2k
      return result;
427
2.54M
  }
428
3.40M
  return Curl_cwriter_write(data, writer->next, type, buf, blen);
429
3.44M
}
430
431
static const struct Curl_cwtype hds_cw_collect = {
432
  "hds-collect",
433
  NULL,
434
  0,
435
  Curl_cwriter_def_init,
436
  hds_cw_collect_write,
437
  Curl_cwriter_def_flush,
438
  Curl_cwriter_def_close,
439
  sizeof(struct hds_cw_collect_ctx)
440
};
441
442
CURLcode Curl_headers_init(struct Curl_easy *data)
443
1.23M
{
444
1.23M
  struct Curl_cwriter *writer;
445
1.23M
  CURLcode result;
446
447
1.23M
  if(data->conn && (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) {
448
    /* avoid installing it twice */
449
1.12M
    if(Curl_cwriter_get_by_name(data, hds_cw_collect.name))
450
425k
      return CURLE_OK;
451
452
700k
    result = Curl_cwriter_create(&writer, data, &hds_cw_collect,
453
700k
                                 CURL_CW_PROTOCOL);
454
700k
    if(result)
455
31
      return result;
456
457
700k
    result = Curl_cwriter_add(data, writer);
458
700k
    if(result) {
459
150
      Curl_cwriter_free(data, writer);
460
150
      return result;
461
150
    }
462
700k
  }
463
807k
  return CURLE_OK;
464
1.23M
}
465
466
/*
467
 * Curl_headers_cleanup(). Free all stored headers and associated memory.
468
 */
469
CURLcode Curl_headers_cleanup(struct Curl_easy *data)
470
1.89M
{
471
1.89M
  struct Curl_llist_node *e;
472
1.89M
  struct Curl_llist_node *n;
473
474
4.16M
  for(e = Curl_llist_head(&data->state.httphdrs); e; e = n) {
475
2.26M
    struct Curl_header_store *hs = Curl_node_elem(e);
476
2.26M
    n = Curl_node_next(e);
477
2.26M
    curlx_free(hs);
478
2.26M
  }
479
1.89M
  headers_reset(data);
480
1.89M
  return CURLE_OK;
481
1.89M
}
482
483
#else /* HTTP-disabled builds below */
484
485
CURLHcode curl_easy_header(CURL *easy,
486
                           const char *name,
487
                           size_t index,
488
                           unsigned int origin,
489
                           int request,
490
                           struct curl_header **hout)
491
{
492
  (void)easy;
493
  (void)name;
494
  (void)index;
495
  (void)origin;
496
  (void)request;
497
  (void)hout;
498
  return CURLHE_NOT_BUILT_IN;
499
}
500
501
struct curl_header *curl_easy_nextheader(CURL *easy,
502
                                         unsigned int type,
503
                                         int request,
504
                                         struct curl_header *prev)
505
{
506
  (void)easy;
507
  (void)type;
508
  (void)request;
509
  (void)prev;
510
  return NULL;
511
}
512
#endif