lib1540.c 3.4 KB

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