Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/formdata.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
struct Curl_easy;
27
28
#include "formdata.h"
29
30
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_FORM_API)
31
32
#include "urldata.h" /* for struct Curl_easy */
33
#include "mime.h"
34
#include "curlx/strdup.h"
35
#include "bufref.h"
36
#include "curlx/fopen.h"
37
38
39
0
#define HTTPPOST_PTRNAME     CURL_HTTPPOST_PTRNAME
40
0
#define HTTPPOST_FILENAME    CURL_HTTPPOST_FILENAME
41
0
#define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS
42
0
#define HTTPPOST_READFILE    CURL_HTTPPOST_READFILE
43
0
#define HTTPPOST_PTRBUFFER   CURL_HTTPPOST_PTRBUFFER
44
0
#define HTTPPOST_CALLBACK    CURL_HTTPPOST_CALLBACK
45
0
#define HTTPPOST_BUFFER      CURL_HTTPPOST_BUFFER
46
47
/***************************************************************************
48
 *
49
 * AddHttpPost()
50
 *
51
 * Adds an HttpPost structure to the list, if parent_post is given becomes
52
 * a subpost of parent_post instead of a direct list element.
53
 *
54
 * Returns newly allocated HttpPost on success and NULL if malloc failed.
55
 *
56
 ***************************************************************************/
57
static struct curl_httppost *AddHttpPost(struct FormInfo *src,
58
                                         struct curl_httppost *parent_post,
59
                                         struct curl_httppost **httppost,
60
                                         struct curl_httppost **last_post)
61
0
{
62
0
  struct curl_httppost *post;
63
0
  size_t namelength = src->namelength;
64
0
  if(!namelength && Curl_bufref_ptr(&src->name))
65
0
    namelength = strlen(Curl_bufref_ptr(&src->name));
66
0
  if((src->bufferlength > LONG_MAX) || (namelength > LONG_MAX))
67
    /* avoid overflow in typecasts below */
68
0
    return NULL;
69
0
  post = curlx_calloc(1, sizeof(struct curl_httppost));
70
0
  if(post) {
71
0
    post->name = CURL_UNCONST(Curl_bufref_ptr(&src->name));
72
0
    post->namelength = (long)namelength;
73
0
    post->contents = CURL_UNCONST(Curl_bufref_ptr(&src->value));
74
0
    post->contentlen = src->contentslength;
75
0
    post->buffer = src->buffer;
76
0
    post->bufferlength = (long)src->bufferlength;
77
0
    post->contenttype = CURL_UNCONST(Curl_bufref_ptr(&src->contenttype));
78
0
    post->flags = src->flags | CURL_HTTPPOST_LARGE;
79
0
    post->contentheader = src->contentheader;
80
0
    post->showfilename = CURL_UNCONST(Curl_bufref_ptr(&src->showfilename));
81
0
    post->userp = src->userp;
82
0
  }
83
0
  else
84
0
    return NULL;
85
86
0
  if(parent_post) {
87
    /* now, point our 'more' to the original 'more' */
88
0
    post->more = parent_post->more;
89
90
    /* then move the original 'more' to point to ourselves */
91
0
    parent_post->more = post;
92
0
  }
93
0
  else {
94
    /* make the previous point to this */
95
0
    if(*last_post)
96
0
      (*last_post)->next = post;
97
0
    else
98
0
      (*httppost) = post;
99
100
0
    (*last_post) = post;
101
0
  }
102
0
  return post;
103
0
}
104
105
/* Allocate and initialize a new FormInfo structure. */
106
static struct FormInfo *NewFormInfo(void)
107
0
{
108
0
  struct FormInfo *form_info = curlx_calloc(1, sizeof(struct FormInfo));
109
110
0
  if(form_info) {
111
0
    Curl_bufref_init(&form_info->name);
112
0
    Curl_bufref_init(&form_info->value);
113
0
    Curl_bufref_init(&form_info->contenttype);
114
0
    Curl_bufref_init(&form_info->showfilename);
115
0
  }
116
117
0
  return form_info;
118
0
}
119
120
/* Replace the target field data by a dynamic copy of it. */
121
static CURLcode FormInfoCopyField(struct bufref *field, size_t len)
122
0
{
123
0
  const char *value = Curl_bufref_ptr(field);
124
0
  CURLcode result = CURLE_OK;
125
126
0
  if(value) {
127
0
    if(!len)
128
0
      len = strlen(value);
129
0
    result = Curl_bufref_memdup0(field, value, len);
130
0
  }
131
132
0
  return result;
133
0
}
134
135
/***************************************************************************
136
 *
137
 * AddFormInfo()
138
 *
139
 * Adds a FormInfo structure to the list presented by parent.
140
 *
141
 ***************************************************************************/
