2
0

lib1947.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. CURLcode test(char *URL)
  34. {
  35. CURL *curl;
  36. CURLcode res = CURLE_OK;
  37. struct curl_header *h;
  38. int count = 0;
  39. unsigned int origins;
  40. global_init(CURL_GLOBAL_DEFAULT);
  41. easy_init(curl);
  42. /* perform a request that involves redirection */
  43. easy_setopt(curl, CURLOPT_URL, URL);
  44. easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
  45. easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  46. res = curl_easy_perform(curl);
  47. if(res) {
  48. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  49. curl_easy_strerror(res));
  50. goto test_cleanup;
  51. }
  52. /* count the number of requests by reading the first header of each
  53. request. */
  54. origins = (CURLH_HEADER|CURLH_TRAILER|CURLH_CONNECT|
  55. CURLH_1XX|CURLH_PSEUDO);
  56. do {
  57. h = curl_easy_nextheader(curl, origins, count, NULL);
  58. if(h)
  59. count++;
  60. } while(h);
  61. printf("count = %u\n", count);
  62. /* perform another request - without redirect */
  63. easy_setopt(curl, CURLOPT_URL, libtest_arg2);
  64. res = curl_easy_perform(curl);
  65. if(res) {
  66. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  67. curl_easy_strerror(res));
  68. goto test_cleanup;
  69. }
  70. /* count the number of requests again. */
  71. count = 0;
  72. do {
  73. h = curl_easy_nextheader(curl, origins, count, NULL);
  74. if(h)
  75. count++;
  76. } while(h);
  77. printf("count = %u\n", count);
  78. test_cleanup:
  79. curl_easy_cleanup(curl);
  80. curl_global_cleanup();
  81. return res;
  82. }