lib1540.c (3725B)
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 "first.h" 25 26 #include "testtrace.h" 27 #include "memdebug.h" 28 29 struct t1540_transfer_status { 30 CURL *easy; 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->easy, 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(char *URL) 85 { 86 CURL *curls = 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(curls); 97 st.easy = curls; /* to allow callbacks access */ 98 99 easy_setopt(curls, CURLOPT_URL, URL); 100 easy_setopt(curls, CURLOPT_WRITEFUNCTION, t1540_write_cb); 101 easy_setopt(curls, CURLOPT_WRITEDATA, &st); 102 easy_setopt(curls, CURLOPT_HEADERFUNCTION, t1540_header_callback); 103 easy_setopt(curls, CURLOPT_HEADERDATA, &st); 104 105 easy_setopt(curls, CURLOPT_XFERINFOFUNCTION, please_continue); 106 easy_setopt(curls, CURLOPT_XFERINFODATA, &st); 107 easy_setopt(curls, CURLOPT_NOPROGRESS, 0L); 108 109 libtest_debug_config.nohex = 1; 110 libtest_debug_config.tracetime = 1; 111 test_setopt(curls, CURLOPT_DEBUGDATA, &libtest_debug_config); 112 easy_setopt(curls, CURLOPT_DEBUGFUNCTION, libtest_debug_cb); 113 easy_setopt(curls, CURLOPT_VERBOSE, 1L); 114 115 res = curl_easy_perform(curls); 116 117 test_cleanup: 118 119 curl_easy_cleanup(curls); 120 curl_global_cleanup(); 121 122 return res; /* return the final return code */ 123 }