Coverage Report

Created: 2025-10-10 06:09

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
25
#include "curl_setup.h"
26
27
#include "urldata.h"
28
#include "strdup.h"
29
#include "sendf.h"
30
#include "headers.h"
31
#include "curlx/strparse.h"
32
33
/* The last 2 #include files should be in this order */
34
#include "curl_memory.h"
35
#include "memdebug.h"
36
37
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_HEADERS_API)
38
39
/* Generate the curl_header struct for the user. This function MUST assign all
40
   struct fields in the output struct. */
41
static void copy_header_external(struct Curl_header_store *hs,
42
                                 size_t index,
43
                                 size_t amount,
44
                                 struct Curl_llist_node *e,
45
                                 struct curl_header *hout)
46
0
{
47
0
  struct curl_header *h = hout;
48
0
  h->name = hs->name;
49
0
  h->value = hs->value;
50
0
  h->amount = amount;
51
0
  h->index = index;
52
  /* this will randomly OR a reserved bit for the sole purpose of making it
53
     impossible for applications to do == comparisons, as that would otherwise
54
     be very tempting and then lead to the reserved bits not being reserved
55
     anymore. */
56
0
  h->origin = (unsigned int)(hs->type | (1 << 27));
57
0
  h->anchor = e;
58
0
}
59
60
/* public API */
61
CURLHcode curl_easy_header(CURL *easy,
62
                           const char *name,
63
                           size_t nameindex,
64
                           unsigned int type,
65
                           int request,
66
                           struct curl_header **hout)
67
0
{
68
0
  struct Curl_llist_node *e;
69
0
  struct Curl_llist_node *e_pick = NULL;
70
0
  struct Curl_easy *data = easy;
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
     (type > (CURLH_HEADER|CURLH_TRAILER|CURLH_CONNECT|CURLH_1XX|
77
0
              CURLH_PSEUDO)) || !type || (request < -1))
78
0
    return CURLHE_BAD_ARGUMENT;
79
0
  if(!Curl_llist_count(&data->state.httphdrs))
80
0
    return CURLHE_NOHEADERS; /* no headers available */
81
0
  if(request > data->state.requests)
82
0
    return CURLHE_NOREQUEST;
83
0
  if(request == -1)
84
0
    request = data->state.requests;
85
86
  /* we need a first round to count amount of this header */
87
0
  for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) {
88
0
    hs = Curl_node_elem(e);
89
0
    if(curl_strequal(hs->name, name) &&
90
0
       (hs->type & type) &&
91
0
       (hs->request == request)) {
92
0
      amount++;
93
0
      pick = hs;
94
0
      e_pick = e;
95
0
    }
96
0
  }
97
0
  if(!amount)
98
0
    return CURLHE_MISSING;
99
0
  else if(nameindex >= amount)
100
0
    return CURLHE_BADINDEX;
101
102
0
  if(nameindex == amount - 1)
103
    /* if the last or only occurrence is what's asked for, then we know it */
104
0
    hs = pick;
105
0
  else {
106
0
    for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) {
107
0
      hs = Curl_node_elem(e);
108
0
      if(curl_strequal(hs->name, name) &&
109
0
         (hs->type & type) &&
110
0
         (hs->request == request) &&
111
0
         (match++ == nameindex)) {
112
0
        e_pick = e;
113
0
        break;
114
0
      }
115
0
    }
116
0
    if(!e) /* this should not happen */
117
0
      return CURLHE_MISSING;
118
0
  }
119
  /* this is the name we want */
120
0
  copy_header_external(hs, nameindex, amount, e_pick,
121
0
                       &data->state.headerout[0]);
122
0
  *hout = &data->state.headerout[0];
123
0
  return CURLHE_OK;
124
0
}
125
126
/* public API */
127
struct curl_header *curl_easy_nextheader(CURL *easy,
128
                                         unsigned int type,
129
                                         int request,
130
                                         struct curl_header *prev)
