1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22#include "test.h"
23
24#include "memdebug.h"
25
26static char data[]=
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";
32#else
33 "this is what we post to the silly web server\n";
34#endif
35
36struct WriteThis {
37 char *readptr;
38 size_t sizeleft;
39};
40
41static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
42{
43#ifdef LIB587
44 (void)ptr;
45 (void)size;
46 (void)nmemb;
47 (void)userp;
48 return CURL_READFUNC_ABORT;
49#else
50
51 struct WriteThis *pooh = (struct WriteThis *)userp;
52
53 if(size*nmemb < 1)
54 return 0;
55
56 if(pooh->sizeleft) {
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! */
61 }
62
63 return 0; /* no more data left to deliver */
64#endif
65}
66
67static int once(char *URL, bool oldstyle)
68{
69 CURL *curl;
70 CURLcode res = CURLE_OK;
71 CURLFORMcode formrc;
72
73 struct curl_httppost *formpost = NULL;
74 struct curl_httppost *lastptr = NULL;
75 struct WriteThis pooh;
76 struct WriteThis pooh2;
77
78 pooh.readptr = data;
79 pooh.sizeleft = strlen(data);
80
81 /* Fill in the file upload field */
82 if(oldstyle) {
83 formrc = curl_formadd(&formpost,
84 &lastptr,
85 CURLFORM_COPYNAME, "sendfile",
86 CURLFORM_STREAM, &pooh,
87 CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
88 CURLFORM_FILENAME, "postit2.c",
89 CURLFORM_END);
90 }
91 else {
92 /* new style */
93 formrc = curl_formadd(&formpost,
94 &lastptr,
95 CURLFORM_COPYNAME, "sendfile alternative",
96 CURLFORM_STREAM, &pooh,
97 CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
98 CURLFORM_FILENAME, "file name 2",
99 CURLFORM_END);
100 }
101
102 if(formrc)
103 printf("curl_formadd(1) = %d\n", (int)formrc);
104
105 /* Now add the same data with another name and make it not look like
106 a file upload but still using the callback */
107
108 pooh2.readptr = data;
109 pooh2.sizeleft = strlen(data);
110
111 /* Fill in the file upload field */
112 formrc = curl_formadd(&formpost,
113 &lastptr,
114 CURLFORM_COPYNAME, "callbackdata",
115 CURLFORM_STREAM, &pooh2,
116 CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
117 CURLFORM_END);
118
119 if(formrc)
120 printf("curl_formadd(2) = %d\n", (int)formrc);
121
122 /* Fill in the filename field */
123 formrc = curl_formadd(&formpost,
124 &lastptr,
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",
131#else
132 CURLFORM_COPYCONTENTS, "postit2.c",
133#endif
134 CURLFORM_END);
135
136 if(formrc)
137 printf("curl_formadd(3) = %d\n", (int)formrc);
138
139 /* Fill in a submit field too */
140 formrc = curl_formadd(&formpost,
141 &lastptr,
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",
147#else
148 CURLFORM_COPYCONTENTS, "send",
149#endif
150 CURLFORM_CONTENTTYPE, "text/plain",
151 CURLFORM_END);
152
153 if(formrc)
154 printf("curl_formadd(4) = %d\n", (int)formrc);
155
156 formrc = curl_formadd(&formpost, &lastptr,
157 CURLFORM_COPYNAME, "somename",
158 CURLFORM_BUFFER, "somefile.txt",
159 CURLFORM_BUFFERPTR, "blah blah",
160 CURLFORM_BUFFERLENGTH, (long)9,
161 CURLFORM_END);
162
163 if(formrc)
164 printf("curl_formadd(5) = %d\n", (int)formrc);
165
166 curl = curl_easy_init();
167 if(!curl) {
168 fprintf(stderr, "curl_easy_init() failed\n");
169 curl_formfree(formpost);
170 curl_global_cleanup();
171 return TEST_ERR_MAJOR_BAD;
172 }
173
174 /* First set the URL that is about to receive our POST. */
175 test_setopt(curl, CURLOPT_URL, URL);
176
177 /* Now specify we want to POST data */
178 test_setopt(curl, CURLOPT_POST, 1L);
179
180 /* Set the expected POST size */
181 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
182
183 /* we want to use our own read function */
184 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
185
186 /* send a multi-part formpost */
187 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
188
189 /* get verbose debug output please */
190 test_setopt(curl, CURLOPT_VERBOSE, 1L);
191
192 /* include headers in the output */
193 test_setopt(curl, CURLOPT_HEADER, 1L);
194
195 /* Perform the request, res will get the return code */
196 res = curl_easy_perform(curl);
197
198test_cleanup:
199
200 /* always cleanup */
201 curl_easy_cleanup(curl);
202
203 /* now cleanup the formpost chain */
204 curl_formfree(formpost);
205
206 return res;
207}
208
209int test(char *URL)
210{
211 int res;
212
213 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
214 fprintf(stderr, "curl_global_init() failed\n");
215 return TEST_ERR_MAJOR_BAD;
216 }
217
218 res = once(URL, TRUE); /* old */
219 if(!res)
220 res = once(URL, FALSE); /* new */
221
222 curl_global_cleanup();
223
224 return res;
225}
226