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