142
static void AddFormInfo(struct FormInfo *form_info, struct FormInfo *parent)
143
0
{
144
0
  form_info->flags |= HTTPPOST_FILENAME;
145
146
0
  if(parent) {
147
    /* now, point our 'more' to the original 'more' */
148
0
    form_info->more = parent->more;
149
150
    /* then move the original 'more' to point to ourselves */
151
0
    parent->more = form_info;
152
0
  }
153
0
}
154
155
static void free_formlist(struct FormInfo *ptr)
156
0
{
157
0
  for(; ptr != NULL; ptr = ptr->more) {
158
0
    Curl_bufref_free(&ptr->name);
159
0
    Curl_bufref_free(&ptr->value);
160
0
    Curl_bufref_free(&ptr->contenttype);
161
0
    Curl_bufref_free(&ptr->showfilename);
162
0
  }
163
0
}
164
165
/***************************************************************************
166
 *
167
 * FormAdd()
168
 *
169
 * Stores a formpost parameter and builds the appropriate linked list.
170
 *
171
 * Has two principal functionalities: using files and byte arrays as post
172
 * parts. Byte arrays are either copied or the pointer is stored (as the user
173
 * requests) while for files only the filename and not the content is stored.
174
 *
175
 * While you may have only one byte array for each name, multiple filenames
176
 * are allowed (and because of this feature CURLFORM_END is needed after
177
 * using CURLFORM_FILE).
178
 *
179
 * Examples:
180
 *
181
 * Simple name/value pair with copied contents:
182
 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
183
 *              CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
184
 *
185
 * name/value pair where only the content pointer is remembered:
186
 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
187
 *              CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10,
188
 *              CURLFORM_END);
189
 * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
190
 *
191
 * storing a filename (CONTENTTYPE is optional!):
192
 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
193
 *              CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
194
 *              CURLFORM_END);
195
 *
196
 * storing multiple filenames:
197
 * curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
198
 *              CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2",
199
 *              CURLFORM_END);
200
 *
201
 * Returns:
202
 * CURL_FORMADD_OK             on success
203
 * CURL_FORMADD_MEMORY         if the FormInfo allocation fails
204
 * CURL_FORMADD_OPTION_TWICE   if one option is given twice for one Form
205
 * CURL_FORMADD_NULL           if a null pointer was given for a char
206
 * CURL_FORMADD_MEMORY         if the allocation of a FormInfo struct failed
207
 * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
208
 * CURL_FORMADD_INCOMPLETE     if the some FormInfo is not complete (or error)
209
 * CURL_FORMADD_MEMORY         if an HttpPost struct cannot be allocated
210
 * CURL_FORMADD_MEMORY         if some allocation for string copying failed.
211
 * CURL_FORMADD_ILLEGAL_ARRAY  if an illegal option is used in an array
212
 *
213
 ***************************************************************************/
214
215
static CURLFORMcode FormAddCheck(struct FormInfo *first_form,
216
                                 struct curl_httppost **httppost,
217
                                 struct curl_httppost **last_post)
