CURLOPT_DEBUGFUNCTION.3 5.5 KB

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