5
5
* | (__| |_| | _ <| |___
6
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
8
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
10
10
* This software is licensed as described in the file COPYING, which
11
11
* you should have received as part of this distribution. The terms
18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: formdata.c,v 1.94 2006-07-27 22:35:09 bagder Exp $
21
* $Id: formdata.c,v 1.99 2007-03-31 21:01:18 bagder Exp $
22
22
***************************************************************************/
25
25
Debug the form generator stand-alone by compiling this source file with:
27
gcc -DHAVE_CONFIG_H -I../ -g -D_FORM_DEBUG -o formdata -I../include formdata.c strequal.c
27
gcc -DHAVE_CONFIG_H -I../ -g -D_FORM_DEBUG -DCURLDEBUG -o formdata -I../include formdata.c strequal.c memdebug.c mprintf.c strerror.c
29
29
run the 'formdata' executable the output should end with:
30
30
All Tests seem to have worked ...
73
73
Content-Disposition: attachment; filename="inet_ntoa_r.h"
74
74
Content-Type: text/plain
76
Content-Disposition: attachment; filename="Makefile.b32.resp"
76
Content-Disposition: attachment; filename="Makefile.b32"
77
77
Content-Type: text/plain
79
79
Content-Disposition: attachment; filename="inet_ntoa_r.h"
87
87
Content-Disposition: attachment; filename="inet_ntoa_r.h"
88
88
Content-Type: text/plain
90
Content-Disposition: attachment; filename="Makefile.b32.resp"
90
Content-Disposition: attachment; filename="Makefile.b32"
91
91
Content-Type: text/plain
93
93
Content-Disposition: attachment; filename="inet_ntoa_r.h"
283
282
text/plain so we don't actually need to set this: */
284
283
contenttype = HTTPPOST_CONTENTTYPE_DEFAULT;
286
for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
287
if(strlen(filename) >= strlen(ctts[i].extension)) {
288
if(strequal(filename +
289
strlen(filename) - strlen(ctts[i].extension),
290
ctts[i].extension)) {
291
contenttype = ctts[i].type;
285
if(filename) { /* in case a NULL was passed in */
286
for(i=0; i<sizeof(ctts)/sizeof(ctts[0]); i++) {
287
if(strlen(filename) >= strlen(ctts[i].extension)) {
288
if(strequal(filename +
289
strlen(filename) - strlen(ctts[i].extension),
290
ctts[i].extension)) {
291
contenttype = ctts[i].type;
452
457
* Set the Name property.
454
459
case CURLFORM_PTRNAME:
460
#ifdef CURL_DOES_CONVERSIONS
461
/* treat CURLFORM_PTR like CURLFORM_COPYNAME so we'll
462
have safe memory for the eventual conversion */
455
464
current_form->flags |= HTTPPOST_PTRNAME; /* fall through */
456
466
case CURLFORM_COPYNAME:
457
467
if (current_form->name)
458
468
return_value = CURL_FORMADD_OPTION_TWICE;
528
538
if (current_form->value) {
529
539
if (current_form->flags & HTTPPOST_FILENAME) {
531
if (!(current_form = AddFormInfo(strdup(filename),
532
NULL, current_form)))
541
if ((current_form = AddFormInfo(strdup(filename),
542
NULL, current_form)) == NULL)
533
543
return_value = CURL_FORMADD_MEMORY;
562
572
if (current_form->value) {
563
573
if (current_form->flags & HTTPPOST_BUFFER) {
565
if (!(current_form = AddFormInfo(strdup(filename),
566
NULL, current_form)))
575
if ((current_form = AddFormInfo(strdup(filename),
576
NULL, current_form)) == NULL)
567
577
return_value = CURL_FORMADD_MEMORY;
614
624
if (current_form->contenttype) {
615
625
if (current_form->flags & HTTPPOST_FILENAME) {
616
626
if (contenttype) {
617
if (!(current_form = AddFormInfo(NULL,
627
if ((current_form = AddFormInfo(NULL,
629
current_form)) == NULL)
620
630
return_value = CURL_FORMADD_MEMORY;
872
882
* Curl_formclean() is used from http.c, this cleans a built FormData linked
875
void Curl_formclean(struct FormData *form)
885
void Curl_formclean(struct FormData **form_ptr)
887
struct FormData *next, *form;
894
next=form->next; /* the following form line */
895
free(form->line); /* free the line */
896
free(form); /* free the struct */
898
} while ((form = next) != NULL); /* continue */
903
#ifdef CURL_DOES_CONVERSIONS
905
* Curl_formcovert() is used from http.c, this converts any
906
form items that need to be sent in the network encoding.
907
Returns CURLE_OK on success.
909
CURLcode Curl_formconvert(struct SessionHandle *data, struct FormData *form)
877
911
struct FormData *next;
918
return CURLE_BAD_FUNCTION_ARGUMENT;
883
921
next=form->next; /* the following form line */
884
free(form->line); /* free the line */
885
free(form); /* free the struct */
887
} while((form=next)); /* continue */
922
if (form->type == FORM_DATA) {
923
rc = Curl_convert_to_network(data, form->line, form->length);
924
/* Curl_convert_to_network calls failf if unsuccessful */
928
} while ((form = next) != NULL); /* continue */
931
#endif /* CURL_DOES_CONVERSIONS */
1194
1237
if(file->contenttype &&
1195
1238
!checkprefix("text/", file->contenttype)) {
1196
1239
/* this is not a text content, mention our binary encoding */
1197
size += AddFormData(&form, "\r\nContent-Transfer-Encoding: binary", 0);
1240
result = AddFormDataf(&form, &size,
1241
"\r\nContent-Transfer-Encoding: binary");
1233
1279
char buffer[512];
1234
while((nread = fread(buffer, 1, sizeof(buffer), fileread))) {
1235
result = AddFormData(&form, FORM_DATA, buffer, nread, &size);
1280
while ((nread = fread(buffer, 1, sizeof(buffer), fileread)) != 0) {
1281
result = AddFormData(&form, FORM_CONTENT, buffer, nread, &size);
1242
Curl_formclean(firstform);
1288
Curl_formclean(&firstform);
1243
1289
free(boundary);
1249
Curl_formclean(firstform);
1297
"\n==> Curl_getFormData couldn't open/read \"%s\"\n",
1300
Curl_formclean(&firstform);
1250
1301
free(boundary);
1251
1302
*finalform = NULL;
1252
1303
return CURLE_READ_ERROR;
1256
1307
else if (post->flags & HTTPPOST_BUFFER) {
1257
1308
/* include contents of buffer */
1258
result = AddFormData(&form, FORM_DATA, post->buffer,
1309
result = AddFormData(&form, FORM_CONTENT, post->buffer,
1259
1310
post->bufferlength, &size);
1265
1316
/* include the contents we got */
1266
result = AddFormData(&form, FORM_DATA, post->contents,
1317
result = AddFormData(&form, FORM_CONTENT, post->contents,
1267
1318
post->contentslength, &size);
1271
} while((file = file->more)); /* for each specified file for this field */
1322
} while ((file = file->more) != NULL); /* for each specified file for this field */
1273
Curl_formclean(firstform);
1324
Curl_formclean(&firstform);
1274
1325
free(boundary);
1398
1449
form->data = form->data->next; /* advance */
1400
} while(form->data && (form->data->type == FORM_DATA));
1451
} while(form->data && (form->data->type != FORM_FILE));
1401
1452
/* If we got an empty line and we have more data, we proceed to the next
1402
1453
line immediately to avoid returning zero before we've reached the end.
1403
1454
This is the bug reported November 22 1999 on curl 6.3. (Daniel) */
1464
1515
char value5[] = "value for PTRCONTENTS + CONTENTSLENGTH";
1465
1516
char value6[] = "value for PTRCOTNENTS + CONTENTSLENGTH + CONTENTTYPE";
1466
1517
char value7[] = "inet_ntoa_r.h";
1467
char value8[] = "Makefile.b32.resp";
1518
char value8[] = "Makefile.b32";
1468
1519
char type2[] = "image/gif";
1469
1520
char type6[] = "text/plain";
1470
1521
char type7[] = "text/html";
1552
form=Curl_getFormData(httppost, &size);
1604
rc = Curl_getFormData(&form, httppost, NULL, &size);
1605
if(rc != CURLE_OK) {
1606
if(rc != CURLE_READ_ERROR) {
1607
const char *errortext = curl_easy_strerror(rc);
1608
fprintf(stdout, "\n==> Curl_getFormData error: %s\n", errortext);
1554
1613
Curl_FormInit(&formread, form);
1557
1616
nread = Curl_FormReader(buffer, 1, sizeof(buffer),
1558
1617
(FILE *)&formread);
1562
1621
fwrite(buffer, nread, 1, stdout);