Coverage Report

Created: 2022-12-01 06:42

/src/curl/lib/headers.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 2022, 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 "strcase.h"
30
#include "headers.h"
31
32
/* The last 3 #include files should be in this order */
33
#include "curl_printf.h"
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_easy *data,
42
                                 struct Curl_header_store *hs,
43
                                 size_t index,
44
                                 size_t amount,
45
                                 struct Curl_llist_element *e,
46
                                 struct curl_header **hout)
47
0
{
48
0
  struct curl_header *h = *hout = &data->state.headerout;
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 = 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_element *e;
70
0
  struct Curl_llist_element *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 = data->state.httphdrs.head; e; e = e->next) {
89
0
    hs = e->ptr;
90
0
    if(strcasecompare(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 = data->state.httphdrs.head; e; e = e->next) {
108
0
      hs = e->ptr;
109
0
      if(strcasecompare(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 shouldn't happen */
118
0
      return CURLHE_MISSING;
119
0
  }
120
  /* this is the name we want */
121
0
  copy_header_external(data, hs, nameindex, amount, e_pick, hout);
122
0
  return CURLHE_OK;
123
0
}
124
125
/* public API */
126
struct curl_header *curl_easy_nextheader(CURL *easy,
127
                                         unsigned int type,
128
                                         int request,
129
                                         struct curl_header *prev)
130
0
{
131
0
  struct Curl_easy *data = easy;
132
0
  struct Curl_llist_element *pick;
133
0
  struct Curl_llist_element *e;
134
0
  struct Curl_header_store *hs;
135
0
  struct curl_header *hout;
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 = pick->next;
150
0
  }
151
0
  else
152
0
    pick = data->state.httphdrs.head;
153
154
0
  if(pick) {
155
    /* make sure it is the next header of the desired type */
156
0
    do {
157
0
      hs = pick->ptr;
158
0
      if((hs->type & type) && (hs->request == request))
159
0
        break;
160
0
      pick = pick->next;
161
0
    } while(pick);
162
0
  }
163
164
0
  if(!pick)
165
    /* no more headers available */
166
0
    return NULL;
167
168
0
  hs = pick->ptr;
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 = data->state.httphdrs.head; e; e = e->next) {
173
0
    struct Curl_header_store *check = e->ptr;
174
0
    if(strcasecompare(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(data, hs, index, amount, pick, &hout);
183
0
  return hout;
184
0
}
185
186
static CURLcode namevalue(char *header, size_t hlen, unsigned int type,
187
                           char **name, char **value)
188
0
{
189
0
  char *end = header + hlen - 1; /* point to the last byte */
190
0
  DEBUGASSERT(hlen);
191
0
  *name = header;
192
193
0
  if(type == CURLH_PSEUDO) {
194
0
    if(*header != ':')
195
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
196
0
    header++;
197
0
  }
198
199
  /* Find the end of the header name */
200
0
  while(*header && (*header != ':'))
201
0
    ++header;
202
203
0
  if(*header)
204
    /* Skip over colon, null it */
205
0
    *header++ = 0;
206
0
  else
207
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
208
209
  /* skip all leading space letters */
210
0
  while(*header && ISBLANK(*header))
211
0
    header++;
212
213
0
  *value = header;
214
215
  /* skip all trailing space letters */
216
0
  while((end > header) && ISSPACE(*end))
217
0
    *end-- = 0; /* nul terminate */
218
0
  return CURLE_OK;
219
0
}
220
221
static CURLcode unfold_value(struct Curl_easy *data, const char *value,
222
                             size_t vlen)  /* length of the incoming header */
223
0
{
224
0
  struct Curl_header_store *hs;
225
0
  struct Curl_header_store *newhs;
226
0
  size_t olen; /* length of the old value */
227
0
  size_t oalloc; /* length of the old name + value + separator */
228
0
  size_t offset;
229
0
  DEBUGASSERT(data->state.prevhead);
230
0
  hs = data->state.prevhead;
231
0
  olen = strlen(hs->value);
232
0
  offset = hs->value - hs->buffer;
233
0
  oalloc = olen + offset + 1;
234
235
  /* skip all trailing space letters */
236
0
  while(vlen && ISSPACE(value[vlen - 1]))
237
0
    vlen--;
238
239
  /* save only one leading space */
240
0
  while((vlen > 1) && ISBLANK(value[0]) && ISBLANK(value[1])) {
241
0
    vlen--;
242
0
    value++;
243
0
  }
244
245
  /* since this header block might move in the realloc below, it needs to
246
     first be unlinked from the list and then re-added again after the
247
     realloc */
248
0
  Curl_llist_remove(&data->state.httphdrs, &hs->node, NULL);
249
250
  /* new size = struct + new value length + old name+value length */
251
0
  newhs = Curl_saferealloc(hs, sizeof(*hs) + vlen + oalloc + 1);
252
0
  if(!newhs)
253
0
    return CURLE_OUT_OF_MEMORY;
254
  /* ->name' and ->value point into ->buffer (to keep the header allocation
255
     in a single memory block), which now potentially have moved. Adjust
256
     them. */
257
0
  newhs->name = newhs->buffer;
258
0
  newhs->value = &newhs->buffer[offset];
259
260
  /* put the data at the end of the previous data, not the newline */
261
0
  memcpy(&newhs->value[olen], value, vlen);
262
0
  newhs->value[olen + vlen] = 0; /* null-terminate at newline */
263
264
  /* insert this node into the list of headers */
265
0
  Curl_llist_insert_next(&data->state.httphdrs, data->state.httphdrs.tail,
266
0
                         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
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
295
0
  }
296
0
  hlen = end - header + 1;
297
298
0
  if((header[0] == ' ') || (header[0] == '\t')) {
299
0
    if(data->state.prevhead)
300
      /* line folding, append value to the previous header's value */
301
0
      return unfold_value(data, header, hlen);
302
0
    else
303
      /* can't unfold without a previous header */
304
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
305
0
  }
306
307
0
  hs = calloc(1, sizeof(*hs) + hlen);
308
0
  if(!hs)
309
0
    return CURLE_OUT_OF_MEMORY;
310
0
  memcpy(hs->buffer, header, hlen);
311
0
  hs->buffer[hlen] = 0; /* nul terminate */
312
313
0
  result = namevalue(hs->buffer, hlen, type, &name, &value);
314
0
  if(result)
315
0
    goto fail;
316
317
0
  hs->name = name;
318
0
  hs->value = value;
319
0
  hs->type = type;
320
0
  hs->request = data->state.requests;
321
322
  /* insert this node into the list of headers */
323
0
  Curl_llist_insert_next(&data->state.httphdrs, data->state.httphdrs.tail,
324
0
                         hs, &hs->node);
325
0
  data->state.prevhead = hs;
326
0
  return CURLE_OK;
327
0
  fail:
328
0
  free(hs);
329
0
  return result;
330
0
}
331
332
/*
333
 * Curl_headers_init(). Init the headers subsystem.
334
 */
335
static void headers_init(struct Curl_easy *data)
336
89.4k
{
337
89.4k
  Curl_llist_init(&data->state.httphdrs, NULL);
338
89.4k
}
339
340
/*
341
 * Curl_headers_cleanup(). Free all stored headers and associated memory.
342
 */
343
CURLcode Curl_headers_cleanup(struct Curl_easy *data)
344
89.4k
{
345
89.4k
  struct Curl_llist_element *e;
346
89.4k
  struct Curl_llist_element *n;
347
348
89.4k
  for(e = data->state.httphdrs.head; e; e = n) {
349
0
    struct Curl_header_store *hs = e->ptr;
350
0
    n = e->next;
351
0
    free(hs);
352
0
  }
353
89.4k
  headers_init(data);
354
89.4k
  return CURLE_OK;
355
89.4k
}
356
357
#else /* HTTP-disabled builds below */
358
359
CURLHcode curl_easy_header(CURL *easy,
360
                           const char *name,
361
                           size_t index,
362
                           unsigned int origin,
363
                           int request,
364
                           struct curl_header **hout)
365
{
366
  (void)easy;
367
  (void)name;
368
  (void)index;
369
  (void)origin;
370
  (void)request;
371
  (void)hout;
372
  return CURLHE_NOT_BUILT_IN;
373
}
374
375
struct curl_header *curl_easy_nextheader(CURL *easy,
376
                                         unsigned int type,
377
                                         int request,
378
                                         struct curl_header *prev)
379
{
380
  (void)easy;
381
  (void)type;
382
  (void)request;
383
  (void)prev;
384
  return NULL;
385
}
386
#endif