1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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";
33 "this is what we post to the silly web server";
36 static const char name[] = "fieldname";
39 /* This test attempts to use all form API features that are not
43 /* curl_formget callback to count characters. */
44 static size_t count_chars(void *userp, const char *buf, size_t len)
46 size_t *pcounter = (size_t *) userp;
57 CURLcode res = TEST_ERR_MAJOR_BAD;
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;
66 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
67 fprintf(stderr, "curl_global_init() failed\n");
68 return TEST_ERR_MAJOR_BAD;
71 /* Check proper name and data copying, as well as headers. */
72 headers = curl_slist_append(NULL, "X-customheader-1: Header 1 data");
76 headers2 = curl_slist_append(headers, "X-customheader-2: Header 2 data");
81 headers2 = curl_slist_append(headers, "Content-Type: text/plain");
86 formrc = curl_formadd(&formpost, &lastptr,
87 CURLFORM_COPYNAME, &name,
88 CURLFORM_COPYCONTENTS, &data,
89 CURLFORM_CONTENTHEADER, headers,
93 printf("curl_formadd(1) = %d\n", (int) formrc);
97 /* Use a form array for the non-copy test. */
98 formarray[0].option = CURLFORM_PTRCONTENTS;
99 formarray[0].value = data;
100 formarray[1].option = CURLFORM_CONTENTSLENGTH;
101 formarray[1].value = (char *) strlen(data) - 1;
102 formarray[2].option = CURLFORM_END;
103 formarray[2].value = NULL;
104 formrc = curl_formadd(&formpost,
106 CURLFORM_PTRNAME, name,
107 CURLFORM_NAMELENGTH, strlen(name) - 1,
108 CURLFORM_ARRAY, formarray,
109 CURLFORM_FILENAME, "remotefile.txt",
113 printf("curl_formadd(2) = %d\n", (int) formrc);
117 /* Now change in-memory data to affect CURLOPT_PTRCONTENTS value.
118 Copied values (first field) must not be affected.
119 CURLOPT_PTRNAME actually copies the name thus we do not test this here. */
122 /* Check multi-files and content type propagation. */
123 formrc = curl_formadd(&formpost,
125 CURLFORM_COPYNAME, "multifile",
126 CURLFORM_FILE, libtest_arg2, /* Set in first.c. */
127 CURLFORM_FILE, libtest_arg2,
128 CURLFORM_CONTENTTYPE, "text/whatever",
129 CURLFORM_FILE, libtest_arg2,
133 printf("curl_formadd(3) = %d\n", (int) formrc);
137 /* Check data from file content. */
138 formrc = curl_formadd(&formpost,
140 CURLFORM_COPYNAME, "filecontents",
141 CURLFORM_FILECONTENT, libtest_arg2,
145 printf("curl_formadd(4) = %d\n", (int) formrc);
149 /* Measure the current form length.
150 * This is done before including stdin data because we want to reuse it
151 * and stdin cannot be rewound.
153 curl_formget(formpost, (void *) &formlength, count_chars);
155 /* Include length in data for external check. */
156 curl_msnprintf(flbuf, sizeof flbuf, "%lu", (unsigned long) formlength);
157 formrc = curl_formadd(&formpost,
159 CURLFORM_COPYNAME, "formlength",
160 CURLFORM_COPYCONTENTS, &flbuf,
163 printf("curl_formadd(5) = %d\n", (int) formrc);
167 /* Check stdin (may be problematic on some platforms). */
168 formrc = curl_formadd(&formpost,
170 CURLFORM_COPYNAME, "standardinput",
174 printf("curl_formadd(6) = %d\n", (int) formrc);
178 curl = curl_easy_init();
180 fprintf(stderr, "curl_easy_init() failed\n");
184 /* First set the URL that is about to receive our POST. */
185 test_setopt(curl, CURLOPT_URL, URL);
187 /* send a multi-part formpost */
188 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
190 /* get verbose debug output please */
191 test_setopt(curl, CURLOPT_VERBOSE, 1L);
193 /* include headers in the output */
194 test_setopt(curl, CURLOPT_HEADER, 1L);
196 /* Perform the request, res will get the return code */
197 res = curl_easy_perform(curl);
202 curl_easy_cleanup(curl);
204 /* now cleanup the formpost chain */
205 curl_formfree(formpost);
206 curl_slist_free_all(headers);
208 curl_global_cleanup();