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