Coverage Report

Created: 2026-10-15 06:18

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