mirror of
https://github.com/moparisthebest/curl
synced 2025-03-11 07:39:50 -04:00
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.
This commit is contained in:
parent
98d9dc7840
commit
e8c442952d
@ -123,6 +123,7 @@ Content-Disposition: form-data; name="FILECONTENT"
|
|||||||
#include "curl_rand.h"
|
#include "curl_rand.h"
|
||||||
#include "strequal.h"
|
#include "strequal.h"
|
||||||
#include "curl_memory.h"
|
#include "curl_memory.h"
|
||||||
|
#include "sendf.h"
|
||||||
|
|
||||||
#define _MPRINTF_REPLACE /* use our functions only */
|
#define _MPRINTF_REPLACE /* use our functions only */
|
||||||
#include <curl/mprintf.h>
|
#include <curl/mprintf.h>
|
||||||
@ -975,7 +976,7 @@ int curl_formget(struct curl_httppost *form, void *arg,
|
|||||||
curl_off_t size;
|
curl_off_t size;
|
||||||
struct FormData *data, *ptr;
|
struct FormData *data, *ptr;
|
||||||
|
|
||||||
rc = Curl_getFormData(&data, form, NULL, &size);
|
rc = Curl_getformdata(NULL, &data, form, NULL, &size);
|
||||||
if(rc != CURLE_OK)
|
if(rc != CURLE_OK)
|
||||||
return (int)rc;
|
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
|
* (possibly huge) multipart formdata. The input list is in 'post', while the
|
||||||
* output resulting linked lists gets stored in '*finalform'. *sizep will get
|
* output resulting linked lists gets stored in '*finalform'. *sizep will get
|
||||||
* the total size of the whole POST.
|
* the total size of the whole POST.
|
||||||
* A multipart/form_data content-type is built, unless a custom content-type
|
* A multipart/form_data content-type is built, unless a custom content-type
|
||||||
* is passed in '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,
|
struct curl_httppost *post,
|
||||||
const char *custom_content_type,
|
const char *custom_content_type,
|
||||||
curl_off_t *sizep)
|
curl_off_t *sizep)
|
||||||
@ -1271,23 +1277,6 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
return result;
|
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");
|
result = AddFormDataf(&form, &size, "\r\n\r\n");
|
||||||
if(result)
|
if(result)
|
||||||
break;
|
break;
|
||||||
@ -1327,50 +1316,31 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(result) {
|
|
||||||
Curl_formclean(&firstform);
|
|
||||||
free(boundary);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef _FORM_DEBUG
|
if(data)
|
||||||
fprintf(stderr,
|
failf(data, "couldn't open file \"%s\"\n", file->contents);
|
||||||
"\n==> Curl_getFormData couldn't open/read \"%s\"\n",
|
|
||||||
file->contents);
|
|
||||||
#endif
|
|
||||||
Curl_formclean(&firstform);
|
|
||||||
free(boundary);
|
|
||||||
*finalform = NULL;
|
*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 */
|
/* include contents of buffer */
|
||||||
result = AddFormData(&form, FORM_CONTENT, post->buffer,
|
result = AddFormData(&form, FORM_CONTENT, post->buffer,
|
||||||
post->bufferlength, &size);
|
post->bufferlength, &size);
|
||||||
if(result)
|
else if(post->flags & HTTPPOST_CALLBACK)
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if(post->flags & HTTPPOST_CALLBACK) {
|
|
||||||
/* the contents should be read with the callback and the size
|
/* the contents should be read with the callback and the size
|
||||||
is set with the contentslength */
|
is set with the contentslength */
|
||||||
result = AddFormData(&form, FORM_CALLBACK, post->userp,
|
result = AddFormData(&form, FORM_CALLBACK, post->userp,
|
||||||
post->contentslength, &size);
|
post->contentslength, &size);
|
||||||
if(result)
|
else
|
||||||
break;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/* include the contents we got */
|
/* include the contents we got */
|
||||||
result = AddFormData(&form, FORM_CONTENT, post->contents,
|
result = AddFormData(&form, FORM_CONTENT, post->contents,
|
||||||
post->contentslength, &size);
|
post->contentslength, &size);
|
||||||
if(result)
|
|
||||||
break;
|
file = file->more;
|
||||||
}
|
} while(file && !result); /* for each specified file for this field */
|
||||||
} while((file = file->more) != NULL); /* for each specified file for this field */
|
|
||||||
if(result) {
|
if(result) {
|
||||||
Curl_formclean(&firstform);
|
Curl_formclean(&firstform);
|
||||||
free(boundary);
|
free(boundary);
|
||||||
@ -1666,11 +1636,11 @@ int main(int argc, argv_item_t argv[])
|
|||||||
CURLFORM_END))
|
CURLFORM_END))
|
||||||
++errors;
|
++errors;
|
||||||
|
|
||||||
rc = Curl_getFormData(&form, httppost, NULL, &size);
|
rc = Curl_getformdata(NULL, &form, httppost, NULL, &size);
|
||||||
if(rc != CURLE_OK) {
|
if(rc != CURLE_OK) {
|
||||||
if(rc != CURLE_READ_ERROR) {
|
if(rc != CURLE_READ_ERROR) {
|
||||||
const char *errortext = curl_easy_strerror(rc);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -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
|
* This software is licensed as described in the file COPYING, which
|
||||||
* you should have received as part of this distribution. The terms
|
* 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 );
|
int Curl_FormInit(struct Form *form, struct FormData *formdata );
|
||||||
|
|
||||||
CURLcode
|
CURLcode Curl_getformdata(struct SessionHandle *data,
|
||||||
Curl_getFormData(struct FormData **,
|
struct FormData **,
|
||||||
struct curl_httppost *post,
|
struct curl_httppost *post,
|
||||||
const char *custom_contenttype,
|
const char *custom_contenttype,
|
||||||
curl_off_t *size);
|
curl_off_t *size);
|
||||||
|
|
||||||
/* fread() emulation */
|
/* fread() emulation */
|
||||||
size_t Curl_FormReader(char *buffer,
|
size_t Curl_FormReader(char *buffer,
|
||||||
|
18
lib/http.c
18
lib/http.c
@ -2375,19 +2375,15 @@ CURLcode Curl_http(struct connectdata *conn, bool *done)
|
|||||||
#endif /* CURL_DISABLE_PROXY */
|
#endif /* CURL_DISABLE_PROXY */
|
||||||
|
|
||||||
if(HTTPREQ_POST_FORM == httpreq) {
|
if(HTTPREQ_POST_FORM == httpreq) {
|
||||||
/* we must build the whole darned post sequence first, so that we have
|
/* we must build the whole post sequence first, so that we have a size of
|
||||||
a size of the whole shebang before we start to send it */
|
the whole transfer before we start to send it */
|
||||||
result = Curl_getFormData(&http->sendit, data->set.httppost,
|
result = Curl_getformdata(data, &http->sendit, data->set.httppost,
|
||||||
Curl_checkheaders(data, "Content-Type:"),
|
Curl_checkheaders(data, "Content-Type:"),
|
||||||
&http->postsize);
|
&http->postsize);
|
||||||
if(CURLE_OK != result) {
|
if(result)
|
||||||
/* Curl_getFormData() doesn't use failf() */
|
return result;
|
||||||
failf(data, "failed creating formpost data");
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
http->p_accept = Curl_checkheaders(data, "Accept:")?NULL:"Accept: */*\r\n";
|
http->p_accept = Curl_checkheaders(data, "Accept:")?NULL:"Accept: */*\r\n";
|
||||||
|
|
||||||
if(( (HTTPREQ_POST == httpreq) ||
|
if(( (HTTPREQ_POST == httpreq) ||
|
||||||
|
Loading…
x
Reference in New Issue
Block a user