• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 static const char *show[]={
30   "daTE",
31   "Server",
32   "content-type",
33   "content-length",
34   "location",
35   "set-cookie",
36   "silly-thing",
37   "fold",
38   NULL
39 };
40 
41 #ifdef LIB1946
42 #define HEADER_REQUEST 0
43 #else
44 #define HEADER_REQUEST -1
45 #endif
46 
showem(CURL * easy,unsigned int type)47 static void showem(CURL *easy, unsigned int type)
48 {
49   int i;
50   struct curl_header *header;
51   for(i = 0; show[i]; i++) {
52     if(CURLHE_OK == curl_easy_header(easy, show[i], 0, type, HEADER_REQUEST,
53                                      &header)) {
54       if(header->amount > 1) {
55         /* more than one, iterate over them */
56         size_t index = 0;
57         size_t amount = header->amount;
58         do {
59           printf("- %s == %s (%u/%u)\n", header->name, header->value,
60                  (int)index, (int)amount);
61 
62           if(++index == amount)
63             break;
64           if(CURLHE_OK != curl_easy_header(easy, show[i], index, type,
65                                            HEADER_REQUEST, &header))
66             break;
67         } while(1);
68       }
69       else {
70         /* only one of this */
71         printf(" %s == %s\n", header->name, header->value);
72       }
73     }
74   }
75 }
76 
write_cb(char * data,size_t n,size_t l,void * userp)77 static size_t write_cb(char *data, size_t n, size_t l, void *userp)
78 {
79   /* take care of the data here, ignored in this example */
80   (void)data;
81   (void)userp;
82   return n*l;
83 }
test(char * URL)84 int test(char *URL)
85 {
86   CURL *easy = NULL;
87   CURLcode res = CURLE_OK;
88 
89   global_init(CURL_GLOBAL_DEFAULT);
90   easy_init(easy);
91   easy_setopt(easy, CURLOPT_URL, URL);
92   easy_setopt(easy, CURLOPT_VERBOSE, 1L);
93   easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1L);
94   /* ignores any content */
95   easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb);
96 
97   /* if there's a proxy set, use it */
98   if(libtest_arg2 && *libtest_arg2) {
99     easy_setopt(easy, CURLOPT_PROXY, libtest_arg2);
100     easy_setopt(easy, CURLOPT_HTTPPROXYTUNNEL, 1L);
101   }
102   res = curl_easy_perform(easy);
103   if(res)
104     goto test_cleanup;
105 
106   showem(easy, CURLH_HEADER);
107   if(libtest_arg2 && *libtest_arg2) {
108     /* now show connect headers only */
109     showem(easy, CURLH_CONNECT);
110   }
111   showem(easy, CURLH_1XX);
112   showem(easy, CURLH_TRAILER);
113 
114 test_cleanup:
115   curl_easy_cleanup(easy);
116   curl_global_cleanup();
117   return (int)res;
118 }
119