lib1947.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #include "memdebug.h"
  26. static size_t writecb(char *data, size_t n, size_t l, void *userp)
  27. {
  28. /* ignore the data */
  29. (void)data;
  30. (void)userp;
  31. return n*l;
  32. }
  33. int test(char *URL)
  34. {
  35. CURL *curl;
  36. CURLcode res;
  37. curl_global_init(CURL_GLOBAL_DEFAULT);
  38. curl = curl_easy_init();
  39. if(curl) {
  40. struct curl_header *h;
  41. int count = 0;
  42. int origins;
  43. /* perform a request that involves redirection */
  44. curl_easy_setopt(curl, CURLOPT_URL, URL);
  45. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
  46. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  47. res = curl_easy_perform(curl);
  48. if(res)
  49. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  50. curl_easy_strerror(res));
  51. /* count the number of requests by reading the first header of each
  52. request. */
  53. origins = (CURLH_HEADER|CURLH_TRAILER|CURLH_CONNECT|
  54. CURLH_1XX|CURLH_PSEUDO);
  55. do {
  56. h = curl_easy_nextheader(curl, origins, count, NULL);
  57. if(h)
  58. count++;
  59. } while(h);
  60. printf("count = %u\n", count);
  61. /* perform another request - without redirect */
  62. curl_easy_setopt(curl, CURLOPT_URL, libtest_arg2);
  63. res = curl_easy_perform(curl);
  64. if(res)
  65. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  66. curl_easy_strerror(res));
  67. /* count the number of requests again. */
  68. count = 0;
  69. do {
  70. h = curl_easy_nextheader(curl, origins, count, NULL);
  71. if(h)
  72. count++;
  73. } while(h);
  74. printf("count = %u\n", count);
  75. curl_easy_cleanup(curl);
  76. }
  77. curl_global_cleanup();
  78. return 0;
  79. }