1CURLOPT_DEBUGFUNCTION(3) curl_easy_setopt options CURLOPT_DEBUGFUNCTION(3)
2
3
4
6 CURLOPT_DEBUGFUNCTION - debug callback
7
9 #include <curl/curl.h>
10
11 typedef enum {
12 CURLINFO_TEXT = 0,
13 CURLINFO_HEADER_IN, /* 1 */
14 CURLINFO_HEADER_OUT, /* 2 */
15 CURLINFO_DATA_IN, /* 3 */
16 CURLINFO_DATA_OUT, /* 4 */
17 CURLINFO_SSL_DATA_IN, /* 5 */
18 CURLINFO_SSL_DATA_OUT, /* 6 */
19 CURLINFO_END
20 } curl_infotype;
21
22 int debug_callback(CURL *handle,
23 curl_infotype type,
24 char *data,
25 size_t size,
26 void *userptr);
27
28 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_DEBUGFUNCTION,
29 debug_callback);
30
32 Pass a pointer to your callback function, which should match the proto‐
33 type shown above.
34
35 CURLOPT_DEBUGFUNCTION(3) replaces the standard debug function used when
36 CURLOPT_VERBOSE(3) is in effect. This callback receives debug informa‐
37 tion, as specified in the type argument. This function must return 0.
38 The data pointed to by the char * passed to this function WILL NOT be
39 null-terminated, but will be exactly of the size as told by the size
40 argument.
41
42 The userptr argument is the pointer set with CURLOPT_DEBUGDATA(3).
43
44 Available curl_infotype values:
45
46 CURLINFO_TEXT
47 The data is informational text.
48
49 CURLINFO_HEADER_IN
50 The data is header (or header-like) data received from the peer.
51
52 CURLINFO_HEADER_OUT
53 The data is header (or header-like) data sent to the peer.
54
55 CURLINFO_DATA_IN
56 The data is protocol data received from the peer.
57
58 CURLINFO_DATA_OUT
59 The data is protocol data sent to the peer.
60
61 CURLINFO_SSL_DATA_OUT
62 The data is SSL/TLS (binary) data sent to the peer.
63
64 CURLINFO_SSL_DATA_IN
65 The data is SSL/TLS (binary) data received from the peer.
66
68 NULL
69
71 All
72
74 static
75 void dump(const char *text,
76 FILE *stream, unsigned char *ptr, size_t size)
77 {
78 size_t i;
79 size_t c;
80 unsigned int width=0x10;
81
82 fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
83 text, (long)size, (long)size);
84
85 for(i=0; i<size; i+= width) {
86 fprintf(stream, "%4.4lx: ", (long)i);
87
88 /* show hex to the left */
89 for(c = 0; c < width; c++) {
90 if(i+c < size)
91 fprintf(stream, "%02x ", ptr[i+c]);
92 else
93 fputs(" ", stream);
94 }
95
96 /* show data on the right */
97 for(c = 0; (c < width) && (i+c < size); c++) {
98 char x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x80) ? ptr[i+c] : '.';
99 fputc(x, stream);
100 }
101
102 fputc('\n', stream); /* newline */
103 }
104 }
105
106 static
107 int my_trace(CURL *handle, curl_infotype type,
108 char *data, size_t size,
109 void *userp)
110 {
111 const char *text;
112 (void)handle; /* prevent compiler warning */
113 (void)userp;
114
115 switch (type) {
116 case CURLINFO_TEXT:
117 fprintf(stderr, "== Info: %s", data);
118 default: /* in case a new one is introduced to shock us */
119 return 0;
120
121 case CURLINFO_HEADER_OUT:
122 text = "=> Send header";
123 break;
124 case CURLINFO_DATA_OUT:
125 text = "=> Send data";
126 break;
127 case CURLINFO_SSL_DATA_OUT:
128 text = "=> Send SSL data";
129 break;
130 case CURLINFO_HEADER_IN:
131 text = "<= Recv header";
132 break;
133 case CURLINFO_DATA_IN:
134 text = "<= Recv data";
135 break;
136 case CURLINFO_SSL_DATA_IN:
137 text = "<= Recv SSL data";
138 break;
139 }
140
141 dump(text, stderr, (unsigned char *)data, size);
142 return 0;
143 }
144
145 int main(void)
146 {
147 CURL *curl;
148 CURLcode res;
149
150 curl = curl_easy_init();
151 if(curl) {
152 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
153
154 /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
155 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
156
157 /* example.com is redirected, so we tell libcurl to follow redirection */
158 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
159
160 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
161 res = curl_easy_perform(curl);
162 /* Check for errors */
163 if(res != CURLE_OK)
164 fprintf(stderr, "curl_easy_perform() failed: %s\n",
165 curl_easy_strerror(res));
166
167 /* always cleanup */
168 curl_easy_cleanup(curl);
169 }
170 return 0;
171 }
172
174 Always
175
177 Returns CURLE_OK
178
180 CURLOPT_VERBOSE(3), CURLOPT_DEBUGDATA(3),
181
182
183
184libcurl 7.71.1 June 25, 2020 CURLOPT_DEBUGFUNCTION(3)