multi-debugcallback.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /* This is an example showing the multi interface and the debug callback. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. /* somewhat unix-specific */
  26. #include <sys/time.h>
  27. #include <unistd.h>
  28. /* curl stuff */
  29. #include <curl/curl.h>
  30. typedef char bool;
  31. #define TRUE 1
  32. static
  33. void dump(const char *text,
  34. FILE *stream, unsigned char *ptr, size_t size,
  35. bool nohex)
  36. {
  37. size_t i;
  38. size_t c;
  39. unsigned int width=0x10;
  40. if(nohex)
  41. /* without the hex output, we can fit more on screen */
  42. width = 0x40;
  43. fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
  44. text, (long)size, (long)size);
  45. for(i=0; i<size; i+= width) {
  46. fprintf(stream, "%4.4lx: ", (long)i);
  47. if(!nohex) {
  48. /* hex not disabled, show it */
  49. for(c = 0; c < width; c++)
  50. if(i+c < size)
  51. fprintf(stream, "%02x ", ptr[i+c]);
  52. else
  53. fputs(" ", stream);
  54. }
  55. for(c = 0; (c < width) && (i+c < size); c++) {
  56. /* check for 0D0A; if found, skip past and start a new line of output */
  57. if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && 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 && ptr[i+c+2]==0x0A) {
  65. i+=(c+3-width);
  66. break;
  67. }
  68. }
  69. fputc('\n', stream); /* newline */
  70. }
  71. fflush(stream);
  72. }
  73. static
  74. int my_trace(CURL *handle, curl_infotype type,
  75. unsigned char *data, size_t size,
  76. void *userp)
  77. {
  78. const char *text;
  79. (void)userp;
  80. (void)handle; /* prevent compiler warning */
  81. switch (type) {
  82. case CURLINFO_TEXT:
  83. fprintf(stderr, "== Info: %s", data);
  84. default: /* in case a new one is introduced to shock us */
  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. }
  99. dump(text, stderr, data, size, TRUE);
  100. return 0;
  101. }
  102. /*
  103. * Simply download a HTTP file.
  104. */
  105. int main(void)
  106. {
  107. CURL *http_handle;
  108. CURLM *multi_handle;
  109. int still_running; /* keep number of running handles */
  110. http_handle = curl_easy_init();
  111. /* set the options (I left out a few, you'll get the point anyway) */
  112. curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
  113. curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
  114. curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
  115. /* init a multi stack */
  116. multi_handle = curl_multi_init();
  117. /* add the individual transfers */
  118. curl_multi_add_handle(multi_handle, http_handle);
  119. /* we start some action by calling perform right away */
  120. curl_multi_perform(multi_handle, &still_running);
  121. while(still_running) {
  122. struct timeval timeout;
  123. int rc; /* select() return code */
  124. fd_set fdread;
  125. fd_set fdwrite;
  126. fd_set fdexcep;
  127. int maxfd = -1;
  128. long curl_timeo = -1;
  129. FD_ZERO(&fdread);
  130. FD_ZERO(&fdwrite);
  131. FD_ZERO(&fdexcep);
  132. /* set a suitable timeout to play around with */
  133. timeout.tv_sec = 1;
  134. timeout.tv_usec = 0;
  135. curl_multi_timeout(multi_handle, &curl_timeo);
  136. if(curl_timeo >= 0) {
  137. timeout.tv_sec = curl_timeo / 1000;
  138. if(timeout.tv_sec > 1)
  139. timeout.tv_sec = 1;
  140. else
  141. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  142. }
  143. /* get file descriptors from the transfers */
  144. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  145. /* In a real-world program you OF COURSE check the return code of the
  146. function calls. On success, the value of maxfd is guaranteed to be
  147. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  148. case of (maxfd == -1), we call select(0, ...), which is basically equal
  149. to sleep. */
  150. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  151. switch(rc) {
  152. case -1:
  153. /* select error */
  154. still_running = 0;
  155. printf("select() returns error, this is badness\n");
  156. break;
  157. case 0:
  158. default:
  159. /* timeout or readable/writable sockets */
  160. curl_multi_perform(multi_handle, &still_running);
  161. break;
  162. }
  163. }
  164. curl_multi_cleanup(multi_handle);
  165. curl_easy_cleanup(http_handle);
  166. return 0;
  167. }