218
0
{
219
0
  const char *prevtype = NULL;
220
0
  struct FormInfo *form = NULL;
221
0
  struct curl_httppost *post = NULL;
222
223
  /* go through the list, check for completeness and if everything is
224
   * alright add the HttpPost item otherwise set retval accordingly */
225
226
0
  for(form = first_form; form != NULL; form = form->more) {
227
0
    const char *name = Curl_bufref_ptr(&form->name);
228
229
0
    if(((!name || !Curl_bufref_ptr(&form->value)) && !post) ||
230
0
       (form->contentslength &&
231
0
        (form->flags & HTTPPOST_FILENAME)) ||
232
0
       ((form->flags & HTTPPOST_FILENAME) &&
233
0
        (form->flags & HTTPPOST_PTRCONTENTS)) ||
234
235
0
       (!form->buffer &&
236
0
        (form->flags & HTTPPOST_BUFFER) &&
237
0
        (form->flags & HTTPPOST_PTRBUFFER)) ||
238
239
0
       ((form->flags & HTTPPOST_READFILE) &&
240
0
        (form->flags & HTTPPOST_PTRCONTENTS))
241
0
      ) {
242
0
      return CURL_FORMADD_INCOMPLETE;
243
0
    }
244
0
    if(((form->flags & HTTPPOST_FILENAME) ||
245
0
        (form->flags & HTTPPOST_BUFFER)) &&
246
0
       !Curl_bufref_ptr(&form->contenttype)) {
247
0
      const char *f = Curl_bufref_ptr((form->flags & HTTPPOST_BUFFER) ?
248
0
                                      &form->showfilename : &form->value);
249
0
      const char *type = Curl_mime_contenttype(f);
250
0
      if(!type)
251
0
        type = prevtype;
252
0
      if(!type)
253
0
        type = FILE_CONTENTTYPE_DEFAULT;
254
255
      /* our contenttype is missing */
256
0
      if(Curl_bufref_memdup0(&form->contenttype, type, strlen(type)))
257
0
        return CURL_FORMADD_MEMORY;
258
0
    }
259
0
    if(name && form->namelength) {
260
0
      if(memchr(name, 0, form->namelength))
261
0
        return CURL_FORMADD_NULL;
262
0
    }
263
0
    if(!(form->flags & HTTPPOST_PTRNAME)) {
264
      /* Note that there is small risk that form->name is NULL here if the app
265
         passed in a bad combo, so we check for that. */
266
0
      if(FormInfoCopyField(&form->name, form->namelength))
267
0
        return CURL_FORMADD_MEMORY;
268
0
    }
269
0
    if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
270
0
                        HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
271
0
                        HTTPPOST_CALLBACK))) {
272
0
      if(FormInfoCopyField(&form->value, (size_t)form->contentslength))
273
0
        return CURL_FORMADD_MEMORY;
274
0
    }
275
0
    post = AddHttpPost(form, post, httppost, last_post);
276
277
0
    if(!post)
278
0
      return CURL_FORMADD_MEMORY;
279
280
0
    if(Curl_bufref_ptr(&form->contenttype))
281
0
      prevtype = Curl_bufref_ptr(&form->contenttype);
282
0
  }
283
284
0
  return CURL_FORMADD_OK;
285
0
}
286
287
/* Shallow cleanup. Remove the newly created chain, the structs only and not
288
   the content they point to */
289
static void free_chain(struct curl_httppost *c)
290
0
{
291
0
  while(c) {
292
0
    struct curl_httppost *next = c->next;
293
0
    if(c->more)
294
0
      free_chain(c->more);
295
0
    curlx_free(c);
296
0
    c = next;
297
0
  }
298
0
}
299
300
static CURLFORMcode FormAdd(struct curl_httppost **httppost,
301
                            struct curl_httppost **last_post, va_list params)
