multi-debugcallback.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * multi interface and debug callback
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. /* somewhat unix-specific */
  31. #include <sys/time.h>
  32. #include <unistd.h>
  33. /* curl stuff */
  34. #include <curl/curl.h>
  35. typedef char bool;
  36. #define TRUE 1
  37. static
  38. void dump(const char *text,
  39. FILE *stream, unsigned char *ptr, size_t size,
  40. bool nohex)
  41. {
  42. size_t i;
  43. size_t c;
  44. unsigned int width = 0x10;
  45. if(nohex)
  46. /* without the hex output, we can fit more on screen */
  47. width = 0x40;
  48. fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
  49. text, (unsigned long)size, (unsigned long)size);
  50. for(i = 0; i<size; i += width) {
  51. fprintf(stream, "%4.4lx: ", (unsigned long)i);
  52. if(!nohex) {
  53. /* hex not disabled, show it */
  54. for(c = 0; c < width; c++)
  55. if(i + c < size)
  56. fprintf(stream, "%02x ", ptr[i + c]);
  57. else
  58. fputs(" ", stream);
  59. }
  60. for(c = 0; (c < width) && (i + c < size); c++) {
  61. /* check for 0D0A; if found, skip past and start a new line of output */
  62. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  63. ptr[i + c + 1] == 0x0A) {
  64. i += (c + 2 - width);
  65. break;
  66. }
  67. fprintf(stream, "%c",
  68. (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
  69. /* check again for 0D0A, to avoid an extra \n if it's at width */
  70. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  71. ptr[i + c + 2] == 0x0A) {
  72. i += (c + 3 - width);
  73. break;
  74. }
  75. }
  76. fputc('\n', stream); /* newline */
  77. }
  78. fflush(stream);
  79. }
  80. static
  81. int my_trace(CURL *handle, curl_infotype type,
  82. unsigned char *data, size_t size,
  83. void *userp)
  84. {
  85. const char *text;
  86. (void)userp;
  87. (void)handle; /* prevent compiler warning */
  88. switch(type) {
  89. case CURLINFO_TEXT:
  90. fprintf(stderr, "== Info: %s", data);
  91. /* FALLTHROUGH */
  92. default: /* in case a new one is introduced to shock us */
  93. return 0;
  94. case CURLINFO_HEADER_OUT:
  95. text = "=> Send header";
  96. break;
  97. case CURLINFO_DATA_OUT:
  98. text = "=> Send data";
  99. break;
  100. case CURLINFO_HEADER_IN:
  101. text = "<= Recv header";
  102. break;
  103. case CURLINFO_DATA_IN:
  104. text = "<= Recv data";
  105. break;
  106. }
  107. dump(text, stderr, data, size, TRUE);
  108. return 0;
  109. }
  110. /*
  111. * Simply download an HTTP file.
  112. */
  113. int main(void)
  114. {
  115. CURL *http_handle;
  116. CURLM *multi_handle;
  117. int still_running = 0; /* keep number of running handles */
  118. http_handle = curl_easy_init();
  119. /* set the options (I left out a few, you will get the point anyway) */
  120. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  121. curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
  122. curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
  123. /* init a multi stack */
  124. multi_handle = curl_multi_init();
  125. /* add the individual transfers */
  126. curl_multi_add_handle(multi_handle, http_handle);
  127. do {
  128. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  129. if(still_running)
  130. /* wait for activity, timeout or "nothing" */
  131. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  132. if(mc)
  133. break;
  134. } while(still_running);
  135. curl_multi_cleanup(multi_handle);
  136. curl_easy_cleanup(http_handle);
  137. return 0;
  138. }