#include "test.h"
#include "memdebug.h"
static char data[] =
#ifdef CURL_DOES_CONVERSIONS
"\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
"\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
"\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72";
#else
"this is what we post to the silly web server";
#endif
static const char name[] = "fieldname";
static size_t count_chars(void *userp, const char *buf, size_t len)
{
size_t *pcounter = (size_t *) userp;
(void) buf;
*pcounter += len;
return len;
}
int test(char *URL)
{
CURL *curl = NULL;
CURLcode res = TEST_ERR_MAJOR_BAD;
CURLFORMcode formrc;
struct curl_slist *headers, *headers2 = NULL;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_forms formarray[3];
size_t formlength = 0;
char flbuf[32];
long contentlength = 0;
if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed\n");
return TEST_ERR_MAJOR_BAD;
}
headers = curl_slist_append(NULL, "X-customheader-1: Header 1 data");
if(!headers) {
goto test_cleanup;
}
headers2 = curl_slist_append(headers, "X-customheader-2: Header 2 data");
if(!headers2) {
goto test_cleanup;
}
headers = headers2;
headers2 = curl_slist_append(headers, "Content-Type: text/plain");
if(!headers2) {
goto test_cleanup;
}
headers = headers2;
formrc = curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, &name,
CURLFORM_COPYCONTENTS, &data,
CURLFORM_CONTENTHEADER, headers,
CURLFORM_END);
if(formrc) {
printf("curl_formadd(1) = %d\n", (int) formrc);
goto test_cleanup;
}
contentlength = (long)(strlen(data) - 1);
formarray[0].option = CURLFORM_PTRCONTENTS;
formarray[0].value = data;
formarray[1].option = CURLFORM_CONTENTSLENGTH;
formarray[1].value = (char *)(size_t)contentlength;
formarray[2].option = CURLFORM_END;
formarray[2].value = NULL;
formrc = curl_formadd(&formpost,
&lastptr,
CURLFORM_PTRNAME, name,
CURLFORM_NAMELENGTH, strlen(name) - 1,
CURLFORM_ARRAY, formarray,
CURLFORM_FILENAME, "remotefile.txt",
CURLFORM_END);
if(formrc) {
printf("curl_formadd(2) = %d\n", (int) formrc);
goto test_cleanup;
}
data[0]++;
formrc = curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "multifile",
CURLFORM_FILE, libtest_arg2,
CURLFORM_FILE, libtest_arg2,
CURLFORM_CONTENTTYPE, "text/whatever",
CURLFORM_FILE, libtest_arg2,
CURLFORM_END);
if(formrc) {
printf("curl_formadd(3) = %d\n", (int) formrc);
goto test_cleanup;
}
formrc = curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filecontents",
CURLFORM_FILECONTENT, libtest_arg2,
CURLFORM_END);
if(formrc) {
printf("curl_formadd(4) = %d\n", (int) formrc);
goto test_cleanup;
}
curl_formget(formpost, (void *) &formlength, count_chars);
curl_msnprintf(flbuf, sizeof(flbuf), "%lu", (unsigned long) formlength);
formrc = curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "formlength",
CURLFORM_COPYCONTENTS, &flbuf,
CURLFORM_END);
if(formrc) {
printf("curl_formadd(5) = %d\n", (int) formrc);
goto test_cleanup;
}
formrc = curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "standardinput",
CURLFORM_FILE, "-",
CURLFORM_END);
if(formrc) {
printf("curl_formadd(6) = %d\n", (int) formrc);
goto test_cleanup;
}
curl = curl_easy_init();
if(!curl) {
fprintf(stderr, "curl_easy_init() failed\n");
goto test_cleanup;
}
test_setopt(curl, CURLOPT_URL, URL);
test_setopt(curl, CURLOPT_HTTPPOST, formpost);
test_setopt(curl, CURLOPT_VERBOSE, 1L);
test_setopt(curl, CURLOPT_HEADER, 1L);
res = curl_easy_perform(curl);
test_cleanup:
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all(headers);
curl_global_cleanup();
return res;
}