302
0
{
303
0
  struct FormInfo *first_form, *curr, *form = NULL;
304
0
  CURLFORMcode retval = CURL_FORMADD_OK;
305
0
  CURLformoption option;
306
0
  const struct curl_forms *forms = NULL;
307
0
  char *avalue = NULL;
308
0
  struct curl_httppost *newchain = NULL;
309
0
  struct curl_httppost *lastnode = NULL;
310
311
0
#define form_ptr_arg(t) (forms ? (t)(void *)avalue : va_arg(params, t))
312
0
#ifdef HAVE_UINTPTR_T
313
0
#define form_int_arg(t) (forms ? (t)(uintptr_t)avalue : va_arg(params, t))
314
#else
315
#define form_int_arg(t) (forms ? (t)(void *)avalue : va_arg(params, t))
316
#endif
317
318
  /*
319
   * We need to allocate the first struct to fill in.
320
   */
321
0
  first_form = NewFormInfo();
322
0
  if(!first_form)
323
0
    return CURL_FORMADD_MEMORY;
324
325
0
  curr = first_form;
326
327
  /*
328
   * Loop through all the options set. Break if we have an error to report.
329
   */
330
0
  while(retval == CURL_FORMADD_OK) {
331
332
    /* first see if we have more parts of the array param */
333
0
    if(forms) {
334
      /* get the upcoming option from the given array */
335
0
      option = forms->option;
336
0
      avalue = (char *)CURL_UNCONST(forms->value);
337
338
0
      forms++; /* advance this to next entry */
339
0
      if(CURLFORM_END == option) {
340
        /* end of array state */
341
0
        forms = NULL;
342
0
        continue;
343
0
      }
344
0
    }
345
0
    else {
346
      /* This is not array-state, get next option. This gets an 'int' with
347
         va_arg() because CURLformoption might be a smaller type than int and
348
         might cause compiler warnings and wrong behavior. */
349
0
      option = (CURLformoption)va_arg(params, int);
350
0
      if(CURLFORM_END == option)
351
0
        break;
352
0
    }
353
354
0
    switch(option) {
355
0
    case CURLFORM_ARRAY:
356
0
      if(forms)
357
        /* we do not support an array from within an array */
358
0
        retval = CURL_FORMADD_ILLEGAL_ARRAY;
359
0
      else {
360
0
        forms = va_arg(params, struct curl_forms *);
361
0
        if(!forms)
362
0
          retval = CURL_FORMADD_NULL;
363
0
      }
364
0
      break;
365
366
      /*
367
       * Set the Name property.
368
       */
369
0
    case CURLFORM_PTRNAME:
370
0
      curr->flags |= HTTPPOST_PTRNAME;
371
0
      FALLTHROUGH();
372
0
    case CURLFORM_COPYNAME:
373
0
      if(Curl_bufref_ptr(&curr->name))
374
0
        retval = CURL_FORMADD_OPTION_TWICE;
375
0
      else {
376
0
        avalue = form_ptr_arg(char *);
377
0
        if(avalue)
378
0
          Curl_bufref_set(&curr->name, avalue, 0, NULL); /* No copy yet. */
379
0
        else
380
0
          retval = CURL_FORMADD_NULL;
381
0
      }
382
0
      break;
383
0
    case CURLFORM_NAMELENGTH:
384
0
      if(curr->namelength)
385
0
        retval = CURL_FORMADD_OPTION_TWICE;
386
0
      else
387
0
        curr->namelength = (size_t)form_int_arg(long);
388
0
      break;
389
390
      /*
391
       * Set the contents property.
392
       */
393
0
    case CURLFORM_PTRCONTENTS:
394
0
      curr->flags |= HTTPPOST_PTRCONTENTS;
395
0
      FALLTHROUGH();
396
0
    case CURLFORM_COPYCONTENTS:
397
0
      if(Curl_bufref_ptr(&curr->value))
398
0
        retval = CURL_FORMADD_OPTION_TWICE;
399
0
      else {
400
0
        avalue = form_ptr_arg(char *);
401
0
        if(avalue)
402
0
          Curl_bufref_set(&curr->value, avalue, 0, NULL); /* No copy yet. */
403
0
        else
404
0
          retval = CURL_FORMADD_NULL;
405
0
      }
406
0
      break;
407
0
    case CURLFORM_CONTENTSLENGTH:
408
0
      curr->contentslength = (curl_off_t)(size_t)form_int_arg(long);
409
0
      break;
410
411
0
    case CURLFORM_CONTENTLEN:
412
0
      curr->flags |= CURL_HTTPPOST_LARGE;
413
0
      curr->contentslength = form_int_arg(curl_off_t);
414
0
      break;
415
416
      /* Get contents from a given filename */
417
0
    case CURLFORM_FILECONTENT:
418
0
      if(curr->flags & (HTTPPOST_PTRCONTENTS | HTTPPOST_READFILE))
419
0
        retval = CURL_FORMADD_OPTION_TWICE;
420
0
      else {
421
0
        avalue = form_ptr_arg(char *);
422
0
        if(avalue) {
423
0
          if(Curl_bufref_memdup0(&curr->value, avalue, strlen(avalue)))
424
0
            retval = CURL_FORMADD_MEMORY;
425
0
          else
426
0
            curr->flags |= HTTPPOST_READFILE;
427
0
        }
428
0
        else
429
0
          retval = CURL_FORMADD_NULL;
430
0
      }
431
0
      break;
432
433
      /* We upload a file */
434
0
    case CURLFORM_FILE:
435
0
      avalue = form_ptr_arg(char *);
436
0
      if(Curl_bufref_ptr(&curr->value)) {
437
0
        if(curr->flags & HTTPPOST_FILENAME) {
438
0
          if(avalue) {
439
0
            form = NewFormInfo();
440
0
            if(!form ||
441
0
               Curl_bufref_memdup0(&form->value, avalue, strlen(avalue))) {
442
0
              curlx_free(form);
443
0
              retval = CURL_FORMADD_MEMORY;
444
0
            }
445
0
            else {
446
0
              AddFormInfo(form, curr);
447
0
              curr = form;
448
0
              form = NULL;
449
0
            }
450
0
          }
451
0
          else
452
0
            retval = CURL_FORMADD_NULL;
453
0
        }
454
0
        else
455
0
          retval = CURL_FORMADD_OPTION_TWICE;
456
0
      }
457
0
      else {
458
0
        if(avalue) {
459
0
          if(Curl_bufref_memdup0(&curr->value, avalue, strlen(avalue)))
460
0
            retval = CURL_FORMADD_MEMORY;
461
0
          else
462
0
            curr->flags |= HTTPPOST_FILENAME;
463
0
        }
464
0
        else
465
0
          retval = CURL_FORMADD_NULL;
466
0
      }
467
0
      break;
468
469
0
    case CURLFORM_BUFFERPTR:
470
0
      curr->flags |= HTTPPOST_PTRBUFFER | HTTPPOST_BUFFER;
471
0
      if(curr->buffer)
472
0
        retval = CURL_FORMADD_OPTION_TWICE;
473
0
      else {
474
0
        avalue = form_ptr_arg(char *);
475
0
        if(avalue) {
476
0
          curr->buffer = avalue; /* store for the moment */
477
          /* Make value non-NULL to be accepted as fine */
478
0
          Curl_bufref_set(&curr->value, avalue, 0, NULL);
479
0
        }
480
0
        else
481
0
          retval = CURL_FORMADD_NULL;
482
0
      }
483
0
      break;
484
485
0
    case CURLFORM_BUFFERLENGTH:
486
0
      if(curr->bufferlength)
487
0
        retval = CURL_FORMADD_OPTION_TWICE;
488
0
      else
489
0
        curr->bufferlength = (size_t)form_int_arg(long);
490
0
      break;
491
492
0
    case CURLFORM_STREAM:
493
0
      curr->flags |= HTTPPOST_CALLBACK;
494
0
      if(curr->userp)
495
0
        retval = CURL_FORMADD_OPTION_TWICE;
496
0
      else {
497
0
        avalue = form_ptr_arg(char *);
498
0
        if(avalue) {
499
0
          curr->userp = avalue;
500
          /* The following line is not strictly true but we derive a value
501
             from this later on and we need this non-NULL to be accepted as
502
             a fine form part */
503
0
          Curl_bufref_set(&curr->value, avalue, 0, NULL);
504
0
        }
505
0
        else
506
0
          retval = CURL_FORMADD_NULL;
507
0
      }
508
0
      break;
509
510
0
    case CURLFORM_CONTENTTYPE:
511
0
      avalue = form_ptr_arg(char *);
512
0
      if(Curl_bufref_ptr(&curr->contenttype)) {
513
0
        if(curr->flags & HTTPPOST_FILENAME) {
514
0
          if(avalue) {
515
0
            form = NewFormInfo();
516
0
            if(!form || Curl_bufref_memdup0(&form->contenttype, avalue,
517
0
                                            strlen(avalue))) {
518
0
              curlx_free(form);
519
0
              retval = CURL_FORMADD_MEMORY;
520
0
            }
521
0
            else {
522
0
              AddFormInfo(form, curr);
523
0
              curr = form;
524
0
              form = NULL;
525
0
            }
526
0
          }
527
0
          else
528
0
            retval = CURL_FORMADD_NULL;
529
0
        }
530
0
        else
531
0
          retval = CURL_FORMADD_OPTION_TWICE;
532
0
      }
533
0
      else if(avalue) {
534
0
        if(Curl_bufref_memdup0(&curr->contenttype, avalue, strlen(avalue)))
535
0
          retval = CURL_FORMADD_MEMORY;
536
0
      }
537
0
      else
538
0
        retval = CURL_FORMADD_NULL;
539
0
      break;
540
541
0
    case CURLFORM_CONTENTHEADER: {
542
      /* this "cast increases required alignment of target type" but
543
         we consider it OK anyway */
544
0
      struct curl_slist *list = form_ptr_arg(struct curl_slist *);
545
546
0
      if(curr->contentheader)
547
0
        retval = CURL_FORMADD_OPTION_TWICE;
548
0
      else
549
0
        curr->contentheader = list;
550
551
0
      break;
552
0
    }
553
0
    case CURLFORM_FILENAME:
554
0
    case CURLFORM_BUFFER:
555
0
      avalue = form_ptr_arg(char *);
556
0
      if(Curl_bufref_ptr(&curr->showfilename))
557
0
        retval = CURL_FORMADD_OPTION_TWICE;
558
0
      else if(Curl_bufref_memdup0(&curr->showfilename, avalue, strlen(avalue)))
559
0
        retval = CURL_FORMADD_MEMORY;
560
0
      break;
561
562
0
    default:
563
0
      retval = CURL_FORMADD_UNKNOWN_OPTION;
564
0
      break;
565
0
    }
566
0
  }
567
568
0
  if(!retval)
569
0
    retval = FormAddCheck(first_form, &newchain, &lastnode);
570
571
0
  if(retval)
572
    /* On error, free allocated fields for all nodes of the FormInfo linked
573
       list without deallocating nodes. List nodes are deallocated later on */
574
0
    free_formlist(first_form);
575
576
  /* Always deallocate FormInfo linked list nodes without touching node
577
     fields given that these have either been deallocated or are owned
578
     now by the httppost linked list */
579
0
  while(first_form) {
580
0
    struct FormInfo *ptr = first_form->more;
581
0
    curlx_free(first_form);
582
0
    first_form = ptr;
583
0
  }
584
585
0
  if(!retval) {
586
    /* Only if all is fine, link the new chain into the provided list */
587
0
    if(*last_post)
588
0
      (*last_post)->next = newchain;
589
0
    else
590
0
      (*httppost) = newchain;
591
592
0
    (*last_post) = lastnode;
593
0
  }
594
0
  else
595
0
    free_chain(newchain);
596
597
0
  return retval;
598
0
#undef form_ptr_arg
599
0
#undef form_int_arg
600
0
}
601
602
/*
603
 * curl_formadd() is a public API to add a section to the multipart formpost.
604
 *
605
 * @unittest: 1308
606
 */
