Coverage Report

Created: 2026-10-22 06:50

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
40.0k
{
42
40.0k
  struct curl_header *h = hout;
43
40.0k
  h->name = hs->name;
44
40.0k
  h->value = hs->value;
45
40.0k
  h->amount = amount;
46
40.0k
  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
40.0k
  h->origin = (unsigned int)(hs->type | (1 << 27));
52
40.0k
  h->anchor = e;
53
40.0k
}
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
29.2k
{
63
29.2k
  struct Curl_eapi_guard guard;
64
29.2k
  CURLHcode hresult = CURLHE_OK;
65
29.2k
  CURLcode result;
66
67
29.2k
  if(CURL_EAPI_ENTER(&guard, curl, easy_header, &result)) {
68
29.2k
    struct Curl_easy *data = curl;
69
29.2k
    struct Curl_llist_node *e;
70
29.2k
    struct Curl_llist_node *e_pick = NULL;
71
29.2k
    size_t match = 0;
72
29.2k
    size_t amount = 0;
73
29.2k
    struct Curl_header_store *hs = NULL;
74
29.2k
    struct Curl_header_store *pick = NULL;
75
29.2k
    if(!name || !hout || !data ||
76
29.2k
       (origin > (CURLH_HEADER | CURLH_TRAILER | CURLH_CONNECT | CURLH_1XX |
77
29.2k
                  CURLH_PSEUDO)) || !origin || (request < -1)) {
78
0
      hresult = CURLHE_BAD_ARGUMENT;
79
0
      goto out;
80
0
    }
81
29.2k
    if(!Curl_llist_count(&data->state.httphdrs)) {
82
14.7k
      hresult = CURLHE_NOHEADERS; /* no headers available */
83
14.7k
      goto out;
84
14.7k
    }
85
14.5k
    if(request > data->state.requests) {
86
0
      hresult = CURLHE_NOREQUEST;
87
0
      goto out;
88
0
    }
89
14.5k
    if(request == -1)
90
14.5k
      request = data->state.requests;
91
92
    /* we need a first round to count amount of this header */
93
157k
    for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) {
94
142k
      hs = Curl_node_elem(e);
95
142k
      if(curl_strequal(hs->name, name) &&
96
1.23k
         (hs->type & origin) &&
97
1.23k
         (hs->request == request)) {
98
1.04k
        amount++;
99
1.04k
        pick = hs;
100
1.04k
        e_pick = e;
101
1.04k
      }
102
142k
    }
103
14.5k
    if(!amount)
104
14.3k
      hresult = CURLHE_MISSING;
105
198
    else if(nameindex >= amount)
106
0
      hresult = CURLHE_BADINDEX;
107
14.5k
    if(hresult)
108
14.3k
      goto out;
109
110
198
    if(nameindex == amount - 1)
111
      /* if the last or only occurrence is what's asked for, then we know it */
112
149
      hs = pick;
113
49
    else {
114
819
      for(e = Curl_llist_head(&data->state.httphdrs); e;
115
819
          e = Curl_node_next(e)) {
116
819
        hs = Curl_node_elem(e);
117
819
        if(curl_strequal(hs->name, name) &&
118
138
           (hs->type & origin) &&
119
138
           (hs->request == request) &&
120
49
           (match++ == nameindex)) {
121
49
          e_pick = e;
122
49
          break;
123
49
        }
124
819
      }
125
49
      if(!e) { /* this should not happen */
126
0
        hresult = CURLHE_MISSING;
127
0
        goto out;
128
0
      }
129
49
    }
130
    /* this is the name we want */
131
198
    copy_header_external(hs, nameindex, amount, e_pick,
132
198
                         &data->state.headerout[0]);
133
198
    *hout = &data->state.headerout[0];
134
198
    hresult = CURLHE_OK;
135
198
  }
136
29.2k
out:
137
29.2k
  CURL_EAPI_LEAVE(&guard);
138
29.2k
  if(result)
139
0
    hresult = Curl_eapi_hcode(result);
140
29.2k
  return hresult;
