Coverage Report

Created: 2026-09-14 07:05

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