1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #include <curl/curl.h>
27 /* Length of the random boundary string. */
28 #define BOUNDARY_LENGTH 40
30 #if !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY)
32 #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
36 #include "urldata.h" /* for struct SessionHandle */
38 #include "curl_rand.h"
40 #include "curl_memory.h"
43 #define _MPRINTF_REPLACE /* use our functions only */
44 #include <curl/mprintf.h>
46 /* The last #include file should be: */
49 #endif /* !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY) */
51 #ifndef CURL_DISABLE_HTTP
54 static char *Curl_basename(char *path);
55 #define basename(x) Curl_basename((x))
58 static size_t readfromfile(struct Form *form, char *buffer, size_t size);
60 /* What kind of Content-Type to use on un-specified files with unrecognized
62 #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
64 #define FORM_FILE_SEPARATOR ','
65 #define FORM_TYPE_SEPARATOR ';'
67 /***************************************************************************
71 * Adds a HttpPost structure to the list, if parent_post is given becomes
72 * a subpost of parent_post instead of a direct list element.
74 * Returns newly allocated HttpPost on success and NULL if malloc failed.
76 ***************************************************************************/
77 static struct curl_httppost *
78 AddHttpPost(char *name, size_t namelength,
79 char *value, size_t contentslength,
80 char *buffer, size_t bufferlength,
83 struct curl_slist* contentHeader,
84 char *showfilename, char *userp,
85 struct curl_httppost *parent_post,
86 struct curl_httppost **httppost,
87 struct curl_httppost **last_post)
89 struct curl_httppost *post;
90 post = calloc(1, sizeof(struct curl_httppost));
93 post->namelength = (long)(name?(namelength?namelength:strlen(name)):0);
94 post->contents = value;
95 post->contentslength = (long)contentslength;
96 post->buffer = buffer;
97 post->bufferlength = (long)bufferlength;
98 post->contenttype = contenttype;
99 post->contentheader = contentHeader;
100 post->showfilename = showfilename;
108 /* now, point our 'more' to the original 'more' */
109 post->more = parent_post->more;
111 /* then move the original 'more' to point to ourselves */
112 parent_post->more = post;
115 /* make the previous point to this */
117 (*last_post)->next = post;
126 /***************************************************************************
130 * Adds a FormInfo structure to the list presented by parent_form_info.
132 * Returns newly allocated FormInfo on success and NULL if malloc failed/
133 * parent_form_info is NULL.
135 ***************************************************************************/
136 static FormInfo * AddFormInfo(char *value,
138 FormInfo *parent_form_info)
141 form_info = calloc(1, sizeof(struct FormInfo));
144 form_info->value = value;
146 form_info->contenttype = contenttype;
147 form_info->flags = HTTPPOST_FILENAME;
152 if(parent_form_info) {
153 /* now, point our 'more' to the original 'more' */
154 form_info->more = parent_form_info->more;
156 /* then move the original 'more' to point to ourselves */
157 parent_form_info->more = form_info;
165 /***************************************************************************
167 * ContentTypeForFilename()
169 * Provides content type for filename if one of the known types (else
170 * (either the prevtype or the default is returned).
172 * Returns some valid contenttype for filename.
174 ***************************************************************************/
175 static const char * ContentTypeForFilename (const char *filename,
176 const char *prevtype)
178 const char *contenttype = NULL;
181 * No type was specified, we scan through a few well-known
182 * extensions and pick the first we match!
188 static const struct ContentType ctts[]={
189 {".gif", "image/gif"},
190 {".jpg", "image/jpeg"},
191 {".jpeg", "image/jpeg"},
192 {".txt", "text/plain"},
193 {".html", "text/html"},
194 {".xml", "application/xml"}
198 /* default to the previously set/used! */
199 contenttype = prevtype;
201 contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
203 if(filename) { /* in case a NULL was passed in */
204 for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
205 if(strlen(filename) >= strlen(ctts[i].extension)) {
206 if(strequal(filename +
207 strlen(filename) - strlen(ctts[i].extension),
208 ctts[i].extension)) {
209 contenttype = ctts[i].type;
215 /* we have a contenttype by now */
219 /***************************************************************************
223 * Copies the 'source' data to a newly allocated buffer buffer (that is
224 * returned). Uses buffer_length if not null, else uses strlen to determine
225 * the length of the buffer to be copied
227 * Returns the new pointer or NULL on failure.
229 ***************************************************************************/
230 static char *memdup(const char *src, size_t buffer_length)
237 length = buffer_length;
239 length = strlen(src);
243 /* no length and a NULL src pointer! */
246 buffer = malloc(length+add);
248 return NULL; /* fail */
250 memcpy(buffer, src, length);
252 /* if length unknown do null termination */
254 buffer[length] = '\0';
259 /***************************************************************************
263 * Stores a formpost parameter and builds the appropriate linked list.
265 * Has two principal functionalities: using files and byte arrays as
266 * post parts. Byte arrays are either copied or just the pointer is stored
267 * (as the user requests) while for files only the filename and not the
270 * While you may have only one byte array for each name, multiple filenames
271 * are allowed (and because of this feature CURLFORM_END is needed after
272 * using CURLFORM_FILE).
276 * Simple name/value pair with copied contents:
277 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
278 * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
280 * name/value pair where only the content pointer is remembered:
281 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
282 * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
283 * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
285 * storing a filename (CONTENTTYPE is optional!):
286 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
287 * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
290 * storing multiple filenames:
291 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
292 * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
295 * CURL_FORMADD_OK on success
296 * CURL_FORMADD_MEMORY if the FormInfo allocation fails
297 * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
298 * CURL_FORMADD_NULL if a null pointer was given for a char
299 * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
300 * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
301 * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
302 * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
303 * CURL_FORMADD_MEMORY if some allocation for string copying failed.
304 * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
306 ***************************************************************************/
309 CURLFORMcode FormAdd(struct curl_httppost **httppost,
310 struct curl_httppost **last_post,
313 FormInfo *first_form, *current_form, *form = NULL;
314 CURLFORMcode return_value = CURL_FORMADD_OK;
315 const char *prevtype = NULL;
316 struct curl_httppost *post = NULL;
317 CURLformoption option;
318 struct curl_forms *forms = NULL;
319 char *array_value=NULL; /* value read from an array */
321 /* This is a state variable, that if TRUE means that we're parsing an
322 array that we got passed to us. If FALSE we're parsing the input
323 va_list arguments. */
324 bool array_state = FALSE;
327 * We need to allocate the first struct to fill in.
329 first_form = calloc(1, sizeof(struct FormInfo));
331 return CURL_FORMADD_MEMORY;
333 current_form = first_form;
336 * Loop through all the options set. Break if we have an error to report.
338 while(return_value == CURL_FORMADD_OK) {
340 /* first see if we have more parts of the array param */
341 if(array_state && forms) {
342 /* get the upcoming option from the given array */
343 option = forms->option;
344 array_value = (char *)forms->value;
346 forms++; /* advance this to next entry */
347 if(CURLFORM_END == option) {
348 /* end of array state */
354 /* This is not array-state, get next option */
355 option = va_arg(params, CURLformoption);
356 if(CURLFORM_END == option)
363 /* we don't support an array from within an array */
364 return_value = CURL_FORMADD_ILLEGAL_ARRAY;
366 forms = va_arg(params, struct curl_forms *);
370 return_value = CURL_FORMADD_NULL;
375 * Set the Name property.
377 case CURLFORM_PTRNAME:
378 #ifdef CURL_DOES_CONVERSIONS
379 /* Treat CURLFORM_PTR like CURLFORM_COPYNAME so that libcurl will copy
380 * the data in all cases so that we'll have safe memory for the eventual
384 current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
386 case CURLFORM_COPYNAME:
387 if(current_form->name)
388 return_value = CURL_FORMADD_OPTION_TWICE;
390 char *name = array_state?
391 array_value:va_arg(params, char *);
393 current_form->name = name; /* store for the moment */
395 return_value = CURL_FORMADD_NULL;
398 case CURLFORM_NAMELENGTH:
399 if(current_form->namelength)
400 return_value = CURL_FORMADD_OPTION_TWICE;
402 current_form->namelength =
403 array_state?(size_t)array_value:(size_t)va_arg(params, long);
407 * Set the contents property.
409 case CURLFORM_PTRCONTENTS:
410 current_form->flags |= HTTPPOST_PTRCONTENTS; /* fall through */
411 case CURLFORM_COPYCONTENTS:
412 if(current_form->value)
413 return_value = CURL_FORMADD_OPTION_TWICE;
416 array_state?array_value:va_arg(params, char *);
418 current_form->value = value; /* store for the moment */
420 return_value = CURL_FORMADD_NULL;
423 case CURLFORM_CONTENTSLENGTH:
424 if(current_form->contentslength)
425 return_value = CURL_FORMADD_OPTION_TWICE;
427 current_form->contentslength =
428 array_state?(size_t)array_value:(size_t)va_arg(params, long);
431 /* Get contents from a given file name */
432 case CURLFORM_FILECONTENT:
433 if(current_form->flags != 0)
434 return_value = CURL_FORMADD_OPTION_TWICE;
436 const char *filename = array_state?
437 array_value:va_arg(params, char *);
439 current_form->value = strdup(filename);
440 if(!current_form->value)
441 return_value = CURL_FORMADD_MEMORY;
443 current_form->flags |= HTTPPOST_READFILE;
444 current_form->value_alloc = TRUE;
448 return_value = CURL_FORMADD_NULL;
452 /* We upload a file */
455 const char *filename = array_state?array_value:
456 va_arg(params, char *);
458 if(current_form->value) {
459 if(current_form->flags & HTTPPOST_FILENAME) {
461 if((current_form = AddFormInfo(strdup(filename),
462 NULL, current_form)) == NULL)
463 return_value = CURL_FORMADD_MEMORY;
466 return_value = CURL_FORMADD_NULL;
469 return_value = CURL_FORMADD_OPTION_TWICE;
473 current_form->value = strdup(filename);
474 if(!current_form->value)
475 return_value = CURL_FORMADD_MEMORY;
477 current_form->flags |= HTTPPOST_FILENAME;
478 current_form->value_alloc = TRUE;
482 return_value = CURL_FORMADD_NULL;
487 case CURLFORM_BUFFERPTR:
488 current_form->flags |= HTTPPOST_PTRBUFFER|HTTPPOST_BUFFER;
489 if(current_form->buffer)
490 return_value = CURL_FORMADD_OPTION_TWICE;
493 array_state?array_value:va_arg(params, char *);
495 current_form->buffer = buffer; /* store for the moment */
496 current_form->value = buffer; /* make it non-NULL to be accepted
500 return_value = CURL_FORMADD_NULL;
504 case CURLFORM_BUFFERLENGTH:
505 if(current_form->bufferlength)
506 return_value = CURL_FORMADD_OPTION_TWICE;
508 current_form->bufferlength =
509 array_state?(size_t)array_value:(size_t)va_arg(params, long);
512 case CURLFORM_STREAM:
513 current_form->flags |= HTTPPOST_CALLBACK;
514 if(current_form->userp)
515 return_value = CURL_FORMADD_OPTION_TWICE;
518 array_state?array_value:va_arg(params, char *);
520 current_form->userp = userp;
521 current_form->value = userp; /* this isn't strictly true but we
522 derive a value from this later on
523 and we need this non-NULL to be
524 accepted as a fine form part */
527 return_value = CURL_FORMADD_NULL;
531 case CURLFORM_CONTENTTYPE:
533 const char *contenttype =
534 array_state?array_value:va_arg(params, char *);
535 if(current_form->contenttype) {
536 if(current_form->flags & HTTPPOST_FILENAME) {
538 if((current_form = AddFormInfo(NULL,
540 current_form)) == NULL)
541 return_value = CURL_FORMADD_MEMORY;
544 return_value = CURL_FORMADD_NULL;
547 return_value = CURL_FORMADD_OPTION_TWICE;
551 current_form->contenttype = strdup(contenttype);
552 if(!current_form->contenttype)
553 return_value = CURL_FORMADD_MEMORY;
555 current_form->contenttype_alloc = TRUE;
558 return_value = CURL_FORMADD_NULL;
562 case CURLFORM_CONTENTHEADER:
564 /* this "cast increases required alignment of target type" but
565 we consider it OK anyway */
566 struct curl_slist* list = array_state?
567 (struct curl_slist*)array_value:
568 va_arg(params, struct curl_slist*);
570 if(current_form->contentheader)
571 return_value = CURL_FORMADD_OPTION_TWICE;
573 current_form->contentheader = list;
577 case CURLFORM_FILENAME:
578 case CURLFORM_BUFFER:
580 const char *filename = array_state?array_value:
581 va_arg(params, char *);
582 if(current_form->showfilename)
583 return_value = CURL_FORMADD_OPTION_TWICE;
585 current_form->showfilename = strdup(filename);
586 if(!current_form->showfilename)
587 return_value = CURL_FORMADD_MEMORY;
589 current_form->showfilename_alloc = TRUE;
594 return_value = CURL_FORMADD_UNKNOWN_OPTION;
599 if(CURL_FORMADD_OK == return_value) {
600 /* go through the list, check for completeness and if everything is
601 * alright add the HttpPost item otherwise set return_value accordingly */
604 for(form = first_form;
607 if(((!form->name || !form->value) && !post) ||
608 ( (form->contentslength) &&
609 (form->flags & HTTPPOST_FILENAME) ) ||
610 ( (form->flags & HTTPPOST_FILENAME) &&
611 (form->flags & HTTPPOST_PTRCONTENTS) ) ||
614 (form->flags & HTTPPOST_BUFFER) &&
615 (form->flags & HTTPPOST_PTRBUFFER) ) ||
617 ( (form->flags & HTTPPOST_READFILE) &&
618 (form->flags & HTTPPOST_PTRCONTENTS) )
620 return_value = CURL_FORMADD_INCOMPLETE;
624 if(((form->flags & HTTPPOST_FILENAME) ||
625 (form->flags & HTTPPOST_BUFFER)) &&
626 !form->contenttype ) {
627 /* our contenttype is missing */
629 = strdup(ContentTypeForFilename(form->value, prevtype));
630 if(!form->contenttype) {
631 return_value = CURL_FORMADD_MEMORY;
634 form->contenttype_alloc = TRUE;
636 if(!(form->flags & HTTPPOST_PTRNAME) &&
637 (form == first_form) ) {
638 /* Note that there's small risk that form->name is NULL here if the
639 app passed in a bad combo, so we better check for that first. */
641 /* copy name (without strdup; possibly contains null characters) */
642 form->name = memdup(form->name, form->namelength);
644 return_value = CURL_FORMADD_MEMORY;
647 form->name_alloc = TRUE;
649 if(!(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
650 HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
651 HTTPPOST_CALLBACK)) ) {
652 /* copy value (without strdup; possibly contains null characters) */
653 form->value = memdup(form->value, form->contentslength);
655 return_value = CURL_FORMADD_MEMORY;
658 form->value_alloc = TRUE;
660 post = AddHttpPost(form->name, form->namelength,
661 form->value, form->contentslength,
662 form->buffer, form->bufferlength,
663 form->contenttype, form->flags,
664 form->contentheader, form->showfilename,
670 return_value = CURL_FORMADD_MEMORY;
674 if(form->contenttype)
675 prevtype = form->contenttype;
681 /* we return on error, free possibly allocated fields */
687 if(form->value_alloc)
689 if(form->contenttype_alloc)
690 free(form->contenttype);
691 if(form->showfilename_alloc)
692 free(form->showfilename);
696 /* always delete the allocated memory before returning */
698 while(form != NULL) {
699 FormInfo *delete_form;
710 * curl_formadd() is a public API to add a section to the multipart formpost.
715 CURLFORMcode curl_formadd(struct curl_httppost **httppost,
716 struct curl_httppost **last_post,
721 va_start(arg, last_post);
722 result = FormAdd(httppost, last_post, arg);
728 * AddFormData() adds a chunk of data to the FormData linked list.
730 * size is incremented by the chunk length, unless it is NULL
732 static CURLcode AddFormData(struct FormData **formp,
738 struct FormData *newform = malloc(sizeof(struct FormData));
740 return CURLE_OUT_OF_MEMORY;
741 newform->next = NULL;
743 if(type <= FORM_CONTENT) {
744 /* we make it easier for plain strings: */
746 length = strlen((char *)line);
748 newform->line = malloc(length+1);
751 return CURLE_OUT_OF_MEMORY;
753 memcpy(newform->line, line, length);
754 newform->length = length;
755 newform->line[length]=0; /* zero terminate for easier debugging */
758 /* For callbacks and files we don't have any actual data so we just keep a
759 pointer to whatever this points to */
760 newform->line = (char *)line;
762 newform->type = type;
765 (*formp)->next = newform;
772 if(type != FORM_FILE)
773 /* for static content as well as callback data we add the size given
777 /* Since this is a file to be uploaded here, add the size of the actual
779 if(!strequal("-", newform->line)) {
781 if(!stat(newform->line, &file)) {
782 *size += file.st_size;
791 * AddFormDataf() adds printf()-style formatted data to the formdata chain.
794 static CURLcode AddFormDataf(struct FormData **formp,
796 const char *fmt, ...)
801 vsnprintf(s, sizeof(s), fmt, ap);
804 return AddFormData(formp, FORM_DATA, s, 0, size);
808 * Curl_formclean() is used from http.c, this cleans a built FormData linked
811 void Curl_formclean(struct FormData **form_ptr)
813 struct FormData *next, *form;
820 next=form->next; /* the following form line */
821 if(form->type <= FORM_CONTENT)
822 free(form->line); /* free the line */
823 free(form); /* free the struct */
825 } while((form = next) != NULL); /* continue */
832 * Serialize a curl_httppost struct.
833 * Returns 0 on success.
837 int curl_formget(struct curl_httppost *form, void *arg,
838 curl_formget_callback append)
842 struct FormData *data, *ptr;
844 rc = Curl_getformdata(NULL, &data, form, NULL, &size);
848 for(ptr = data; ptr; ptr = ptr->next) {
849 if((ptr->type == FORM_FILE) || (ptr->type == FORM_CALLBACK)) {
854 Curl_FormInit(&temp, ptr);
857 nread = readfromfile(&temp, buffer, sizeof(buffer));
858 if((nread == (size_t) -1) ||
859 (nread > sizeof(buffer)) ||
860 (nread != append(arg, buffer, nread))) {
863 Curl_formclean(&data);
869 if(ptr->length != append(arg, ptr->line, ptr->length)) {
870 Curl_formclean(&data);
875 Curl_formclean(&data);
880 * curl_formfree() is an external function to free up a whole form post
883 void curl_formfree(struct curl_httppost *form)
885 struct curl_httppost *next;
888 /* no form to free, just get out of this */
892 next=form->next; /* the following form line */
894 /* recurse to sub-contents */
896 curl_formfree(form->more);
898 if(!(form->flags & HTTPPOST_PTRNAME) && form->name)
899 free(form->name); /* free the name */
901 (HTTPPOST_PTRCONTENTS|HTTPPOST_BUFFER|HTTPPOST_CALLBACK)) &&
903 free(form->contents); /* free the contents */
904 if(form->contenttype)
905 free(form->contenttype); /* free the content type */
906 if(form->showfilename)
907 free(form->showfilename); /* free the faked file name */
908 free(form); /* free the struct */
910 } while((form = next) != NULL); /* continue */
913 #ifndef HAVE_BASENAME
915 (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
918 The basename() function shall take the pathname pointed to by path and
919 return a pointer to the final component of the pathname, deleting any
920 trailing '/' characters.
922 If the string pointed to by path consists entirely of the '/' character,
923 basename() shall return a pointer to the string "/". If the string pointed
924 to by path is exactly "//", it is implementation-defined whether '/' or "//"
927 If path is a null pointer or points to an empty string, basename() shall
928 return a pointer to the string ".".
930 The basename() function may modify the string pointed to by path, and may
931 return a pointer to static storage that may then be overwritten by a
932 subsequent call to basename().
934 The basename() function need not be reentrant. A function that is not
935 required to be reentrant is not required to be thread-safe.
938 static char *Curl_basename(char *path)
940 /* Ignore all the details above for now and make a quick and simple
941 implementaion here */
945 s1=strrchr(path, '/');
946 s2=strrchr(path, '\\');
949 path = (s1 > s2? s1 : s2)+1;
960 static char *strippath(const char *fullfile)
964 filename = strdup(fullfile); /* duplicate since basename() may ruin the
965 buffer it works on */
968 base = strdup(basename(filename));
970 free(filename); /* free temporary buffer */
972 return base; /* returns an allocated string or NULL ! */
976 * Curl_getformdata() converts a linked list of "meta data" into a complete
977 * (possibly huge) multipart formdata. The input list is in 'post', while the
978 * output resulting linked lists gets stored in '*finalform'. *sizep will get
979 * the total size of the whole POST.
980 * A multipart/form_data content-type is built, unless a custom content-type
981 * is passed in 'custom_content_type'.
983 * This function will not do a failf() for the potential memory failures but
984 * should for all other errors it spots. Just note that this function MAY get
985 * a NULL pointer in the 'data' argument.
988 CURLcode Curl_getformdata(struct SessionHandle *data,
989 struct FormData **finalform,
990 struct curl_httppost *post,
991 const char *custom_content_type,
994 struct FormData *form = NULL;
995 struct FormData *firstform;
996 struct curl_httppost *file;
997 CURLcode result = CURLE_OK;
999 curl_off_t size=0; /* support potentially ENORMOUS formposts */
1001 char *fileboundary=NULL;
1002 struct curl_slist* curList;
1004 *finalform=NULL; /* default form is empty */
1007 return result; /* no input => no output! */
1009 boundary = Curl_FormBoundary();
1011 return CURLE_OUT_OF_MEMORY;
1013 /* Make the first line of the output */
1014 result = AddFormDataf(&form, NULL,
1015 "%s; boundary=%s\r\n",
1016 custom_content_type?custom_content_type:
1017 "Content-Type: multipart/form-data",
1024 /* we DO NOT include that line in the total size of the POST, since it'll be
1025 part of the header! */
1032 result = AddFormDataf(&form, &size, "\r\n");
1038 result = AddFormDataf(&form, &size, "--%s\r\n", boundary);
1042 /* Maybe later this should be disabled when a custom_content_type is
1043 passed, since Content-Disposition is not meaningful for all multipart
1046 result = AddFormDataf(&form, &size,
1047 "Content-Disposition: form-data; name=\"");
1051 result = AddFormData(&form, FORM_DATA, post->name, post->namelength,
1056 result = AddFormDataf(&form, &size, "\"");
1061 /* If used, this is a link to more file names, we must then do
1062 the magic to include several files with the same field name */
1064 fileboundary = Curl_FormBoundary();
1066 result = AddFormDataf(&form, &size,
1067 "\r\nContent-Type: multipart/mixed,"
1078 /* If 'showfilename' is set, that is a faked name passed on to us
1079 to use to in the formpost. If that is not set, the actually used
1080 local file name should be added. */
1083 /* if multiple-file */
1084 char *filebasename= NULL;
1085 if(!file->showfilename) {
1086 filebasename = strippath(file->contents);
1088 Curl_formclean(&firstform);
1090 return CURLE_OUT_OF_MEMORY;
1094 result = AddFormDataf(&form, &size,
1095 "\r\n--%s\r\nContent-Disposition: "
1096 "attachment; filename=\"%s\"",
1098 (file->showfilename?file->showfilename:
1105 else if(post->flags & (HTTPPOST_FILENAME|HTTPPOST_BUFFER|
1106 HTTPPOST_CALLBACK)) {
1107 /* it should be noted that for the HTTPPOST_FILENAME and
1108 HTTPPOST_CALLBACK cases the ->showfilename struct member is always
1109 assigned at this point */
1110 if(post->showfilename || (post->flags & HTTPPOST_FILENAME)) {
1112 (!post->showfilename)?strippath(post->contents):NULL;
1114 result = AddFormDataf(&form, &size,
1115 "; filename=\"%s\"",
1116 (post->showfilename?post->showfilename:
1126 if(file->contenttype) {
1127 /* we have a specified type */
1128 result = AddFormDataf(&form, &size,
1129 "\r\nContent-Type: %s",
1135 curList = file->contentheader;
1137 /* Process the additional headers specified for this form */
1138 result = AddFormDataf( &form, &size, "\r\n%s", curList->data );
1141 curList = curList->next;
1144 Curl_formclean(&firstform);
1149 result = AddFormDataf(&form, &size, "\r\n\r\n");
1153 if((post->flags & HTTPPOST_FILENAME) ||
1154 (post->flags & HTTPPOST_READFILE)) {
1155 /* we should include the contents from the specified file */
1158 fileread = strequal("-", file->contents)?
1159 stdin:fopen(file->contents, "rb"); /* binary read for win32 */
1162 * VMS: This only allows for stream files on VMS. Stream files are
1163 * OK, as are FIXED & VAR files WITHOUT implied CC For implied CC,
1164 * every record needs to have a \n appended & 1 added to SIZE
1168 if(fileread != stdin) {
1169 /* close the file again */
1171 /* add the file name only - for later reading from this */
1172 result = AddFormData(&form, FORM_FILE, file->contents, 0, &size);
1175 /* When uploading from stdin, we can't know the size of the file,
1176 * thus must read the full file as before. We *could* use chunked
1177 * transfer-encoding, but that only works for HTTP 1.1 and we
1178 * can't be sure we work with such a server.
1182 while((nread = fread(buffer, 1, sizeof(buffer), fileread)) != 0) {
1183 result = AddFormData(&form, FORM_CONTENT, buffer, nread, &size);
1191 failf(data, "couldn't open file \"%s\"\n", file->contents);
1193 result = CURLE_READ_ERROR;
1196 else if(post->flags & HTTPPOST_BUFFER)
1197 /* include contents of buffer */
1198 result = AddFormData(&form, FORM_CONTENT, post->buffer,
1199 post->bufferlength, &size);
1200 else if(post->flags & HTTPPOST_CALLBACK)
1201 /* the contents should be read with the callback and the size
1202 is set with the contentslength */
1203 result = AddFormData(&form, FORM_CALLBACK, post->userp,
1204 post->contentslength, &size);
1206 /* include the contents we got */
1207 result = AddFormData(&form, FORM_CONTENT, post->contents,
1208 post->contentslength, &size);
1211 } while(file && !result); /* for each specified file for this field */
1214 Curl_formclean(&firstform);
1220 /* this was a multiple-file inclusion, make a termination file
1222 result = AddFormDataf(&form, &size,
1230 } while((post = post->next) != NULL); /* for each field */
1232 Curl_formclean(&firstform);
1237 /* end-boundary for everything */
1238 result = AddFormDataf(&form, &size,
1242 Curl_formclean(&firstform);
1251 *finalform=firstform;
1257 * Curl_FormInit() inits the struct 'form' points to with the 'formdata'
1258 * and resets the 'sent' counter.
1260 int Curl_FormInit(struct Form *form, struct FormData *formdata )
1263 return 1; /* error */
1265 form->data = formdata;
1268 form->fread_func = ZERO_NULL;
1276 * The read callback that this function may use can return a value larger than
1277 * 'size' (which then this function returns) that indicates a problem and it
1278 * must be properly dealt with
1280 static size_t readfromfile(struct Form *form, char *buffer,
1284 bool callback = (form->data->type == FORM_CALLBACK)?TRUE:FALSE;
1287 if(form->fread_func == ZERO_NULL)
1290 nread = form->fread_func(buffer, 1, size, form->data->line);
1294 /* this file hasn't yet been opened */
1295 form->fp = fopen(form->data->line, "rb"); /* b is for binary */
1297 return (size_t)-1; /* failure */
1299 nread = fread(buffer, 1, size, form->fp);
1302 /* this is the last chunk from the file, move on */
1307 form->data = form->data->next;
1314 * Curl_FormReader() is the fread() emulation function that will be used to
1315 * deliver the formdata to the transfer loop and then sent away to the peer.
1317 size_t Curl_FormReader(char *buffer,
1326 form=(struct Form *)mydata;
1328 wantedsize = size * nitems;
1331 return 0; /* nothing, error, empty */
1333 if((form->data->type == FORM_FILE) ||
1334 (form->data->type == FORM_CALLBACK)) {
1335 gotsize = readfromfile(form, buffer, wantedsize);
1338 /* If positive or -1, return. If zero, continue! */
1343 if((form->data->length - form->sent ) > wantedsize - gotsize) {
1345 memcpy(buffer + gotsize , form->data->line + form->sent,
1346 wantedsize - gotsize);
1348 form->sent += wantedsize-gotsize;
1353 memcpy(buffer+gotsize,
1354 form->data->line + form->sent,
1355 (form->data->length - form->sent) );
1356 gotsize += form->data->length - form->sent;
1360 form->data = form->data->next; /* advance */
1362 } while(form->data && (form->data->type < FORM_CALLBACK));
1363 /* If we got an empty line and we have more data, we proceed to the next
1364 line immediately to avoid returning zero before we've reached the end. */
1370 * Curl_formpostheader() returns the first line of the formpost, the
1371 * request-header part (which is not part of the request-body like the rest of
1374 char *Curl_formpostheader(void *formp, size_t *len)
1377 struct Form *form=(struct Form *)formp;
1380 return 0; /* nothing, ERROR! */
1382 header = form->data->line;
1383 *len = form->data->length;
1385 form->data = form->data->next; /* advance */
1390 #else /* CURL_DISABLE_HTTP */
1391 CURLFORMcode curl_formadd(struct curl_httppost **httppost,
1392 struct curl_httppost **last_post,
1397 return CURL_FORMADD_DISABLED;
1400 int curl_formget(struct curl_httppost *form, void *arg,
1401 curl_formget_callback append)
1406 return CURL_FORMADD_DISABLED;
1409 void curl_formfree(struct curl_httppost *form)
1412 /* does nothing HTTP is disabled */
1415 #endif /* CURL_DISABLE_HTTP */
1417 #if !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY)
1420 * Curl_FormBoundary() creates a suitable boundary string and returns an
1421 * allocated one. This is also used by SSL-code so it must be present even
1422 * if HTTP is disabled!
1424 char *Curl_FormBoundary(void)
1429 static const char table16[]="0123456789abcdef";
1431 retstring = malloc(BOUNDARY_LENGTH+1);
1434 return NULL; /* failed */
1436 strcpy(retstring, "----------------------------");
1438 for(i=strlen(retstring); i<BOUNDARY_LENGTH; i++)
1439 retstring[i] = table16[Curl_rand()%16];
1441 /* 28 dashes and 12 hexadecimal digits makes 12^16 (184884258895036416)
1443 retstring[BOUNDARY_LENGTH]=0; /* zero terminate */
1448 #endif /* !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY) */