Coverage Report

Created: 2026-09-14 07:06

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
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
 * 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
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 *forminfo_new(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 forminfo_copyfield(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
 * 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
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; 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 && memchr(name, 0, form->namelength))
260
0
      return CURL_FORMADD_NULL;
261
0
    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
0
       forminfo_copyfield(&form->name, form->namelength))
265
0
      return CURL_FORMADD_MEMORY;
266
0
    if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
267
0
                        HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
268
0
                        HTTPPOST_CALLBACK)) &&
269
0
       forminfo_copyfield(&form->value, (size_t)form->contentslength))
270
0
      return CURL_FORMADD_MEMORY;
271
0
    post = httppost_add(form, post, httppost, last_post);
272
273
0
    if(!post)
274
0
      return CURL_FORMADD_MEMORY;
275
276
0
    if(Curl_bufref_ptr(&form->contenttype))
277
0
      prevtype = Curl_bufref_ptr(&form->contenttype);
278
0
  }
279
280
0
  return CURL_FORMADD_OK;
281
0
}
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
0
{
299
0
  struct FormInfo *first_form, *curr, *form = NULL;
300
0
  CURLFORMcode retval = CURL_FORMADD_OK;
301
0
  CURLformoption option;
302
0
  const struct curl_forms *forms = NULL;
303
0
  char *avalue = NULL;
304
0
  struct curl_httppost *newchain = NULL;
305
0
  struct curl_httppost *lastnode = NULL;
306
307
0
#define form_ptr_arg(t) (forms ? (t)(void *)avalue : va_arg(params, t))
308
0
#ifdef HAVE_UINTPTR_T
309
0
#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
0
  first_form = forminfo_new();
318
0
  if(!first_form)
319
0
    return CURL_FORMADD_MEMORY;
320
321
0
  curr = first_form;
322
323
  /*
324
   * Loop through all the options set. Break if we have an error to report.
325
   */
326
0
  while(retval == CURL_FORMADD_OK) {
327
328
    /* first see if we have more parts of the array param */
329
0
    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
0
    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
0
      option = (CURLformoption)va_arg(params, int);
346
0
      if(CURLFORM_END == option)
347
0
        break;
348
0
    }
349
350
0
    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
0
    case CURLFORM_COPYNAME:
369
0
      if(Curl_bufref_ptr(&curr->name))
370
0
        retval = CURL_FORMADD_OPTION_TWICE;
371
0
      else {
372
0
        avalue = form_ptr_arg(char *);
373
0
        if(avalue)
374
0
          Curl_bufref_set(&curr->name, avalue, 0, NULL); /* No copy yet. */
375
0
        else
376
0
          retval = CURL_FORMADD_NULL;
377
0
      }
378
0
      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
0
    case CURLFORM_PTRCONTENTS:
390
0
      curr->flags |= HTTPPOST_PTRCONTENTS;
391
0
      FALLTHROUGH();
392
0
    case CURLFORM_COPYCONTENTS:
393
0
      if(Curl_bufref_ptr(&curr->value))
394
0
        retval = CURL_FORMADD_OPTION_TWICE;
395
0
      else {
396
0
        avalue = form_ptr_arg(char *);
397
0
        if(avalue)
398
0
          Curl_bufref_set(&curr->value, avalue, 0, NULL); /* No copy yet. */
399
0
        else
400
0
          retval = CURL_FORMADD_NULL;
401
0
      }
402
0
      break;
403
0
    case CURLFORM_CONTENTSLENGTH:
404
0
      curr->contentslength = (curl_off_t)(size_t)form_int_arg(long);
405
0
      break;
406
407
0
    case CURLFORM_CONTENTLEN:
408
0
      curr->flags |= CURL_HTTPPOST_LARGE;
409
0
      curr->contentslength = form_int_arg(curl_off_t);
410
0
      break;
411
412
      /* Get contents from a given filename */
413
0
    case CURLFORM_FILECONTENT:
414
0
      if(curr->flags & (HTTPPOST_PTRCONTENTS | HTTPPOST_READFILE))
415
0
        retval = CURL_FORMADD_OPTION_TWICE;
416
0
      else {
417
0
        avalue = form_ptr_arg(char *);
418
0
        if(avalue) {
419
0
          if(Curl_bufref_memdup0(&curr->value, avalue, strlen(avalue)))
420
0
            retval = CURL_FORMADD_MEMORY;
421
0
          else
422
0
            curr->flags |= HTTPPOST_READFILE;
423
0
        }
424
0
        else
425
0
          retval = CURL_FORMADD_NULL;
426
0
      }
427
0
      break;
428
429
      /* We upload a file */
430
0
    case CURLFORM_FILE:
431
0
      avalue = form_ptr_arg(char *);
432
0
      if(Curl_bufref_ptr(&curr->value)) {
433
0
        if(curr->flags & HTTPPOST_FILENAME) {
434
0
          if(avalue) {
435
0
            form = forminfo_new();
436
0
            if(!form ||
437
0
               Curl_bufref_memdup0(&form->value, avalue, strlen(avalue))) {
438
0
              curlx_free(form);
439
0
              retval = CURL_FORMADD_MEMORY;
440
0
            }
441
0
            else {
442
0
              forminfo_add(form, curr);
443
0
              curr = form;
444
0
              form = NULL;
445
0
            }
446
0
          }
447
0
          else
448
0
            retval = CURL_FORMADD_NULL;
449
0
        }
450
0
        else
451
0
          retval = CURL_FORMADD_OPTION_TWICE;
452
0
      }
453
0
      else {
454
0
        if(avalue) {
455
0
          if(Curl_bufref_memdup0(&curr->value, avalue, strlen(avalue)))
456
0
            retval = CURL_FORMADD_MEMORY;
457
0
          else
458
0
            curr->flags |= HTTPPOST_FILENAME;
459
0
        }
460
0
        else
461
0
          retval = CURL_FORMADD_NULL;
462
0
      }
463
0
      break;
464
465
0
    case CURLFORM_BUFFERPTR:
466
0
      curr->flags |= HTTPPOST_PTRBUFFER | HTTPPOST_BUFFER;
467
0
      if(curr->buffer)
468
0
        retval = CURL_FORMADD_OPTION_TWICE;
469
0
      else {
470
0
        avalue = form_ptr_arg(char *);
471
0
        if(avalue) {
472
0
          curr->buffer = avalue; /* store for the moment */
473
          /* Make value non-NULL to be accepted as fine */
474
0
          Curl_bufref_set(&curr->value, avalue, 0, NULL);
475
0
        }
476
0
        else
477
0
          retval = CURL_FORMADD_NULL;
478
0
      }
479
0
      break;
480
481
0
    case CURLFORM_BUFFERLENGTH:
482
0
      if(curr->bufferlength)
483
0
        retval = CURL_FORMADD_OPTION_TWICE;
484
0
      else
485
0
        curr->bufferlength = (size_t)form_int_arg(long);
486
0
      break;
487
488
0
    case CURLFORM_STREAM:
489
0
      curr->flags |= HTTPPOST_CALLBACK;
490
0
      if(curr->userp)
491
0
        retval = CURL_FORMADD_OPTION_TWICE;
492
0
      else {
493
0
        avalue = form_ptr_arg(char *);
494
0
        if(avalue) {
495
0
          curr->userp = avalue;
496
          /* The following line is not strictly true but we derive a value
497
             from this later on and we need this non-NULL to be accepted as
498
             a fine form part */
499
0
          Curl_bufref_set(&curr->value, avalue, 0, NULL);
500
0
        }
501
0
        else
502
0
          retval = CURL_FORMADD_NULL;
503
0
      }
504
0
      break;
505
506
0
    case CURLFORM_CONTENTTYPE:
507
0
      avalue = form_ptr_arg(char *);
508
0
      if(Curl_bufref_ptr(&curr->contenttype)) {
509
0
        if(curr->flags & HTTPPOST_FILENAME) {
510
0
          if(avalue) {
511
0
            form = forminfo_new();
512
0
            if(!form || Curl_bufref_memdup0(&form->contenttype, avalue,
513
0
                                            strlen(avalue))) {
514
0
              curlx_free(form);
515
0
              retval = CURL_FORMADD_MEMORY;
516
0
            }
517
0
            else {
518
0
              forminfo_add(form, curr);
519
0
              curr = form;
520
0
              form = NULL;
521
0
            }
522
0
          }
523
0
          else
524
0
            retval = CURL_FORMADD_NULL;
525
0
        }
526
0
        else
527
0
          retval = CURL_FORMADD_OPTION_TWICE;
528
0
      }
529
0
      else if(avalue) {
530
0
        if(Curl_bufref_memdup0(&curr->contenttype, avalue, strlen(avalue)))
531
0
          retval = CURL_FORMADD_MEMORY;
532
0
      }
533
0
      else
534
0
        retval = CURL_FORMADD_NULL;
535
0
      break;
536
537
0
    case CURLFORM_CONTENTHEADER: {
538
      /* this "cast increases required alignment of target type" but
539
         we consider it OK anyway */
540
0
      struct curl_slist *list = form_ptr_arg(struct curl_slist *);
541
542
0
      if(curr->contentheader)
543
0
        retval = CURL_FORMADD_OPTION_TWICE;
544
0
      else
545
0
        curr->contentheader = list;
546
547
0
      break;
548
0
    }
549
0
    case CURLFORM_FILENAME:
550
0
    case CURLFORM_BUFFER:
551
0
      avalue = form_ptr_arg(char *);
552
0
      if(Curl_bufref_ptr(&curr->showfilename))
553
0
        retval = CURL_FORMADD_OPTION_TWICE;
554
0
      else if(Curl_bufref_memdup0(&curr->showfilename, avalue, strlen(avalue)))
555
0
        retval = CURL_FORMADD_MEMORY;
556
0
      break;
557
558
0
    default:
559
0
      retval = CURL_FORMADD_UNKNOWN_OPTION;
560
0
      break;
561
0
    }
