multi-debugcallback.c 4.2 KB

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