lib1540.c 3.3 KB

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