562
0
  }
563
564
0
  if(!retval)
565
0
    retval = formadd_check(first_form, &newchain, &lastnode);
566
567
0
  if(retval)
568
    /* On error, free allocated fields for all nodes of the FormInfo linked
569
       list without deallocating nodes. List nodes are deallocated later on */
570
0
    free_formlist(first_form);
571
572
  /* Always deallocate FormInfo linked list nodes without touching node
573
     fields given that these have either been deallocated or are owned
574
     now by the httppost linked list */
575
0
  while(first_form) {
576
0
    struct FormInfo *ptr = first_form->more;
577
0
    curlx_free(first_form);
578
0
    first_form = ptr;
579
0
  }
580
581
0
  if(!retval) {
582
    /* Only if all is fine, link the new chain into the provided list */
583
0
    if(*last_post)
584
0
      (*last_post)->next = newchain;
585
0
    else
586
0
      (*httppost) = newchain;
587
588
0
    (*last_post) = lastnode;
589
0
  }
590
0
  else
591
0
    free_chain(newchain);
592
593
0
  return retval;
594
0
#undef form_ptr_arg
595
0
#undef form_int_arg
596
0
}
597
598
/*
599
 * curl_formadd() is a public API to add a section to the multipart formpost.
600
 *
601
 * @unittest: 1308
602
 */
