Coverage Report

Created: 2026-02-26 06:32

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