xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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 | |
| 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 | |
| 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 | } |
| 84 | int test(char *URL) |
| 85 | { |
| 86 | CURL *easy; |
| 87 | |
| 88 | curl_global_init(CURL_GLOBAL_DEFAULT); |
| 89 | |
| 90 | easy = curl_easy_init(); |
| 91 | if(easy) { |
| 92 | CURLcode res; |
| 93 | curl_easy_setopt(easy, CURLOPT_URL, URL); |
| 94 | curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L); |
| 95 | curl_easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1L); |
| 96 | /* ignores any content */ |
| 97 | curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb); |
| 98 | |
| 99 | /* if there's a proxy set, use it */ |
| 100 | if(libtest_arg2 && *libtest_arg2) { |
| 101 | curl_easy_setopt(easy, CURLOPT_PROXY, libtest_arg2); |
| 102 | curl_easy_setopt(easy, CURLOPT_HTTPPROXYTUNNEL, 1L); |
| 103 | } |
| 104 | res = curl_easy_perform(easy); |
| 105 | if(res) { |
| 106 | printf("badness: %d\n", (int)res); |
| 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 | curl_easy_cleanup(easy); |
| 116 | } |
| 117 | curl_global_cleanup(); |
| 118 | return 0; |
| 119 | } |