603
604
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
605
                          struct curl_httppost **last_post, ...)
606
0
{
607
0
  va_list arg;
608
0
  CURLFORMcode form;
609
0
  va_start(arg, last_post);
610
0
  form = formadd(httppost, last_post, arg);
611
0
  va_end(arg);
612
0
  return form;
613
0
}
614
615
/*
616
 * curl_formget()
617
 * Serialize a curl_httppost struct.
618
 * Returns 0 on success.
619
 *
620
 * @unittest: 1308
621
 */
622
int curl_formget(struct curl_httppost *form, void *arg,
623
                 curl_formget_callback append)
624
0
{
625
0
  CURLcode result;
626
0
  curl_mimepart toppart;
627
628
  /* Validate callback is provided */
629
0
  if(!append)
630
0
    return (int)CURLE_BAD_FUNCTION_ARGUMENT;
631
632
0
  Curl_mime_initpart(&toppart); /* default form is empty */
633
0
  result = Curl_getformdata(NULL, &toppart, form, NULL);
634
0
  if(!result)
635
0
    result = Curl_mime_prepare_headers(NULL, &toppart, "multipart/form-data",
636
0
                                       NULL, MIMESTRATEGY_FORM);
637
638
0
  while(!result) {
639
0
    char buffer[8192];
640
0
    size_t nread = Curl_mime_read(buffer, 1, sizeof(buffer), &toppart);
641
642
0
    if(!nread)
643
0
      break;
644
645
0
    if(nread > sizeof(buffer) || append(arg, buffer, nread) != nread) {
646
0
      result = CURLE_READ_ERROR;
647
0
      if(nread == CURL_READFUNC_ABORT)
648
0
        result = CURLE_ABORTED_BY_CALLBACK;
649
0
    }
650
0
  }
651
652
0
  Curl_mime_cleanpart(&toppart);
653
0
  return (int)result;
654
0
}
655
656
/*
657
 * curl_formfree() is an external function to free up a whole form post
658
 * chain
659
 */
