xref: /curl/tests/libtest/lib579.c (revision 71cf0d1f)
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 const char * const testpost[]={
29   "one",
30   "two",
31   "three",
32   "and a final longer crap: four",
33   NULL
34 };
35 
36 
37 struct WriteThis {
38   int counter;
39 };
40 
41 static bool started = FALSE;
42 static size_t last_ul = 0;
43 static size_t last_ul_total = 0;
44 
progress_final_report(void)45 static void progress_final_report(void)
46 {
47   FILE *moo = fopen(libtest_arg2, "ab");
48   fprintf(moo, "Progress: end UL %zu/%zu\n", last_ul, last_ul_total);
49   started = FALSE;
50   fclose(moo);
51 }
52 
progress_callback(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)53 static int progress_callback(void *clientp, double dltotal, double dlnow,
54                              double ultotal, double ulnow)
55 {
56   (void)clientp; /* UNUSED */
57   (void)dltotal; /* UNUSED */
58   (void)dlnow; /* UNUSED */
59 
60   if(started && ulnow <= 0.0 && last_ul) {
61     progress_final_report();
62   }
63 
64   last_ul = (size_t)ulnow;
65   last_ul_total = (size_t)ultotal;
66   if(!started) {
67     FILE *moo = fopen(libtest_arg2, "ab");
68     fprintf(moo, "Progress: start UL %zu/%zu\n", last_ul, last_ul_total);
69     started = TRUE;
70     fclose(moo);
71   }
72 
73   return 0;
74 }
75 
read_callback(char * ptr,size_t size,size_t nmemb,void * userp)76 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
77 {
78   struct WriteThis *pooh = (struct WriteThis *)userp;
79   const char *data;
80 
81   if(size*nmemb < 1)
82     return 0;
83 
84   data = testpost[pooh->counter];
85 
86   if(data) {
87     size_t len = strlen(data);
88     memcpy(ptr, data, len);
89     pooh->counter++; /* advance pointer */
90     return len;
91   }
92   return 0;                         /* no more data left to deliver */
93 }
94 
test(char * URL)95 CURLcode test(char *URL)
96 {
97   CURL *curl;
98   CURLcode res = CURLE_OK;
99   struct curl_slist *slist = NULL;
100   struct WriteThis pooh;
101   pooh.counter = 0;
102 
103   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
104     fprintf(stderr, "curl_global_init() failed\n");
105     return TEST_ERR_MAJOR_BAD;
106   }
107 
108   curl = curl_easy_init();
109   if(!curl) {
110     fprintf(stderr, "curl_easy_init() failed\n");
111     curl_global_cleanup();
112     return TEST_ERR_MAJOR_BAD;
113   }
114 
115   slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
116   if(!slist) {
117     fprintf(stderr, "curl_slist_append() failed\n");
118     curl_easy_cleanup(curl);
119     curl_global_cleanup();
120     return TEST_ERR_MAJOR_BAD;
121   }
122 
123   /* First set the URL that is about to receive our POST. */
124   test_setopt(curl, CURLOPT_URL, URL);
125 
126   /* Now specify we want to POST data */
127   test_setopt(curl, CURLOPT_POST, 1L);
128 
129   /* we want to use our own read function */
130   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
131 
132   /* pointer to pass to our read function */
133   test_setopt(curl, CURLOPT_READDATA, &pooh);
134 
135   /* get verbose debug output please */
136   test_setopt(curl, CURLOPT_VERBOSE, 1L);
137 
138   /* include headers in the output */
139   test_setopt(curl, CURLOPT_HEADER, 1L);
140 
141   /* enforce chunked transfer by setting the header */
142   test_setopt(curl, CURLOPT_HTTPHEADER, slist);
143 
144   test_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
145   test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
146 
147   /* we want to use our own progress function */
148   test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
149   CURL_IGNORE_DEPRECATION(
150     test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
151   )
152 
153   /* Perform the request, res will get the return code */
154   res = curl_easy_perform(curl);
155 
156   progress_final_report();
157 
158 test_cleanup:
159 
160   /* clean up the headers list */
161   if(slist)
162     curl_slist_free_all(slist);
163 
164   /* always cleanup */
165   curl_easy_cleanup(curl);
166   curl_global_cleanup();
167 
168   return res;
169 }
170