lib695.c (3462B)
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 /* write callback that does nothing */ 29 static size_t write_it(char *ptr, size_t size, size_t nmemb, void *userdata) 30 { 31 (void) ptr; 32 (void) userdata; 33 return size * nmemb; 34 } 35 36 static CURLcode test_lib695(char *URL) 37 { 38 CURL *curl = NULL; 39 curl_mime *mime1 = NULL; 40 curl_mime *mime2 = NULL; 41 curl_mimepart *part; 42 CURLcode res = TEST_ERR_FAILURE; 43 44 /* 45 * Check proper rewind when reusing a mime structure. 46 */ 47 48 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 49 curl_mfprintf(stderr, "curl_global_init() failed\n"); 50 return TEST_ERR_MAJOR_BAD; 51 } 52 53 curl = curl_easy_init(); 54 55 /* First set the URL that is about to receive our POST. */ 56 test_setopt(curl, CURLOPT_URL, URL); 57 58 /* get verbose debug output please */ 59 test_setopt(curl, CURLOPT_VERBOSE, 1L); 60 61 /* Do not write anything. */ 62 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_it); 63 64 /* Build the first mime structure. */ 65 mime1 = curl_mime_init(curl); 66 part = curl_mime_addpart(mime1); 67 curl_mime_data(part, "<title>hello</title>", CURL_ZERO_TERMINATED); 68 curl_mime_type(part, "text/html"); 69 curl_mime_name(part, "data"); 70 71 /* Use first mime structure as top level MIME POST. */ 72 curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime1); 73 74 /* Perform the request, res gets the return code */ 75 res = curl_easy_perform(curl); 76 77 /* Check for errors */ 78 if(res != CURLE_OK) 79 curl_mfprintf(stderr, "curl_easy_perform() 1 failed: %s\n", 80 curl_easy_strerror(res)); 81 else { 82 /* phase two, create a mime struct using the mime1 handle */ 83 mime2 = curl_mime_init(curl); 84 part = curl_mime_addpart(mime2); 85 86 /* use the new mime setup */ 87 curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime2); 88 89 /* Reuse previous mime structure as a child. */ 90 res = curl_mime_subparts(part, mime1); 91 92 if(res != CURLE_OK) 93 curl_mfprintf(stderr, "curl_mime_subparts() failed: %sn", 94 curl_easy_strerror(res)); 95 else { 96 mime1 = NULL; 97 98 /* Perform the request, res gets the return code */ 99 res = curl_easy_perform(curl); 100 101 /* Check for errors */ 102 if(res != CURLE_OK) 103 curl_mfprintf(stderr, "curl_easy_perform() 2 failed: %s\n", 104 curl_easy_strerror(res)); 105 } 106 } 107 108 test_cleanup: 109 curl_easy_cleanup(curl); 110 curl_mime_free(mime1); 111 curl_mime_free(mime2); 112 curl_global_cleanup(); 113 return res; 114 }