607
608
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
609
                          struct curl_httppost **last_post, ...)
610
0
{
611
0
  va_list arg;
612
0
  CURLFORMcode result;
613
0
  va_start(arg, last_post);
614
0
  result = FormAdd(httppost, last_post, arg);
615
0
  va_end(arg);
616
0
  return result;
617
0
}
618
619
/*
620
 * curl_formget()
621
 * Serialize a curl_httppost struct.
622
 * Returns 0 on success.
623
 *
624
 * @unittest: 1308
625
 */
626
int curl_formget(struct curl_httppost *form, void *arg,
627
                 curl_formget_callback append)
628
0
{
629
0
  CURLcode result;
630
0
  curl_mimepart toppart;
631
632
  /* Validate callback is provided */
633
0
  if(!append)
634
0
    return (int)CURLE_BAD_FUNCTION_ARGUMENT;
635
636
0
  Curl_mime_initpart(&toppart); /* default form is empty */
637
0
  result = Curl_getformdata(NULL, &toppart, form, NULL);
638
0
  if(!result)
639
0
    result = Curl_mime_prepare_headers(NULL, &toppart, "multipart/form-data",
640
0
                                       NULL, MIMESTRATEGY_FORM);
641
642
0
  while(!result) {
643
0
    char buffer[8192];
644
0
    size_t nread = Curl_mime_read(buffer, 1, sizeof(buffer), &toppart);
645
646
0
    if(!nread)
647
0
      break;
648
649
0
    if(nread > sizeof(buffer) || append(arg, buffer, nread) != nread) {
650
0
      result = CURLE_READ_ERROR;
651
0
      if(nread == CURL_READFUNC_ABORT)
652
0
        result = CURLE_ABORTED_BY_CALLBACK;
653
0
    }
654
0
  }
655
656
0
  Curl_mime_cleanpart(&toppart);
657
0
  return (int)result;
658
0
}
659
660
/*
661
 * curl_formfree() is an external function to free up a whole form post
662
 * chain
663
 */
