2
0

lib1540.c 3.6 KB

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