lib1541.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 hd_count;
  31. int bd_count;
  32. CURLcode result;
  33. };
  34. #define KN(a) a, #a
  35. static int geterr(const char *name, CURLcode val, int lineno)
  36. {
  37. printf("CURLINFO_%s returned %d, \"%s\" on line %d\n",
  38. name, val, curl_easy_strerror(val), lineno);
  39. return (int)val;
  40. }
  41. static void report_time(const char *key, const char *where, curl_off_t time,
  42. bool ok)
  43. {
  44. if(ok)
  45. printf("%s on %s is OK\n", key, where);
  46. else
  47. printf("%s on %s is WRONG: %" CURL_FORMAT_CURL_OFF_T "\n",
  48. key, where, time);
  49. }
  50. static void check_time(CURL *easy, int key, const char *name,
  51. const char *where)
  52. {
  53. curl_off_t tval;
  54. CURLcode res = curl_easy_getinfo(easy, (CURLINFO)key, &tval);
  55. if(res) {
  56. geterr(name, res, __LINE__);
  57. }
  58. else
  59. report_time(name, where, tval, tval > 0);
  60. }
  61. static void check_time0(CURL *easy, int key, const char *name,
  62. const char *where)
  63. {
  64. curl_off_t tval;
  65. CURLcode res = curl_easy_getinfo(easy, (CURLINFO)key, &tval);
  66. if(res) {
  67. geterr(name, res, __LINE__);
  68. }
  69. else
  70. report_time(name, where, tval, !tval);
  71. }
  72. static size_t header_callback(void *ptr, size_t size, size_t nmemb,
  73. void *userp)
  74. {
  75. struct transfer_status *st = (struct transfer_status *)userp;
  76. size_t len = size * nmemb;
  77. (void)ptr;
  78. if(!st->hd_count++) {
  79. /* first header, check some CURLINFO value to be reported. See #13125 */
  80. check_time(st->easy, KN(CURLINFO_CONNECT_TIME_T), "1st header");
  81. check_time(st->easy, KN(CURLINFO_PRETRANSFER_TIME_T), "1st header");
  82. check_time(st->easy, KN(CURLINFO_STARTTRANSFER_TIME_T), "1st header");
  83. /* continuously updated */
  84. check_time(st->easy, KN(CURLINFO_TOTAL_TIME_T), "1st header");
  85. /* no SSL, must be 0 */
  86. check_time0(st->easy, KN(CURLINFO_APPCONNECT_TIME_T), "1st header");
  87. /* download not really started */
  88. check_time0(st->easy, KN(CURLINFO_SPEED_DOWNLOAD_T), "1st header");
  89. }
  90. (void)fwrite(ptr, size, nmemb, stdout);
  91. return len;
  92. }
  93. static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  94. {
  95. struct transfer_status *st = (struct transfer_status *)userp;
  96. (void)ptr;
  97. (void)st;
  98. fwrite(ptr, size, nmemb, stdout);
  99. return size * nmemb;
  100. }
  101. int test(char *URL)
  102. {
  103. CURL *curls = NULL;
  104. int res = 0;
  105. struct transfer_status st;
  106. start_test_timing();
  107. memset(&st, 0, sizeof(st));
  108. global_init(CURL_GLOBAL_ALL);
  109. easy_init(curls);
  110. st.easy = curls; /* to allow callbacks access */
  111. easy_setopt(curls, CURLOPT_URL, URL);
  112. easy_setopt(curls, CURLOPT_WRITEFUNCTION, write_callback);
  113. easy_setopt(curls, CURLOPT_WRITEDATA, &st);
  114. easy_setopt(curls, CURLOPT_HEADERFUNCTION, header_callback);
  115. easy_setopt(curls, CURLOPT_HEADERDATA, &st);
  116. easy_setopt(curls, CURLOPT_NOPROGRESS, 0L);
  117. res = curl_easy_perform(curls);
  118. check_time(curls, KN(CURLINFO_CONNECT_TIME_T), "done");
  119. check_time(curls, KN(CURLINFO_PRETRANSFER_TIME_T), "done");
  120. check_time(curls, KN(CURLINFO_STARTTRANSFER_TIME_T), "done");
  121. /* no SSL, must be 0 */
  122. check_time0(curls, KN(CURLINFO_APPCONNECT_TIME_T), "done");
  123. check_time(curls, KN(CURLINFO_SPEED_DOWNLOAD_T), "done");
  124. check_time(curls, KN(CURLINFO_TOTAL_TIME_T), "done");
  125. test_cleanup:
  126. curl_easy_cleanup(curls);
  127. curl_global_cleanup();
  128. return (int)res; /* return the final return code */
  129. }