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
progress_callback(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)28 static int progress_callback(void *clientp, double dltotal,
29 double dlnow, double ultotal, double ulnow)
30 {
31 (void)clientp;
32 (void)ulnow;
33 (void)ultotal;
34
35 if((dltotal > 0.0) && (dlnow > dltotal)) {
36 /* this should not happen with test case 599 */
37 printf("%.0f > %.0f !!\n", dltotal, dlnow);
38 return -1;
39 }
40
41 return 0;
42 }
43
test(char * URL)44 CURLcode test(char *URL)
45 {
46 CURL *curl;
47 CURLcode res = CURLE_OK;
48 double content_length = 0.0;
49
50 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
51 fprintf(stderr, "curl_global_init() failed\n");
52 return TEST_ERR_MAJOR_BAD;
53 }
54
55 curl = curl_easy_init();
56 if(!curl) {
57 fprintf(stderr, "curl_easy_init() failed\n");
58 curl_global_cleanup();
59 return TEST_ERR_MAJOR_BAD;
60 }
61
62 /* First set the URL that is about to receive our POST. */
63 test_setopt(curl, CURLOPT_URL, URL);
64
65 /* we want to use our own progress function */
66 test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
67 CURL_IGNORE_DEPRECATION(
68 test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
69 )
70
71 /* get verbose debug output please */
72 test_setopt(curl, CURLOPT_VERBOSE, 1L);
73
74 /* follow redirects */
75 test_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
76
77 /* include headers in the output */
78 test_setopt(curl, CURLOPT_HEADER, 1L);
79
80 /* Perform the request, res will get the return code */
81 res = curl_easy_perform(curl);
82
83 if(!res) {
84 FILE *moo;
85 CURL_IGNORE_DEPRECATION(
86 res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD,
87 &content_length);
88 )
89 moo = fopen(libtest_arg2, "wb");
90 if(moo) {
91 fprintf(moo, "CL %.0f\n", content_length);
92 fclose(moo);
93 }
94 }
95
96 test_cleanup:
97
98 /* always cleanup */
99 curl_easy_cleanup(curl);
100 curl_global_cleanup();
101
102 return res;
103 }
104