Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/tests/libtest/lib1540.c
2649 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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 "first.h"
25
26
#include "testtrace.h"
27
#include "memdebug.h"
28
29
struct t1540_transfer_status {
30
CURL *curl;
31
int halted;
32
int counter; /* count write callback invokes */
33
int please; /* number of times xferinfo is called while halted */
34
};
35
36
static int please_continue(void *userp,
37
curl_off_t dltotal,
38
curl_off_t dlnow,
39
curl_off_t ultotal,
40
curl_off_t ulnow)
41
{
42
struct t1540_transfer_status *st = (struct t1540_transfer_status *)userp;
43
(void)dltotal;
44
(void)dlnow;
45
(void)ultotal;
46
(void)ulnow;
47
if(st->halted) {
48
st->please++;
49
if(st->please == 2) {
50
/* waited enough, unpause! */
51
curl_easy_pause(st->curl, CURLPAUSE_CONT);
52
}
53
}
54
curl_mfprintf(stderr, "xferinfo: paused %d\n", st->halted);
55
return 0; /* go on */
56
}
57
58
static size_t t1540_header_callback(char *ptr, size_t size, size_t nmemb,
59
void *userp)
60
{
61
size_t len = size * nmemb;
62
(void)userp;
63
(void)fwrite(ptr, size, nmemb, stdout);
64
return len;
65
}
66
67
static size_t t1540_write_cb(char *ptr, size_t size, size_t nmemb, void *userp)
68
{
69
struct t1540_transfer_status *st = (struct t1540_transfer_status *)userp;
70
size_t len = size * nmemb;
71
st->counter++;
72
if(st->counter > 1) {
73
/* the first call puts us on pause, so subsequent calls are after
74
unpause */
75
fwrite(ptr, size, nmemb, stdout);
76
return len;
77
}
78
if(len)
79
curl_mprintf("Got bytes but pausing!\n");
80
st->halted = 1;
81
return CURL_WRITEFUNC_PAUSE;
82
}
83
84
static CURLcode test_lib1540(const char *URL)
85
{
86
CURL *curl = NULL;
87
CURLcode res = CURLE_OK;
88
struct t1540_transfer_status st;
89
90
start_test_timing();
91
92
memset(&st, 0, sizeof(st));
93
94
global_init(CURL_GLOBAL_ALL);
95
96
easy_init(curl);
97
st.curl = curl; /* to allow callbacks access */
98
99
easy_setopt(curl, CURLOPT_URL, URL);
100
easy_setopt(curl, CURLOPT_WRITEFUNCTION, t1540_write_cb);
101
easy_setopt(curl, CURLOPT_WRITEDATA, &st);
102
easy_setopt(curl, CURLOPT_HEADERFUNCTION, t1540_header_callback);
103
easy_setopt(curl, CURLOPT_HEADERDATA, &st);
104
105
easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, please_continue);
106
easy_setopt(curl, CURLOPT_XFERINFODATA, &st);
107
easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
108
109
debug_config.nohex = TRUE;
110
debug_config.tracetime = TRUE;
111
test_setopt(curl, CURLOPT_DEBUGDATA, &debug_config);
112
easy_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
113
easy_setopt(curl, CURLOPT_VERBOSE, 1L);
114
115
res = curl_easy_perform(curl);
116
117
test_cleanup:
118
119
curl_easy_cleanup(curl);
120
curl_global_cleanup();
121
122
return res; /* return the final return code */
123
}
124
125