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