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