Coverage Report

Created: 2025-07-11 06:33

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