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