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