lib667.c (3306B)
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 t667_WriteThis { 29 const char *readptr; 30 curl_off_t sizeleft; 31 }; 32 33 static size_t t667_read_cb(char *ptr, size_t size, size_t nmemb, void *userp) 34 { 35 struct t667_WriteThis *pooh = (struct t667_WriteThis *)userp; 36 int eof; 37 38 if(size*nmemb < 1) 39 return 0; 40 41 eof = pooh->sizeleft <= 0; 42 if(!eof) 43 pooh->sizeleft--; 44 45 if(!eof) { 46 *ptr = *pooh->readptr; /* copy one single byte */ 47 pooh->readptr++; /* advance pointer */ 48 return 1; /* we return 1 byte at a time! */ 49 } 50 51 return 0; /* no more data left to deliver */ 52 } 53 54 static CURLcode test_lib667(char *URL) 55 { 56 static const char testdata[] = "dummy"; 57 58 CURL *easy = NULL; 59 curl_mime *mime = NULL; 60 curl_mimepart *part; 61 CURLcode res = TEST_ERR_FAILURE; 62 struct t667_WriteThis pooh; 63 64 /* 65 * Check proper handling of mime encoder feature when the part read callback 66 * delivers data bytes one at a time. Use chunked encoding for accurate test. 67 */ 68 69 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 70 curl_mfprintf(stderr, "curl_global_init() failed\n"); 71 return TEST_ERR_MAJOR_BAD; 72 } 73 74 easy = curl_easy_init(); 75 76 /* First set the URL that is about to receive our POST. */ 77 test_setopt(easy, CURLOPT_URL, URL); 78 79 /* get verbose debug output please */ 80 test_setopt(easy, CURLOPT_VERBOSE, 1L); 81 82 /* include headers in the output */ 83 test_setopt(easy, CURLOPT_HEADER, 1L); 84 85 /* Prepare the callback structure. */ 86 pooh.readptr = testdata; 87 pooh.sizeleft = (curl_off_t) strlen(testdata); 88 89 /* Build the mime tree. */ 90 mime = curl_mime_init(easy); 91 part = curl_mime_addpart(mime); 92 curl_mime_name(part, "field"); 93 curl_mime_encoder(part, "base64"); 94 /* Using an undefined length forces chunked transfer. */ 95 curl_mime_data_cb(part, (curl_off_t) -1, t667_read_cb, 96 NULL, NULL, &pooh); 97 98 /* Bind mime data to its easy handle. */ 99 test_setopt(easy, CURLOPT_MIMEPOST, mime); 100 101 /* Send data. */ 102 res = curl_easy_perform(easy); 103 if(res != CURLE_OK) { 104 curl_mfprintf(stderr, "curl_easy_perform() failed\n"); 105 } 106 107 test_cleanup: 108 curl_easy_cleanup(easy); 109 curl_mime_free(mime); 110 curl_global_cleanup(); 111 return res; 112 }