Coverage Report

Created: 2025-12-04 06:52

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