2024-01-17 18:32:44 +08:00
|
|
|
---
|
2024-02-28 18:28:10 +08:00
|
|
|
c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2024-01-17 18:32:44 +08:00
|
|
|
SPDX-License-Identifier: curl
|
|
|
|
Title: CURLOPT_DEBUGFUNCTION
|
|
|
|
Section: 3
|
|
|
|
Source: libcurl
|
|
|
|
See-also:
|
|
|
|
- CURLINFO_CONN_ID (3)
|
|
|
|
- CURLINFO_XFER_ID (3)
|
|
|
|
- CURLOPT_DEBUGDATA (3)
|
|
|
|
- CURLOPT_VERBOSE (3)
|
|
|
|
- curl_global_trace (3)
|
|
|
|
---
|
|
|
|
|
|
|
|
# NAME
|
|
|
|
|
|
|
|
CURLOPT_DEBUGFUNCTION - debug callback
|
|
|
|
|
|
|
|
# SYNOPSIS
|
|
|
|
|
|
|
|
~~~c
|
2014-06-19 05:35:48 +08:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
CURLINFO_TEXT = 0,
|
|
|
|
CURLINFO_HEADER_IN, /* 1 */
|
|
|
|
CURLINFO_HEADER_OUT, /* 2 */
|
|
|
|
CURLINFO_DATA_IN, /* 3 */
|
|
|
|
CURLINFO_DATA_OUT, /* 4 */
|
|
|
|
CURLINFO_SSL_DATA_IN, /* 5 */
|
|
|
|
CURLINFO_SSL_DATA_OUT, /* 6 */
|
|
|
|
CURLINFO_END
|
|
|
|
} curl_infotype;
|
|
|
|
|
|
|
|
int debug_callback(CURL *handle,
|
|
|
|
curl_infotype type,
|
|
|
|
char *data,
|
|
|
|
size_t size,
|
2023-02-07 22:58:17 +08:00
|
|
|
void *clientp);
|
2014-06-19 05:35:48 +08:00
|
|
|
|
|
|
|
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_DEBUGFUNCTION,
|
|
|
|
debug_callback);
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# DESCRIPTION
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
Pass a pointer to your callback function, which should match the prototype
|
|
|
|
shown above.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
CURLOPT_DEBUGFUNCTION(3) replaces the standard debug function used when
|
|
|
|
CURLOPT_VERBOSE(3) is in effect. This callback receives debug
|
|
|
|
information, as specified in the *type* argument. This function must
|
|
|
|
return 0. The *data* pointed to by the char * passed to this function is
|
|
|
|
not null-terminated, but is exactly of the *size* as told by the
|
|
|
|
*size* argument.
|
|
|
|
|
|
|
|
The *clientp* argument is the pointer set with CURLOPT_DEBUGDATA(3).
|
|
|
|
|
|
|
|
Available **curl_infotype** values:
|
2014-06-19 05:35:48 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
## CURLINFO_TEXT
|
2014-06-19 05:35:48 +08:00
|
|
|
|
|
|
|
The data is informational text.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLINFO_HEADER_IN
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
The data is header (or header-like) data received from the peer.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLINFO_HEADER_OUT
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
The data is header (or header-like) data sent to the peer.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLINFO_DATA_IN
|
|
|
|
|
2022-12-16 19:45:20 +08:00
|
|
|
The data is the unprocessed protocol data received from the peer. Even if the
|
2024-03-01 03:55:28 +08:00
|
|
|
data is encoded or compressed, it is not provided decoded nor decompressed
|
2023-08-22 23:40:39 +08:00
|
|
|
to this callback. If you need the data in decoded and decompressed form, use
|
2024-01-17 18:32:44 +08:00
|
|
|
CURLOPT_WRITEFUNCTION(3).
|
|
|
|
|
|
|
|
## CURLINFO_DATA_OUT
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
The data is protocol data sent to the peer.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLINFO_SSL_DATA_OUT
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
The data is SSL/TLS (binary) data sent to the peer.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLINFO_SSL_DATA_IN
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
The data is SSL/TLS (binary) data received from the peer.
|
2023-10-05 15:19:47 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
WARNING: This callback may be called with the curl *handle* set to an
|
2023-10-05 15:19:47 +08:00
|
|
|
internal handle. (Added in 8.4.0)
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
If you need to distinguish your curl *handle* from internal handles then
|
|
|
|
set CURLOPT_PRIVATE(3) on your handle.
|
|
|
|
|
|
|
|
# DEFAULT
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
NULL
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# PROTOCOLS
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
All
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# EXAMPLE
|
|
|
|
|
|
|
|
~~~c
|
2014-10-21 16:26:40 +08:00
|
|
|
static
|
|
|
|
void dump(const char *text,
|
|
|
|
FILE *stream, unsigned char *ptr, size_t size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t c;
|
2023-12-04 17:50:42 +08:00
|
|
|
unsigned int width = 0x10;
|
2014-10-21 16:26:40 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
|
2014-10-21 16:26:40 +08:00
|
|
|
text, (long)size, (long)size);
|
|
|
|
|
2023-12-04 17:50:42 +08:00
|
|
|
for(i = 0; i < size; i += width) {
|
2014-10-21 16:26:40 +08:00
|
|
|
fprintf(stream, "%4.4lx: ", (long)i);
|
|
|
|
|
|
|
|
/* show hex to the left */
|
|
|
|
for(c = 0; c < width; c++) {
|
2023-12-04 17:50:42 +08:00
|
|
|
if(i + c < size)
|
|
|
|
fprintf(stream, "%02x ", ptr[i + c]);
|
2014-10-21 16:26:40 +08:00
|
|
|
else
|
|
|
|
fputs(" ", stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* show data on the right */
|
2023-12-04 17:50:42 +08:00
|
|
|
for(c = 0; (c < width) && (i + c < size); c++) {
|
|
|
|
char x = (ptr[i + c] >= 0x20 && ptr[i + c] < 0x80) ? ptr[i + c] : '.';
|
2016-02-21 05:23:05 +08:00
|
|
|
fputc(x, stream);
|
|
|
|
}
|
2014-10-21 16:26:40 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
fputc('\n', stream); /* newline */
|
2014-10-21 16:26:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int my_trace(CURL *handle, curl_infotype type,
|
|
|
|
char *data, size_t size,
|
2023-02-07 22:58:17 +08:00
|
|
|
void *clientp)
|
2014-10-21 16:26:40 +08:00
|
|
|
{
|
|
|
|
const char *text;
|
|
|
|
(void)handle; /* prevent compiler warning */
|
2023-02-07 22:58:17 +08:00
|
|
|
(void)clientp;
|
2014-10-21 16:26:40 +08:00
|
|
|
|
2023-12-04 17:50:42 +08:00
|
|
|
switch(type) {
|
2014-10-21 16:26:40 +08:00
|
|
|
case CURLINFO_TEXT:
|
2022-11-07 19:15:24 +08:00
|
|
|
fputs("== Info: ", stderr);
|
|
|
|
fwrite(data, size, 1, stderr);
|
2014-10-21 16:26:40 +08:00
|
|
|
default: /* in case a new one is introduced to shock us */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case CURLINFO_HEADER_OUT:
|
|
|
|
text = "=> Send header";
|
|
|
|
break;
|
|
|
|
case CURLINFO_DATA_OUT:
|
|
|
|
text = "=> Send data";
|
|
|
|
break;
|
|
|
|
case CURLINFO_SSL_DATA_OUT:
|
|
|
|
text = "=> Send SSL data";
|
|
|
|
break;
|
|
|
|
case CURLINFO_HEADER_IN:
|
|
|
|
text = "<= Recv header";
|
|
|
|
break;
|
|
|
|
case CURLINFO_DATA_IN:
|
|
|
|
text = "<= Recv data";
|
|
|
|
break;
|
|
|
|
case CURLINFO_SSL_DATA_IN:
|
|
|
|
text = "<= Recv SSL data";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dump(text, stderr, (unsigned char *)data, size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if(curl) {
|
|
|
|
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
|
|
|
|
|
|
|
|
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
|
|
|
|
|
|
|
/* example.com is redirected, so we tell libcurl to follow redirection */
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
|
2020-09-17 05:04:07 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
|
2014-10-21 16:26:40 +08:00
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
/* Check for errors */
|
|
|
|
if(res != CURLE_OK)
|
2024-01-17 18:32:44 +08:00
|
|
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
2014-10-21 16:26:40 +08:00
|
|
|
curl_easy_strerror(res));
|
|
|
|
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# AVAILABILITY
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
Always
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# RETURN VALUE
|
|
|
|
|
2014-06-19 05:35:48 +08:00
|
|
|
Returns CURLE_OK
|