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 "test.h" 25 26#include "memdebug.h" 27 28static char buffer[17000]; /* more than 16K */ 29 30int test(char *URL) 31{ 32 CURL *curl = NULL; 33 CURLcode res = CURLE_OK; 34 curl_mime *mime = NULL; 35 curl_mimepart *part; 36 size_t i; 37 38 /* Checks huge binary-encoded mime post. */ 39 40 /* Create a buffer with pseudo-binary data. */ 41 for(i = 0; i < sizeof(buffer); i++) 42 if(i % 77 == 76) 43 buffer[i] = '\n'; 44 else 45 buffer[i] = (char) (0x41 + i % 26); /* A...Z */ 46 47 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 48 fprintf(stderr, "curl_global_init() failed\n"); 49 return TEST_ERR_MAJOR_BAD; 50 } 51 52 curl = curl_easy_init(); 53 if(!curl) { 54 fprintf(stderr, "curl_easy_init() failed\n"); 55 res = (CURLcode) TEST_ERR_MAJOR_BAD; 56 goto test_cleanup; 57 } 58 59 /* Build mime structure. */ 60 mime = curl_mime_init(curl); 61 if(!mime) { 62 fprintf(stderr, "curl_mime_init() failed\n"); 63 res = (CURLcode) TEST_ERR_MAJOR_BAD; 64 goto test_cleanup; 65 } 66 part = curl_mime_addpart(mime); 67 if(!part) { 68 fprintf(stderr, "curl_mime_addpart() failed\n"); 69 res = (CURLcode) TEST_ERR_MAJOR_BAD; 70 goto test_cleanup; 71 } 72 res = curl_mime_name(part, "upfile"); 73 if(res) { 74 fprintf(stderr, "curl_mime_name() failed\n"); 75 goto test_cleanup; 76 } 77 res = curl_mime_filename(part, "myfile.txt"); 78 if(res) { 79 fprintf(stderr, "curl_mime_filename() failed\n"); 80 goto test_cleanup; 81 } 82 res = curl_mime_data(part, buffer, sizeof(buffer)); 83 if(res) { 84 fprintf(stderr, "curl_mime_data() failed\n"); 85 goto test_cleanup; 86 } 87 res = curl_mime_encoder(part, "binary"); 88 if(res) { 89 fprintf(stderr, "curl_mime_encoder() failed\n"); 90 goto test_cleanup; 91 } 92 93 /* First set the URL that is about to receive our mime mail. */ 94 test_setopt(curl, CURLOPT_URL, URL); 95 96 /* Post form */ 97 test_setopt(curl, CURLOPT_MIMEPOST, mime); 98 99 /* Shorten upload buffer. */ 100 test_setopt(curl, CURLOPT_UPLOAD_BUFFERSIZE, 16411L); 101 102 /* get verbose debug output please */ 103 test_setopt(curl, CURLOPT_VERBOSE, 1L); 104 105 /* include headers in the output */ 106 test_setopt(curl, CURLOPT_HEADER, 1L); 107 108 /* Perform the request, res will get the return code */ 109 res = curl_easy_perform(curl); 110 111test_cleanup: 112 113 /* always cleanup */ 114 curl_easy_cleanup(curl); 115 116 /* now cleanup the mime structure */ 117 curl_mime_free(mime); 118 119 curl_global_cleanup(); 120 121 return res; 122} 123