lib643.c (7218B)
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 "first.h" 25 26 #include "memdebug.h" 27 28 struct t643_WriteThis { 29 const char *readptr; 30 curl_off_t sizeleft; 31 }; 32 33 static size_t t643_read_cb(char *ptr, size_t size, size_t nmemb, void *userp) 34 { 35 struct t643_WriteThis *pooh = (struct t643_WriteThis *)userp; 36 int eof; 37 38 if(size*nmemb < 1) 39 return 0; 40 41 if(testnum == 643) { 42 eof = pooh->sizeleft <= 0; 43 if(!eof) 44 pooh->sizeleft--; 45 } 46 else { 47 eof = !*pooh->readptr; 48 } 49 50 if(!eof) { 51 *ptr = *pooh->readptr; /* copy one single byte */ 52 pooh->readptr++; /* advance pointer */ 53 return 1; /* we return 1 byte at a time! */ 54 } 55 56 return 0; /* no more data left to deliver */ 57 } 58 59 static CURLcode t643_test_once(char *URL, bool oldstyle) 60 { 61 static const char testdata[] = "dummy\n"; 62 63 CURL *curl; 64 CURLcode res = CURLE_OK; 65 66 curl_mime *mime = NULL; 67 curl_mimepart *part = NULL; 68 struct t643_WriteThis pooh; 69 struct t643_WriteThis pooh2; 70 curl_off_t datasize = -1; 71 72 pooh.readptr = testdata; 73 if(testnum == 643) 74 datasize = (curl_off_t)strlen(testdata); 75 pooh.sizeleft = datasize; 76 77 curl = curl_easy_init(); 78 if(!curl) { 79 curl_mfprintf(stderr, "curl_easy_init() failed\n"); 80 curl_global_cleanup(); 81 return TEST_ERR_MAJOR_BAD; 82 } 83 84 mime = curl_mime_init(curl); 85 if(!mime) { 86 curl_mfprintf(stderr, "curl_mime_init() failed\n"); 87 curl_easy_cleanup(curl); 88 curl_global_cleanup(); 89 return TEST_ERR_MAJOR_BAD; 90 } 91 92 part = curl_mime_addpart(mime); 93 if(!part) { 94 curl_mfprintf(stderr, "curl_mime_addpart(1) failed\n"); 95 curl_mime_free(mime); 96 curl_easy_cleanup(curl); 97 curl_global_cleanup(); 98 return TEST_ERR_MAJOR_BAD; 99 } 100 101 /* Fill in the file upload part */ 102 if(oldstyle) { 103 res = curl_mime_name(part, "sendfile"); 104 if(!res) 105 res = curl_mime_data_cb(part, datasize, t643_read_cb, 106 NULL, NULL, &pooh); 107 if(!res) 108 res = curl_mime_filename(part, "postit2.c"); 109 } 110 else { 111 /* new style */ 112 res = curl_mime_name(part, "sendfile alternative"); 113 if(!res) 114 res = curl_mime_data_cb(part, datasize, t643_read_cb, 115 NULL, NULL, &pooh); 116 if(!res) 117 res = curl_mime_filename(part, "file name 2"); 118 } 119 120 if(res) 121 curl_mprintf("curl_mime_xxx(1) = %s\n", curl_easy_strerror(res)); 122 123 /* Now add the same data with another name and make it not look like 124 a file upload but still using the callback */ 125 126 pooh2.readptr = testdata; 127 if(testnum == 643) 128 datasize = (curl_off_t)strlen(testdata); 129 pooh2.sizeleft = datasize; 130 131 part = curl_mime_addpart(mime); 132 if(!part) { 133 curl_mfprintf(stderr, "curl_mime_addpart(2) failed\n"); 134 curl_mime_free(mime); 135 curl_easy_cleanup(curl); 136 curl_global_cleanup(); 137 return TEST_ERR_MAJOR_BAD; 138 } 139 /* Fill in the file upload part */ 140 res = curl_mime_name(part, "callbackdata"); 141 if(!res) 142 res = curl_mime_data_cb(part, datasize, t643_read_cb, 143 NULL, NULL, &pooh2); 144 145 if(res) 146 curl_mprintf("curl_mime_xxx(2) = %s\n", curl_easy_strerror(res)); 147 148 part = curl_mime_addpart(mime); 149 if(!part) { 150 curl_mfprintf(stderr, "curl_mime_addpart(3) failed\n"); 151 curl_mime_free(mime); 152 curl_easy_cleanup(curl); 153 curl_global_cleanup(); 154 return TEST_ERR_MAJOR_BAD; 155 } 156 157 /* Fill in the filename field */ 158 res = curl_mime_name(part, "filename"); 159 if(!res) 160 res = curl_mime_data(part, "postit2.c", 161 CURL_ZERO_TERMINATED); 162 163 if(res) 164 curl_mprintf("curl_mime_xxx(3) = %s\n", curl_easy_strerror(res)); 165 166 /* Fill in a submit field too */ 167 part = curl_mime_addpart(mime); 168 if(!part) { 169 curl_mfprintf(stderr, "curl_mime_addpart(4) failed\n"); 170 curl_mime_free(mime); 171 curl_easy_cleanup(curl); 172 curl_global_cleanup(); 173 return TEST_ERR_MAJOR_BAD; 174 } 175 res = curl_mime_name(part, "submit"); 176 if(!res) 177 res = curl_mime_data(part, "send", 178 CURL_ZERO_TERMINATED); 179 180 if(res) 181 curl_mprintf("curl_mime_xxx(4) = %s\n", curl_easy_strerror(res)); 182 183 part = curl_mime_addpart(mime); 184 if(!part) { 185 curl_mfprintf(stderr, "curl_mime_addpart(5) failed\n"); 186 curl_mime_free(mime); 187 curl_easy_cleanup(curl); 188 curl_global_cleanup(); 189 return TEST_ERR_MAJOR_BAD; 190 } 191 res = curl_mime_name(part, "somename"); 192 if(!res) 193 res = curl_mime_filename(part, "somefile.txt"); 194 if(!res) 195 res = curl_mime_data(part, "blah blah", 9); 196 197 if(res) 198 curl_mprintf("curl_mime_xxx(5) = %s\n", curl_easy_strerror(res)); 199 200 /* First set the URL that is about to receive our POST. */ 201 test_setopt(curl, CURLOPT_URL, URL); 202 203 /* send a multi-part mimepost */ 204 test_setopt(curl, CURLOPT_MIMEPOST, mime); 205 206 /* get verbose debug output please */ 207 test_setopt(curl, CURLOPT_VERBOSE, 1L); 208 209 /* include headers in the output */ 210 test_setopt(curl, CURLOPT_HEADER, 1L); 211 212 /* Perform the request, res will get the return code */ 213 res = curl_easy_perform(curl); 214 215 test_cleanup: 216 217 /* always cleanup */ 218 curl_easy_cleanup(curl); 219 220 /* now cleanup the mimepost structure */ 221 curl_mime_free(mime); 222 223 return res; 224 } 225 226 static CURLcode t643_cyclic_add(void) 227 { 228 CURL *easy = curl_easy_init(); 229 curl_mime *mime = curl_mime_init(easy); 230 curl_mimepart *part = curl_mime_addpart(mime); 231 CURLcode a1 = curl_mime_subparts(part, mime); 232 233 if(a1 == CURLE_BAD_FUNCTION_ARGUMENT) { 234 curl_mime *submime = curl_mime_init(easy); 235 curl_mimepart *subpart = curl_mime_addpart(submime); 236 237 curl_mime_subparts(part, submime); 238 a1 = curl_mime_subparts(subpart, mime); 239 } 240 241 curl_mime_free(mime); 242 curl_easy_cleanup(easy); 243 if(a1 != CURLE_BAD_FUNCTION_ARGUMENT) 244 /* that should have failed */ 245 return TEST_ERR_FAILURE; 246 247 return CURLE_OK; 248 } 249 250 static CURLcode test_lib643(char *URL) 251 { 252 CURLcode res; 253 254 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 255 curl_mfprintf(stderr, "curl_global_init() failed\n"); 256 return TEST_ERR_MAJOR_BAD; 257 } 258 259 res = t643_test_once(URL, TRUE); /* old */ 260 if(!res) 261 res = t643_test_once(URL, FALSE); /* new */ 262 263 if(!res) 264 res = t643_cyclic_add(); 265 266 curl_global_cleanup(); 267 268 return res; 269 }