debug.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* <DESC>
  25. * Show how CURLOPT_DEBUGFUNCTION can be used.
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <curl/curl.h>
  30. struct data {
  31. char trace_ascii; /* 1 or 0 */
  32. };
  33. static
  34. void dump(const char *text,
  35. FILE *stream, unsigned char *ptr, size_t size,
  36. char nohex)
  37. {
  38. size_t i;
  39. size_t c;
  40. unsigned int width = 0x10;
  41. if(nohex)
  42. /* without the hex output, we can fit more on screen */
  43. width = 0x40;
  44. fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
  45. text, (unsigned long)size, (unsigned long)size);
  46. for(i = 0; i < size; i += width) {
  47. fprintf(stream, "%4.4lx: ", (unsigned long)i);
  48. if(!nohex) {
  49. /* hex not disabled, show it */
  50. for(c = 0; c < width; c++)
  51. if(i + c < size)
  52. fprintf(stream, "%02x ", ptr[i + c]);
  53. else
  54. fputs(" ", stream);
  55. }
  56. for(c = 0; (c < width) && (i + c < size); c++) {
  57. /* check for 0D0A; if found, skip past and start a new line of output */
  58. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  59. ptr[i + c + 1] == 0x0A) {
  60. i += (c + 2 - width);
  61. break;
  62. }
  63. fprintf(stream, "%c",
  64. (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
  65. /* check again for 0D0A, to avoid an extra \n if it's at width */
  66. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  67. ptr[i + c + 2] == 0x0A) {
  68. i += (c + 3 - width);
  69. break;
  70. }
  71. }
  72. fputc('\n', stream); /* newline */
  73. }
  74. fflush(stream);
  75. }
  76. static
  77. int my_trace(CURL *handle, curl_infotype type,
  78. char *data, size_t size,
  79. void *userp)
  80. {
  81. struct data *config = (struct data *)userp;
  82. const char *text;
  83. (void)handle; /* prevent compiler warning */
  84. switch(type) {
  85. case CURLINFO_TEXT:
  86. fprintf(stderr, "== Info: %s", data);
  87. return 0;
  88. case CURLINFO_HEADER_OUT:
  89. text = "=> Send header";
  90. break;
  91. case CURLINFO_DATA_OUT:
  92. text = "=> Send data";
  93. break;
  94. case CURLINFO_SSL_DATA_OUT:
  95. text = "=> Send SSL data";
  96. break;
  97. case CURLINFO_HEADER_IN:
  98. text = "<= Recv header";
  99. break;
  100. case CURLINFO_DATA_IN:
  101. text = "<= Recv data";
  102. break;
  103. case CURLINFO_SSL_DATA_IN:
  104. text = "<= Recv SSL data";
  105. break;
  106. default: /* in case a new one is introduced to shock us */
  107. return 0;
  108. }
  109. dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
  110. return 0;
  111. }
  112. int main(void)
  113. {
  114. CURL *curl;
  115. CURLcode res;
  116. struct data config;
  117. config.trace_ascii = 1; /* enable ASCII tracing */
  118. curl = curl_easy_init();
  119. if(curl) {
  120. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
  121. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
  122. /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
  123. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  124. /* example.com is redirected, so we tell libcurl to follow redirection */
  125. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  126. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
  127. res = curl_easy_perform(curl);
  128. /* Check for errors */
  129. if(res != CURLE_OK)
  130. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  131. curl_easy_strerror(res));
  132. /* always cleanup */
  133. curl_easy_cleanup(curl);
  134. }
  135. return 0;
  136. }