lib1523.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.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. /* test case and code based on https://github.com/curl/curl/issues/3927 */
  24. #include "testutil.h"
  25. #include "warnless.h"
  26. #include "memdebug.h"
  27. static int dload_progress_cb(void *a, curl_off_t b, curl_off_t c,
  28. curl_off_t d, curl_off_t e)
  29. {
  30. (void)a;
  31. (void)b;
  32. (void)c;
  33. (void)d;
  34. (void)e;
  35. return 0;
  36. }
  37. static size_t write_cb(char *d, size_t n, size_t l, void *p)
  38. {
  39. /* take care of the data here, ignored in this example */
  40. (void)d;
  41. (void)p;
  42. return n*l;
  43. }
  44. static CURLcode run(CURL *hnd, long limit, long time)
  45. {
  46. curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_LIMIT, limit);
  47. curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_TIME, time);
  48. return curl_easy_perform(hnd);
  49. }
  50. int test(char *URL)
  51. {
  52. CURLcode ret;
  53. CURL *hnd;
  54. char buffer[CURL_ERROR_SIZE];
  55. curl_global_init(CURL_GLOBAL_ALL);
  56. hnd = curl_easy_init();
  57. curl_easy_setopt(hnd, CURLOPT_URL, URL);
  58. curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_cb);
  59. curl_easy_setopt(hnd, CURLOPT_ERRORBUFFER, buffer);
  60. curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0L);
  61. curl_easy_setopt(hnd, CURLOPT_XFERINFOFUNCTION, dload_progress_cb);
  62. printf("Start: %d\n", time(NULL));
  63. ret = run(hnd, 1, 2);
  64. if(ret)
  65. fprintf(stderr, "error %d: %s\n", ret, buffer);
  66. ret = run(hnd, 12000, 1);
  67. if(ret != CURLE_OPERATION_TIMEDOUT)
  68. fprintf(stderr, "error %d: %s\n", ret, buffer);
  69. else
  70. ret = 0;
  71. printf("End: %d\n", time(NULL));
  72. curl_easy_cleanup(hnd);
  73. curl_global_cleanup();
  74. return (int)ret;
  75. }