141
29.2k
}
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
377k
{
150
377k
  const struct nextheader_cache_entry *e1 = p1;
151
377k
  const struct nextheader_cache_entry *e2 = p2;
152
377k
  const struct Curl_header_store *h1 = e1->header;
153
377k
  const struct Curl_header_store *h2 = e2->header;
154
377k
  const char *n1 = h1->name;
155
377k
  const char *n2 = h2->name;
156
157
3.38M
  while(*n1 && *n2) {
158
3.14M
    unsigned char c1 = (unsigned char)Curl_raw_toupper(*n1++);
159
3.14M
    unsigned char c2 = (unsigned char)Curl_raw_toupper(*n2++);
160
3.14M
    if(c1 != c2)
161
133k
      return c1 > c2 ? 1 : -1;
162
3.14M
  }
163
243k
  if(*n1)
164
6.11k
    return 1;
165
237k
  if(*n2)
166
468
    return -1;
167
237k
  if(e1->order != e2->order)
168
237k
    return e1->order > e2->order ? 1 : -1;
169
0
  return 0;
170
237k
}
171
172
static bool nextheader_cache_build(struct Curl_easy *data,
173
                                   unsigned int origin,
174
                                   int request, size_t count)
175
11.1k
{
176
11.1k
  struct nextheader_cache_entry *headers;
177
11.1k
  struct Curl_llist_node *e;
178
11.1k
  size_t i = 0;
179
11.1k
  size_t first;
180
181
11.1k
  headers = curlx_malloc(sizeof(*headers) * count);
182
11.1k
  if(!headers)
183
17
    return FALSE;
184
185
112k
  for(e = Curl_llist_head(&data->state.httphdrs); e;
186
101k
      e = Curl_node_next(e)) {
187
101k
    struct Curl_header_store *hs = Curl_node_elem(e);
188
101k
    if((hs->type & origin) && (hs->request == request)) {
189
99.0k
      headers[i].header = hs;
190
99.0k
      headers[i].order = i;
191
99.0k
      i++;
192
99.0k
    }
193
101k
  }
194
195
11.1k
  qsort(headers, i, sizeof(*headers), nextheader_compare);
196
36.1k
  for(first = 0; first < i;) {
197
25.0k
    size_t last = first + 1;
198
25.0k
    size_t index;
199
99.0k
    while((last < i) &&
200
87.9k
          curl_strequal(headers[first].header->name,
201
87.9k
                        headers[last].header->name))
202
73.9k
      last++;
203
124k
    for(index = first; index < last; index++) {
204
99.0k
      headers[index].header->nh_amount = last - first;
205
99.0k
      headers[index].header->nh_index = index - first;
206
99.0k
    }
207
25.0k
    first = last;
208
25.0k
  }
209
11.1k
  curlx_free(headers);
210
211
11.1k
  data->state.nh_origin = origin;
212
11.1k
  data->state.nh_request = request;
213
11.1k
  data->state.nh_count = count;
214
11.1k
  return TRUE;
215
11.1k
}
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
68.2k
{
223
68.2k
  struct Curl_eapi_guard guard;
224
68.2k
  struct curl_header *hd = NULL;
225
68.2k
  CURLcode result;
226
227
68.2k
  if(CURL_EAPI_ENTER(&guard, curl, easy_nextheader, &result)) {
228
68.2k
    struct Curl_easy *data = curl;
229
68.2k
    struct Curl_llist_node *pick;
230
68.2k
    struct Curl_llist_node *e;
231
68.2k
    struct Curl_header_store *hs;
232
68.2k
    size_t amount = 0;
233
68.2k
    size_t index = 0;
234
68.2k
    size_t count;
235
236
68.2k
    if(request > data->state.requests)
237
0
      goto out;
238
68.2k
    if(request == -1)
239
68.2k
      request = data->state.requests;
240
241
68.2k
    if(prev) {
242
38.9k
      pick = prev->anchor;
243
38.9k
      if(!pick)
244
        /* something is wrong */
245
0
        goto out;
246
38.9k
      pick = Curl_node_next(pick);
247
38.9k
    }
248
29.2k
    else
249
29.2k
      pick = Curl_llist_head(&data->state.httphdrs);
250
251
68.2k
    if(pick) {
252
      /* make sure it is the next header of the desired type */
253
83.4k
      do {
254
83.4k
        hs = Curl_node_elem(pick);
255
83.4k
        if((hs->type & origin) && (hs->request == request))
256
39.8k
          break;
257
43.5k
        pick = Curl_node_next(pick);
258
43.5k
      } while(pick);
259
43.2k
    }
260
261
68.2k
    if(!pick)
262
      /* no more headers available */
263
28.3k
      goto out;
264
265
39.8k
    hs = Curl_node_elem(pick);
266
39.8k
    count = Curl_llist_count(&data->state.httphdrs);
267
268
39.8k
    if(((data->state.nh_count == count) &&
269
28.7k
        (data->state.nh_origin == origin) &&
270
28.7k
        (data->state.nh_request == request)) ||
271
39.8k
       nextheader_cache_build(data, origin, request, count)) {
272
39.8k
      amount = hs->nh_amount;
273
39.8k
      index = hs->nh_index;
274
39.8k
    }
275
17
    else {
276
      /* count number of occurrences of this name within the mask and figure
277
         out the index for the currently selected entry */
278
399
      for(e = Curl_llist_head(&data->state.httphdrs); e;
279
382
          e = Curl_node_next(e)) {
280
382
        struct Curl_header_store *check = Curl_node_elem(e);
281
382
        if(curl_strequal(hs->name, check->name) &&
282
44
           (check->request == request) &&
283
37
           (check->type & origin))
284
37
          amount++;
285
382
        if(e == pick)
286
17
          index = amount - 1;
287
382
      }
288
17
    }
289
39.8k
    copy_header_external(hs, index, amount, pick,
290
39.8k
                         &data->state.headerout[1]);
291
39.8k
    hd = &data->state.headerout[1];
292
39.8k
  }
293
68.2k
out:
294
68.2k
  CURL_EAPI_LEAVE(&guard);
295
68.2k
  return hd;
296
68.2k
}
297
298
static CURLcode namevalue(char *header, size_t hlen, unsigned int type,
299
                          char **name, char **value)
