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 "testutil.h"
27 #include "warnless.h"
28 #include "memdebug.h"
29
30 struct transfer_status {
31 CURL *easy;
32 int halted;
33 int counter; /* count write callback invokes */
34 int please; /* number of times xferinfo is called while halted */
35 };
36
please_continue(void * userp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)37 static int please_continue(void *userp,
38 curl_off_t dltotal,
39 curl_off_t dlnow,
40 curl_off_t ultotal,
41 curl_off_t ulnow)
42 {
43 struct transfer_status *st = (struct transfer_status *)userp;
44 (void)dltotal;
45 (void)dlnow;
46 (void)ultotal;
47 (void)ulnow;
48 if(st->halted) {
49 st->please++;
50 if(st->please == 2) {
51 /* waited enough, unpause! */
52 curl_easy_pause(st->easy, CURLPAUSE_CONT);
53 }
54 }
55 fprintf(stderr, "xferinfo: paused %d\n", st->halted);
56 return 0; /* go on */
57 }
58
header_callback(void * ptr,size_t size,size_t nmemb,void * userp)59 static size_t header_callback(void *ptr, size_t size, size_t nmemb,
60 void *userp)
61 {
62 size_t len = size * nmemb;
63 (void)userp;
64 (void)fwrite(ptr, size, nmemb, stdout);
65 return len;
66 }
67
write_callback(void * ptr,size_t size,size_t nmemb,void * userp)68 static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *userp)
69 {
70 struct transfer_status *st = (struct transfer_status *)userp;
71 size_t len = size * nmemb;
72 st->counter++;
73 if(st->counter > 1) {
74 /* the first call puts us on pause, so subsequent calls are after
75 unpause */
76 fwrite(ptr, size, nmemb, stdout);
77 return len;
78 }
79 if(len)
80 printf("Got bytes but pausing!\n");
81 st->halted = 1;
82 return CURL_WRITEFUNC_PAUSE;
83 }
84
test(char * URL)85 CURLcode test(char *URL)
86 {
87 CURL *curls = NULL;
88 CURLcode res = CURLE_OK;
89 struct transfer_status st;
90
91 start_test_timing();
92
93 memset(&st, 0, sizeof(st));
94
95 global_init(CURL_GLOBAL_ALL);
96
97 easy_init(curls);
98 st.easy = curls; /* to allow callbacks access */
99
100 easy_setopt(curls, CURLOPT_URL, URL);
101 easy_setopt(curls, CURLOPT_WRITEFUNCTION, write_callback);
102 easy_setopt(curls, CURLOPT_WRITEDATA, &st);
103 easy_setopt(curls, CURLOPT_HEADERFUNCTION, header_callback);
104 easy_setopt(curls, CURLOPT_HEADERDATA, &st);
105
106 easy_setopt(curls, CURLOPT_XFERINFOFUNCTION, please_continue);
107 easy_setopt(curls, CURLOPT_XFERINFODATA, &st);
108 easy_setopt(curls, CURLOPT_NOPROGRESS, 0L);
109
110 res = curl_easy_perform(curls);
111
112 test_cleanup:
113
114 curl_easy_cleanup(curls);
115 curl_global_cleanup();
116
117 return res; /* return the final return code */
118 }
119