131
0
{
132
0
  struct Curl_easy *data = easy;
133
0
  struct Curl_llist_node *pick;
134
0
  struct Curl_llist_node *e;
135
0
  struct Curl_header_store *hs;
136
0
  size_t amount = 0;
137
0
  size_t index = 0;
138
139
0
  if(request > data->state.requests)
140
0
    return NULL;
141
0
  if(request == -1)
142
0
    request = data->state.requests;
143
144
0
  if(prev) {
145
0
    pick = prev->anchor;
146
0
    if(!pick)
147
      /* something is wrong */
148
0
      return NULL;
149
0
    pick = Curl_node_next(pick);
150
0
  }
151
0
  else
152
0
    pick = Curl_llist_head(&data->state.httphdrs);
153
154
0
  if(pick) {
155
    /* make sure it is the next header of the desired type */
156
0
    do {
157
0
      hs = Curl_node_elem(pick);
158
0
      if((hs->type & type) && (hs->request == request))
159
0
        break;
160
0
      pick = Curl_node_next(pick);
161
0
    } while(pick);
162
0
  }
163
164
0
  if(!pick)
165
    /* no more headers available */
166
0
    return NULL;
167
168
0
  hs = Curl_node_elem(pick);
169
170
  /* count number of occurrences of this name within the mask and figure out
171
     the index for the currently selected entry */
172
0
  for(e = Curl_llist_head(&data->state.httphdrs); e; e = Curl_node_next(e)) {
173
0
    struct Curl_header_store *check = Curl_node_elem(e);
174
0
    if(curl_strequal(hs->name, check->name) &&
175
0
       (check->request == request) &&
176
0
       (check->type & type))
177
0
      amount++;
178
0
    if(e == pick)
179
0
      index = amount - 1;
180
0
  }
181
182
0
  copy_header_external(hs, index, amount, pick,
183
0
                       &data->state.headerout[1]);
184
0
  return &data->state.headerout[1];
185
0
}
186
187
static CURLcode namevalue(char *header, size_t hlen, unsigned int type,
188
                          char **name, char **value)
189
0
{
190
0
  char *end = header + hlen - 1; /* point to the last byte */
191
0
  DEBUGASSERT(hlen);
192
0
  *name = header;
193
194
0
  if(type == CURLH_PSEUDO) {
195
0
    if(*header != ':')
196
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
197
0
    header++;
198
0
  }
199
200
  /* Find the end of the header name */
201
0
  while(*header && (*header != ':'))
202
0
    ++header;
203
204
0
  if(*header)
205
    /* Skip over colon, null it */
206
0
    *header++ = 0;
207
0
  else
208
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
209
210
  /* skip all leading blank letters */
211
0
  while(ISBLANK(*header))
212
0
    header++;
213
214
0
  *value = header;
215
216
  /* skip all trailing space letters */
217
0
  while((end > header) && ISBLANK(*end))
218
0
    *end-- = 0; /* null-terminate */
219
0
  return CURLE_OK;
220
0
}
221
222
static CURLcode unfold_value(struct Curl_easy *data, const char *value,
223
                             size_t vlen)  /* length of the incoming header */
