2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
2004-06-03 09:03:57 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 09:20:26 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2018-02-04 03:23:11 -05:00
|
|
|
* Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2004-06-03 09:03:57 -04:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2004-06-10 03:46:24 -04:00
|
|
|
#include <curl/curl.h>
|
2000-08-24 10:26:33 -04:00
|
|
|
|
2014-09-13 13:09:56 -04:00
|
|
|
#ifndef CURL_DISABLE_HTTP
|
2002-06-11 07:13:01 -04:00
|
|
|
|
2004-10-05 02:55:09 -04:00
|
|
|
#if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
|
2004-10-01 02:36:11 -04:00
|
|
|
#include <libgen.h>
|
|
|
|
#endif
|
2011-07-24 22:08:08 -04:00
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
#include "urldata.h" /* for struct Curl_easy */
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "formdata.h"
|
2017-09-02 12:47:10 -04:00
|
|
|
#include "mime.h"
|
|
|
|
#include "non-ascii.h"
|
2013-12-17 17:32:47 -05:00
|
|
|
#include "vtls/vtls.h"
|
2016-09-30 11:15:05 -04:00
|
|
|
#include "strcase.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "sendf.h"
|
2014-10-17 06:59:32 -04:00
|
|
|
#include "strdup.h"
|
2016-11-11 08:53:36 -05:00
|
|
|
#include "rand.h"
|
2018-08-09 11:19:24 -04:00
|
|
|
#include "warnless.h"
|
2016-04-29 09:46:40 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2015-03-24 18:12:03 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2000-10-09 07:12:34 -04:00
|
|
|
|
2006-06-24 17:46:41 -04:00
|
|
|
|
2015-10-31 17:48:21 -04:00
|
|
|
#define HTTPPOST_PTRNAME CURL_HTTPPOST_PTRNAME
|
|
|
|
#define HTTPPOST_FILENAME CURL_HTTPPOST_FILENAME
|
|
|
|
#define HTTPPOST_PTRCONTENTS CURL_HTTPPOST_PTRCONTENTS
|
|
|
|
#define HTTPPOST_READFILE CURL_HTTPPOST_READFILE
|
|
|
|
#define HTTPPOST_PTRBUFFER CURL_HTTPPOST_PTRBUFFER
|
|
|
|
#define HTTPPOST_CALLBACK CURL_HTTPPOST_CALLBACK
|
|
|
|
#define HTTPPOST_BUFFER CURL_HTTPPOST_BUFFER
|
|
|
|
|
2001-08-21 09:18:07 -04:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* AddHttpPost()
|
2004-06-03 09:03:57 -04:00
|
|
|
*
|
2001-08-21 09:18:07 -04:00
|
|
|
* Adds a HttpPost structure to the list, if parent_post is given becomes
|
|
|
|
* a subpost of parent_post instead of a direct list element.
|
|
|
|
*
|
2001-08-28 04:54:33 -04:00
|
|
|
* Returns newly allocated HttpPost on success and NULL if malloc failed.
|
2001-08-21 09:18:07 -04:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
2002-03-11 10:18:59 -05:00
|
|
|
static struct curl_httppost *
|
2008-03-31 06:02:23 -04:00
|
|
|
AddHttpPost(char *name, size_t namelength,
|
2015-10-23 18:52:25 -04:00
|
|
|
char *value, curl_off_t contentslength,
|
2008-03-31 06:02:23 -04:00
|
|
|
char *buffer, size_t bufferlength,
|
2002-03-11 10:18:59 -05:00
|
|
|
char *contenttype,
|
|
|
|
long flags,
|
2016-11-23 01:53:24 -05:00
|
|
|
struct curl_slist *contentHeader,
|
2008-03-31 06:02:23 -04:00
|
|
|
char *showfilename, char *userp,
|
2002-03-11 10:18:59 -05:00
|
|
|
struct curl_httppost *parent_post,
|
|
|
|
struct curl_httppost **httppost,
|
|
|
|
struct curl_httppost **last_post)
|
2001-08-21 09:18:07 -04:00
|
|
|
{
|
2002-03-11 10:18:59 -05:00
|
|
|
struct curl_httppost *post;
|
2009-11-18 05:33:54 -05:00
|
|
|
post = calloc(1, sizeof(struct curl_httppost));
|
2001-08-21 09:18:07 -04:00
|
|
|
if(post) {
|
|
|
|
post->name = name;
|
2004-02-27 08:21:14 -05:00
|
|
|
post->namelength = (long)(name?(namelength?namelength:strlen(name)):0);
|
2001-08-21 09:18:07 -04:00
|
|
|
post->contents = value;
|
2015-10-23 18:52:25 -04:00
|
|
|
post->contentlen = contentslength;
|
2002-06-12 17:40:59 -04:00
|
|
|
post->buffer = buffer;
|
2004-03-03 08:11:28 -05:00
|
|
|
post->bufferlength = (long)bufferlength;
|
2001-08-28 04:54:33 -04:00
|
|
|
post->contenttype = contenttype;
|
2001-12-14 07:59:16 -05:00
|
|
|
post->contentheader = contentHeader;
|
2002-03-11 10:18:59 -05:00
|
|
|
post->showfilename = showfilename;
|
2017-05-11 04:01:58 -04:00
|
|
|
post->userp = userp;
|
2015-10-23 18:52:25 -04:00
|
|
|
post->flags = flags | CURL_HTTPPOST_LARGE;
|
2001-08-21 09:18:07 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(parent_post) {
|
2001-08-21 09:18:07 -04:00
|
|
|
/* now, point our 'more' to the original 'more' */
|
|
|
|
post->more = parent_post->more;
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2001-08-21 09:18:07 -04:00
|
|
|
/* then move the original 'more' to point to ourselves */
|
2004-06-03 09:03:57 -04:00
|
|
|
parent_post->more = post;
|
2001-08-21 09:18:07 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* make the previous point to this */
|
|
|
|
if(*last_post)
|
|
|
|
(*last_post)->next = post;
|
|
|
|
else
|
|
|
|
(*httppost) = post;
|
2004-06-03 09:03:57 -04:00
|
|
|
|
|
|
|
(*last_post) = post;
|
2001-08-21 09:18:07 -04:00
|
|
|
}
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
2001-08-28 04:54:33 -04:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* AddFormInfo()
|
2004-06-03 09:03:57 -04:00
|
|
|
*
|
2001-08-28 04:54:33 -04:00
|
|
|
* Adds a FormInfo structure to the list presented by parent_form_info.
|
|
|
|
*
|
|
|
|
* Returns newly allocated FormInfo on success and NULL if malloc failed/
|
|
|
|
* parent_form_info is NULL.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2001-10-03 03:54:42 -04:00
|
|
|
static FormInfo * AddFormInfo(char *value,
|
|
|
|
char *contenttype,
|
|
|
|
FormInfo *parent_form_info)
|
2001-08-28 04:54:33 -04:00
|
|
|
{
|
|
|
|
FormInfo *form_info;
|
2009-11-18 05:33:54 -05:00
|
|
|
form_info = calloc(1, sizeof(struct FormInfo));
|
2001-08-28 04:54:33 -04:00
|
|
|
if(form_info) {
|
2007-11-05 04:45:09 -05:00
|
|
|
if(value)
|
2001-08-28 04:54:33 -04:00
|
|
|
form_info->value = value;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(contenttype)
|
2001-08-28 04:54:33 -04:00
|
|
|
form_info->contenttype = contenttype;
|
|
|
|
form_info->flags = HTTPPOST_FILENAME;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(parent_form_info) {
|
2001-08-28 04:54:33 -04:00
|
|
|
/* now, point our 'more' to the original 'more' */
|
|
|
|
form_info->more = parent_form_info->more;
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2001-08-28 04:54:33 -04:00
|
|
|
/* then move the original 'more' to point to ourselves */
|
|
|
|
parent_form_info->more = form_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
return form_info;
|
|
|
|
}
|
|
|
|
|
2001-08-21 09:18:07 -04:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* FormAdd()
|
2004-06-03 09:03:57 -04:00
|
|
|
*
|
2004-05-04 05:31:04 -04:00
|
|
|
* Stores a formpost parameter and builds the appropriate linked list.
|
2001-08-21 09:18:07 -04:00
|
|
|
*
|
|
|
|
* Has two principal functionalities: using files and byte arrays as
|
|
|
|
* post parts. Byte arrays are either copied or just the pointer is stored
|
|
|
|
* (as the user requests) while for files only the filename and not the
|
|
|
|
* content is stored.
|
|
|
|
*
|
|
|
|
* While you may have only one byte array for each name, multiple filenames
|
|
|
|
* are allowed (and because of this feature CURLFORM_END is needed after
|
|
|
|
* using CURLFORM_FILE).
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* Simple name/value pair with copied contents:
|
|
|
|
* curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
|
2001-10-03 03:54:42 -04:00
|
|
|
* CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
|
2001-08-21 09:18:07 -04:00
|
|
|
*
|
|
|
|
* name/value pair where only the content pointer is remembered:
|
|
|
|
* curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
|
2001-10-03 03:54:42 -04:00
|
|
|
* CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
|
2001-08-21 09:18:07 -04:00
|
|
|
* (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
|
|
|
|
*
|
|
|
|
* storing a filename (CONTENTTYPE is optional!):
|
|
|
|
* curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
|
|
|
|
* CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
|
|
|
|
* CURLFORM_END);
|
|
|
|
*
|
|
|
|
* storing multiple filenames:
|
|
|
|
* curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
|
|
|
|
* CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
|
|
|
|
*
|
2001-10-03 03:54:42 -04:00
|
|
|
* Returns:
|
2002-05-21 03:44:27 -04:00
|
|
|
* CURL_FORMADD_OK on success
|
|
|
|
* CURL_FORMADD_MEMORY if the FormInfo allocation fails
|
|
|
|
* CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
|
|
|
|
* CURL_FORMADD_NULL if a null pointer was given for a char
|
|
|
|
* CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
|
|
|
|
* CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
|
2011-04-20 09:17:42 -04:00
|
|
|
* CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
|
2002-05-21 03:44:27 -04:00
|
|
|
* CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
|
|
|
|
* CURL_FORMADD_MEMORY if some allocation for string copying failed.
|
|
|
|
* CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
|
2001-08-21 09:18:07 -04:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
static
|
2002-05-21 03:44:27 -04:00
|
|
|
CURLFORMcode FormAdd(struct curl_httppost **httppost,
|
|
|
|
struct curl_httppost **last_post,
|
|
|
|
va_list params)
|
2001-08-21 09:18:07 -04:00
|
|
|
{
|
2004-05-12 05:02:23 -04:00
|
|
|
FormInfo *first_form, *current_form, *form = NULL;
|
2002-05-21 03:44:27 -04:00
|
|
|
CURLFORMcode return_value = CURL_FORMADD_OK;
|
2001-08-21 09:18:07 -04:00
|
|
|
const char *prevtype = NULL;
|
2002-03-11 10:18:59 -05:00
|
|
|
struct curl_httppost *post = NULL;
|
2001-10-03 03:54:42 -04:00
|
|
|
CURLformoption option;
|
|
|
|
struct curl_forms *forms = NULL;
|
2017-09-09 17:09:06 -04:00
|
|
|
char *array_value = NULL; /* value read from an array */
|
2001-10-03 03:54:42 -04:00
|
|
|
|
|
|
|
/* This is a state variable, that if TRUE means that we're parsing an
|
|
|
|
array that we got passed to us. If FALSE we're parsing the input
|
|
|
|
va_list arguments. */
|
|
|
|
bool array_state = FALSE;
|
2001-08-21 09:18:07 -04:00
|
|
|
|
2001-10-03 03:54:42 -04:00
|
|
|
/*
|
|
|
|
* We need to allocate the first struct to fill in.
|
|
|
|
*/
|
2009-11-18 05:33:54 -05:00
|
|
|
first_form = calloc(1, sizeof(struct FormInfo));
|
2004-05-11 10:48:53 -04:00
|
|
|
if(!first_form)
|
2002-05-21 03:44:27 -04:00
|
|
|
return CURL_FORMADD_MEMORY;
|
2001-08-21 09:18:07 -04:00
|
|
|
|
2004-05-11 10:48:53 -04:00
|
|
|
current_form = first_form;
|
|
|
|
|
2001-10-03 03:54:42 -04:00
|
|
|
/*
|
2004-05-11 10:48:53 -04:00
|
|
|
* Loop through all the options set. Break if we have an error to report.
|
2001-10-03 03:54:42 -04:00
|
|
|
*/
|
2007-11-05 04:45:09 -05:00
|
|
|
while(return_value == CURL_FORMADD_OK) {
|
2001-10-03 03:54:42 -04:00
|
|
|
|
|
|
|
/* first see if we have more parts of the array param */
|
2011-04-22 17:01:30 -04:00
|
|
|
if(array_state && forms) {
|
2001-10-03 03:54:42 -04:00
|
|
|
/* get the upcoming option from the given array */
|
2001-10-03 05:31:16 -04:00
|
|
|
option = forms->option;
|
2002-03-19 02:32:35 -05:00
|
|
|
array_value = (char *)forms->value;
|
2001-10-03 05:31:16 -04:00
|
|
|
|
|
|
|
forms++; /* advance this to next entry */
|
2007-11-05 04:45:09 -05:00
|
|
|
if(CURLFORM_END == option) {
|
2001-10-03 03:54:42 -04:00
|
|
|
/* end of array state */
|
|
|
|
array_state = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* This is not array-state, get next option */
|
|
|
|
option = va_arg(params, CURLformoption);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(CURLFORM_END == option)
|
2001-08-21 09:18:07 -04:00
|
|
|
break;
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(option) {
|
2001-10-03 03:54:42 -04:00
|
|
|
case CURLFORM_ARRAY:
|
2002-03-13 07:10:20 -05:00
|
|
|
if(array_state)
|
|
|
|
/* we don't support an array from within an array */
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_ILLEGAL_ARRAY;
|
2002-03-13 07:10:20 -05:00
|
|
|
else {
|
|
|
|
forms = va_arg(params, struct curl_forms *);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(forms)
|
2002-03-13 07:10:20 -05:00
|
|
|
array_state = TRUE;
|
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_NULL;
|
2002-03-13 07:10:20 -05:00
|
|
|
}
|
2001-10-03 03:54:42 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the Name property.
|
|
|
|
*/
|
|
|
|
case CURLFORM_PTRNAME:
|
2007-01-14 09:57:51 -05:00
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
2011-04-19 18:48:20 -04:00
|
|
|
/* Treat CURLFORM_PTR like CURLFORM_COPYNAME so that libcurl will copy
|
|
|
|
* the data in all cases so that we'll have safe memory for the eventual
|
|
|
|
* conversion.
|
|
|
|
*/
|
2007-01-14 09:57:51 -05:00
|
|
|
#else
|
2001-10-03 03:54:42 -04:00
|
|
|
current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
|
2007-01-14 09:57:51 -05:00
|
|
|
#endif
|
2017-02-28 16:45:28 -05:00
|
|
|
/* FALLTHROUGH */
|
2001-10-03 03:54:42 -04:00
|
|
|
case CURLFORM_COPYNAME:
|
2007-11-05 04:45:09 -05:00
|
|
|
if(current_form->name)
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2001-10-03 03:54:42 -04:00
|
|
|
else {
|
2002-03-19 02:32:35 -05:00
|
|
|
char *name = array_state?
|
|
|
|
array_value:va_arg(params, char *);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(name)
|
2001-10-03 03:54:42 -04:00
|
|
|
current_form->name = name; /* store for the moment */
|
2001-08-28 04:54:33 -04:00
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_NULL;
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CURLFORM_NAMELENGTH:
|
2007-11-05 04:45:09 -05:00
|
|
|
if(current_form->namelength)
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2001-10-03 03:54:42 -04:00
|
|
|
else
|
2002-03-13 07:10:20 -05:00
|
|
|
current_form->namelength =
|
2007-08-27 02:31:28 -04:00
|
|
|
array_state?(size_t)array_value:(size_t)va_arg(params, long);
|
2001-10-03 03:54:42 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the contents property.
|
|
|
|
*/
|
|
|
|
case CURLFORM_PTRCONTENTS:
|
2018-07-09 12:52:05 -04:00
|
|
|
current_form->flags |= HTTPPOST_PTRCONTENTS;
|
|
|
|
/* FALLTHROUGH */
|
2001-10-03 03:54:42 -04:00
|
|
|
case CURLFORM_COPYCONTENTS:
|
2007-11-05 04:45:09 -05:00
|
|
|
if(current_form->value)
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2001-10-03 03:54:42 -04:00
|
|
|
else {
|
2002-03-19 02:32:35 -05:00
|
|
|
char *value =
|
|
|
|
array_state?array_value:va_arg(params, char *);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(value)
|
2001-10-03 03:54:42 -04:00
|
|
|
current_form->value = value; /* store for the moment */
|
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_NULL;
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CURLFORM_CONTENTSLENGTH:
|
2015-10-23 18:52:25 -04:00
|
|
|
current_form->contentslength =
|
|
|
|
array_state?(size_t)array_value:(size_t)va_arg(params, long);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLFORM_CONTENTLEN:
|
|
|
|
current_form->flags |= CURL_HTTPPOST_LARGE;
|
|
|
|
current_form->contentslength =
|
2016-03-18 03:19:31 -04:00
|
|
|
array_state?(curl_off_t)(size_t)array_value:va_arg(params, curl_off_t);
|
2001-10-03 03:54:42 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Get contents from a given file name */
|
|
|
|
case CURLFORM_FILECONTENT:
|
2013-07-23 16:10:44 -04:00
|
|
|
if(current_form->flags & (HTTPPOST_PTRCONTENTS|HTTPPOST_READFILE))
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2001-10-03 03:54:42 -04:00
|
|
|
else {
|
2007-08-27 02:31:28 -04:00
|
|
|
const char *filename = array_state?
|
2002-03-13 07:10:20 -05:00
|
|
|
array_value:va_arg(params, char *);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(filename) {
|
2001-10-03 03:54:42 -04:00
|
|
|
current_form->value = strdup(filename);
|
2004-05-12 05:02:23 -04:00
|
|
|
if(!current_form->value)
|
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
2004-05-13 10:13:12 -04:00
|
|
|
else {
|
2004-05-12 05:02:23 -04:00
|
|
|
current_form->flags |= HTTPPOST_READFILE;
|
2004-05-13 10:13:12 -04:00
|
|
|
current_form->value_alloc = TRUE;
|
|
|
|
}
|
2001-08-21 09:18:07 -04:00
|
|
|
}
|
2001-08-28 04:54:33 -04:00
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_NULL;
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* We upload a file */
|
|
|
|
case CURLFORM_FILE:
|
|
|
|
{
|
2007-08-27 02:31:28 -04:00
|
|
|
const char *filename = array_state?array_value:
|
2002-03-19 02:32:35 -05:00
|
|
|
va_arg(params, char *);
|
2002-03-13 07:10:20 -05:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(current_form->value) {
|
|
|
|
if(current_form->flags & HTTPPOST_FILENAME) {
|
|
|
|
if(filename) {
|
2011-12-21 09:38:47 -05:00
|
|
|
char *fname = strdup(filename);
|
|
|
|
if(!fname)
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
2011-12-21 09:38:47 -05:00
|
|
|
else {
|
|
|
|
form = AddFormInfo(fname, NULL, current_form);
|
|
|
|
if(!form) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(fname);
|
2011-12-21 09:38:47 -05:00
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
form->value_alloc = TRUE;
|
|
|
|
current_form = form;
|
|
|
|
form = NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_NULL;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2001-08-21 09:18:07 -04:00
|
|
|
}
|
|
|
|
else {
|
2007-11-05 04:45:09 -05:00
|
|
|
if(filename) {
|
2001-10-03 03:54:42 -04:00
|
|
|
current_form->value = strdup(filename);
|
2004-05-12 05:02:23 -04:00
|
|
|
if(!current_form->value)
|
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
else {
|
|
|
|
current_form->flags |= HTTPPOST_FILENAME;
|
|
|
|
current_form->value_alloc = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2001-10-03 03:54:42 -04:00
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_NULL;
|
2001-08-21 09:18:07 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-06-12 17:40:59 -04:00
|
|
|
|
|
|
|
case CURLFORM_BUFFERPTR:
|
2011-08-08 04:21:03 -04:00
|
|
|
current_form->flags |= HTTPPOST_PTRBUFFER|HTTPPOST_BUFFER;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(current_form->buffer)
|
2002-06-12 17:40:59 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
|
|
|
else {
|
|
|
|
char *buffer =
|
|
|
|
array_state?array_value:va_arg(params, char *);
|
2011-08-08 04:21:03 -04:00
|
|
|
if(buffer) {
|
2002-06-12 17:40:59 -04:00
|
|
|
current_form->buffer = buffer; /* store for the moment */
|
2011-08-08 04:21:03 -04:00
|
|
|
current_form->value = buffer; /* make it non-NULL to be accepted
|
|
|
|
as fine */
|
|
|
|
}
|
2002-06-12 17:40:59 -04:00
|
|
|
else
|
|
|
|
return_value = CURL_FORMADD_NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLFORM_BUFFERLENGTH:
|
2007-11-05 04:45:09 -05:00
|
|
|
if(current_form->bufferlength)
|
2002-06-12 17:40:59 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
|
|
|
else
|
|
|
|
current_form->bufferlength =
|
2007-08-27 02:31:28 -04:00
|
|
|
array_state?(size_t)array_value:(size_t)va_arg(params, long);
|
2002-06-12 17:40:59 -04:00
|
|
|
break;
|
|
|
|
|
2008-03-31 06:02:23 -04:00
|
|
|
case CURLFORM_STREAM:
|
|
|
|
current_form->flags |= HTTPPOST_CALLBACK;
|
|
|
|
if(current_form->userp)
|
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
|
|
|
else {
|
|
|
|
char *userp =
|
|
|
|
array_state?array_value:va_arg(params, char *);
|
|
|
|
if(userp) {
|
|
|
|
current_form->userp = userp;
|
|
|
|
current_form->value = userp; /* this isn't strictly true but we
|
|
|
|
derive a value from this later on
|
|
|
|
and we need this non-NULL to be
|
|
|
|
accepted as a fine form part */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return_value = CURL_FORMADD_NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-10-03 03:54:42 -04:00
|
|
|
case CURLFORM_CONTENTTYPE:
|
|
|
|
{
|
2007-08-27 02:31:28 -04:00
|
|
|
const char *contenttype =
|
2002-03-19 02:32:35 -05:00
|
|
|
array_state?array_value:va_arg(params, char *);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(current_form->contenttype) {
|
|
|
|
if(current_form->flags & HTTPPOST_FILENAME) {
|
|
|
|
if(contenttype) {
|
2011-12-21 09:38:47 -05:00
|
|
|
char *type = strdup(contenttype);
|
|
|
|
if(!type)
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
2011-12-21 09:38:47 -05:00
|
|
|
else {
|
|
|
|
form = AddFormInfo(NULL, type, current_form);
|
|
|
|
if(!form) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(type);
|
2011-12-21 09:38:47 -05:00
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
form->contenttype_alloc = TRUE;
|
|
|
|
current_form = form;
|
|
|
|
form = NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
else
|
|
|
|
return_value = CURL_FORMADD_NULL;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
|
|
|
else
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
|
|
|
else {
|
2007-11-05 04:45:09 -05:00
|
|
|
if(contenttype) {
|
2004-10-06 03:50:18 -04:00
|
|
|
current_form->contenttype = strdup(contenttype);
|
2004-05-12 05:02:23 -04:00
|
|
|
if(!current_form->contenttype)
|
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
else
|
|
|
|
current_form->contenttype_alloc = TRUE;
|
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
else
|
|
|
|
return_value = CURL_FORMADD_NULL;
|
|
|
|
}
|
2001-08-21 09:18:07 -04:00
|
|
|
break;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
2001-12-14 07:59:16 -05:00
|
|
|
case CURLFORM_CONTENTHEADER:
|
|
|
|
{
|
2002-03-19 02:32:35 -05:00
|
|
|
/* this "cast increases required alignment of target type" but
|
|
|
|
we consider it OK anyway */
|
2016-11-23 01:53:24 -05:00
|
|
|
struct curl_slist *list = array_state?
|
|
|
|
(struct curl_slist *)(void *)array_value:
|
|
|
|
va_arg(params, struct curl_slist *);
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2011-04-22 17:01:30 -04:00
|
|
|
if(current_form->contentheader)
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2001-12-14 07:59:16 -05:00
|
|
|
else
|
|
|
|
current_form->contentheader = list;
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2001-12-14 07:59:16 -05:00
|
|
|
break;
|
|
|
|
}
|
2002-03-11 10:18:59 -05:00
|
|
|
case CURLFORM_FILENAME:
|
2011-08-08 04:21:03 -04:00
|
|
|
case CURLFORM_BUFFER:
|
2002-03-11 10:18:59 -05:00
|
|
|
{
|
2007-08-27 02:31:28 -04:00
|
|
|
const char *filename = array_state?array_value:
|
2002-03-11 10:18:59 -05:00
|
|
|
va_arg(params, char *);
|
2011-04-22 17:01:30 -04:00
|
|
|
if(current_form->showfilename)
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_OPTION_TWICE;
|
2004-05-12 05:02:23 -04:00
|
|
|
else {
|
2002-03-11 10:18:59 -05:00
|
|
|
current_form->showfilename = strdup(filename);
|
2004-05-12 05:02:23 -04:00
|
|
|
if(!current_form->showfilename)
|
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
else
|
|
|
|
current_form->showfilename_alloc = TRUE;
|
|
|
|
}
|
2002-03-11 10:18:59 -05:00
|
|
|
break;
|
|
|
|
}
|
2001-10-03 03:54:42 -04:00
|
|
|
default:
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_UNKNOWN_OPTION;
|
2011-08-08 04:21:03 -04:00
|
|
|
break;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
|
|
|
|
2011-12-21 09:38:47 -05:00
|
|
|
if(CURL_FORMADD_OK != return_value) {
|
|
|
|
/* On error, free allocated fields for all nodes of the FormInfo linked
|
|
|
|
list without deallocating nodes. List nodes are deallocated later on */
|
|
|
|
FormInfo *ptr;
|
|
|
|
for(ptr = first_form; ptr != NULL; ptr = ptr->more) {
|
|
|
|
if(ptr->name_alloc) {
|
|
|
|
Curl_safefree(ptr->name);
|
|
|
|
ptr->name_alloc = FALSE;
|
|
|
|
}
|
|
|
|
if(ptr->value_alloc) {
|
|
|
|
Curl_safefree(ptr->value);
|
|
|
|
ptr->value_alloc = FALSE;
|
|
|
|
}
|
|
|
|
if(ptr->contenttype_alloc) {
|
|
|
|
Curl_safefree(ptr->contenttype);
|
|
|
|
ptr->contenttype_alloc = FALSE;
|
|
|
|
}
|
|
|
|
if(ptr->showfilename_alloc) {
|
|
|
|
Curl_safefree(ptr->showfilename);
|
|
|
|
ptr->showfilename_alloc = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-21 03:44:27 -04:00
|
|
|
if(CURL_FORMADD_OK == return_value) {
|
2008-07-08 17:16:18 -04:00
|
|
|
/* go through the list, check for completeness and if everything is
|
2001-10-03 03:54:42 -04:00
|
|
|
* alright add the HttpPost item otherwise set return_value accordingly */
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2001-10-29 08:21:25 -05:00
|
|
|
post = NULL;
|
|
|
|
for(form = first_form;
|
|
|
|
form != NULL;
|
|
|
|
form = form->more) {
|
2011-04-22 17:01:30 -04:00
|
|
|
if(((!form->name || !form->value) && !post) ||
|
|
|
|
( (form->contentslength) &&
|
|
|
|
(form->flags & HTTPPOST_FILENAME) ) ||
|
|
|
|
( (form->flags & HTTPPOST_FILENAME) &&
|
|
|
|
(form->flags & HTTPPOST_PTRCONTENTS) ) ||
|
|
|
|
|
|
|
|
( (!form->buffer) &&
|
|
|
|
(form->flags & HTTPPOST_BUFFER) &&
|
|
|
|
(form->flags & HTTPPOST_PTRBUFFER) ) ||
|
|
|
|
|
|
|
|
( (form->flags & HTTPPOST_READFILE) &&
|
|
|
|
(form->flags & HTTPPOST_PTRCONTENTS) )
|
2008-03-31 06:02:23 -04:00
|
|
|
) {
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_INCOMPLETE;
|
2001-10-03 03:54:42 -04:00
|
|
|
break;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(((form->flags & HTTPPOST_FILENAME) ||
|
|
|
|
(form->flags & HTTPPOST_BUFFER)) &&
|
|
|
|
!form->contenttype) {
|
|
|
|
char *f = form->flags & HTTPPOST_BUFFER?
|
|
|
|
form->showfilename : form->value;
|
2018-02-04 03:23:11 -05:00
|
|
|
char const *type;
|
|
|
|
type = Curl_mime_contenttype(f);
|
|
|
|
if(!type)
|
|
|
|
type = prevtype;
|
|
|
|
if(!type)
|
|
|
|
type = FILE_CONTENTTYPE_DEFAULT;
|
2017-03-10 08:28:37 -05:00
|
|
|
|
|
|
|
/* our contenttype is missing */
|
2018-02-04 03:23:11 -05:00
|
|
|
form->contenttype = strdup(type);
|
2017-03-10 08:28:37 -05:00
|
|
|
if(!form->contenttype) {
|
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
break;
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
form->contenttype_alloc = TRUE;
|
|
|
|
}
|
2017-09-21 20:08:29 -04:00
|
|
|
if(form->name && form->namelength) {
|
|
|
|
/* Name should not contain nul bytes. */
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < form->namelength; i++)
|
|
|
|
if(!form->name[i]) {
|
|
|
|
return_value = CURL_FORMADD_NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(return_value != CURL_FORMADD_OK)
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(!(form->flags & HTTPPOST_PTRNAME) &&
|
|
|
|
(form == first_form) ) {
|
|
|
|
/* Note that there's small risk that form->name is NULL here if the
|
|
|
|
app passed in a bad combo, so we better check for that first. */
|
|
|
|
if(form->name) {
|
2017-09-21 20:08:29 -04:00
|
|
|
/* copy name (without strdup; possibly not nul-terminated) */
|
2017-03-10 08:28:37 -05:00
|
|
|
form->name = Curl_memdup(form->name, form->namelength?
|
|
|
|
form->namelength:
|
2017-09-09 17:55:08 -04:00
|
|
|
strlen(form->name) + 1);
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(!form->name) {
|
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
form->name_alloc = TRUE;
|
|
|
|
}
|
|
|
|
if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
|
|
|
|
HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
|
|
|
|
HTTPPOST_CALLBACK)) && form->value) {
|
|
|
|
/* copy value (without strdup; possibly contains null characters) */
|
|
|
|
size_t clen = (size_t) form->contentslength;
|
|
|
|
if(!clen)
|
2017-09-09 17:55:08 -04:00
|
|
|
clen = strlen(form->value) + 1;
|
2015-10-23 18:52:25 -04:00
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
form->value = Curl_memdup(form->value, clen);
|
2015-10-23 18:52:25 -04:00
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
if(!form->value) {
|
2002-05-21 03:44:27 -04:00
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
2004-05-11 10:48:53 -04:00
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
form->value_alloc = TRUE;
|
|
|
|
}
|
|
|
|
post = AddHttpPost(form->name, form->namelength,
|
|
|
|
form->value, form->contentslength,
|
|
|
|
form->buffer, form->bufferlength,
|
|
|
|
form->contenttype, form->flags,
|
|
|
|
form->contentheader, form->showfilename,
|
|
|
|
form->userp,
|
|
|
|
post, httppost,
|
|
|
|
last_post);
|
|
|
|
|
|
|
|
if(!post) {
|
|
|
|
return_value = CURL_FORMADD_MEMORY;
|
|
|
|
break;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
|
|
|
|
if(form->contenttype)
|
|
|
|
prevtype = form->contenttype;
|
2001-08-28 04:54:33 -04:00
|
|
|
}
|
2011-12-21 09:38:47 -05:00
|
|
|
if(CURL_FORMADD_OK != return_value) {
|
|
|
|
/* On error, free allocated fields for nodes of the FormInfo linked
|
|
|
|
list which are not already owned by the httppost linked list
|
|
|
|
without deallocating nodes. List nodes are deallocated later on */
|
|
|
|
FormInfo *ptr;
|
|
|
|
for(ptr = form; ptr != NULL; ptr = ptr->more) {
|
|
|
|
if(ptr->name_alloc) {
|
|
|
|
Curl_safefree(ptr->name);
|
|
|
|
ptr->name_alloc = FALSE;
|
|
|
|
}
|
|
|
|
if(ptr->value_alloc) {
|
|
|
|
Curl_safefree(ptr->value);
|
|
|
|
ptr->value_alloc = FALSE;
|
|
|
|
}
|
|
|
|
if(ptr->contenttype_alloc) {
|
|
|
|
Curl_safefree(ptr->contenttype);
|
|
|
|
ptr->contenttype_alloc = FALSE;
|
|
|
|
}
|
|
|
|
if(ptr->showfilename_alloc) {
|
|
|
|
Curl_safefree(ptr->showfilename);
|
|
|
|
ptr->showfilename_alloc = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2004-05-12 05:02:23 -04:00
|
|
|
}
|
2004-05-11 10:48:53 -04:00
|
|
|
}
|
|
|
|
|
2011-12-21 09:38:47 -05:00
|
|
|
/* Always deallocate FormInfo linked list nodes without touching node
|
|
|
|
fields given that these have either been deallocated or are owned
|
|
|
|
now by the httppost linked list */
|
|
|
|
while(first_form) {
|
|
|
|
FormInfo *ptr = first_form->more;
|
2015-03-16 10:01:15 -04:00
|
|
|
free(first_form);
|
2011-12-21 09:38:47 -05:00
|
|
|
first_form = ptr;
|
2001-10-03 03:54:42 -04:00
|
|
|
}
|
2001-08-28 04:54:33 -04:00
|
|
|
|
|
|
|
return return_value;
|
2001-08-21 09:18:07 -04:00
|
|
|
}
|
|
|
|
|
2004-04-23 06:37:52 -04:00
|
|
|
/*
|
|
|
|
* curl_formadd() is a public API to add a section to the multipart formpost.
|
2011-06-10 08:40:46 -04:00
|
|
|
*
|
|
|
|
* @unittest: 1308
|
2004-04-23 06:37:52 -04:00
|
|
|
*/
|
|
|
|
|
2002-05-21 03:44:27 -04:00
|
|
|
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
|
2004-04-23 06:37:52 -04:00
|
|
|
struct curl_httppost **last_post,
|
|
|
|
...)
|
2001-08-21 09:18:07 -04:00
|
|
|
{
|
|
|
|
va_list arg;
|
2002-05-21 03:44:27 -04:00
|
|
|
CURLFORMcode result;
|
2001-08-21 09:18:07 -04:00
|
|
|
va_start(arg, last_post);
|
|
|
|
result = FormAdd(httppost, last_post, arg);
|
|
|
|
va_end(arg);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-06-24 17:46:41 -04:00
|
|
|
/*
|
|
|
|
* curl_formget()
|
|
|
|
* Serialize a curl_httppost struct.
|
|
|
|
* Returns 0 on success.
|
2011-06-10 08:40:46 -04:00
|
|
|
*
|
|
|
|
* @unittest: 1308
|
2006-06-24 17:46:41 -04:00
|
|
|
*/
|
|
|
|
int curl_formget(struct curl_httppost *form, void *arg,
|
|
|
|
curl_formget_callback append)
|
|
|
|
{
|
2014-10-23 16:56:35 -04:00
|
|
|
CURLcode result;
|
2017-09-05 05:45:21 -04:00
|
|
|
curl_mimepart toppart;
|
2006-06-24 17:46:41 -04:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
Curl_mime_initpart(&toppart, NULL); /* default form is empty */
|
|
|
|
result = Curl_getformdata(NULL, &toppart, form, NULL);
|
|
|
|
if(!result)
|
|
|
|
result = Curl_mime_prepare_headers(&toppart, "multipart/form-data",
|
|
|
|
NULL, MIMESTRATEGY_FORM);
|
|
|
|
|
|
|
|
while(!result) {
|
|
|
|
char buffer[8192];
|
2018-05-11 17:40:58 -04:00
|
|
|
size_t nread = Curl_mime_read(buffer, 1, sizeof(buffer), &toppart);
|
2017-09-02 12:47:10 -04:00
|
|
|
|
|
|
|
if(!nread)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch(nread) {
|
|
|
|
default:
|
|
|
|
if(append(arg, buffer, nread) != nread)
|
|
|
|
result = CURLE_READ_ERROR;
|
|
|
|
break;
|
|
|
|
case CURL_READFUNC_ABORT:
|
|
|
|
case CURL_READFUNC_PAUSE:
|
|
|
|
break;
|
2006-06-24 17:46:41 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 12:47:10 -04:00
|
|
|
|
|
|
|
Curl_mime_cleanpart(&toppart);
|
|
|
|
return (int) result;
|
2006-06-24 17:46:41 -04:00
|
|
|
}
|
|
|
|
|
2004-04-23 06:37:52 -04:00
|
|
|
/*
|
|
|
|
* curl_formfree() is an external function to free up a whole form post
|
|
|
|
* chain
|
|
|
|
*/
|
2002-03-11 10:18:59 -05:00
|
|
|
void curl_formfree(struct curl_httppost *form)
|
2000-11-17 09:06:24 -05:00
|
|
|
{
|
2002-03-11 10:18:59 -05:00
|
|
|
struct curl_httppost *next;
|
2001-04-06 01:52:23 -04:00
|
|
|
|
|
|
|
if(!form)
|
|
|
|
/* no form to free, just get out of this */
|
|
|
|
return;
|
|
|
|
|
2000-11-17 09:06:24 -05:00
|
|
|
do {
|
2017-09-09 17:09:06 -04:00
|
|
|
next = form->next; /* the following form line */
|
2000-11-17 09:06:24 -05:00
|
|
|
|
|
|
|
/* recurse to sub-contents */
|
2015-03-11 13:15:33 -04:00
|
|
|
curl_formfree(form->more);
|
2000-11-17 09:06:24 -05:00
|
|
|
|
2015-03-11 12:41:01 -04:00
|
|
|
if(!(form->flags & HTTPPOST_PTRNAME))
|
2000-11-17 09:06:24 -05:00
|
|
|
free(form->name); /* free the name */
|
2011-08-08 04:21:03 -04:00
|
|
|
if(!(form->flags &
|
2015-03-11 12:41:01 -04:00
|
|
|
(HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK))
|
|
|
|
)
|
2000-11-17 09:06:24 -05:00
|
|
|
free(form->contents); /* free the contents */
|
2015-03-11 12:41:01 -04:00
|
|
|
free(form->contenttype); /* free the content type */
|
|
|
|
free(form->showfilename); /* free the faked file name */
|
2000-11-17 09:06:24 -05:00
|
|
|
free(form); /* free the struct */
|
2016-12-13 19:29:44 -05:00
|
|
|
form = next;
|
|
|
|
} while(form); /* continue */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2013-01-21 17:20:09 -05:00
|
|
|
|
2017-09-21 20:08:29 -04:00
|
|
|
/* Set mime part name, taking care of non nul-terminated name string. */
|
|
|
|
static CURLcode setname(curl_mimepart *part, const char *name, size_t len)
|
|
|
|
{
|
|
|
|
char *zname;
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
if(!name || !len)
|
|
|
|
return curl_mime_name(part, name);
|
|
|
|
zname = malloc(len + 1);
|
|
|
|
if(!zname)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
memcpy(zname, name, len);
|
|
|
|
zname[len] = '\0';
|
|
|
|
res = curl_mime_name(part, zname);
|
|
|
|
free(zname);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-04-23 06:37:52 -04:00
|
|
|
/*
|
2017-09-02 12:47:10 -04:00
|
|
|
* Curl_getformdata() converts a linked list of "meta data" into a mime
|
|
|
|
* structure. The input list is in 'post', while the output is stored in
|
|
|
|
* mime part at '*finalform'.
|
2010-10-20 08:57:43 -04:00
|
|
|
*
|
|
|
|
* This function will not do a failf() for the potential memory failures but
|
|
|
|
* should for all other errors it spots. Just note that this function MAY get
|
|
|
|
* a NULL pointer in the 'data' argument.
|
2004-04-23 06:37:52 -04:00
|
|
|
*/
|
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
CURLcode Curl_getformdata(struct Curl_easy *data,
|
2017-09-05 05:45:21 -04:00
|
|
|
curl_mimepart *finalform,
|
2002-04-15 07:19:03 -04:00
|
|
|
struct curl_httppost *post,
|
2017-09-02 12:47:10 -04:00
|
|
|
curl_read_callback fread_func)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2002-04-15 07:19:03 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2017-09-05 05:45:21 -04:00
|
|
|
curl_mime *form = NULL;
|
|
|
|
curl_mimepart *part;
|
2017-09-02 12:47:10 -04:00
|
|
|
struct curl_httppost *file;
|
2001-12-14 07:59:16 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
Curl_mime_cleanpart(finalform); /* default form is empty */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
if(!post)
|
2002-04-15 07:19:03 -04:00
|
|
|
return result; /* no input => no output! */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
form = curl_mime_init(data);
|
|
|
|
if(!form)
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
2001-08-28 04:54:33 -04:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
if(!result)
|
|
|
|
result = curl_mime_subparts(finalform, form);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
/* Process each top part. */
|
|
|
|
for(; !result && post; post = post->next) {
|
|
|
|
/* If we have more than a file here, create a mime subpart and fill it. */
|
2018-06-02 16:52:56 -04:00
|
|
|
curl_mime *multipart = form;
|
1999-12-29 09:20:26 -05:00
|
|
|
if(post->more) {
|
2017-09-02 12:47:10 -04:00
|
|
|
part = curl_mime_addpart(form);
|
|
|
|
if(!part)
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
if(!result)
|
2017-09-21 20:08:29 -04:00
|
|
|
result = setname(part, post->name, post->namelength);
|
2017-09-02 12:47:10 -04:00
|
|
|
if(!result) {
|
|
|
|
multipart = curl_mime_init(data);
|
|
|
|
if(!multipart)
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
2011-12-21 09:38:47 -05:00
|
|
|
}
|
2017-09-02 12:47:10 -04:00
|
|
|
if(!result)
|
|
|
|
result = curl_mime_subparts(part, multipart);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
/* Generate all the part contents. */
|
|
|
|
for(file = post; !result && file; file = file->more) {
|
|
|
|
/* Create the part. */
|
|
|
|
part = curl_mime_addpart(multipart);
|
|
|
|
if(!part)
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
2002-03-11 10:18:59 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
/* Set the headers. */
|
|
|
|
if(!result)
|
|
|
|
result = curl_mime_headers(part, file->contentheader, 0);
|
2004-10-01 02:36:11 -04:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
/* Set the content type. */
|
2017-10-12 14:36:16 -04:00
|
|
|
if(!result && file->contenttype)
|
2017-09-02 12:47:10 -04:00
|
|
|
result = curl_mime_type(part, file->contenttype);
|
2004-06-03 09:03:57 -04:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
/* Set field name. */
|
|
|
|
if(!result && !post->more)
|
2017-09-21 20:08:29 -04:00
|
|
|
result = setname(part, post->name, post->namelength);
|
2000-10-09 18:29:35 -04:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
/* Process contents. */
|
|
|
|
if(!result) {
|
|
|
|
curl_off_t clen = post->contentslength;
|
2001-12-14 07:59:16 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
if(post->flags & CURL_HTTPPOST_LARGE)
|
|
|
|
clen = post->contentlen;
|
|
|
|
if(!clen)
|
|
|
|
clen = -1;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
if(post->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE)) {
|
2017-09-06 08:42:03 -04:00
|
|
|
if(!strcmp(file->contents, "-")) {
|
|
|
|
/* There are a few cases where the code below won't work; in
|
|
|
|
particular, freopen(stdin) by the caller is not guaranteed
|
|
|
|
to result as expected. This feature has been kept for backward
|
|
|
|
compatibility: use of "-" pseudo file name should be avoided. */
|
|
|
|
result = curl_mime_data_cb(part, (curl_off_t) -1,
|
|
|
|
(curl_read_callback) fread,
|
2018-08-09 11:19:24 -04:00
|
|
|
CURLX_FUNCTION_CAST(curl_seek_callback,
|
|
|
|
fseek),
|
2017-09-06 08:42:03 -04:00
|
|
|
NULL, (void *) stdin);
|
|
|
|
}
|
2017-09-03 09:45:43 -04:00
|
|
|
else
|
|
|
|
result = curl_mime_filedata(part, file->contents);
|
2017-09-02 12:47:10 -04:00
|
|
|
if(!result && (post->flags & HTTPPOST_READFILE))
|
|
|
|
result = curl_mime_filename(part, NULL);
|
2002-06-12 17:40:59 -04:00
|
|
|
}
|
2017-09-02 12:47:10 -04:00
|
|
|
else if(post->flags & HTTPPOST_BUFFER)
|
|
|
|
result = curl_mime_data(part, post->buffer,
|
|
|
|
post->bufferlength? post->bufferlength: -1);
|
|
|
|
else if(post->flags & HTTPPOST_CALLBACK)
|
|
|
|
/* the contents should be read with the callback and the size is set
|
|
|
|
with the contentslength */
|
|
|
|
result = curl_mime_data_cb(part, clen,
|
|
|
|
fread_func, NULL, NULL, post->userp);
|
2002-01-19 06:08:05 -05:00
|
|
|
else {
|
2017-09-02 12:47:10 -04:00
|
|
|
result = curl_mime_data(part, post->contents, (ssize_t) clen);
|
|
|
|
#ifdef CURL_DOES_CONVERSIONS
|
|
|
|
/* Convert textual contents now. */
|
|
|
|
if(!result && data && part->datasize)
|
|
|
|
result = Curl_convert_to_network(data, part->data, part->datasize);
|
|
|
|
#endif
|
2002-06-12 17:40:59 -04:00
|
|
|
}
|
2004-06-03 09:03:57 -04:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
/* Set fake file name. */
|
|
|
|
if(!result && post->showfilename)
|
|
|
|
if(post->more || (post->flags & (HTTPPOST_FILENAME | HTTPPOST_BUFFER |
|
|
|
|
HTTPPOST_CALLBACK)))
|
|
|
|
result = curl_mime_filename(part, post->showfilename);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2004-05-10 03:11:52 -04:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-09-02 12:47:10 -04:00
|
|
|
if(result)
|
|
|
|
Curl_mime_cleanpart(finalform);
|
2002-04-15 07:19:03 -04:00
|
|
|
|
|
|
|
return result;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2004-06-09 04:22:02 -04:00
|
|
|
#else /* CURL_DISABLE_HTTP */
|
|
|
|
CURLFORMcode curl_formadd(struct curl_httppost **httppost,
|
|
|
|
struct curl_httppost **last_post,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
(void)httppost;
|
|
|
|
(void)last_post;
|
|
|
|
return CURL_FORMADD_DISABLED;
|
|
|
|
}
|
|
|
|
|
2006-07-20 16:04:52 -04:00
|
|
|
int curl_formget(struct curl_httppost *form, void *arg,
|
|
|
|
curl_formget_callback append)
|
2006-06-24 17:46:41 -04:00
|
|
|
{
|
2006-07-20 16:04:52 -04:00
|
|
|
(void) form;
|
2006-06-24 17:46:41 -04:00
|
|
|
(void) arg;
|
|
|
|
(void) append;
|
|
|
|
return CURL_FORMADD_DISABLED;
|
|
|
|
}
|
|
|
|
|
2004-06-13 04:59:37 -04:00
|
|
|
void curl_formfree(struct curl_httppost *form)
|
|
|
|
{
|
|
|
|
(void)form;
|
|
|
|
/* does nothing HTTP is disabled */
|
|
|
|
}
|
|
|
|
|
2005-11-24 15:38:20 -05:00
|
|
|
|
2013-02-14 04:32:04 -05:00
|
|
|
#endif /* !defined(CURL_DISABLE_HTTP) */
|