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 ***************************************************************************/
24 #include <curl/curl.h>
26 /* Length of the random boundary string. */
27 #define BOUNDARY_LENGTH 40
29 #if !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY)
36 #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
39 #include "urldata.h" /* for struct SessionHandle */
41 #include "curl_rand.h"
43 #include "curl_memory.h"
46 #define _MPRINTF_REPLACE /* use our functions only */
47 #include <curl/mprintf.h>
49 /* The last #include file should be: */
52 #endif /* !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY) */
54 #ifndef CURL_DISABLE_HTTP
57 static char *Curl_basename(char *path);
58 #define basename(x) Curl_basename((x))
61 static size_t readfromfile(struct Form *form, char *buffer, size_t size);
63 /* What kind of Content-Type to use on un-specified files with unrecognized
65 #define HTTPPOST_CONTENTTYPE_DEFAULT "application/octet-stream"
67 #define FORM_FILE_SEPARATOR ','
68 #define FORM_TYPE_SEPARATOR ';'
70 /***************************************************************************
74 * Adds a HttpPost structure to the list, if parent_post is given becomes
75 * a subpost of parent_post instead of a direct list element.
77 * Returns newly allocated HttpPost on success and NULL if malloc failed.
79 ***************************************************************************/
80 static struct curl_httppost *
81 AddHttpPost(char *name, size_t namelength,
82 char *value, size_t contentslength,
83 char *buffer, size_t bufferlength,
86 struct curl_slist* contentHeader,
87 char *showfilename, char *userp,
88 struct curl_httppost *parent_post,
89 struct curl_httppost **httppost,
90 struct curl_httppost **last_post)
92 struct curl_httppost *post;
93 post = calloc(1, sizeof(struct curl_httppost));
96 post->namelength = (long)(name?(namelength?namelength:strlen(name)):0);
97 post->contents = value;
98 post->contentslength = (long)contentslength;
99 post->buffer = buffer;
100 post->bufferlength = (long)bufferlength;
101 post->contenttype = contenttype;
102 post->contentheader = contentHeader;
103 post->showfilename = showfilename;
111 /* now, point our 'more' to the original 'more' */
112 post->more = parent_post->more;
114 /* then move the original 'more' to point to ourselves */
115 parent_post->more = post;
118 /* make the previous point to this */
120 (*last_post)->next = post;
129 /***************************************************************************
133 * Adds a FormInfo structure to the list presented by parent_form_info.
135 * Returns newly allocated FormInfo on success and NULL if malloc failed/
136 * parent_form_info is NULL.
138 ***************************************************************************/
139 static FormInfo * AddFormInfo(char *value,
141 FormInfo *parent_form_info)
144 form_info = calloc(1, sizeof(struct FormInfo));
147 form_info->value = value;
149 form_info->contenttype = contenttype;
150 form_info->flags = HTTPPOST_FILENAME;
155 if(parent_form_info) {
156 /* now, point our 'more' to the original 'more' */
157 form_info->more = parent_form_info->more;
159 /* then move the original 'more' to point to ourselves */
160 parent_form_info->more = form_info;
168 /***************************************************************************
170 * ContentTypeForFilename()
172 * Provides content type for filename if one of the known types (else
173 * (either the prevtype or the default is returned).
175 * Returns some valid contenttype for filename.
177 ***************************************************************************/
178 static const char * ContentTypeForFilename (const char *filename,
179 const char *prevtype)
181 const char *contenttype = NULL;
184 * No type was specified, we scan through a few well-known
185 * extensions and pick the first we match!
191 static const struct ContentType ctts[]={
192 {".gif", "image/gif"},
193 {".jpg", "image/jpeg"},
194 {".jpeg", "image/jpeg"},
195 {".txt", "text/plain"},
196 {".html", "text/html"},
197 {".xml", "application/xml"}
201 /* default to the previously set/used! */
202 contenttype = prevtype;
204 contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
206 if(filename) { /* in case a NULL was passed in */
207 for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
208 if(strlen(filename) >= strlen(ctts[i].extension)) {
209 if(strequal(filename +
210 strlen(filename) - strlen(ctts[i].extension),
211 ctts[i].extension)) {
212 contenttype = ctts[i].type;
218 /* we have a contenttype by now */
222 /***************************************************************************
226 * Copies the 'source' data to a newly allocated buffer buffer (that is
227 * returned). Uses buffer_length if not null, else uses strlen to determine
228 * the length of the buffer to be copied
230 * Returns the new pointer or NULL on failure.
232 ***************************************************************************/
233 static char *memdup(const char *src, size_t buffer_length)
240 length = buffer_length;
242 length = strlen(src);
246 /* no length and a NULL src pointer! */
249 buffer = malloc(length+add);
251 return NULL; /* fail */
253 memcpy(buffer, src, length);
255 /* if length unknown do null termination */
257 buffer[length] = '\0';
262 /***************************************************************************
266 * Stores a formpost parameter and builds the appropriate linked list.
268 * Has two principal functionalities: using files and byte arrays as
269 * post parts. Byte arrays are either copied or just the pointer is stored
270 * (as the user requests) while for files only the filename and not the
273 * While you may have only one byte array for each name, multiple filenames
274 * are allowed (and because of this feature CURLFORM_END is needed after
275 * using CURLFORM_FILE).
279 * Simple name/value pair with copied contents:
280 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
281 * CURLFORM_COPYCONTENTS, "value", CURLFORM_END);
283 * name/value pair where only the content pointer is remembered:
284 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
285 * CURLFORM_PTRCONTENTS, ptr, CURLFORM_CONTENTSLENGTH, 10, CURLFORM_END);
286 * (if CURLFORM_CONTENTSLENGTH is missing strlen () is used)
288 * storing a filename (CONTENTTYPE is optional!):
289 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
290 * CURLFORM_FILE, "filename1", CURLFORM_CONTENTTYPE, "plain/text",
293 * storing multiple filenames:
294 * curl_formadd (&post, &last, CURLFORM_COPYNAME, "name",
295 * CURLFORM_FILE, "filename1", CURLFORM_FILE, "filename2", CURLFORM_END);
298 * CURL_FORMADD_OK on success
299 * CURL_FORMADD_MEMORY if the FormInfo allocation fails
300 * CURL_FORMADD_OPTION_TWICE if one option is given twice for one Form
301 * CURL_FORMADD_NULL if a null pointer was given for a char
302 * CURL_FORMADD_MEMORY if the allocation of a FormInfo struct failed
303 * CURL_FORMADD_UNKNOWN_OPTION if an unknown option was used
304 * CURL_FORMADD_INCOMPLETE if the some FormInfo is not complete (or error)
305 * CURL_FORMADD_MEMORY if a HttpPost struct cannot be allocated
306 * CURL_FORMADD_MEMORY if some allocation for string copying failed.
307 * CURL_FORMADD_ILLEGAL_ARRAY if an illegal option is used in an array
309 ***************************************************************************/
312 CURLFORMcode FormAdd(struct curl_httppost **httppost,
313 struct curl_httppost **last_post,
316 FormInfo *first_form, *current_form, *form = NULL;
317 CURLFORMcode return_value = CURL_FORMADD_OK;
318 const char *prevtype = NULL;
319 struct curl_httppost *post = NULL;
320 CURLformoption option;
321 struct curl_forms *forms = NULL;
322 char *array_value=NULL; /* value read from an array */
324 /* This is a state variable, that if TRUE means that we're parsing an
325 array that we got passed to us. If FALSE we're parsing the input
326 va_list arguments. */
327 bool array_state = FALSE;
330 * We need to allocate the first struct to fill in.
332 first_form = calloc(1, sizeof(struct FormInfo));
334 return CURL_FORMADD_MEMORY;
336 current_form = first_form;
339 * Loop through all the options set. Break if we have an error to report.
341 while(return_value == CURL_FORMADD_OK) {
343 /* first see if we have more parts of the array param */
344 if( array_state && forms ) {
345 /* get the upcoming option from the given array */
346 option = forms->option;
347 array_value = (char *)forms->value;
349 forms++; /* advance this to next entry */
350 if(CURLFORM_END == option) {
351 /* end of array state */
357 /* This is not array-state, get next option */
358 option = va_arg(params, CURLformoption);
359 if(CURLFORM_END == option)
366 /* we don't support an array from within an array */
367 return_value = CURL_FORMADD_ILLEGAL_ARRAY;
369 forms = va_arg(params, struct curl_forms *);
373 return_value = CURL_FORMADD_NULL;
378 * Set the Name property.
380 case CURLFORM_PTRNAME:
381 #ifdef CURL_DOES_CONVERSIONS
382 /* Treat CURLFORM_PTR like CURLFORM_COPYNAME so that libcurl will copy
383 * the data in all cases so that we'll have safe memory for the eventual
387 current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
389 case CURLFORM_COPYNAME:
390 if(current_form->name)
391 return_value = CURL_FORMADD_OPTION_TWICE;
393 char *name = array_state?
394 array_value:va_arg(params, char *);
396 current_form->name = name; /* store for the moment */
398 return_value = CURL_FORMADD_NULL;
401 case CURLFORM_NAMELENGTH:
402 if(current_form->namelength)
403 return_value = CURL_FORMADD_OPTION_TWICE;
405 current_form->namelength =
406 array_state?(size_t)array_value:(size_t)va_arg(params, long);
410 * Set the contents property.
412 case CURLFORM_PTRCONTENTS:
413 current_form->flags |= HTTPPOST_PTRCONTENTS; /* fall through */
414 case CURLFORM_COPYCONTENTS:
415 if(current_form->value)
416 return_value = CURL_FORMADD_OPTION_TWICE;
419 array_state?array_value:va_arg(params, char *);
421 current_form->value = value; /* store for the moment */
423 return_value = CURL_FORMADD_NULL;
426 case CURLFORM_CONTENTSLENGTH:
427 if(current_form->contentslength)
428 return_value = CURL_FORMADD_OPTION_TWICE;
430 current_form->contentslength =
431 array_state?(size_t)array_value:(size_t)va_arg(params, long);
434 /* Get contents from a given file name */
435 case CURLFORM_FILECONTENT:
436 if(current_form->flags != 0)
437 return_value = CURL_FORMADD_OPTION_TWICE;
439 const char *filename = array_state?
440 array_value:va_arg(params, char *);
442 current_form->value = strdup(filename);
443 if(!current_form->value)
444 return_value = CURL_FORMADD_MEMORY;
446 current_form->flags |= HTTPPOST_READFILE;
447 current_form->value_alloc = TRUE;
451 return_value = CURL_FORMADD_NULL;
455 /* We upload a file */
458 const char *filename = array_state?array_value:
459 va_arg(params, char *);
461 if(current_form->value) {
462 if(current_form->flags & HTTPPOST_FILENAME) {
464 if((current_form = AddFormInfo(strdup(filename),
465 NULL, current_form)) == NULL)
466 return_value = CURL_FORMADD_MEMORY;
469 return_value = CURL_FORMADD_NULL;
472 return_value = CURL_FORMADD_OPTION_TWICE;
476 current_form->value = strdup(filename);
477 if(!current_form->value)
478 return_value = CURL_FORMADD_MEMORY;
480 current_form->flags |= HTTPPOST_FILENAME;
481 current_form->value_alloc = TRUE;
485 return_value = CURL_FORMADD_NULL;
490 case CURLFORM_BUFFER:
492 const char *filename = array_state?array_value:
493 va_arg(params, char *);
495 if(current_form->value) {
496 if(current_form->flags & HTTPPOST_BUFFER) {
498 if((current_form = AddFormInfo(strdup(filename),
499 NULL, current_form)) == NULL)
500 return_value = CURL_FORMADD_MEMORY;
503 return_value = CURL_FORMADD_NULL;
506 return_value = CURL_FORMADD_OPTION_TWICE;
510 current_form->value = strdup(filename);
511 if(!current_form->value)
512 return_value = CURL_FORMADD_MEMORY;
515 return_value = CURL_FORMADD_NULL;
516 current_form->flags |= HTTPPOST_BUFFER;
521 case CURLFORM_BUFFERPTR:
522 current_form->flags |= HTTPPOST_PTRBUFFER;
523 if(current_form->buffer)
524 return_value = CURL_FORMADD_OPTION_TWICE;
527 array_state?array_value:va_arg(params, char *);
529 current_form->buffer = buffer; /* store for the moment */
531 return_value = CURL_FORMADD_NULL;
535 case CURLFORM_BUFFERLENGTH:
536 if(current_form->bufferlength)
537 return_value = CURL_FORMADD_OPTION_TWICE;
539 current_form->bufferlength =
540 array_state?(size_t)array_value:(size_t)va_arg(params, long);
543 case CURLFORM_STREAM:
544 current_form->flags |= HTTPPOST_CALLBACK;
545 if(current_form->userp)
546 return_value = CURL_FORMADD_OPTION_TWICE;
549 array_state?array_value:va_arg(params, char *);
551 current_form->userp = userp;
552 current_form->value = userp; /* this isn't strictly true but we
553 derive a value from this later on
554 and we need this non-NULL to be
555 accepted as a fine form part */
558 return_value = CURL_FORMADD_NULL;
562 case CURLFORM_CONTENTTYPE:
564 const char *contenttype =
565 array_state?array_value:va_arg(params, char *);
566 if(current_form->contenttype) {
567 if(current_form->flags & HTTPPOST_FILENAME) {
569 if((current_form = AddFormInfo(NULL,
571 current_form)) == NULL)
572 return_value = CURL_FORMADD_MEMORY;
575 return_value = CURL_FORMADD_NULL;
578 return_value = CURL_FORMADD_OPTION_TWICE;
582 current_form->contenttype = strdup(contenttype);
583 if(!current_form->contenttype)
584 return_value = CURL_FORMADD_MEMORY;
586 current_form->contenttype_alloc = TRUE;
589 return_value = CURL_FORMADD_NULL;
593 case CURLFORM_CONTENTHEADER:
595 /* this "cast increases required alignment of target type" but
596 we consider it OK anyway */
597 struct curl_slist* list = array_state?
598 (struct curl_slist*)array_value:
599 va_arg(params, struct curl_slist*);
601 if( current_form->contentheader )
602 return_value = CURL_FORMADD_OPTION_TWICE;
604 current_form->contentheader = list;
608 case CURLFORM_FILENAME:
610 const char *filename = array_state?array_value:
611 va_arg(params, char *);
612 if( current_form->showfilename )
613 return_value = CURL_FORMADD_OPTION_TWICE;
615 current_form->showfilename = strdup(filename);
616 if(!current_form->showfilename)
617 return_value = CURL_FORMADD_MEMORY;
619 current_form->showfilename_alloc = TRUE;
624 return_value = CURL_FORMADD_UNKNOWN_OPTION;
628 if(CURL_FORMADD_OK == return_value) {
629 /* go through the list, check for completeness and if everything is
630 * alright add the HttpPost item otherwise set return_value accordingly */
633 for(form = first_form;
636 if( ((!form->name || !form->value) && !post) ||
637 ( (form->contentslength) &&
638 (form->flags & HTTPPOST_FILENAME) ) ||
639 ( (form->flags & HTTPPOST_FILENAME) &&
640 (form->flags & HTTPPOST_PTRCONTENTS) ) ||
643 (form->flags & HTTPPOST_BUFFER) &&
644 (form->flags & HTTPPOST_PTRBUFFER) ) ||
646 ( (form->flags & HTTPPOST_READFILE) &&
647 (form->flags & HTTPPOST_PTRCONTENTS) )
649 return_value = CURL_FORMADD_INCOMPLETE;
653 if( ((form->flags & HTTPPOST_FILENAME) ||
654 (form->flags & HTTPPOST_BUFFER)) &&
655 !form->contenttype ) {
656 /* our contenttype is missing */
658 = strdup(ContentTypeForFilename(form->value, prevtype));
659 if(!form->contenttype) {
660 return_value = CURL_FORMADD_MEMORY;
663 form->contenttype_alloc = TRUE;
665 if( !(form->flags & HTTPPOST_PTRNAME) &&
666 (form == first_form) ) {
667 /* Note that there's small risk that form->name is NULL here if the
668 app passed in a bad combo, so we better check for that first. */
670 /* copy name (without strdup; possibly contains null characters) */
671 form->name = memdup(form->name, form->namelength);
673 return_value = CURL_FORMADD_MEMORY;
676 form->name_alloc = TRUE;
678 if( !(form->flags & (HTTPPOST_FILENAME | HTTPPOST_READFILE |
679 HTTPPOST_PTRCONTENTS | HTTPPOST_PTRBUFFER |
680 HTTPPOST_CALLBACK)) ) {
681 /* copy value (without strdup; possibly contains null characters) */
682 form->value = memdup(form->value, form->contentslength);
684 return_value = CURL_FORMADD_MEMORY;
687 form->value_alloc = TRUE;
689 post = AddHttpPost(form->name, form->namelength,
690 form->value, form->contentslength,
691 form->buffer, form->bufferlength,
692 form->contenttype, form->flags,
693 form->contentheader, form->showfilename,
699 return_value = CURL_FORMADD_MEMORY;
703 if(form->contenttype)
704 prevtype = form->contenttype;
710 /* we return on error, free possibly allocated fields */
716 if(form->value_alloc)
718 if(form->contenttype_alloc)
719 free(form->contenttype);
720 if(form->showfilename_alloc)
721 free(form->showfilename);
725 /* always delete the allocated memory before returning */
727 while(form != NULL) {
728 FormInfo *delete_form;
739 * curl_formadd() is a public API to add a section to the multipart formpost.
742 CURLFORMcode curl_formadd(struct curl_httppost **httppost,
743 struct curl_httppost **last_post,
748 va_start(arg, last_post);
749 result = FormAdd(httppost, last_post, arg);
755 * AddFormData() adds a chunk of data to the FormData linked list.
757 * size is incremented by the chunk length, unless it is NULL
759 static CURLcode AddFormData(struct FormData **formp,
765 struct FormData *newform = malloc(sizeof(struct FormData));
767 return CURLE_OUT_OF_MEMORY;
768 newform->next = NULL;
770 if(type <= FORM_CONTENT) {
771 /* we make it easier for plain strings: */
773 length = strlen((char *)line);
775 newform->line = malloc(length+1);
778 return CURLE_OUT_OF_MEMORY;
780 memcpy(newform->line, line, length);
781 newform->length = length;
782 newform->line[length]=0; /* zero terminate for easier debugging */
785 /* For callbacks and files we don't have any actual data so we just keep a
786 pointer to whatever this points to */
787 newform->line = (char *)line;
789 newform->type = type;
792 (*formp)->next = newform;
799 if(type != FORM_FILE)
800 /* for static content as well as callback data we add the size given
804 /* Since this is a file to be uploaded here, add the size of the actual
806 if(!strequal("-", newform->line)) {
808 if(!stat(newform->line, &file)) {
809 *size += file.st_size;
818 * AddFormDataf() adds printf()-style formatted data to the formdata chain.
821 static CURLcode AddFormDataf(struct FormData **formp,
823 const char *fmt, ...)
828 vsnprintf(s, sizeof(s), fmt, ap);
831 return AddFormData(formp, FORM_DATA, s, 0, size);
835 * Curl_formclean() is used from http.c, this cleans a built FormData linked
838 void Curl_formclean(struct FormData **form_ptr)
840 struct FormData *next, *form;
847 next=form->next; /* the following form line */
848 if(form->type <= FORM_CONTENT)
849 free(form->line); /* free the line */
850 free(form); /* free the struct */
852 } while((form = next) != NULL); /* continue */
859 * Serialize a curl_httppost struct.
860 * Returns 0 on success.
862 int curl_formget(struct curl_httppost *form, void *arg,
863 curl_formget_callback append)
867 struct FormData *data, *ptr;
869 rc = Curl_getformdata(NULL, &data, form, NULL, &size);
873 for(ptr = data; ptr; ptr = ptr->next) {
874 if(ptr->type == FORM_FILE) {
879 Curl_FormInit(&temp, ptr);
882 nread = readfromfile(&temp, buffer, sizeof(buffer));
883 if((nread == (size_t) -1) || (nread != append(arg, buffer, nread))) {
887 Curl_formclean(&data);
890 } while(nread == sizeof(buffer));
893 if(ptr->length != append(arg, ptr->line, ptr->length)) {
894 Curl_formclean(&data);
899 Curl_formclean(&data);
904 * curl_formfree() is an external function to free up a whole form post
907 void curl_formfree(struct curl_httppost *form)
909 struct curl_httppost *next;
912 /* no form to free, just get out of this */
916 next=form->next; /* the following form line */
918 /* recurse to sub-contents */
920 curl_formfree(form->more);
922 if( !(form->flags & HTTPPOST_PTRNAME) && form->name)
923 free(form->name); /* free the name */
924 if( !(form->flags & (HTTPPOST_PTRCONTENTS|HTTPPOST_CALLBACK)) &&
926 free(form->contents); /* free the contents */
927 if(form->contenttype)
928 free(form->contenttype); /* free the content type */
929 if(form->showfilename)
930 free(form->showfilename); /* free the faked file name */
931 free(form); /* free the struct */
933 } while((form = next) != NULL); /* continue */
936 #ifndef HAVE_BASENAME
938 (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
941 The basename() function shall take the pathname pointed to by path and
942 return a pointer to the final component of the pathname, deleting any
943 trailing '/' characters.
945 If the string pointed to by path consists entirely of the '/' character,
946 basename() shall return a pointer to the string "/". If the string pointed
947 to by path is exactly "//", it is implementation-defined whether '/' or "//"
950 If path is a null pointer or points to an empty string, basename() shall
951 return a pointer to the string ".".
953 The basename() function may modify the string pointed to by path, and may
954 return a pointer to static storage that may then be overwritten by a
955 subsequent call to basename().
957 The basename() function need not be reentrant. A function that is not
958 required to be reentrant is not required to be thread-safe.
961 static char *Curl_basename(char *path)
963 /* Ignore all the details above for now and make a quick and simple
964 implementaion here */
968 s1=strrchr(path, '/');
969 s2=strrchr(path, '\\');
972 path = (s1 > s2? s1 : s2)+1;
983 static char *strippath(const char *fullfile)
987 filename = strdup(fullfile); /* duplicate since basename() may ruin the
988 buffer it works on */
991 base = strdup(basename(filename));
993 free(filename); /* free temporary buffer */
995 return base; /* returns an allocated string or NULL ! */
999 * Curl_getformdata() converts a linked list of "meta data" into a complete
1000 * (possibly huge) multipart formdata. The input list is in 'post', while the
1001 * output resulting linked lists gets stored in '*finalform'. *sizep will get
1002 * the total size of the whole POST.
1003 * A multipart/form_data content-type is built, unless a custom content-type
1004 * is passed in 'custom_content_type'.
1006 * This function will not do a failf() for the potential memory failures but
1007 * should for all other errors it spots. Just note that this function MAY get
1008 * a NULL pointer in the 'data' argument.
1011 CURLcode Curl_getformdata(struct SessionHandle *data,
1012 struct FormData **finalform,
1013 struct curl_httppost *post,
1014 const char *custom_content_type,
1017 struct FormData *form = NULL;
1018 struct FormData *firstform;
1019 struct curl_httppost *file;
1020 CURLcode result = CURLE_OK;
1022 curl_off_t size=0; /* support potentially ENORMOUS formposts */
1024 char *fileboundary=NULL;
1025 struct curl_slist* curList;
1027 *finalform=NULL; /* default form is empty */
1030 return result; /* no input => no output! */
1032 boundary = Curl_FormBoundary();
1034 return CURLE_OUT_OF_MEMORY;
1036 /* Make the first line of the output */
1037 result = AddFormDataf(&form, NULL,
1038 "%s; boundary=%s\r\n",
1039 custom_content_type?custom_content_type:
1040 "Content-Type: multipart/form-data",
1047 /* we DO NOT include that line in the total size of the POST, since it'll be
1048 part of the header! */
1055 result = AddFormDataf(&form, &size, "\r\n");
1061 result = AddFormDataf(&form, &size, "--%s\r\n", boundary);
1065 /* Maybe later this should be disabled when a custom_content_type is
1066 passed, since Content-Disposition is not meaningful for all multipart
1069 result = AddFormDataf(&form, &size,
1070 "Content-Disposition: form-data; name=\"");
1074 result = AddFormData(&form, FORM_DATA, post->name, post->namelength,
1079 result = AddFormDataf(&form, &size, "\"");
1084 /* If used, this is a link to more file names, we must then do
1085 the magic to include several files with the same field name */
1087 fileboundary = Curl_FormBoundary();
1089 result = AddFormDataf(&form, &size,
1090 "\r\nContent-Type: multipart/mixed,"
1101 /* If 'showfilename' is set, that is a faked name passed on to us
1102 to use to in the formpost. If that is not set, the actually used
1103 local file name should be added. */
1106 /* if multiple-file */
1107 char *filebasename= NULL;
1108 if(!file->showfilename) {
1109 filebasename = strippath(file->contents);
1111 Curl_formclean(&firstform);
1113 return CURLE_OUT_OF_MEMORY;
1117 result = AddFormDataf(&form, &size,
1118 "\r\n--%s\r\nContent-Disposition: "
1119 "attachment; filename=\"%s\"",
1121 (file->showfilename?file->showfilename:
1128 else if(post->flags & (HTTPPOST_FILENAME|HTTPPOST_BUFFER|
1129 HTTPPOST_CALLBACK)) {
1130 /* it should be noted that for the HTTPPOST_FILENAME and
1131 HTTPPOST_CALLBACK cases the ->showfilename struct member is always
1132 assigned at this point */
1134 (!post->showfilename)?strippath(post->contents):NULL;
1136 result = AddFormDataf(&form, &size,
1137 "; filename=\"%s\"",
1138 (post->showfilename?post->showfilename:
1147 if(file->contenttype) {
1148 /* we have a specified type */
1149 result = AddFormDataf(&form, &size,
1150 "\r\nContent-Type: %s",
1156 curList = file->contentheader;
1158 /* Process the additional headers specified for this form */
1159 result = AddFormDataf( &form, &size, "\r\n%s", curList->data );
1162 curList = curList->next;
1165 Curl_formclean(&firstform);
1170 result = AddFormDataf(&form, &size, "\r\n\r\n");
1174 if((post->flags & HTTPPOST_FILENAME) ||
1175 (post->flags & HTTPPOST_READFILE)) {
1176 /* we should include the contents from the specified file */
1179 fileread = strequal("-", file->contents)?
1180 stdin:fopen(file->contents, "rb"); /* binary read for win32 */
1183 * VMS: This only allows for stream files on VMS. Stream files are
1184 * OK, as are FIXED & VAR files WITHOUT implied CC For implied CC,
1185 * every record needs to have a \n appended & 1 added to SIZE
1189 if(fileread != stdin) {
1190 /* close the file again */
1192 /* add the file name only - for later reading from this */
1193 result = AddFormData(&form, FORM_FILE, file->contents, 0, &size);
1196 /* When uploading from stdin, we can't know the size of the file,
1197 * thus must read the full file as before. We *could* use chunked
1198 * transfer-encoding, but that only works for HTTP 1.1 and we
1199 * can't be sure we work with such a server.
1203 while((nread = fread(buffer, 1, sizeof(buffer), fileread)) != 0) {
1204 result = AddFormData(&form, FORM_CONTENT, buffer, nread, &size);
1212 failf(data, "couldn't open file \"%s\"\n", file->contents);
1214 result = CURLE_READ_ERROR;
1217 else if(post->flags & HTTPPOST_BUFFER)
1218 /* include contents of buffer */
1219 result = AddFormData(&form, FORM_CONTENT, post->buffer,
1220 post->bufferlength, &size);
1221 else if(post->flags & HTTPPOST_CALLBACK)
1222 /* the contents should be read with the callback and the size
1223 is set with the contentslength */
1224 result = AddFormData(&form, FORM_CALLBACK, post->userp,
1225 post->contentslength, &size);
1227 /* include the contents we got */
1228 result = AddFormData(&form, FORM_CONTENT, post->contents,
1229 post->contentslength, &size);
1232 } while(file && !result); /* for each specified file for this field */
1235 Curl_formclean(&firstform);
1241 /* this was a multiple-file inclusion, make a termination file
1243 result = AddFormDataf(&form, &size,
1251 } while((post = post->next) != NULL); /* for each field */
1253 Curl_formclean(&firstform);
1258 /* end-boundary for everything */
1259 result = AddFormDataf(&form, &size,
1263 Curl_formclean(&firstform);
1272 *finalform=firstform;
1278 * Curl_FormInit() inits the struct 'form' points to with the 'formdata'
1279 * and resets the 'sent' counter.
1281 int Curl_FormInit(struct Form *form, struct FormData *formdata )
1284 return 1; /* error */
1286 form->data = formdata;
1289 form->fread_func = ZERO_NULL;
1294 static size_t readfromfile(struct Form *form, char *buffer,
1298 bool callback = (bool)(form->data->type == FORM_CALLBACK);
1301 nread = form->fread_func(buffer, 1, size, form->data->line);
1304 /* this file hasn't yet been opened */
1305 form->fp = fopen(form->data->line, "rb"); /* b is for binary */
1307 return (size_t)-1; /* failure */
1309 nread = fread(buffer, 1, size, form->fp);
1311 if(!nread || nread > size) {
1312 /* this is the last chunk from the file, move on */
1317 form->data = form->data->next;
1324 * Curl_FormReader() is the fread() emulation function that will be used to
1325 * deliver the formdata to the transfer loop and then sent away to the peer.
1327 size_t Curl_FormReader(char *buffer,
1336 form=(struct Form *)mydata;
1338 wantedsize = size * nitems;
1341 return 0; /* nothing, error, empty */
1343 if((form->data->type == FORM_FILE) ||
1344 (form->data->type == FORM_CALLBACK)) {
1345 gotsize = readfromfile(form, buffer, wantedsize);
1348 /* If positive or -1, return. If zero, continue! */
1353 if( (form->data->length - form->sent ) > wantedsize - gotsize) {
1355 memcpy(buffer + gotsize , form->data->line + form->sent,
1356 wantedsize - gotsize);
1358 form->sent += wantedsize-gotsize;
1363 memcpy(buffer+gotsize,
1364 form->data->line + form->sent,
1365 (form->data->length - form->sent) );
1366 gotsize += form->data->length - form->sent;
1370 form->data = form->data->next; /* advance */
1372 } while(form->data && (form->data->type < FORM_CALLBACK));
1373 /* If we got an empty line and we have more data, we proceed to the next
1374 line immediately to avoid returning zero before we've reached the end. */
1380 * Curl_formpostheader() returns the first line of the formpost, the
1381 * request-header part (which is not part of the request-body like the rest of
1384 char *Curl_formpostheader(void *formp, size_t *len)
1387 struct Form *form=(struct Form *)formp;
1390 return 0; /* nothing, ERROR! */
1392 header = form->data->line;
1393 *len = form->data->length;
1395 form->data = form->data->next; /* advance */
1400 #else /* CURL_DISABLE_HTTP */
1401 CURLFORMcode curl_formadd(struct curl_httppost **httppost,
1402 struct curl_httppost **last_post,
1407 return CURL_FORMADD_DISABLED;
1410 int curl_formget(struct curl_httppost *form, void *arg,
1411 curl_formget_callback append)
1416 return CURL_FORMADD_DISABLED;
1419 void curl_formfree(struct curl_httppost *form)
1422 /* does nothing HTTP is disabled */
1425 #endif /* CURL_DISABLE_HTTP */
1427 #if !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY)
1430 * Curl_FormBoundary() creates a suitable boundary string and returns an
1431 * allocated one. This is also used by SSL-code so it must be present even
1432 * if HTTP is disabled!
1434 char *Curl_FormBoundary(void)
1439 static const char table16[]="0123456789abcdef";
1441 retstring = malloc(BOUNDARY_LENGTH+1);
1444 return NULL; /* failed */
1446 strcpy(retstring, "----------------------------");
1448 for(i=strlen(retstring); i<BOUNDARY_LENGTH; i++)
1449 retstring[i] = table16[Curl_rand()%16];
1451 /* 28 dashes and 12 hexadecimal digits makes 12^16 (184884258895036416)
1453 retstring[BOUNDARY_LENGTH]=0; /* zero terminate */
1458 #endif /* !defined(CURL_DISABLE_HTTP) || defined(USE_SSLEAY) */