300
143k
{
301
143k
  char *end = header + hlen - 1; /* point to the last byte */
302
143k
  DEBUGASSERT(hlen);
303
143k
  *name = header;
304
305
143k
  if(type == CURLH_PSEUDO) {
306
1.47k
    if(*header != ':')
307
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
308
1.47k
    header++;
309
1.47k
  }
310
311
  /* Find the end of the header name */
312
1.73M
  while(*header && (*header != ':'))
313
1.58M
    ++header;
314
315
143k
  if(*header)
316
    /* Skip over colon, null it */
317
142k
    *header++ = 0;
318
523
  else
319
523
    return CURLE_BAD_FUNCTION_ARGUMENT;
320
321
  /* skip all leading blank letters */
322
270k
  while(ISBLANK(*header))
323
128k
    header++;
324
325
142k
  *value = header;
326
327
  /* skip all trailing space letters */
328
144k
  while((end > header) && ISBLANK(*end))
329
1.41k
    *end-- = 0; /* null-terminate */
330
142k
  return CURLE_OK;
331
143k
}
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
183k
{
342
183k
  char *value = NULL;
343
183k
  char *name = NULL;
344
183k
  struct Curl_header_store *hs;
345
183k
  CURLcode result = CURLE_OUT_OF_MEMORY;
346
183k
  const size_t ilen = hlen;
347
348
183k
  if((header[0] == '\r') || (header[0] == '\n'))
349
    /* ignore the body separator */
350
38.7k
    return CURLE_OK;
351
352
  /* trim off newline characters */
353
145k
  if(hlen && (header[hlen - 1] == '\n'))
354
141k
    hlen--;
355
145k
  if(hlen && (header[hlen - 1] == '\r'))
356
104k
    hlen--;
357
145k
  if(hlen == ilen)
358
    /* neither CR nor LF as terminator is not a valid header */
359
2.01k
    return CURLE_WEIRD_SERVER_REPLY;
360
361
143k
  if(ISBLANK(header[0])) {
362
    /* pass leading blanks */
363
787
    while(hlen && ISBLANK(*header)) {
364
595
      header++;
365
595
      hlen--;
366
595
    }
367
192
    if(!hlen)
368
7
      return CURLE_WEIRD_SERVER_REPLY;
369
192
  }
370
143k
  if(Curl_llist_count(&data->state.httphdrs) >= MAX_HTTP_RESP_HEADER_COUNT) {
371
1
    failf(data, "Too many response headers, %d is max",
372
1
          MAX_HTTP_RESP_HEADER_COUNT);
373
1
    return CURLE_TOO_LARGE;
374
1
  }
375
376
143k
  hs = curlx_calloc(1, sizeof(*hs) + hlen);
377
143k
  if(!hs)
378
14
    return CURLE_OUT_OF_MEMORY;
379
143k
  memcpy(hs->buffer, header, hlen);
380
143k
  hs->buffer[hlen] = 0; /* null-terminate */
381
382
143k
  result = namevalue(hs->buffer, hlen, type, &name, &value);
383
143k
  if(!result) {
384
142k
    hs->name = name;
385
142k
    hs->value = value;
386
142k
    hs->type = type;
387
142k
    hs->request = data->state.requests;
388
389
    /* insert this node into the list of headers */
390
142k
    Curl_llist_append(&data->state.httphdrs, hs, &hs->node);
391
142k
  }
392
523
  else {
393
523
    failf(data, "Invalid response header");
394
523
    curlx_free(hs);
395
523
  }
396
143k
  return result;
397
143k
}
398
399
/*
400
 * Curl_headers_reset(). Reset the headers subsystem.
401
 */