664
void curl_formfree(struct curl_httppost *form)
665
0
{
666
0
  struct curl_httppost *next;
667
668
0
  if(!form)
669
    /* no form to free, get out of this */
670
0
    return;
671
672
0
  do {
673
0
    next = form->next;  /* the following form line */
674
675
    /* recurse to sub-contents */
676
0
    curl_formfree(form->more);
677
678
0
    if(!(form->flags & HTTPPOST_PTRNAME))
679
0
      curlx_free(form->name); /* free the name */
680
0
    if(!(form->flags &
681
0
         (HTTPPOST_PTRCONTENTS | HTTPPOST_BUFFER | HTTPPOST_CALLBACK)))
682
0
      curlx_free(form->contents); /* free the contents */
683
0
    curlx_free(form->contenttype); /* free the content type */
684
0
    curlx_free(form->showfilename); /* free the faked filename */
685
0
    curlx_free(form);       /* free the struct */
686
0
    form = next;
687
0
  } while(form); /* continue */
688
0
}
689
690
/* Set mime part name, taking care of non null-terminated name string. */
691
static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
692
0
{
693
0
  char *zname;
694
0
  CURLcode res;
695
696
0
  if(!name || !len)
697
0
    return curl_mime_name(part, name);
698
0
  zname = curlx_memdup0(name, len);
699
0
  if(!zname)
700
0
    return CURLE_OUT_OF_MEMORY;
701
0
  res = curl_mime_name(part, zname);
702
0
  curlx_free(zname);
703
0
  return res;
704
0
}
705
706
/*
707
 * Curl_getformdata() converts a linked list of "meta data" into a mime
708
 * structure. The input list is in 'post', while the output is stored in
709
 * mime part at '*finalform'.
710
 *
711
 * This function will not do a failf() for the potential memory failures but
712
 * should for all other errors it spots. Note that this function MAY get a
713
 * NULL pointer in the 'data' argument.
714
 */
