Coverage Report

Created: 2025-08-26 07:08

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