00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "test.h"
00023
00024 #include "memdebug.h"
00025
00026 static char data[]=
00027 #ifdef CURL_DOES_CONVERSIONS
00028
00029 "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
00030 "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
00031 "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
00032 #else
00033 "this is what we post to the silly web server\n";
00034 #endif
00035
00036 struct WriteThis {
00037 char *readptr;
00038 size_t sizeleft;
00039 };
00040
00041 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
00042 {
00043 #ifdef LIB587
00044 (void)ptr;
00045 (void)size;
00046 (void)nmemb;
00047 (void)userp;
00048 return CURL_READFUNC_ABORT;
00049 #else
00050
00051 struct WriteThis *pooh = (struct WriteThis *)userp;
00052
00053 if(size*nmemb < 1)
00054 return 0;
00055
00056 if(pooh->sizeleft) {
00057 *(char *)ptr = pooh->readptr[0];
00058 pooh->readptr++;
00059 pooh->sizeleft--;
00060 return 1;
00061 }
00062
00063 return 0;
00064 #endif
00065 }
00066
00067 static int once(char *URL, bool oldstyle)
00068 {
00069 CURL *curl;
00070 CURLcode res=CURLE_OK;
00071 CURLFORMcode formrc;
00072
00073 struct curl_httppost *formpost=NULL;
00074 struct curl_httppost *lastptr=NULL;
00075 struct WriteThis pooh;
00076 struct WriteThis pooh2;
00077
00078 pooh.readptr = data;
00079 pooh.sizeleft = strlen(data);
00080
00081
00082 if(oldstyle) {
00083 formrc = curl_formadd(&formpost,
00084 &lastptr,
00085 CURLFORM_COPYNAME, "sendfile",
00086 CURLFORM_STREAM, &pooh,
00087 CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
00088 CURLFORM_FILENAME, "postit2.c",
00089 CURLFORM_END);
00090 }
00091 else {
00092
00093 formrc = curl_formadd(&formpost,
00094 &lastptr,
00095 CURLFORM_COPYNAME, "sendfile alternative",
00096 CURLFORM_STREAM, &pooh,
00097 CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
00098 CURLFORM_FILENAME, "file name 2",
00099 CURLFORM_END);
00100 }
00101
00102 if(formrc)
00103 printf("curl_formadd(1) = %d\n", (int)formrc);
00104
00105
00106
00107
00108 pooh2.readptr = data;
00109 pooh2.sizeleft = strlen(data);
00110
00111
00112 formrc = curl_formadd(&formpost,
00113 &lastptr,
00114 CURLFORM_COPYNAME, "callbackdata",
00115 CURLFORM_STREAM, &pooh2,
00116 CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
00117 CURLFORM_END);
00118
00119 if(formrc)
00120 printf("curl_formadd(1) = %d\n", (int)formrc);
00121
00122
00123 formrc = curl_formadd(&formpost,
00124 &lastptr,
00125 CURLFORM_COPYNAME, "filename",
00126 #ifdef CURL_DOES_CONVERSIONS
00127
00128
00129 CURLFORM_COPYCONTENTS,
00130 "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
00131 #else
00132 CURLFORM_COPYCONTENTS, "postit2.c",
00133 #endif
00134 CURLFORM_END);
00135
00136 if(formrc)
00137 printf("curl_formadd(2) = %d\n", (int)formrc);
00138
00139
00140 formrc = curl_formadd(&formpost,
00141 &lastptr,
00142 CURLFORM_COPYNAME, "submit",
00143 #ifdef CURL_DOES_CONVERSIONS
00144
00145
00146 CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
00147 #else
00148 CURLFORM_COPYCONTENTS, "send",
00149 #endif
00150 CURLFORM_END);
00151
00152 if(formrc)
00153 printf("curl_formadd(3) = %d\n", (int)formrc);
00154
00155 formrc = curl_formadd(&formpost, &lastptr,
00156 CURLFORM_COPYNAME, "somename",
00157 CURLFORM_BUFFER, "somefile.txt",
00158 CURLFORM_BUFFERPTR, "blah blah",
00159 CURLFORM_BUFFERLENGTH, (long)9,
00160 CURLFORM_END);
00161
00162 if(formrc)
00163 printf("curl_formadd(4) = %d\n", (int)formrc);
00164
00165 curl = curl_easy_init();
00166 if(!curl) {
00167 fprintf(stderr, "curl_easy_init() failed\n");
00168 curl_formfree(formpost);
00169 curl_global_cleanup();
00170 return TEST_ERR_MAJOR_BAD;
00171 }
00172
00173
00174 test_setopt(curl, CURLOPT_URL, URL);
00175
00176
00177 test_setopt(curl, CURLOPT_POST, 1L);
00178
00179
00180 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
00181
00182
00183 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
00184
00185
00186 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
00187
00188
00189 test_setopt(curl, CURLOPT_VERBOSE, 1L);
00190
00191
00192 test_setopt(curl, CURLOPT_HEADER, 1L);
00193
00194
00195 res = curl_easy_perform(curl);
00196
00197 test_cleanup:
00198
00199
00200 curl_easy_cleanup(curl);
00201
00202
00203 curl_formfree(formpost);
00204
00205 return res;
00206 }
00207
00208 int test(char *URL)
00209 {
00210 int res;
00211
00212 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
00213 fprintf(stderr, "curl_global_init() failed\n");
00214 return TEST_ERR_MAJOR_BAD;
00215 }
00216
00217 res = once(URL, TRUE);
00218 if(!res)
00219 res = once(URL, FALSE);
00220
00221 curl_global_cleanup();
00222
00223 return res;
00224 }