2
0

debug.c 3.7 KB

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