224
0
{
225
0
  struct Curl_header_store *hs;
226
0
  struct Curl_header_store *newhs;
227
0
  size_t olen; /* length of the old value */
228
0
  size_t oalloc; /* length of the old name + value + separator */
229
0
  size_t offset;
230
0
  DEBUGASSERT(data->state.prevhead);
231
0
  hs = data->state.prevhead;
232
0
  olen = strlen(hs->value);
233
0
  offset = hs->value - hs->buffer;
234
0
  oalloc = olen + offset + 1;
235
236
  /* skip all trailing space letters */
237
0
  while(vlen && ISBLANK(value[vlen - 1]))
238
0
    vlen--;
239
240
  /* save only one leading space */
241
0
  while((vlen > 1) && ISBLANK(value[0]) && ISBLANK(value[1])) {
242
0
    vlen--;
243
0
    value++;
244
0
  }
245
246
  /* since this header block might move in the realloc below, it needs to
247
     first be unlinked from the list and then re-added again after the
248
     realloc */
249
0
  Curl_node_remove(&hs->node);
250
251
  /* new size = struct + new value length + old name+value length */
252
0
  newhs = Curl_saferealloc(hs, sizeof(*hs) + vlen + oalloc + 1);
253
0
  if(!newhs)
254
0
    return CURLE_OUT_OF_MEMORY;
255
  /* ->name and ->value point into ->buffer (to keep the header allocation
256
     in a single memory block), which now potentially have moved. Adjust
257
     them. */
258
0
  newhs->name = newhs->buffer;
259
0
  newhs->value = &newhs->buffer[offset];
260
261
  /* put the data at the end of the previous data, not the newline */
262
0
  memcpy(&newhs->value[olen], value, vlen);
263
0
  newhs->value[olen + vlen] = 0; /* null-terminate at newline */
264
265
  /* insert this node into the list of headers */
266
0
  Curl_llist_append(&data->state.httphdrs, newhs, &newhs->node);
267
0
  data->state.prevhead = newhs;
268
0
  return CURLE_OK;
269
0
}
270
271
272
/*
273
 * Curl_headers_push() gets passed a full HTTP header to store. It gets called
274
 * immediately before the header callback. The header is CRLF terminated.
275
 */
276
CURLcode Curl_headers_push(struct Curl_easy *data, const char *header,
277
                           unsigned char type)
278
0
{
279
0
  char *value = NULL;
280
0
  char *name = NULL;
281
0
  char *end;
282
0
  size_t hlen; /* length of the incoming header */
283
0
  struct Curl_header_store *hs;
284
0
  CURLcode result = CURLE_OUT_OF_MEMORY;
285
286
0
  if((header[0] == '\r') || (header[0] == '\n'))
287
    /* ignore the body separator */
288
0
    return CURLE_OK;
289
290
0
  end = strchr(header, '\r');
291
0
  if(!end) {
292
0
    end = strchr(header, '\n');
293
0
    if(!end)
294
      /* neither CR nor LF as terminator is not a valid header */
295
0
      return CURLE_WEIRD_SERVER_REPLY;
296
0
  }
297
0
  hlen = end - header;
298
299
0
  if((header[0] == ' ') || (header[0] == '\t')) {
300
0
    if(data->state.prevhead)
301
      /* line folding, append value to the previous header's value */
302
0
      return unfold_value(data, header, hlen);
303
0
    else {
304
      /* cannot unfold without a previous header. Instead of erroring, just
305
         pass the leading blanks. */
306
0
      while(hlen && ISBLANK(*header)) {
307
0
        header++;
308
0
        hlen--;
309
0
      }
310
0
      if(!hlen)
311
0
        return CURLE_WEIRD_SERVER_REPLY;
312
0
    }
313
0
  }
314
0
  if(Curl_llist_count(&data->state.httphdrs) >= MAX_HTTP_RESP_HEADER_COUNT) {
315
0
    failf(data, "Too many response headers, %d is max",
316
0
          MAX_HTTP_RESP_HEADER_COUNT);
317
0
    return CURLE_TOO_LARGE;
318
0
  }
319
320
0
  hs = calloc(1, sizeof(*hs) + hlen);
321
0
  if(!hs)
322
0
    return CURLE_OUT_OF_MEMORY;
323
0
  memcpy(hs->buffer, header, hlen);
324
0
  hs->buffer[hlen] = 0; /* null-terminate */
325
326
0
  result = namevalue(hs->buffer, hlen, type, &name, &value);
327
0
  if(!result) {
328
0
    hs->name = name;
329
0
    hs->value = value;
330
0
    hs->type = type;
331
0
    hs->request = data->state.requests;
332
333
    /* insert this node into the list of headers */
334
0
    Curl_llist_append(&data->state.httphdrs, hs, &hs->node);
335
0
    data->state.prevhead = hs;
336
0
  }
337
0
  else {
338
0
    failf(data, "Invalid response header");
339
0
    free(hs);
340
0
  }
341
0
  return result;
342
0
}
343
344
/*
345
 * Curl_headers_reset(). Reset the headers subsystem.
346
 */