660
void curl_formfree(struct curl_httppost *form)
661
0
{
662
0
  struct curl_httppost *next;
663
664
0
  if(!form)
665
    /* no form to free, get out of this */
666
0
    return;
667
668
0
  do {
669
0
    next = form->next;  /* the following form line */
670
671
    /* recurse to sub-contents */
672
0
    curl_formfree(form->more);
673
674
0
    if(!(form->flags & HTTPPOST_PTRNAME))
675
0
      curlx_free(form->name); /* free the name */
676
0
    if(!(form->flags &
677
0
         (HTTPPOST_PTRCONTENTS | HTTPPOST_BUFFER | HTTPPOST_CALLBACK)))
678
0
      curlx_free(form->contents); /* free the contents */
679
0
    curlx_free(form->contenttype); /* free the content type */
680
0
    curlx_free(form->showfilename); /* free the faked filename */
681
0
    curlx_free(form);       /* free the struct */
682
0
    form = next;
683
0
  } while(form); /* continue */
684
0
}
685
686
/* Set mime part name, taking care of non null-terminated name string. */
687
static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
688
0
{
689
0
  char *zname;
690
0
  CURLcode result;
691
692
0
  if(!name || !len)
693
0
    return curl_mime_name(part, name);
694
0
  zname = curlx_memdup0(name, len);
695
0
  if(!zname)
696
0
    return CURLE_OUT_OF_MEMORY;
697
0
  result = curl_mime_name(part, zname);
698
0
  curlx_free(zname);
699
0
  return result;
700
0
}
701
702
/*
703
 * Curl_getformdata() converts a linked list of "meta data" into a mime
704
 * structure. The input list is in 'post', while the output is stored in
705
 * mime part at '*finalform'.
706
 *
707
 * This function will not do a failf() for the potential memory failures but
708
 * should for all other errors it spots. Note that this function MAY get a
709
 * NULL pointer in the 'data' argument.
710
 */
711
712
CURLcode Curl_getformdata(CURL *data,
713
                          curl_mimepart *finalform,
714
                          struct curl_httppost *post,
715
                          curl_read_callback fread_func)
