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