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.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 31struct data { 32 char trace_ascii; /* 1 or 0 */ 33}; 34 35static 36void 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 86static 87int 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 return 0; 99 case CURLINFO_HEADER_OUT: 100 text = "=> Send header"; 101 break; 102 case CURLINFO_DATA_OUT: 103 text = "=> Send data"; 104 break; 105 case CURLINFO_SSL_DATA_OUT: 106 text = "=> Send SSL data"; 107 break; 108 case CURLINFO_HEADER_IN: 109 text = "<= Recv header"; 110 break; 111 case CURLINFO_DATA_IN: 112 text = "<= Recv data"; 113 break; 114 case CURLINFO_SSL_DATA_IN: 115 text = "<= Recv SSL data"; 116 break; 117 default: /* in case a new one is introduced to shock us */ 118 return 0; 119 } 120 121 dump(text, stderr, (unsigned char *)data, size, config->trace_ascii); 122 return 0; 123} 124 125int main(void) 126{ 127 CURL *curl; 128 CURLcode res; 129 struct data config; 130 131 config.trace_ascii = 1; /* enable ascii tracing */ 132 133 curl = curl_easy_init(); 134 if(curl) { 135 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace); 136 curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config); 137 138 /* the DEBUGFUNCTION has no effect until we enable VERBOSE */ 139 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 140 141 /* example.com is redirected, so we tell libcurl to follow redirection */ 142 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); 143 144 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/"); 145 res = curl_easy_perform(curl); 146 /* Check for errors */ 147 if(res != CURLE_OK) 148 fprintf(stderr, "curl_easy_perform() failed: %s\n", 149 curl_easy_strerror(res)); 150 151 /* always cleanup */ 152 curl_easy_cleanup(curl); 153 } 154 return 0; 155} 156