lib1518.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "memdebug.h"
  26. /* Test inspired by github issue 3340 */
  27. static size_t writecb(char *buffer, size_t size, size_t nitems,
  28. void *outstream)
  29. {
  30. (void)buffer;
  31. (void)size;
  32. (void)nitems;
  33. (void)outstream;
  34. return 0;
  35. }
  36. int test(char *URL)
  37. {
  38. CURL *curl;
  39. CURLcode res = CURLE_OK;
  40. long curlResponseCode;
  41. long curlRedirectCount;
  42. char *effectiveUrl = NULL;
  43. char *redirectUrl = NULL;
  44. #ifdef LIB1543
  45. CURLU *urlu = NULL;
  46. #endif
  47. curl = curl_easy_init();
  48. if(!curl) {
  49. fprintf(stderr, "curl_easy_init() failed\n");
  50. curl_global_cleanup();
  51. return TEST_ERR_MAJOR_BAD;
  52. }
  53. #ifdef LIB1543
  54. /* set CURLOPT_URLU */
  55. {
  56. CURLUcode rc = CURLUE_OK;
  57. urlu = curl_url();
  58. if(urlu)
  59. rc = curl_url_set(urlu, CURLUPART_URL, URL, CURLU_ALLOW_SPACE);
  60. if(!urlu || rc) {
  61. goto test_cleanup;
  62. }
  63. test_setopt(curl, CURLOPT_CURLU, urlu);
  64. }
  65. test_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  66. #else
  67. test_setopt(curl, CURLOPT_URL, URL);
  68. /* just to make it explicit and visible in this test: */
  69. test_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
  70. #endif
  71. /* Perform the request, res will get the return code */
  72. res = curl_easy_perform(curl);
  73. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &curlResponseCode);
  74. curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &curlRedirectCount);
  75. curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effectiveUrl);
  76. curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &redirectUrl);
  77. res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
  78. printf("res %d\n"
  79. "status %d\n"
  80. "redirects %d\n"
  81. "effectiveurl %s\n"
  82. "redirecturl %s\n",
  83. (int)res,
  84. (int)curlResponseCode,
  85. (int)curlRedirectCount,
  86. effectiveUrl,
  87. redirectUrl ? redirectUrl : "blank");
  88. test_cleanup:
  89. /* always cleanup */
  90. curl_easy_cleanup(curl);
  91. curl_global_cleanup();
  92. #ifdef LIB1543
  93. curl_url_cleanup(urlu);
  94. #endif
  95. return res;
  96. }