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