xref: /curl/tests/libtest/lib1947.c (revision 25cbc2f7)
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 
25 #include "test.h"
26 
27 #include "memdebug.h"
28 
writecb(char * data,size_t n,size_t l,void * userp)29 static size_t writecb(char *data, size_t n, size_t l, void *userp)
30 {
31   /* ignore the data */
32   (void)data;
33   (void)userp;
34   return n*l;
35 }
test(char * URL)36 CURLcode test(char *URL)
37 {
38   CURL *curl;
39   CURLcode res = CURLE_OK;
40   struct curl_header *h;
41   int count = 0;
42   unsigned int origins;
43 
44   global_init(CURL_GLOBAL_DEFAULT);
45 
46   easy_init(curl);
47 
48   /* perform a request that involves redirection */
49   easy_setopt(curl, CURLOPT_URL, URL);
50   easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
51   easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
52   res = curl_easy_perform(curl);
53   if(res) {
54     fprintf(stderr, "curl_easy_perform() failed: %s\n",
55             curl_easy_strerror(res));
56     goto test_cleanup;
57   }
58 
59   /* count the number of requests by reading the first header of each
60      request. */
61   origins = (CURLH_HEADER|CURLH_TRAILER|CURLH_CONNECT|
62              CURLH_1XX|CURLH_PSEUDO);
63   do {
64     h = curl_easy_nextheader(curl, origins, count, NULL);
65     if(h)
66       count++;
67   } while(h);
68   printf("count = %u\n", count);
69 
70   /* perform another request - without redirect */
71   easy_setopt(curl, CURLOPT_URL, libtest_arg2);
72   res = curl_easy_perform(curl);
73   if(res) {
74     fprintf(stderr, "curl_easy_perform() failed: %s\n",
75             curl_easy_strerror(res));
76     goto test_cleanup;
77   }
78 
79   /* count the number of requests again. */
80   count = 0;
81   do {
82     h = curl_easy_nextheader(curl, origins, count, NULL);
83     if(h)
84       count++;
85   } while(h);
86   printf("count = %u\n", count);
87 
88 test_cleanup:
89   curl_easy_cleanup(curl);
90   curl_global_cleanup();
91   return res;
92 }
93