lib1485.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. curl_off_t out_len;
  31. size_t hd_line;
  32. CURLcode result;
  33. int http_status;
  34. };
  35. static size_t header_callback(char *ptr, size_t size, size_t nmemb,
  36. void *userp)
  37. {
  38. struct transfer_status *st = (struct transfer_status *)userp;
  39. const char *hd = ptr;
  40. size_t len = size * nmemb;
  41. CURLcode result;
  42. (void)fwrite(ptr, size, nmemb, stdout);
  43. ++st->hd_line;
  44. if(len == 2 && hd[0] == '\r' && hd[1] == '\n') {
  45. curl_off_t clen;
  46. long httpcode = 0;
  47. /* end of a response */
  48. result = curl_easy_getinfo(st->easy, CURLINFO_RESPONSE_CODE, &httpcode);
  49. fprintf(stderr, "header_callback, get status: %ld, %d\n",
  50. httpcode, result);
  51. if(httpcode < 100 || httpcode >= 1000) {
  52. fprintf(stderr, "header_callback, invalid status: %ld, %d\n",
  53. httpcode, result);
  54. return CURLE_WRITE_ERROR;
  55. }
  56. st->http_status = (int)httpcode;
  57. if(st->http_status >= 200 && st->http_status < 300) {
  58. result = curl_easy_getinfo(st->easy, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
  59. &clen);
  60. fprintf(stderr, "header_callback, info Content-Length: %ld, %d\n",
  61. (long)clen, result);
  62. if(result) {
  63. st->result = result;
  64. return CURLE_WRITE_ERROR;
  65. }
  66. if(clen < 0) {
  67. fprintf(stderr, "header_callback, expected known Content-Length, "
  68. "got: %ld\n", (long)clen);
  69. return CURLE_WRITE_ERROR;
  70. }
  71. }
  72. }
  73. return len;
  74. }
  75. static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  76. {
  77. struct transfer_status *st = (struct transfer_status *)userp;
  78. size_t len = size * nmemb;
  79. fwrite(ptr, size, nmemb, stdout);
  80. st->out_len += (curl_off_t)len;
  81. return len;
  82. }
  83. CURLcode test(char *URL)
  84. {
  85. CURL *curls = NULL;
  86. CURLcode res = CURLE_OK;
  87. struct transfer_status st;
  88. start_test_timing();
  89. memset(&st, 0, sizeof(st));
  90. global_init(CURL_GLOBAL_ALL);
  91. easy_init(curls);
  92. st.easy = curls; /* to allow callbacks access */
  93. easy_setopt(curls, CURLOPT_URL, URL);
  94. easy_setopt(curls, CURLOPT_WRITEFUNCTION, write_callback);
  95. easy_setopt(curls, CURLOPT_WRITEDATA, &st);
  96. easy_setopt(curls, CURLOPT_HEADERFUNCTION, header_callback);
  97. easy_setopt(curls, CURLOPT_HEADERDATA, &st);
  98. easy_setopt(curls, CURLOPT_NOPROGRESS, 1L);
  99. res = curl_easy_perform(curls);
  100. test_cleanup:
  101. curl_easy_cleanup(curls);
  102. curl_global_cleanup();
  103. return res; /* return the final return code */
  104. }