Coverage Report

Created: 2022-10-16 06:45

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