716
0
{
717
0
  CURLcode result = CURLE_OK;
718
0
  curl_mime *form = NULL;
719
0
  curl_mimepart *part;
720
0
  struct curl_httppost *file;
721
722
0
  Curl_mime_cleanpart(finalform); /* default form is empty */
723
724
0
  if(!post)
725
0
    return result; /* no input => no output! */
726
727
0
  form = curl_mime_init(data);
728
0
  if(!form)
729
0
    result = CURLE_OUT_OF_MEMORY;
730
731
0
  if(!result)
732
0
    result = curl_mime_subparts(finalform, form);
733
734
  /* Process each top part. */
735
0
  for(; !result && post; post = post->next) {
736
    /* If we have more than a file here, create a mime subpart and fill it. */
737
0
    curl_mime *multipart = form;
738
0
    if(post->more) {
739
0
      part = curl_mime_addpart(form);
740
0
      if(!part)
741
0
        result = CURLE_OUT_OF_MEMORY;
742
0
      if(!result)
743
0
        result = setname(part, post->name, post->namelength);
744
0
      if(!result) {
745
0
        multipart = curl_mime_init(data);
746
0
        if(!multipart)
747
0
          result = CURLE_OUT_OF_MEMORY;
748
0
      }
749
0
      if(!result)
750
0
        result = curl_mime_subparts(part, multipart);
751
0
    }
752
753
    /* Generate all the part contents. */
754
0
    for(file = post; !result && file; file = file->more) {
755
      /* Create the part. */
756
0
      part = curl_mime_addpart(multipart);
757
0
      if(!part)
758
0
        result = CURLE_OUT_OF_MEMORY;
759
760
      /* Set the headers. */
761
0
      if(!result)
762
0
        result = curl_mime_headers(part, file->contentheader, 0);
763
764
      /* Set the content type. */
765
0
      if(!result && file->contenttype)
766
0
        result = curl_mime_type(part, file->contenttype);
767
768
      /* Set field name. */
769
0
      if(!result && !post->more)
770
0
        result = setname(part, post->name, post->namelength);
771
772
      /* Process contents. */
773
0
      if(!result) {
774
0
        curl_off_t clen = post->contentslength;
775
776
0
        if(post->flags & CURL_HTTPPOST_LARGE)
777
0
          clen = post->contentlen;
778
779
0
        if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) {
780
0
          if(!strcmp(file->contents, "-")) {
781
            /* There are a few cases where the code below will not work; in
782
               particular, freopen(stdin) by the caller is not guaranteed
783
               to result as expected. This feature has been kept for backward
784
               compatibility: use of "-" pseudo filename should be avoided. */
785
0
#if defined(__clang__) && __clang_major__ >= 16
786
0
#pragma clang diagnostic push
787
0
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
788
0
#endif
789
0
            result = curl_mime_data_cb(part, (curl_off_t)-1,
790
0
                                       (curl_read_callback)fread,
791
0
                                       curlx_fseek,
792
0
                                       NULL, (void *)stdin);
793
0
#if defined(__clang__) && __clang_major__ >= 16
794
0
#pragma clang diagnostic pop
795
0
#endif
796
0
          }
797
0
          else
798
0
            result = curl_mime_filedata(part, file->contents);
799
0
          if(!result && (post->flags & HTTPPOST_READFILE))
800
0
            result = curl_mime_filename(part, NULL);
801
0
        }
802
0
        else if(post->flags & HTTPPOST_BUFFER)
803
0
          result = curl_mime_data(part, post->buffer,
804
0
                                  post->bufferlength ?
805
0
                                  post->bufferlength : -1);
806
0
        else if(post->flags & HTTPPOST_CALLBACK) {
807
          /* the contents should be read with the callback and the size is set
808
             with the contentslength */
809
0
          if(!clen)
810
0
            clen = -1;
811
0
          result = curl_mime_data_cb(part, clen,
812
0
                                     fread_func, NULL, NULL, post->userp);
813
0
        }
814
0
        else {
815
0
          size_t uclen;
816
0
          if(!clen)
817
0
            uclen = CURL_ZERO_TERMINATED;
818
0
          else
819
0
            uclen = (size_t)clen;
820
0
          result = curl_mime_data(part, post->contents, uclen);
821
0
        }
822
0
      }
823
824
      /* Set fake filename. */
825
0
      if(!result && post->showfilename &&
826
0
         (post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER |
827
0
                                        HTTPPOST_CALLBACK))))
828
0
        result = curl_mime_filename(part, post->showfilename);
829
0
    }
830
0
  }
831
832
0
  if(result)
833
0
    Curl_mime_cleanpart(finalform);
834
835
0
  return result;
836
0
}
837
838
#else /* if disabled */
839
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
840
                          struct curl_httppost **last_post, ...)
841
{
842
  (void)httppost;
843
  (void)last_post;
844
  return CURL_FORMADD_DISABLED;
845
}
846
847
int curl_formget(struct curl_httppost *form, void *arg,
848
                 curl_formget_callback append)
849
{
850
  (void)form;
851
  (void)arg;
852
  (void)append;
853
  return CURL_FORMADD_DISABLED;
854
}
855
856
void curl_formfree(struct curl_httppost *form)
857
{
858
  (void)form;
859
  /* Nothing to do. */
860
}
861
862
#endif /* if disabled */