347
static void headers_reset(struct Curl_easy *data)
348
5.81k
{
349
5.81k
  Curl_llist_init(&data->state.httphdrs, NULL);
350
5.81k
  data->state.prevhead = NULL;
351
5.81k
}
352
353
struct hds_cw_collect_ctx {
354
  struct Curl_cwriter super;
355
};
356
357
static CURLcode hds_cw_collect_write(struct Curl_easy *data,
358
                                     struct Curl_cwriter *writer, int type,
359
                                     const char *buf, size_t blen)
360
0
{
361
0
  if((type & CLIENTWRITE_HEADER) && !(type & CLIENTWRITE_STATUS)) {
362
0
    unsigned char htype = (unsigned char)
363
0
      (type & CLIENTWRITE_CONNECT ? CURLH_CONNECT :
364
0
       (type & CLIENTWRITE_1XX ? CURLH_1XX :
365
0
        (type & CLIENTWRITE_TRAILER ? CURLH_TRAILER :
366
0
         CURLH_HEADER)));
367
0
    CURLcode result = Curl_headers_push(data, buf, htype);
368
0
    CURL_TRC_WRITE(data, "header_collect pushed(type=%x, len=%zu) -> %d",
369
0
                   htype, blen, result);
370
0
    if(result)
371
0
      return result;
372
0
  }
373
0
  return Curl_cwriter_write(data, writer->next, type, buf, blen);
374
0
}
375
376
static const struct Curl_cwtype hds_cw_collect = {
377
  "hds-collect",
378
  NULL,
379
  Curl_cwriter_def_init,
380
  hds_cw_collect_write,
381
  Curl_cwriter_def_close,
382
  sizeof(struct hds_cw_collect_ctx)
383
};
384
385
CURLcode Curl_headers_init(struct Curl_easy *data)
386
0
{
387
0
  struct Curl_cwriter *writer;
388
0
  CURLcode result;
389
390
0
  if(data->conn && (data->conn->handler->protocol & PROTO_FAMILY_HTTP)) {
391
    /* avoid installing it twice */
392
0
    if(Curl_cwriter_get_by_name(data, hds_cw_collect.name))
393
0
      return CURLE_OK;
394
395
0
    result = Curl_cwriter_create(&writer, data, &hds_cw_collect,
396
0
                                 CURL_CW_PROTOCOL);
397
0
    if(result)
398
0
      return result;
399
400
0
    result = Curl_cwriter_add(data, writer);
401
0
    if(result) {
402
0
      Curl_cwriter_free(data, writer);
403
0
      return result;
404
0
    }
405
0
  }
406
0
  return CURLE_OK;
407
0
}
408
409
/*
410
 * Curl_headers_cleanup(). Free all stored headers and associated memory.
411
 */
412
CURLcode Curl_headers_cleanup(struct Curl_easy *data)
413
5.81k
{
414
5.81k
  struct Curl_llist_node *e;
415
5.81k
  struct Curl_llist_node *n;
416
417
5.81k
  for(e = Curl_llist_head(&data->state.httphdrs); e; e = n) {
418
0
    struct Curl_header_store *hs = Curl_node_elem(e);
419
0
    n = Curl_node_next(e);
420
0
    free(hs);
421
0
  }
422
5.81k
  headers_reset(data);
423
5.81k
  return CURLE_OK;
424
5.81k
}
425
426
#else /* HTTP-disabled builds below */
427
428
CURLHcode curl_easy_header(CURL *easy,
429
                           const char *name,
430
                           size_t index,
431
                           unsigned int origin,
432
                           int request,
433
                           struct curl_header **hout)
434
{
435
  (void)easy;
436
  (void)name;
437
  (void)index;
438
  (void)origin;
439
  (void)request;
440
  (void)hout;
441
  return CURLHE_NOT_BUILT_IN;
442
}
443
444
struct curl_header *curl_easy_nextheader(CURL *easy,
445
                                         unsigned int type,
446
                                         int request,
447
                                         struct curl_header *prev)
448
{
449
  (void)easy;
450
  (void)type;
451
  (void)request;
452
  (void)prev;
453
  return NULL;
454
}
455
#endif