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