CURLOPT_DEBUGFUNCTION.3 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. .\" generated by cd2nroff 0.1 from CURLOPT_DEBUGFUNCTION.md
  2. .TH CURLOPT_DEBUGFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_DEBUGFUNCTION \- debug callback
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. typedef enum {
  9. CURLINFO_TEXT = 0,
  10. CURLINFO_HEADER_IN, /* 1 */
  11. CURLINFO_HEADER_OUT, /* 2 */
  12. CURLINFO_DATA_IN, /* 3 */
  13. CURLINFO_DATA_OUT, /* 4 */
  14. CURLINFO_SSL_DATA_IN, /* 5 */
  15. CURLINFO_SSL_DATA_OUT, /* 6 */
  16. CURLINFO_END
  17. } curl_infotype;
  18. int debug_callback(CURL *handle,
  19. curl_infotype type,
  20. char *data,
  21. size_t size,
  22. void *clientp);
  23. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_DEBUGFUNCTION,
  24. debug_callback);
  25. .fi
  26. .SH DESCRIPTION
  27. Pass a pointer to your callback function, which should match the prototype
  28. shown above.
  29. \fICURLOPT_DEBUGFUNCTION(3)\fP replaces the standard debug function used when
  30. \fICURLOPT_VERBOSE(3)\fP is in effect. This callback receives debug
  31. information, as specified in the \fItype\fP argument. This function must
  32. return 0. The \fIdata\fP pointed to by the char * passed to this function is
  33. not null\-terminated, but is exactly of the \fIsize\fP as told by the
  34. \fIsize\fP argument.
  35. The \fIclientp\fP argument is the pointer set with \fICURLOPT_DEBUGDATA(3)\fP.
  36. Available \fBcurl_infotype\fP values:
  37. .IP CURLINFO_TEXT
  38. The data is informational text.
  39. .IP CURLINFO_HEADER_IN
  40. The data is header (or header\-like) data received from the peer.
  41. .IP CURLINFO_HEADER_OUT
  42. The data is header (or header\-like) data sent to the peer.
  43. .IP CURLINFO_DATA_IN
  44. The data is the unprocessed protocol data received from the peer. Even if the
  45. data is encoded or compressed, it is not provided decoded nor decompressed
  46. to this callback. If you need the data in decoded and decompressed form, use
  47. \fICURLOPT_WRITEFUNCTION(3)\fP.
  48. .IP CURLINFO_DATA_OUT
  49. The data is protocol data sent to the peer.
  50. .IP CURLINFO_SSL_DATA_OUT
  51. The data is SSL/TLS (binary) data sent to the peer.
  52. .IP CURLINFO_SSL_DATA_IN
  53. The data is SSL/TLS (binary) data received from the peer.
  54. .PP
  55. WARNING: This callback may be called with the curl \fIhandle\fP set to an internal
  56. handle. (Added in 8.4.0)
  57. If you need to distinguish your curl \fIhandle\fP from internal handles then set
  58. \fICURLOPT_PRIVATE(3)\fP on your handle.
  59. .SH DEFAULT
  60. NULL
  61. .SH PROTOCOLS
  62. This functionality affects all supported protocols
  63. .SH EXAMPLE
  64. .nf
  65. static
  66. void dump(const char *text,
  67. FILE *stream, unsigned char *ptr, size_t size)
  68. {
  69. size_t i;
  70. size_t c;
  71. unsigned int width = 0x10;
  72. fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\\n",
  73. text, (long)size, (long)size);
  74. for(i = 0; i < size; i += width) {
  75. fprintf(stream, "%4.4lx: ", (long)i);
  76. /* show hex to the left */
  77. for(c = 0; c < width; c++) {
  78. if(i + c < size)
  79. fprintf(stream, "%02x ", ptr[i + c]);
  80. else
  81. fputs(" ", stream);
  82. }
  83. /* show data on the right */
  84. for(c = 0; (c < width) && (i + c < size); c++) {
  85. char x = (ptr[i + c] >= 0x20 && ptr[i + c] < 0x80) ? ptr[i + c] : '.';
  86. fputc(x, stream);
  87. }
  88. fputc('\\n', stream); /* newline */
  89. }
  90. }
  91. static
  92. int my_trace(CURL *handle, curl_infotype type,
  93. char *data, size_t size,
  94. void *clientp)
  95. {
  96. const char *text;
  97. (void)handle; /* prevent compiler warning */
  98. (void)clientp;
  99. switch(type) {
  100. case CURLINFO_TEXT:
  101. fputs("== Info: ", stderr);
  102. fwrite(data, size, 1, stderr);
  103. default: /* in case a new one is introduced to shock us */
  104. return 0;
  105. case CURLINFO_HEADER_OUT:
  106. text = "=> Send header";
  107. break;
  108. case CURLINFO_DATA_OUT:
  109. text = "=> Send data";
  110. break;
  111. case CURLINFO_SSL_DATA_OUT:
  112. text = "=> Send SSL data";
  113. break;
  114. case CURLINFO_HEADER_IN:
  115. text = "<= Recv header";
  116. break;
  117. case CURLINFO_DATA_IN:
  118. text = "<= Recv data";
  119. break;
  120. case CURLINFO_SSL_DATA_IN:
  121. text = "<= Recv SSL data";
  122. break;
  123. }
  124. dump(text, stderr, (unsigned char *)data, size);
  125. return 0;
  126. }
  127. int main(void)
  128. {
  129. CURL *curl;
  130. CURLcode res;
  131. curl = curl_easy_init();
  132. if(curl) {
  133. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
  134. /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
  135. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  136. /* example.com is redirected, so we tell libcurl to follow redirection */
  137. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  138. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
  139. res = curl_easy_perform(curl);
  140. /* Check for errors */
  141. if(res != CURLE_OK)
  142. fprintf(stderr, "curl_easy_perform() failed: %s\\n",
  143. curl_easy_strerror(res));
  144. /* always cleanup */
  145. curl_easy_cleanup(curl);
  146. }
  147. return 0;
  148. }
  149. .fi
  150. .SH AVAILABILITY
  151. Added in curl 7.9.6
  152. .SH RETURN VALUE
  153. Returns CURLE_OK
  154. .SH SEE ALSO
  155. .BR CURLINFO_CONN_ID (3),
  156. .BR CURLINFO_XFER_ID (3),
  157. .BR CURLOPT_DEBUGDATA (3),
  158. .BR CURLOPT_VERBOSE (3),
  159. .BR curl_global_trace (3)