]> granicus.if.org Git - curl/commitdiff
formdata: provide error message
authorDaniel Stenberg <daniel@haxx.se>
Wed, 20 Oct 2010 12:57:43 +0000 (14:57 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 20 Oct 2010 12:57:43 +0000 (14:57 +0200)
When failing to build form post due to an error, the code now does a
proper failf(). Previously libcurl would report an error like "failed
creating formpost data" when a file wasn't possible to open which was
not easy for users to figure out.

I also lower cased a function name to be named more curl-style and
removed some unnecessary code.

lib/formdata.c
lib/formdata.h
lib/http.c

index c98246e207d5d4859d55c7730ef1c248cd135e40..07d7a6b42aa0f804b4716ee5b93184127b3711de 100644 (file)
@@ -123,6 +123,7 @@ Content-Disposition: form-data; name="FILECONTENT"
 #include "curl_rand.h"
 #include "strequal.h"
 #include "curl_memory.h"
+#include "sendf.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -975,7 +976,7 @@ int curl_formget(struct curl_httppost *form, void *arg,
   curl_off_t size;
   struct FormData *data, *ptr;
 
-  rc = Curl_getFormData(&data, form, NULL, &size);
+  rc = Curl_getformdata(NULL, &data, form, NULL, &size);
   if(rc != CURLE_OK)
     return (int)rc;
 
@@ -1105,15 +1106,20 @@ static char *strippath(const char *fullfile)
 }
 
 /*
- * Curl_getFormData() converts a linked list of "meta data" into a complete
+ * Curl_getformdata() converts a linked list of "meta data" into a complete
  * (possibly huge) multipart formdata. The input list is in 'post', while the
  * output resulting linked lists gets stored in '*finalform'. *sizep will get
  * the total size of the whole POST.
  * A multipart/form_data content-type is built, unless a custom content-type
  * is passed in 'custom_content_type'.
+ *
+ * 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.
  */
 
-CURLcode Curl_getFormData(struct FormData **finalform,
+CURLcode Curl_getformdata(struct SessionHandle *data,
+                          struct FormData **finalform,
                           struct curl_httppost *post,
                           const char *custom_content_type,
                           curl_off_t *sizep)
@@ -1271,23 +1277,6 @@ CURLcode Curl_getFormData(struct FormData **finalform,
         return result;
       }
 
-#if 0
-      /* The header Content-Transfer-Encoding: seems to confuse some receivers
-       * (like the built-in PHP engine). While I can't see any reason why it
-       * should, I can just as well skip this to the benefit of the users who
-       * are using such confused receivers.
-       */
-
-      if(file->contenttype &&
-         !checkprefix("text/", file->contenttype)) {
-        /* this is not a text content, mention our binary encoding */
-        result = AddFormDataf(&form, &size,
-                              "\r\nContent-Transfer-Encoding: binary");
-        if(result)
-          break;
-      }
-#endif
-
       result = AddFormDataf(&form, &size, "\r\n\r\n");
       if(result)
         break;
@@ -1327,50 +1316,31 @@ CURLcode Curl_getFormData(struct FormData **finalform,
                 break;
             }
           }
-
-          if(result) {
-            Curl_formclean(&firstform);
-            free(boundary);
-            return result;
-          }
-
         }
         else {
-#ifdef _FORM_DEBUG
-          fprintf(stderr,
-                  "\n==> Curl_getFormData couldn't open/read \"%s\"\n",
-                  file->contents);
-#endif
-          Curl_formclean(&firstform);
-          free(boundary);
+          if(data)
+            failf(data, "couldn't open file \"%s\"\n", file->contents);
           *finalform = NULL;
-          return CURLE_READ_ERROR;
+          result = CURLE_READ_ERROR;
         }
-
       }
-      else if(post->flags & HTTPPOST_BUFFER) {
+      else if(post->flags & HTTPPOST_BUFFER)
         /* include contents of buffer */
         result = AddFormData(&form, FORM_CONTENT, post->buffer,
                              post->bufferlength, &size);
-          if(result)
-            break;
-      }
-      else if(post->flags & HTTPPOST_CALLBACK) {
+      else if(post->flags & HTTPPOST_CALLBACK)
         /* the contents should be read with the callback and the size
            is set with the contentslength */
         result = AddFormData(&form, FORM_CALLBACK, post->userp,
                              post->contentslength, &size);
-        if(result)
-          break;
-      }
-      else {
+      else
         /* include the contents we got */
         result = AddFormData(&form, FORM_CONTENT, post->contents,
                              post->contentslength, &size);
-        if(result)
-          break;
-      }
-    } while((file = file->more) != NULL); /* for each specified file for this field */
+
+      file = file->more;
+    } while(file && !result); /* for each specified file for this field */
+
     if(result) {
       Curl_formclean(&firstform);
       free(boundary);
@@ -1666,11 +1636,11 @@ int main(int argc, argv_item_t argv[])
                   CURLFORM_END))
     ++errors;
 
-  rc = Curl_getFormData(&form, httppost, NULL, &size);
+  rc = Curl_getformdata(NULL, &form, httppost, NULL, &size);
   if(rc != CURLE_OK) {
     if(rc != CURLE_READ_ERROR) {
       const char *errortext = curl_easy_strerror(rc);
-      fprintf(stdout, "\n==> Curl_getFormData error: %s\n", errortext);
+      fprintf(stdout, "\n==> Curl_getformdata error: %s\n", errortext);
     }
     return 0;
   }
index 0c9db44236831d887c18e2286111fea1a2731e5b..22f504bb3d6eb5bcc02107a8d87769afbb791d60 100644 (file)
@@ -7,7 +7,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -70,11 +70,11 @@ typedef struct FormInfo {
 
 int Curl_FormInit(struct Form *form, struct FormData *formdata );
 
-CURLcode
-Curl_getFormData(struct FormData **,
-                 struct curl_httppost *post,
-                 const char *custom_contenttype,
-                 curl_off_t *size);
+CURLcode Curl_getformdata(struct SessionHandle *data,
+                          struct FormData **,
+                          struct curl_httppost *post,
+                          const char *custom_contenttype,
+                          curl_off_t *size);
 
 /* fread() emulation */
 size_t Curl_FormReader(char *buffer,
index 413ef3d891c5f24cc6f7ad3e9819c2a56ad954a0..ed0730c0a1d8168ec19e72956bea19383c060d71 100644 (file)
@@ -2375,19 +2375,15 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
 #endif /* CURL_DISABLE_PROXY */
 
   if(HTTPREQ_POST_FORM == httpreq) {
-    /* we must build the whole darned post sequence first, so that we have
-       a size of the whole shebang before we start to send it */
-     result = Curl_getFormData(&http->sendit, data->set.httppost,
-                               Curl_checkheaders(data, "Content-Type:"),
-                               &http->postsize);
-     if(CURLE_OK != result) {
-       /* Curl_getFormData() doesn't use failf() */
-       failf(data, "failed creating formpost data");
-       return result;
-     }
+    /* we must build the whole post sequence first, so that we have a size of
+       the whole transfer before we start to send it */
+    result = Curl_getformdata(data, &http->sendit, data->set.httppost,
+                              Curl_checkheaders(data, "Content-Type:"),
+                              &http->postsize);
+    if(result)
+      return result;
   }
 
-
   http->p_accept = Curl_checkheaders(data, "Accept:")?NULL:"Accept: */*\r\n";
 
   if(( (HTTPREQ_POST == httpreq) ||