402
static void headers_reset(struct Curl_easy *data)
403
87.6k
{
404
87.6k
  Curl_llist_init(&data->state.httphdrs, NULL);
405
87.6k
  data->state.nh_count = 0;
406
87.6k
}
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
255k
{
416
255k
  if((type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS)) {
417
182k
    unsigned char htype = (unsigned char)
418
182k
      (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
419
182k
       (type & CLIENTWRITE_1XX ? CURLH_1XX :
420
182k
        (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
421
166k
         CURLH_HEADER)));
422
182k
    CURLcode result = Curl_headers_push(data, buf, blen, htype);
423
182k
    CURL_TRC_WRITE(data, "header_collect pushed(type=%x, len=%zu) -> %d",
424
182k
                   htype, blen, (int)result);
425
182k
    if(result)
426
2.55k
      return result;
427
182k
  }
428
253k
  return Curl_cwriter_write(data, writer->next, type, buf, blen);
429
255k
}
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
93.3k
{
444
93.3k
  struct Curl_cwriter *writer;
445
93.3k
  CURLcode result;
446
447
93.3k
  if(data->conn && (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) {
448
    /* avoid installing it twice */
449
92.3k
    if(Curl_cwriter_get_by_name(data, hds_cw_collect.name))
450
32.2k
      return CURLE_OK;
451
452
60.1k
    result = Curl_cwriter_create(&writer, data, &hds_cw_collect,
453
60.1k
                                 CURL_CW_PROTOCOL);
454
60.1k
    if(result)
455
1
      return result;
456
457
60.1k
    result = Curl_cwriter_add(data, writer);
458
60.1k
    if(result) {
459
13
      Curl_cwriter_free(data, writer);
460
13
      return result;
461
13
    }
462
60.1k
  }
463
61.1k
  return CURLE_OK;
464
93.3k
}
465
466
/*
467
 * Curl_headers_cleanup(). Free all stored headers and associated memory.
468
 */
469
CURLcode Curl_headers_cleanup(struct Curl_easy *data)
470
87.6k
{
471
87.6k
  struct Curl_llist_node *e;
472
87.6k
  struct Curl_llist_node *n;
473
474
230k
  for(e = Curl_llist_head(&data->state.httphdrs); e; e = n) {
475
142k
    struct Curl_header_store *hs = Curl_node_elem(e);
476
142k
    n = Curl_node_next(e);
477
142k
    curlx_free(hs);
478
142k
  }
479
87.6k
  headers_reset(data);
480
87.6k
  return CURLE_OK;
481
87.6k
}
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