blob: 3e6f511a2163ce94f33316175346646289b5ea49 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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
28static char data[]=
29 "this is what we post to the silly web server\n";
30
31struct WriteThis {
32 char *readptr;
33 size_t sizeleft;
34};
35
36static 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
62static int 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 = data;
74 pooh.sizeleft = strlen(data);
75
76 /* Fill in the file upload field */
77 if(oldstyle) {
78 formrc = curl_formadd(&formpost,
79 &lastptr,
80 CURLFORM_COPYNAME, "sendfile",
81 CURLFORM_STREAM, &pooh,
82 CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
83 CURLFORM_FILENAME, "postit2.c",
84 CURLFORM_END);
85 }
86 else {
87 /* new style */
88 formrc = curl_formadd(&formpost,
89 &lastptr,
90 CURLFORM_COPYNAME, "sendfile alternative",
91 CURLFORM_STREAM, &pooh,
92 CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
93 CURLFORM_FILENAME, "file name 2",
94 CURLFORM_END);
95 }
96
97 if(formrc)
98 printf("curl_formadd(1) = %d\n", (int)formrc);
99
100 /* Now add the same data with another name and make it not look like
101 a file upload but still using the callback */
102
103 pooh2.readptr = data;
104 pooh2.sizeleft = strlen(data);
105
106 /* Fill in the file upload field */
107 formrc = curl_formadd(&formpost,
108 &lastptr,
109 CURLFORM_COPYNAME, "callbackdata",
110 CURLFORM_STREAM, &pooh2,
111 CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
112 CURLFORM_END);
113
114 if(formrc)
115 printf("curl_formadd(2) = %d\n", (int)formrc);
116
117 /* Fill in the filename field */
118 formrc = curl_formadd(&formpost,
119 &lastptr,
120 CURLFORM_COPYNAME, "filename",
121 CURLFORM_COPYCONTENTS, "postit2.c",
122 CURLFORM_END);
123
124 if(formrc)
125 printf("curl_formadd(3) = %d\n", (int)formrc);
126
127 /* Fill in a submit field too */
128 formrc = curl_formadd(&formpost,
129 &lastptr,
130 CURLFORM_COPYNAME, "submit",
131 CURLFORM_COPYCONTENTS, "send",
132 CURLFORM_CONTENTTYPE, "text/plain",
133 CURLFORM_END);
134
135 if(formrc)
136 printf("curl_formadd(4) = %d\n", (int)formrc);
137
138 formrc = curl_formadd(&formpost, &lastptr,
139 CURLFORM_COPYNAME, "somename",
140 CURLFORM_BUFFER, "somefile.txt",
141 CURLFORM_BUFFERPTR, "blah blah",
142 CURLFORM_BUFFERLENGTH, (long)9,
143 CURLFORM_END);
144
145 if(formrc)
146 printf("curl_formadd(5) = %d\n", (int)formrc);
147
148 curl = curl_easy_init();
149 if(!curl) {
150 fprintf(stderr, "curl_easy_init() failed\n");
151 curl_formfree(formpost);
152 curl_global_cleanup();
153 return TEST_ERR_MAJOR_BAD;
154 }
155
156 /* First set the URL that is about to receive our POST. */
157 test_setopt(curl, CURLOPT_URL, URL);
158
159 /* Now specify we want to POST data */
160 test_setopt(curl, CURLOPT_POST, 1L);
161
162 /* Set the expected POST size */
163 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
164
165 /* we want to use our own read function */
166 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
167
168 /* send a multi-part formpost */
169 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
170
171 /* get verbose debug output please */
172 test_setopt(curl, CURLOPT_VERBOSE, 1L);
173
174 /* include headers in the output */
175 test_setopt(curl, CURLOPT_HEADER, 1L);
176
177 /* Perform the request, res will get the return code */
178 res = curl_easy_perform(curl);
179
180test_cleanup:
181
182 /* always cleanup */
183 curl_easy_cleanup(curl);
184
185 /* now cleanup the formpost chain */
186 curl_formfree(formpost);
187
188 return res;
189}
190
191int test(char *URL)
192{
193 int res;
194
195 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
196 fprintf(stderr, "curl_global_init() failed\n");
197 return TEST_ERR_MAJOR_BAD;
198 }
199
200 res = once(URL, TRUE); /* old */
201 if(!res)
202 res = once(URL, FALSE); /* new */
203
204 curl_global_cleanup();
205
206 return res;
207}