Commit e8c44295 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

formdata: provide error message

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.
parent 98d9dc78
Loading
Loading
Loading
Loading
+21 −51
Original line number Diff line number Diff line
@@ -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;
  }
+6 −6
Original line number Diff line number Diff line
@@ -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,8 +70,8 @@ typedef struct FormInfo {

int Curl_FormInit(struct Form *form, struct FormData *formdata );

CURLcode
Curl_getFormData(struct FormData **,
CURLcode Curl_getformdata(struct SessionHandle *data,
                          struct FormData **,
                          struct curl_httppost *post,
                          const char *custom_contenttype,
                          curl_off_t *size);
+7 −11
Original line number Diff line number Diff line
@@ -2375,18 +2375,14 @@ 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,
    /* 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(CURLE_OK != result) {
       /* Curl_getFormData() doesn't use failf() */
       failf(data, "failed creating formpost data");
    if(result)
      return result;
  }
  }


  http->p_accept = Curl_checkheaders(data, "Accept:")?NULL:"Accept: */*\r\n";