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