715
716
CURLcode Curl_getformdata(CURL *data,
717
                          curl_mimepart *finalform,
718
                          struct curl_httppost *post,
719
                          curl_read_callback fread_func)
720
0
{
721
0
  CURLcode result = CURLE_OK;
722
0
  curl_mime *form = NULL;
723
0
  curl_mimepart *part;
724
0
  struct curl_httppost *file;
725
726
0
  Curl_mime_cleanpart(finalform); /* default form is empty */
727
728
0
  if(!post)
729
0
    return result; /* no input => no output! */
730
731
0
  form = curl_mime_init(data);
732
0
  if(!form)
733
0
    result = CURLE_OUT_OF_MEMORY;
734
735
0
  if(!result)
736
0
    result = curl_mime_subparts(finalform, form);
737
738
  /* Process each top part. */
739
0
  for(; !result && post; post = post->next) {
740
    /* If we have more than a file here, create a mime subpart and fill it. */
741
0
    curl_mime *multipart = form;
742
0
    if(post->more) {
743
0
      part = curl_mime_addpart(form);
744
0
      if(!part)
745
0
        result = CURLE_OUT_OF_MEMORY;
746
0
      if(!result)
747
0
        result = setname(part, post->name, post->namelength);
748
0
      if(!result) {
749
0
        multipart = curl_mime_init(data);
750
0
        if(!multipart)
751
0
          result = CURLE_OUT_OF_MEMORY;
752
0
      }
753
0
      if(!result)
754
0
        result = curl_mime_subparts(part, multipart);
755
0
    }
756
757
    /* Generate all the part contents. */
758
0
    for(file = post; !result && file; file = file->more) {
759
      /* Create the part. */
760
0
      part = curl_mime_addpart(multipart);
761
0
      if(!part)
762
0
        result = CURLE_OUT_OF_MEMORY;
763
764
      /* Set the headers. */
765
0
      if(!result)
766
0
        result = curl_mime_headers(part, file->contentheader, 0);
767
768
      /* Set the content type. */
769
0
      if(!result && file->contenttype)
770
0
        result = curl_mime_type(part, file->contenttype);
771
772
      /* Set field name. */
773
0
      if(!result && !post->more)
774
0
        result = setname(part, post->name, post->namelength);
775
776
      /* Process contents. */
777
0
      if(!result) {
778
0
        curl_off_t clen = post->contentslength;
779
780
0
        if(post->flags & CURL_HTTPPOST_LARGE)
781
0
          clen = post->contentlen;
782
783
0
        if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) {
784
0
          if(!strcmp(file->contents, "-")) {
785
            /* There are a few cases where the code below will not work; in
786
               particular, freopen(stdin) by the caller is not guaranteed
787
               to result as expected. This feature has been kept for backward
788
               compatibility: use of "-" pseudo filename should be avoided. */
789
0
#if defined(__clang__) && __clang_major__ >= 16
790
0
#pragma clang diagnostic push
791
0
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
792
0
#endif
793
0
            result = curl_mime_data_cb(part, (curl_off_t)-1,
794
0
                                       (curl_read_callback)fread,
795
0
                                       curlx_fseek,
796
0
                                       NULL, (void *)stdin);
797
0
#if defined(__clang__) && __clang_major__ >= 16
798
0
#pragma clang diagnostic pop
799
0
#endif
800
0
          }
801
0
          else
802
0
            result = curl_mime_filedata(part, file->contents);
803
0
          if(!result && (post->flags & HTTPPOST_READFILE))
804
0
            result = curl_mime_filename(part, NULL);
805
0
        }
806
0
        else if(post->flags & HTTPPOST_BUFFER)
807
0
          result = curl_mime_data(part, post->buffer,
808
0
                                  post->bufferlength ?
809
0
                                  post->bufferlength : -1);
810
0
        else if(post->flags & HTTPPOST_CALLBACK) {
811
          /* the contents should be read with the callback and the size is set
812
             with the contentslength */
813
0
          if(!clen)
814
0
            clen = -1;
815
0
          result = curl_mime_data_cb(part, clen,
816
0
                                     fread_func, NULL, NULL, post->userp);
817
0
        }
818
0
        else {
819
0
          size_t uclen;
820
0
          if(!clen)
821
0
            uclen = CURL_ZERO_TERMINATED;
822
0
          else
823
0
            uclen = (size_t)clen;
824
0
          result = curl_mime_data(part, post->contents, uclen);
825
0
        }
826
0
      }
827
828
      /* Set fake filename. */
829
0
      if(!result && post->showfilename)
830
0
        if(post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER |
831
0
                                         HTTPPOST_CALLBACK)))
832
0
          result = curl_mime_filename(part, post->showfilename);
833
0
    }
834
0
  }
835
836
0
  if(result)
837
0
    Curl_mime_cleanpart(finalform);
838
839
0
  return result;
840
0
}
841
842
#else /* if disabled */
843
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
844
                          struct curl_httppost **last_post, ...)
845
{
846
  (void)httppost;
847
  (void)last_post;
848
  return CURL_FORMADD_DISABLED;
849
}
850
851
int curl_formget(struct curl_httppost *form, void *arg,
852
                 curl_formget_callback append)
853
{
854
  (void)form;
855
  (void)arg;
856
  (void)append;
857
  return CURL_FORMADD_DISABLED;
858
}
859
860
void curl_formfree(struct curl_httppost *form)
861
{
862
  (void)form;
863
  /* Nothing to do. */
864
}
865
866
#endif /* if disabled */