debug.c 4.2 KB

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