CURLOPT_DEBUGFUNCTION.3 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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 "May 17, 2022" "libcurl 7.85.0" "curl_easy_setopt options"
  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 *userptr);
  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 \fIuserptr\fP argument is the pointer set with \fICURLOPT_DEBUGDATA(3)\fP.
  58. Available curl_infotype 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 protocol data received from the peer.
  67. .IP CURLINFO_DATA_OUT
  68. The data is protocol data sent to the peer.
  69. .IP CURLINFO_SSL_DATA_OUT
  70. The data is SSL/TLS (binary) data sent to the peer.
  71. .IP CURLINFO_SSL_DATA_IN
  72. The data is SSL/TLS (binary) data received from the peer.
  73. .SH DEFAULT
  74. NULL
  75. .SH PROTOCOLS
  76. All
  77. .SH EXAMPLE
  78. .nf
  79. static
  80. void dump(const char *text,
  81. FILE *stream, unsigned char *ptr, size_t size)
  82. {
  83. size_t i;
  84. size_t c;
  85. unsigned int width=0x10;
  86. fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\\n",
  87. text, (long)size, (long)size);
  88. for(i=0; i<size; i+= width) {
  89. fprintf(stream, "%4.4lx: ", (long)i);
  90. /* show hex to the left */
  91. for(c = 0; c < width; c++) {
  92. if(i+c < size)
  93. fprintf(stream, "%02x ", ptr[i+c]);
  94. else
  95. fputs(" ", stream);
  96. }
  97. /* show data on the right */
  98. for(c = 0; (c < width) && (i+c < size); c++) {
  99. char x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x80) ? ptr[i+c] : '.';
  100. fputc(x, stream);
  101. }
  102. fputc('\\n', stream); /* newline */
  103. }
  104. }
  105. static
  106. int my_trace(CURL *handle, curl_infotype type,
  107. char *data, size_t size,
  108. void *userp)
  109. {
  110. const char *text;
  111. (void)handle; /* prevent compiler warning */
  112. (void)userp;
  113. switch (type) {
  114. case CURLINFO_TEXT:
  115. fprintf(stderr, "== Info: %s", data);
  116. default: /* in case a new one is introduced to shock us */
  117. return 0;
  118. case CURLINFO_HEADER_OUT:
  119. text = "=> Send header";
  120. break;
  121. case CURLINFO_DATA_OUT:
  122. text = "=> Send data";
  123. break;
  124. case CURLINFO_SSL_DATA_OUT:
  125. text = "=> Send SSL data";
  126. break;
  127. case CURLINFO_HEADER_IN:
  128. text = "<= Recv header";
  129. break;
  130. case CURLINFO_DATA_IN:
  131. text = "<= Recv data";
  132. break;
  133. case CURLINFO_SSL_DATA_IN:
  134. text = "<= Recv SSL data";
  135. break;
  136. }
  137. dump(text, stderr, (unsigned char *)data, size);
  138. return 0;
  139. }
  140. int main(void)
  141. {
  142. CURL *curl;
  143. CURLcode res;
  144. curl = curl_easy_init();
  145. if(curl) {
  146. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
  147. /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
  148. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  149. /* example.com is redirected, so we tell libcurl to follow redirection */
  150. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  151. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
  152. res = curl_easy_perform(curl);
  153. /* Check for errors */
  154. if(res != CURLE_OK)
  155. fprintf(stderr, "curl_easy_perform() failed: %s\\n",
  156. curl_easy_strerror(res));
  157. /* always cleanup */
  158. curl_easy_cleanup(curl);
  159. }
  160. return 0;
  161. }
  162. .fi
  163. .SH AVAILABILITY
  164. Always
  165. .SH RETURN VALUE
  166. Returns CURLE_OK
  167. .SH "SEE ALSO"
  168. .BR CURLOPT_VERBOSE "(3), " CURLOPT_DEBUGDATA "(3), "