1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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 http://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 */
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;
78 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
79 fprintf(stderr, "curl_global_init() failed\n");
80 return TEST_ERR_MAJOR_BAD;
84 pooh.sizeleft = strlen(data);
86 /* Fill in the file upload field */
87 formrc = curl_formadd(&formpost,
89 CURLFORM_COPYNAME, "sendfile",
90 CURLFORM_STREAM, &pooh,
91 CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
92 CURLFORM_FILENAME, "postit2.c",
96 printf("curl_formadd(1) = %d\n", (int)formrc);
98 /* Now add the same data with another name and make it not look like
99 a file upload but still using the callback */
101 pooh2.readptr = data;
102 pooh2.sizeleft = strlen(data);
104 /* Fill in the file upload field */
105 formrc = curl_formadd(&formpost,
107 CURLFORM_COPYNAME, "callbackdata",
108 CURLFORM_STREAM, &pooh2,
109 CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
113 printf("curl_formadd(1) = %d\n", (int)formrc);
115 /* Fill in the filename field */
116 formrc = curl_formadd(&formpost,
118 CURLFORM_COPYNAME, "filename",
119 #ifdef CURL_DOES_CONVERSIONS
120 /* ASCII representation with escape
121 sequences for non-ASCII platforms */
122 CURLFORM_COPYCONTENTS,
123 "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
125 CURLFORM_COPYCONTENTS, "postit2.c",
130 printf("curl_formadd(2) = %d\n", (int)formrc);
132 /* Fill in a submit field too */
133 formrc = curl_formadd(&formpost,
135 CURLFORM_COPYNAME, "submit",
136 #ifdef CURL_DOES_CONVERSIONS
137 /* ASCII representation with escape
138 sequences for non-ASCII platforms */
139 CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
141 CURLFORM_COPYCONTENTS, "send",
146 printf("curl_formadd(3) = %d\n", (int)formrc);
148 formrc = curl_formadd(&formpost, &lastptr,
149 CURLFORM_COPYNAME, "somename",
150 CURLFORM_BUFFER, "somefile.txt",
151 CURLFORM_BUFFERPTR, "blah blah",
152 CURLFORM_BUFFERLENGTH, (long)9,
156 printf("curl_formadd(4) = %d\n", (int)formrc);
158 if ((curl = curl_easy_init()) == NULL) {
159 fprintf(stderr, "curl_easy_init() failed\n");
160 curl_formfree(formpost);
161 curl_global_cleanup();
162 return TEST_ERR_MAJOR_BAD;
165 /* First set the URL that is about to receive our POST. */
166 test_setopt(curl, CURLOPT_URL, URL);
168 /* Now specify we want to POST data */
169 test_setopt(curl, CURLOPT_POST, 1L);
171 /* Set the expected POST size */
172 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
174 /* we want to use our own read function */
175 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
177 /* send a multi-part formpost */
178 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
180 /* get verbose debug output please */
181 test_setopt(curl, CURLOPT_VERBOSE, 1L);
183 /* include headers in the output */
184 test_setopt(curl, CURLOPT_HEADER, 1L);
186 /* Perform the request, res will get the return code */
187 res = curl_easy_perform(curl);
192 curl_easy_cleanup(curl);
193 curl_global_cleanup();
195 /* now cleanup the formpost chain */
196 curl_formfree(formpost);