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 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 | |
34 | struct WriteThis { |
35 | char *readptr; |
36 | curl_off_t sizeleft; |
37 | }; |
38 | |
39 | static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp) |
40 | { |
41 | struct WriteThis *pooh = (struct WriteThis *)userp; |
42 | size_t len = strlen(pooh->readptr); |
43 | |
44 | (void) size; /* Always 1.*/ |
45 | |
46 | if(len > nmemb) |
47 | len = nmemb; |
48 | if(len) { |
49 | memcpy(ptr, pooh->readptr, len); |
50 | pooh->readptr += len; |
51 | } |
52 | return len; |
53 | } |
54 | |
55 | int test(char *URL) |
56 | { |
57 | CURL *easy = NULL; |
58 | curl_mime *mime = NULL; |
59 | curl_mimepart *part; |
60 | CURLcode result; |
61 | int res = TEST_ERR_FAILURE; |
62 | struct WriteThis pooh1, pooh2; |
63 | |
64 | /* |
65 | * Check early end of part data detection. |
66 | */ |
67 | |
68 | if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { |
69 | fprintf(stderr, "curl_global_init() failed\n" ); |
70 | return TEST_ERR_MAJOR_BAD; |
71 | } |
72 | |
73 | easy = curl_easy_init(); |
74 | |
75 | /* First set the URL that is about to receive our POST. */ |
76 | test_setopt(easy, CURLOPT_URL, URL); |
77 | |
78 | /* get verbose debug output please */ |
79 | test_setopt(easy, CURLOPT_VERBOSE, 1L); |
80 | |
81 | /* include headers in the output */ |
82 | test_setopt(easy, CURLOPT_HEADER, 1L); |
83 | |
84 | /* Prepare the callback structures. */ |
85 | pooh1.readptr = data; |
86 | pooh1.sizeleft = (curl_off_t) strlen(data); |
87 | pooh2 = pooh1; |
88 | |
89 | /* Build the mime tree. */ |
90 | mime = curl_mime_init(easy); |
91 | part = curl_mime_addpart(mime); |
92 | curl_mime_name(part, "field1" ); |
93 | /* Early end of data detection can be done because the data size is known. */ |
94 | curl_mime_data_cb(part, (curl_off_t) strlen(data), |
95 | read_callback, NULL, NULL, &pooh1); |
96 | part = curl_mime_addpart(mime); |
97 | curl_mime_name(part, "field2" ); |
98 | /* Using an undefined length forces chunked transfer and disables early |
99 | end of data detection for this part. */ |
100 | curl_mime_data_cb(part, (curl_off_t) -1, read_callback, NULL, NULL, &pooh2); |
101 | part = curl_mime_addpart(mime); |
102 | curl_mime_name(part, "field3" ); |
103 | /* Regular file part sources early end of data can be detected because |
104 | the file size is known. In addition, and EOF test is performed. */ |
105 | curl_mime_filedata(part, "log/file668.txt" ); |
106 | |
107 | /* Bind mime data to its easy handle. */ |
108 | test_setopt(easy, CURLOPT_MIMEPOST, mime); |
109 | |
110 | /* Send data. */ |
111 | result = curl_easy_perform(easy); |
112 | if(result) { |
113 | fprintf(stderr, "curl_easy_perform() failed\n" ); |
114 | res = (int) result; |
115 | } |
116 | |
117 | test_cleanup: |
118 | curl_easy_cleanup(easy); |
119 | curl_mime_free(mime); |
120 | curl_global_cleanup(); |
121 | return res; |
122 | } |
123 | |