2
0

multi-debugcallback.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /* curl stuff */
  31. #include <curl/curl.h>
  32. #define TRUE 1
  33. static void dump(const char *text, FILE *stream, unsigned char *ptr,
  34. size_t size, 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. unsigned char *data, size_t size,
  77. void *userp)
  78. {
  79. const char *text;
  80. (void)userp;
  81. (void)handle; /* prevent compiler warning */
  82. switch(type) {
  83. case CURLINFO_TEXT:
  84. fprintf(stderr, "== Info: %s", data);
  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_HEADER_IN:
  93. text = "<= Recv header";
  94. break;
  95. case CURLINFO_DATA_IN:
  96. text = "<= Recv data";
  97. break;
  98. default: /* in case a new one is introduced to shock us */
  99. return 0;
  100. }
  101. dump(text, stderr, data, size, TRUE);
  102. return 0;
  103. }
  104. /*
  105. * Simply download an HTTP file.
  106. */
  107. int main(void)
  108. {
  109. CURL *http_handle;
  110. CURLM *multi_handle;
  111. int still_running = 0; /* keep number of running handles */
  112. http_handle = curl_easy_init();
  113. /* set the options (I left out a few, you get the point anyway) */
  114. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  115. curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
  116. curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
  117. /* init a multi stack */
  118. multi_handle = curl_multi_init();
  119. /* add the individual transfers */
  120. curl_multi_add_handle(multi_handle, http_handle);
  121. do {
  122. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  123. if(still_running)
  124. /* wait for activity, timeout or "nothing" */
  125. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  126. if(mc)
  127. break;
  128. } while(still_running);
  129. curl_multi_cleanup(multi_handle);
  130. curl_easy_cleanup(http_handle);
  131. return 0;
  132. }