2
0

debug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.10ld bytes (0x%8.8lx)\n",
  43. text, (long)size, (long)size);
  44. for(i=0; i<size; i+= width) {
  45. fprintf(stream, "%4.4lx: ", (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 && ptr[i+c+1]==0x0A) {
  57. i+=(c+2-width);
  58. break;
  59. }
  60. fprintf(stream, "%c",
  61. (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
  62. /* check again for 0D0A, to avoid an extra \n if it's at width */
  63. if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
  64. i+=(c+3-width);
  65. break;
  66. }
  67. }
  68. fputc('\n', stream); /* newline */
  69. }
  70. fflush(stream);
  71. }
  72. static
  73. int my_trace(CURL *handle, curl_infotype type,
  74. char *data, size_t size,
  75. void *userp)
  76. {
  77. struct data *config = (struct data *)userp;
  78. const char *text;
  79. (void)handle; /* prevent compiler warning */
  80. switch(type) {
  81. case CURLINFO_TEXT:
  82. fprintf(stderr, "== Info: %s", data);
  83. /* FALLTHROUGH */
  84. default: /* in case a new one is introduced to shock us */
  85. return 0;
  86. case CURLINFO_HEADER_OUT:
  87. text = "=> Send header";
  88. break;
  89. case CURLINFO_DATA_OUT:
  90. text = "=> Send data";
  91. break;
  92. case CURLINFO_SSL_DATA_OUT:
  93. text = "=> Send SSL data";
  94. break;
  95. case CURLINFO_HEADER_IN:
  96. text = "<= Recv header";
  97. break;
  98. case CURLINFO_DATA_IN:
  99. text = "<= Recv data";
  100. break;
  101. case CURLINFO_SSL_DATA_IN:
  102. text = "<= Recv SSL data";
  103. break;
  104. }
  105. dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
  106. return 0;
  107. }
  108. int main(void)
  109. {
  110. CURL *curl;
  111. CURLcode res;
  112. struct data config;
  113. config.trace_ascii = 1; /* enable ascii tracing */
  114. curl = curl_easy_init();
  115. if(curl) {
  116. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
  117. curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
  118. /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
  119. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  120. /* example.com is redirected, so we tell libcurl to follow redirection */
  121. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  122. curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
  123. res = curl_easy_perform(curl);
  124. /* Check for errors */
  125. if(res != CURLE_OK)
  126. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  127. curl_easy_strerror(res));
  128. /* always cleanup */
  129. curl_easy_cleanup(curl);
  130. }
  131. return 0;
  132. }