lib668.c (3564B)
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 t668_WriteThis { 29 const char *readptr; 30 curl_off_t sizeleft; 31 }; 32 33 static size_t t668_read_cb(char *ptr, size_t size, size_t nmemb, void *userp) 34 { 35 struct t668_WriteThis *pooh = (struct t668_WriteThis *)userp; 36 size_t len = strlen(pooh->readptr); 37 38 (void) size; /* Always 1.*/ 39 40 if(len > nmemb) 41 len = nmemb; 42 if(len) { 43 memcpy(ptr, pooh->readptr, len); 44 pooh->readptr += len; 45 } 46 return len; 47 } 48 49 static CURLcode test_lib668(char *URL) 50 { 51 static const char testdata[] = "dummy"; 52 53 CURL *easy = NULL; 54 curl_mime *mime = NULL; 55 curl_mimepart *part; 56 CURLcode res = TEST_ERR_FAILURE; 57 struct t668_WriteThis pooh1, pooh2; 58 59 /* 60 * Check early end of part data detection. 61 */ 62 63 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 64 curl_mfprintf(stderr, "curl_global_init() failed\n"); 65 return TEST_ERR_MAJOR_BAD; 66 } 67 68 easy = curl_easy_init(); 69 70 /* First set the URL that is about to receive our POST. */ 71 test_setopt(easy, CURLOPT_URL, URL); 72 73 /* get verbose debug output please */ 74 test_setopt(easy, CURLOPT_VERBOSE, 1L); 75 76 /* include headers in the output */ 77 test_setopt(easy, CURLOPT_HEADER, 1L); 78 79 /* Prepare the callback structures. */ 80 pooh1.readptr = testdata; 81 pooh1.sizeleft = (curl_off_t) strlen(testdata); 82 pooh2 = pooh1; 83 84 /* Build the mime tree. */ 85 mime = curl_mime_init(easy); 86 part = curl_mime_addpart(mime); 87 curl_mime_name(part, "field1"); 88 /* Early end of data detection can be done because the data size is known. */ 89 curl_mime_data_cb(part, (curl_off_t) strlen(testdata), 90 t668_read_cb, NULL, NULL, &pooh1); 91 part = curl_mime_addpart(mime); 92 curl_mime_name(part, "field2"); 93 /* Using an undefined length forces chunked transfer and disables early 94 end of data detection for this part. */ 95 curl_mime_data_cb(part, (curl_off_t) -1, t668_read_cb, 96 NULL, NULL, &pooh2); 97 part = curl_mime_addpart(mime); 98 curl_mime_name(part, "field3"); 99 /* Regular file part sources early end of data can be detected because 100 the file size is known. In addition, and EOF test is performed. */ 101 curl_mime_filedata(part, libtest_arg2); 102 103 /* Bind mime data to its easy handle. */ 104 test_setopt(easy, CURLOPT_MIMEPOST, mime); 105 106 /* Send data. */ 107 res = curl_easy_perform(easy); 108 if(res != CURLE_OK) { 109 curl_mfprintf(stderr, "curl_easy_perform() failed\n"); 110 } 111 112 test_cleanup: 113 curl_easy_cleanup(easy); 114 curl_mime_free(mime); 115 curl_global_cleanup(); 116 return res; 117 }