1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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 ***************************************************************************/
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";
32#else
33 "this is what we post to the silly web server";
34#endif
35
36static const char name[] = "fieldname";
37
38
39/* This test attempts to use all form API features that are not
40 * used elsewhere.
41 */
42
43/* curl_formget callback to count characters. */
44static size_t count_chars(void *userp, const char *buf, size_t len)
45{
46 size_t *pcounter = (size_t *) userp;
47
48 (void) buf;
49 *pcounter += len;
50 return len;
51}
52
53
54int test(char *URL)
55{
56 CURL *curl = NULL;
57 CURLcode res = TEST_ERR_MAJOR_BAD;
58 CURLFORMcode formrc;
59 struct curl_slist *headers, *headers2 = NULL;
60 struct curl_httppost *formpost = NULL;
61 struct curl_httppost *lastptr = NULL;
62 struct curl_forms formarray[3];
63 size_t formlength = 0;
64 char flbuf[32];
65 long contentlength = 0;
66
67 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
68 fprintf(stderr, "curl_global_init() failed\n");
69 return TEST_ERR_MAJOR_BAD;
70 }
71
72 /* Check proper name and data copying, as well as headers. */
73 headers = curl_slist_append(NULL, "X-customheader-1: Header 1 data");
74 if(!headers) {
75 goto test_cleanup;
76 }
77 headers2 = curl_slist_append(headers, "X-customheader-2: Header 2 data");
78 if(!headers2) {
79 goto test_cleanup;
80 }
81 headers = headers2;
82 headers2 = curl_slist_append(headers, "Content-Type: text/plain");
83 if(!headers2) {
84 goto test_cleanup;
85 }
86 headers = headers2;
87 formrc = curl_formadd(&formpost, &lastptr,
88 CURLFORM_COPYNAME, &name,
89 CURLFORM_COPYCONTENTS, &data,
90 CURLFORM_CONTENTHEADER, headers,
91 CURLFORM_END);
92
93 if(formrc) {
94 printf("curl_formadd(1) = %d\n", (int) formrc);
95 goto test_cleanup;
96 }
97
98 contentlength = (long)(strlen(data) - 1);
99
100 /* Use a form array for the non-copy test. */
101 formarray[0].option = CURLFORM_PTRCONTENTS;
102 formarray[0].value = data;
103 formarray[1].option = CURLFORM_CONTENTSLENGTH;
104 formarray[1].value = (char *)(size_t)contentlength;
105 formarray[2].option = CURLFORM_END;
106 formarray[2].value = NULL;
107 formrc = curl_formadd(&formpost,
108 &lastptr,
109 CURLFORM_PTRNAME, name,
110 CURLFORM_NAMELENGTH, strlen(name) - 1,
111 CURLFORM_ARRAY, formarray,
112 CURLFORM_FILENAME, "remotefile.txt",
113 CURLFORM_END);
114
115 if(formrc) {
116 printf("curl_formadd(2) = %d\n", (int) formrc);
117 goto test_cleanup;
118 }
119
120 /* Now change in-memory data to affect CURLOPT_PTRCONTENTS value.
121 Copied values (first field) must not be affected.
122 CURLOPT_PTRNAME actually copies the name thus we do not test this here. */
123 data[0]++;
124
125 /* Check multi-files and content type propagation. */
126 formrc = curl_formadd(&formpost,
127 &lastptr,
128 CURLFORM_COPYNAME, "multifile",
129 CURLFORM_FILE, libtest_arg2, /* Set in first.c. */
130 CURLFORM_FILE, libtest_arg2,
131 CURLFORM_CONTENTTYPE, "text/whatever",
132 CURLFORM_FILE, libtest_arg2,
133 CURLFORM_END);
134
135 if(formrc) {
136 printf("curl_formadd(3) = %d\n", (int) formrc);
137 goto test_cleanup;
138 }
139
140 /* Check data from file content. */
141 formrc = curl_formadd(&formpost,
142 &lastptr,
143 CURLFORM_COPYNAME, "filecontents",
144 CURLFORM_FILECONTENT, libtest_arg2,
145 CURLFORM_END);
146
147 if(formrc) {
148 printf("curl_formadd(4) = %d\n", (int) formrc);
149 goto test_cleanup;
150 }
151
152 /* Measure the current form length.
153 * This is done before including stdin data because we want to reuse it
154 * and stdin cannot be rewound.
155 */
156 curl_formget(formpost, (void *) &formlength, count_chars);
157
158 /* Include length in data for external check. */
159 curl_msnprintf(flbuf, sizeof(flbuf), "%lu", (unsigned long) formlength);
160 formrc = curl_formadd(&formpost,
161 &lastptr,
162 CURLFORM_COPYNAME, "formlength",
163 CURLFORM_COPYCONTENTS, &flbuf,
164 CURLFORM_END);
165 if(formrc) {
166 printf("curl_formadd(5) = %d\n", (int) formrc);
167 goto test_cleanup;
168 }
169
170 /* Check stdin (may be problematic on some platforms). */
171 formrc = curl_formadd(&formpost,
172 &lastptr,
173 CURLFORM_COPYNAME, "standardinput",
174 CURLFORM_FILE, "-",
175 CURLFORM_END);
176 if(formrc) {
177 printf("curl_formadd(6) = %d\n", (int) formrc);
178 goto test_cleanup;
179 }
180
181 curl = curl_easy_init();
182 if(!curl) {
183 fprintf(stderr, "curl_easy_init() failed\n");
184 goto test_cleanup;
185 }
186
187 /* First set the URL that is about to receive our POST. */
188 test_setopt(curl, CURLOPT_URL, URL);
189
190 /* send a multi-part formpost */
191 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
192
193 /* get verbose debug output please */
194 test_setopt(curl, CURLOPT_VERBOSE, 1L);
195
196 /* include headers in the output */
197 test_setopt(curl, CURLOPT_HEADER, 1L);
198
199 /* Perform the request, res will get the return code */
200 res = curl_easy_perform(curl);
201
202test_cleanup:
203
204 /* always cleanup */
205 curl_easy_cleanup(curl);
206
207 /* now cleanup the formpost chain */
208 curl_formfree(formpost);
209 curl_slist_free_all(headers);
210
211 curl_global_cleanup();
212
213 return res;
214}
215