aboutsummaryrefslogtreecommitdiff
path: root/lib/formdata.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/formdata.c')
-rw-r--r--lib/formdata.c106
1 files changed, 81 insertions, 25 deletions
diff --git a/lib/formdata.c b/lib/formdata.c
index 4c53045f2..2d98870e0 100644
--- a/lib/formdata.c
+++ b/lib/formdata.c
@@ -24,7 +24,7 @@
/*
Debug the form generator stand-alone by compiling this source file with:
- gcc -DHAVE_CONFIG_H -I../ -g -D_FORM_DEBUG -o formdata -I../include formdata.c strequal.c
+ gcc -DHAVE_CONFIG_H -I../ -g -D_FORM_DEBUG -DCURLDEBUG -o formdata -I../include formdata.c strequal.c memdebug.c mprintf.c strerror.c
run the 'formdata' executable the output should end with:
All Tests seem to have worked ...
@@ -63,7 +63,7 @@ Content-Type: multipart/mixed, boundary=curlz1s0dkticx49MV1KGcYP5cvfSsz
Content-Disposition: attachment; filename="inet_ntoa_r.h"
Content-Type: text/plain
...
-Content-Disposition: attachment; filename="Makefile.b32.resp"
+Content-Disposition: attachment; filename="Makefile.b32"
Content-Type: text/plain
...
@@ -73,7 +73,7 @@ Content-Type: multipart/mixed, boundary=curlirkYPmPwu6FrJ1vJ1u1BmtIufh1
Content-Disposition: attachment; filename="inet_ntoa_r.h"
Content-Type: text/plain
...
-Content-Disposition: attachment; filename="Makefile.b32.resp"
+Content-Disposition: attachment; filename="Makefile.b32"
Content-Type: text/plain
...
Content-Disposition: attachment; filename="inet_ntoa_r.h"
@@ -87,7 +87,7 @@ Content-Type: multipart/mixed, boundary=curlirkYPmPwu6FrJ1vJ1u1BmtIufh1
Content-Disposition: attachment; filename="inet_ntoa_r.h"
Content-Type: text/plain
...
-Content-Disposition: attachment; filename="Makefile.b32.resp"
+Content-Disposition: attachment; filename="Makefile.b32"
Content-Type: text/plain
...
Content-Disposition: attachment; filename="inet_ntoa_r.h"
@@ -118,6 +118,8 @@ Content-Disposition: form-data; name="FILECONTENT"
#if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
#include <libgen.h>
#endif
+#include "urldata.h" /* for struct SessionHandle */
+#include "easyif.h" /* for Curl_convert_... prototypes */
#include "formdata.h"
#include "strequal.h"
#include "memory.h"
@@ -452,7 +454,12 @@ CURLFORMcode FormAdd(struct curl_httppost **httppost,
* Set the Name property.
*/
case CURLFORM_PTRNAME:
+#ifdef CURL_DOES_CONVERSIONS
+ /* treat CURLFORM_PTR like CURLFORM_COPYNAME so we'll
+ have safe memory for the eventual conversion */
+#else
current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
+#endif
case CURLFORM_COPYNAME:
if (current_form->name)
return_value = CURL_FORMADD_OPTION_TWICE;
@@ -835,7 +842,7 @@ static CURLcode AddFormData(struct FormData **formp,
*formp = newform;
if (size) {
- if(type == FORM_DATA)
+ if((type == FORM_DATA) || (type == FORM_CONTENT))
*size += length;
else {
/* Since this is a file to be uploaded here, add the size of the actual
@@ -872,10 +879,11 @@ static CURLcode AddFormDataf(struct FormData **formp,
* Curl_formclean() is used from http.c, this cleans a built FormData linked
* list
*/
-void Curl_formclean(struct FormData *form)
+void Curl_formclean(struct FormData **form_ptr)
{
- struct FormData *next;
+ struct FormData *next, *form;
+ form = *form_ptr;
if(!form)
return;
@@ -885,8 +893,40 @@ void Curl_formclean(struct FormData *form)
free(form); /* free the struct */
} while ((form = next) != NULL); /* continue */
+
+ *form_ptr = NULL;
}
+#ifdef CURL_DOES_CONVERSIONS
+/*
+ * Curl_formcovert() is used from http.c, this converts any
+ form items that need to be sent in the network encoding.
+ Returns CURLE_OK on success.
+ */
+CURLcode Curl_formconvert(struct SessionHandle *data, struct FormData *form)
+{
+ struct FormData *next;
+ CURLcode rc;
+
+ if(!form)
+ return CURLE_OK;
+
+ if(!data)
+ return CURLE_BAD_FUNCTION_ARGUMENT;
+
+ do {
+ next=form->next; /* the following form line */
+ if (form->type == FORM_DATA) {
+ rc = Curl_convert_to_network(data, form->line, form->length);
+ /* Curl_convert_to_network calls failf if unsuccessful */
+ if (rc != CURLE_OK)
+ return rc;
+ }
+ } while ((form = next) != NULL); /* continue */
+ return CURLE_OK;
+}
+#endif /* CURL_DOES_CONVERSIONS */
+
/*
* curl_formget()
* Serialize a curl_httppost struct.
@@ -917,18 +957,18 @@ int curl_formget(struct curl_httppost *form, void *arg,
if (temp.fp) {
fclose(temp.fp);
}
- Curl_formclean(data);
+ Curl_formclean(&data);
return -1;
}
} while (read == sizeof(buffer));
} else {
if (ptr->length != append(arg, ptr->line, ptr->length)) {
- Curl_formclean(data);
+ Curl_formclean(&data);
return -1;
}
}
}
- Curl_formclean(data);
+ Curl_formclean(&data);
return 0;
}
@@ -1179,7 +1219,7 @@ CURLcode Curl_getFormData(struct FormData **finalform,
curList = curList->next;
}
if (result) {
- Curl_formclean(firstform);
+ Curl_formclean(&firstform);
free(boundary);
return result;
}
@@ -1194,7 +1234,10 @@ CURLcode Curl_getFormData(struct FormData **finalform,
if(file->contenttype &&
!checkprefix("text/", file->contenttype)) {
/* this is not a text content, mention our binary encoding */
- size += AddFormData(&form, "\r\nContent-Transfer-Encoding: binary", 0);
+ result = AddFormDataf(&form, &size,
+ "\r\nContent-Transfer-Encoding: binary");
+ if (result)
+ break;
}
#endif
@@ -1232,21 +1275,26 @@ CURLcode Curl_getFormData(struct FormData **finalform,
size_t nread;
char buffer[512];
while ((nread = fread(buffer, 1, sizeof(buffer), fileread)) != 0) {
- result = AddFormData(&form, FORM_DATA, buffer, nread, &size);
+ result = AddFormData(&form, FORM_CONTENT, buffer, nread, &size);
if (result)
break;
}
}
if (result) {
- Curl_formclean(firstform);
+ Curl_formclean(&firstform);
free(boundary);
return result;
}
}
else {
- Curl_formclean(firstform);
+#ifdef _FORM_DEBUG
+ fprintf(stderr,
+ "\n==> Curl_getFormData couldn't open/read \"%s\"\n",
+ file->contents);
+#endif
+ Curl_formclean(&firstform);
free(boundary);
*finalform = NULL;
return CURLE_READ_ERROR;
@@ -1255,7 +1303,7 @@ CURLcode Curl_getFormData(struct FormData **finalform,
}
else if (post->flags & HTTPPOST_BUFFER) {
/* include contents of buffer */
- result = AddFormData(&form, FORM_DATA, post->buffer,
+ result = AddFormData(&form, FORM_CONTENT, post->buffer,
post->bufferlength, &size);
if (result)
break;
@@ -1263,14 +1311,14 @@ CURLcode Curl_getFormData(struct FormData **finalform,
else {
/* include the contents we got */
- result = AddFormData(&form, FORM_DATA, post->contents,
+ 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 */
if (result) {
- Curl_formclean(firstform);
+ Curl_formclean(&firstform);
free(boundary);
return result;
}
@@ -1288,7 +1336,7 @@ CURLcode Curl_getFormData(struct FormData **finalform,
} while ((post = post->next) != NULL); /* for each field */
if (result) {
- Curl_formclean(firstform);
+ Curl_formclean(&firstform);
free(boundary);
return result;
}
@@ -1298,7 +1346,7 @@ CURLcode Curl_getFormData(struct FormData **finalform,
"\r\n--%s--\r\n",
boundary);
if (result) {
- Curl_formclean(firstform);
+ Curl_formclean(&firstform);
free(boundary);
return result;
}
@@ -1397,7 +1445,7 @@ size_t Curl_FormReader(char *buffer,
form->data = form->data->next; /* advance */
- } while(form->data && (form->data->type == FORM_DATA));
+ } while(form->data && (form->data->type != FORM_FILE));
/* If we got an empty line and we have more data, we proceed to the next
line immediately to avoid returning zero before we've reached the end.
This is the bug reported November 22 1999 on curl 6.3. (Daniel) */
@@ -1464,7 +1512,7 @@ int main()
char value5[] = "value for PTRCONTENTS + CONTENTSLENGTH";
char value6[] = "value for PTRCOTNENTS + CONTENTSLENGTH + CONTENTTYPE";
char value7[] = "inet_ntoa_r.h";
- char value8[] = "Makefile.b32.resp";
+ char value8[] = "Makefile.b32";
char type2[] = "image/gif";
char type6[] = "text/plain";
char type7[] = "text/html";
@@ -1473,7 +1521,8 @@ int main()
int value5length = strlen(value4);
int value6length = strlen(value5);
int errors = 0;
- int size;
+ CURLcode rc;
+ size_t size;
size_t nread;
char buffer[4096];
struct curl_httppost *httppost=NULL;
@@ -1549,7 +1598,14 @@ int main()
CURLFORM_END))
++errors;
- form=Curl_getFormData(httppost, &size);
+ rc = Curl_getFormData(&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);
+ }
+ return 0;
+ }
Curl_FormInit(&formread, form);
@@ -1557,7 +1613,7 @@ int main()
nread = Curl_FormReader(buffer, 1, sizeof(buffer),
(FILE *)&formread);
- if(-1 == nread)
+ if(nread < 1)
break;
fwrite(buffer, nread, 1, stdout);
} while(1);