xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [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.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 | /* <DESC> |
| 25 | * Show how CURLOPT_DEBUGFUNCTION can be used. |
| 26 | * </DESC> |
| 27 | */ |
| 28 | #include <stdio.h> |
| 29 | #include <curl/curl.h> |
| 30 | |
| 31 | struct data { |
| 32 | char trace_ascii; /* 1 or 0 */ |
| 33 | }; |
| 34 | |
| 35 | static |
| 36 | void dump(const char *text, |
| 37 | FILE *stream, unsigned char *ptr, size_t size, |
| 38 | char nohex) |
| 39 | { |
| 40 | size_t i; |
| 41 | size_t c; |
| 42 | |
| 43 | unsigned int width = 0x10; |
| 44 | |
| 45 | if(nohex) |
| 46 | /* without the hex output, we can fit more on screen */ |
| 47 | width = 0x40; |
| 48 | |
| 49 | fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n", |
| 50 | text, (unsigned long)size, (unsigned long)size); |
| 51 | |
| 52 | for(i = 0; i<size; i += width) { |
| 53 | |
| 54 | fprintf(stream, "%4.4lx: ", (unsigned long)i); |
| 55 | |
| 56 | if(!nohex) { |
| 57 | /* hex not disabled, show it */ |
| 58 | for(c = 0; c < width; c++) |
| 59 | if(i + c < size) |
| 60 | fprintf(stream, "%02x ", ptr[i + c]); |
| 61 | else |
| 62 | fputs(" ", stream); |
| 63 | } |
| 64 | |
| 65 | for(c = 0; (c < width) && (i + c < size); c++) { |
| 66 | /* check for 0D0A; if found, skip past and start a new line of output */ |
| 67 | if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D && |
| 68 | ptr[i + c + 1] == 0x0A) { |
| 69 | i += (c + 2 - width); |
| 70 | break; |
| 71 | } |
| 72 | fprintf(stream, "%c", |
| 73 | (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.'); |
| 74 | /* check again for 0D0A, to avoid an extra \n if it's at width */ |
| 75 | if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D && |
| 76 | ptr[i + c + 2] == 0x0A) { |
| 77 | i += (c + 3 - width); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | fputc('\n', stream); /* newline */ |
| 82 | } |
| 83 | fflush(stream); |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | int my_trace(CURL *handle, curl_infotype type, |
| 88 | char *data, size_t size, |
| 89 | void *userp) |
| 90 | { |
| 91 | struct data *config = (struct data *)userp; |
| 92 | const char *text; |
| 93 | (void)handle; /* prevent compiler warning */ |
| 94 | |
| 95 | switch(type) { |
| 96 | case CURLINFO_TEXT: |
| 97 | fprintf(stderr, "== Info: %s", data); |
| 98 | /* FALLTHROUGH */ |
| 99 | default: /* in case a new one is introduced to shock us */ |
| 100 | return 0; |
| 101 | |
| 102 | case CURLINFO_HEADER_OUT: |
| 103 | text = "=> Send header"; |
| 104 | break; |
| 105 | case CURLINFO_DATA_OUT: |
| 106 | text = "=> Send data"; |
| 107 | break; |
| 108 | case CURLINFO_SSL_DATA_OUT: |
| 109 | text = "=> Send SSL data"; |
| 110 | break; |
| 111 | case CURLINFO_HEADER_IN: |
| 112 | text = "<= Recv header"; |
| 113 | break; |
| 114 | case CURLINFO_DATA_IN: |
| 115 | text = "<= Recv data"; |
| 116 | break; |
| 117 | case CURLINFO_SSL_DATA_IN: |
| 118 | text = "<= Recv SSL data"; |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | dump(text, stderr, (unsigned char *)data, size, config->trace_ascii); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int main(void) |
| 127 | { |
| 128 | CURL *curl; |
| 129 | CURLcode res; |
| 130 | struct data config; |
| 131 | |
| 132 | config.trace_ascii = 1; /* enable ascii tracing */ |
| 133 | |
| 134 | curl = curl_easy_init(); |
| 135 | if(curl) { |
| 136 | curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace); |
| 137 | curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config); |
| 138 | |
| 139 | /* the DEBUGFUNCTION has no effect until we enable VERBOSE */ |
| 140 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| 141 | |
| 142 | /* example.com is redirected, so we tell libcurl to follow redirection */ |
| 143 | curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 144 | |
| 145 | curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/"); |
| 146 | res = curl_easy_perform(curl); |
| 147 | /* Check for errors */ |
| 148 | if(res != CURLE_OK) |
| 149 | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
| 150 | curl_easy_strerror(res)); |
| 151 | |
| 152 | /* always cleanup */ |
| 153 | curl_easy_cleanup(curl); |
| 154 | } |
| 155 | return 0; |
| 156 | } |