1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
27 #ifdef CURL_DOES_CONVERSIONS
28 /* ASCII representation with escape sequences for non-ASCII platforms */
29 "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
30 "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
31 "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
33 "this is what we post to the silly web server\n";
41 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
48 return CURL_READFUNC_ABORT;
51 struct WriteThis *pooh = (struct WriteThis *)userp;
57 *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
58 pooh->readptr++; /* advance pointer */
59 pooh->sizeleft--; /* less data left */
60 return 1; /* we return 1 byte at a time! */
63 return 0; /* no more data left to deliver */
67 static int once(char *URL, bool oldstyle)
70 CURLcode res=CURLE_OK;
73 struct curl_httppost *formpost=NULL;
74 struct curl_httppost *lastptr=NULL;
75 struct WriteThis pooh;
76 struct WriteThis pooh2;
79 pooh.sizeleft = strlen(data);
81 /* Fill in the file upload field */
83 formrc = curl_formadd(&formpost,
85 CURLFORM_COPYNAME, "sendfile",
86 CURLFORM_STREAM, &pooh,
87 CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
88 CURLFORM_FILENAME, "postit2.c",
93 formrc = curl_formadd(&formpost,
95 CURLFORM_COPYNAME, "sendfile alternative",
96 CURLFORM_STREAM, &pooh,
97 CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
98 CURLFORM_FILENAME, "file name 2",
103 printf("curl_formadd(1) = %d\n", (int)formrc);
105 /* Now add the same data with another name and make it not look like
106 a file upload but still using the callback */
108 pooh2.readptr = data;
109 pooh2.sizeleft = strlen(data);
111 /* Fill in the file upload field */
112 formrc = curl_formadd(&formpost,
114 CURLFORM_COPYNAME, "callbackdata",
115 CURLFORM_STREAM, &pooh2,
116 CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
120 printf("curl_formadd(1) = %d\n", (int)formrc);
122 /* Fill in the filename field */
123 formrc = curl_formadd(&formpost,
125 CURLFORM_COPYNAME, "filename",
126 #ifdef CURL_DOES_CONVERSIONS
127 /* ASCII representation with escape
128 sequences for non-ASCII platforms */
129 CURLFORM_COPYCONTENTS,
130 "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
132 CURLFORM_COPYCONTENTS, "postit2.c",
137 printf("curl_formadd(2) = %d\n", (int)formrc);
139 /* Fill in a submit field too */
140 formrc = curl_formadd(&formpost,
142 CURLFORM_COPYNAME, "submit",
143 #ifdef CURL_DOES_CONVERSIONS
144 /* ASCII representation with escape
145 sequences for non-ASCII platforms */
146 CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
148 CURLFORM_COPYCONTENTS, "send",
153 printf("curl_formadd(3) = %d\n", (int)formrc);
155 formrc = curl_formadd(&formpost, &lastptr,
156 CURLFORM_COPYNAME, "somename",
157 CURLFORM_BUFFER, "somefile.txt",
158 CURLFORM_BUFFERPTR, "blah blah",
159 CURLFORM_BUFFERLENGTH, (long)9,
163 printf("curl_formadd(4) = %d\n", (int)formrc);
165 if((curl = curl_easy_init()) == NULL) {
166 fprintf(stderr, "curl_easy_init() failed\n");
167 curl_formfree(formpost);
168 curl_global_cleanup();
169 return TEST_ERR_MAJOR_BAD;
172 /* First set the URL that is about to receive our POST. */
173 test_setopt(curl, CURLOPT_URL, URL);
175 /* Now specify we want to POST data */
176 test_setopt(curl, CURLOPT_POST, 1L);
178 /* Set the expected POST size */
179 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
181 /* we want to use our own read function */
182 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
184 /* send a multi-part formpost */
185 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
187 /* get verbose debug output please */
188 test_setopt(curl, CURLOPT_VERBOSE, 1L);
190 /* include headers in the output */
191 test_setopt(curl, CURLOPT_HEADER, 1L);
193 /* Perform the request, res will get the return code */
194 res = curl_easy_perform(curl);
199 curl_easy_cleanup(curl);
201 /* now cleanup the formpost chain */
202 curl_formfree(formpost);
211 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
212 fprintf(stderr, "curl_global_init() failed\n");
213 return TEST_ERR_MAJOR_BAD;
216 res = once(URL, TRUE); /* old */
218 res = once(URL, FALSE); /* new */
220 curl_global_cleanup();