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


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08