multi-debugcallback.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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. * multi interface and debug callback
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. /* somewhat unix-specific */
  29. #include <sys/time.h>
  30. #include <unistd.h>
  31. /* curl stuff */
  32. #include <curl/curl.h>
  33. typedef char bool;
  34. #define TRUE 1
  35. static
  36. void dump(const char *text,
  37. FILE *stream, unsigned char *ptr, size_t size,
  38. bool nohex)
  39. {
  40. size_t i;
  41. size_t c;
  42. unsigned int width = 0x10;
  43. if(nohex)
  44. /* without the hex output, we can fit more on screen */
  45. width = 0x40;
  46. fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
  47. text, (unsigned long)size, (unsigned long)size);
  48. for(i = 0; i<size; i += width) {
  49. fprintf(stream, "%4.4lx: ", (unsigned long)i);
  50. if(!nohex) {
  51. /* hex not disabled, show it */
  52. for(c = 0; c < width; c++)
  53. if(i + c < size)
  54. fprintf(stream, "%02x ", ptr[i + c]);
  55. else
  56. fputs(" ", stream);
  57. }
  58. for(c = 0; (c < width) && (i + c < size); c++) {
  59. /* check for 0D0A; if found, skip past and start a new line of output */
  60. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  61. ptr[i + c + 1] == 0x0A) {
  62. i += (c + 2 - width);
  63. break;
  64. }
  65. fprintf(stream, "%c",
  66. (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
  67. /* check again for 0D0A, to avoid an extra \n if it's at width */
  68. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  69. ptr[i + c + 2] == 0x0A) {
  70. i += (c + 3 - width);
  71. break;
  72. }
  73. }
  74. fputc('\n', stream); /* newline */
  75. }
  76. fflush(stream);
  77. }
  78. static
  79. int my_trace(CURL *handle, curl_infotype type,
  80. unsigned char *data, size_t size,
  81. void *userp)
  82. {
  83. const char *text;
  84. (void)userp;
  85. (void)handle; /* prevent compiler warning */
  86. switch(type) {
  87. case CURLINFO_TEXT:
  88. fprintf(stderr, "== Info: %s", data);
  89. /* FALLTHROUGH */
  90. default: /* in case a new one is introduced to shock us */
  91. return 0;
  92. case CURLINFO_HEADER_OUT:
  93. text = "=> Send header";
  94. break;
  95. case CURLINFO_DATA_OUT:
  96. text = "=> Send data";
  97. break;
  98. case CURLINFO_HEADER_IN:
  99. text = "<= Recv header";
  100. break;
  101. case CURLINFO_DATA_IN:
  102. text = "<= Recv data";
  103. break;
  104. }
  105. dump(text, stderr, data, size, TRUE);
  106. return 0;
  107. }
  108. /*
  109. * Simply download a HTTP file.
  110. */
  111. int main(void)
  112. {
  113. CURL *http_handle;
  114. CURLM *multi_handle;
  115. int still_running = 0; /* keep number of running handles */
  116. http_handle = curl_easy_init();
  117. /* set the options (I left out a few, you'll get the point anyway) */
  118. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  119. curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
  120. curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
  121. /* init a multi stack */
  122. multi_handle = curl_multi_init();
  123. /* add the individual transfers */
  124. curl_multi_add_handle(multi_handle, http_handle);
  125. /* we start some action by calling perform right away */
  126. curl_multi_perform(multi_handle, &still_running);
  127. while(still_running) {
  128. struct timeval timeout;
  129. int rc; /* select() return code */
  130. CURLMcode mc; /* curl_multi_fdset() return code */
  131. fd_set fdread;
  132. fd_set fdwrite;
  133. fd_set fdexcep;
  134. int maxfd = -1;
  135. long curl_timeo = -1;
  136. FD_ZERO(&fdread);
  137. FD_ZERO(&fdwrite);
  138. FD_ZERO(&fdexcep);
  139. /* set a suitable timeout to play around with */
  140. timeout.tv_sec = 1;
  141. timeout.tv_usec = 0;
  142. curl_multi_timeout(multi_handle, &curl_timeo);
  143. if(curl_timeo >= 0) {
  144. timeout.tv_sec = curl_timeo / 1000;
  145. if(timeout.tv_sec > 1)
  146. timeout.tv_sec = 1;
  147. else
  148. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  149. }
  150. /* get file descriptors from the transfers */
  151. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  152. if(mc != CURLM_OK) {
  153. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  154. break;
  155. }
  156. /* On success the value of maxfd is guaranteed to be >= -1. We call
  157. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  158. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  159. to sleep 100ms, which is the minimum suggested value in the
  160. curl_multi_fdset() doc. */
  161. if(maxfd == -1) {
  162. #ifdef _WIN32
  163. Sleep(100);
  164. rc = 0;
  165. #else
  166. /* Portable sleep for platforms other than Windows. */
  167. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  168. rc = select(0, NULL, NULL, NULL, &wait);
  169. #endif
  170. }
  171. else {
  172. /* Note that on some platforms 'timeout' may be modified by select().
  173. If you need access to the original value save a copy beforehand. */
  174. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  175. }
  176. switch(rc) {
  177. case -1:
  178. /* select error */
  179. still_running = 0;
  180. printf("select() returns error, this is badness\n");
  181. break;
  182. case 0:
  183. default:
  184. /* timeout or readable/writable sockets */
  185. curl_multi_perform(multi_handle, &still_running);
  186. break;
  187. }
  188. }
  189. curl_multi_cleanup(multi_handle);
  190. curl_easy_cleanup(http_handle);
  191. return 0;
  192. }