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