multi-debugcallback.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * This is a very simple example using the multi interface and the debug
  11. * callback.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. /* somewhat unix-specific */
  16. #include <sys/time.h>
  17. #include <unistd.h>
  18. /* curl stuff */
  19. #include <curl/curl.h>
  20. typedef char bool;
  21. #define TRUE 1
  22. static
  23. void dump(const char *text,
  24. FILE *stream, unsigned char *ptr, size_t size,
  25. bool nohex)
  26. {
  27. size_t i;
  28. size_t c;
  29. unsigned int width=0x10;
  30. if(nohex)
  31. /* without the hex output, we can fit more on screen */
  32. width = 0x40;
  33. fprintf(stream, "%s, %zd bytes (0x%zx)\n", text, size, size);
  34. for(i=0; i<size; i+= width) {
  35. fprintf(stream, "%04zx: ", i);
  36. if(!nohex) {
  37. /* hex not disabled, show it */
  38. for(c = 0; c < width; c++)
  39. if(i+c < size)
  40. fprintf(stream, "%02x ", ptr[i+c]);
  41. else
  42. fputs(" ", stream);
  43. }
  44. for(c = 0; (c < width) && (i+c < size); c++) {
  45. /* check for 0D0A; if found, skip past and start a new line of output */
  46. if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
  47. i+=(c+2-width);
  48. break;
  49. }
  50. fprintf(stream, "%c",
  51. (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
  52. /* check again for 0D0A, to avoid an extra \n if it's at width */
  53. if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
  54. i+=(c+3-width);
  55. break;
  56. }
  57. }
  58. fputc('\n', stream); /* newline */
  59. }
  60. fflush(stream);
  61. }
  62. static
  63. int my_trace(CURL *handle, curl_infotype type,
  64. char *data, size_t size,
  65. void *userp)
  66. {
  67. const char *text;
  68. (void)handle; /* prevent compiler warning */
  69. switch (type) {
  70. case CURLINFO_TEXT:
  71. fprintf(stderr, "== Info: %s", data);
  72. default: /* in case a new one is introduced to shock us */
  73. return 0;
  74. case CURLINFO_HEADER_OUT:
  75. text = "=> Send header";
  76. break;
  77. case CURLINFO_DATA_OUT:
  78. text = "=> Send data";
  79. break;
  80. case CURLINFO_HEADER_IN:
  81. text = "<= Recv header";
  82. break;
  83. case CURLINFO_DATA_IN:
  84. text = "<= Recv data";
  85. break;
  86. }
  87. dump(text, stderr, data, size, TRUE);
  88. return 0;
  89. }
  90. /*
  91. * Simply download a HTTP file.
  92. */
  93. int main(int argc, char **argv)
  94. {
  95. CURL *http_handle;
  96. CURLM *multi_handle;
  97. int still_running; /* keep number of running handles */
  98. http_handle = curl_easy_init();
  99. /* set the options (I left out a few, you'll get the point anyway) */
  100. curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
  101. curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
  102. curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
  103. /* init a multi stack */
  104. multi_handle = curl_multi_init();
  105. /* add the individual transfers */
  106. curl_multi_add_handle(multi_handle, http_handle);
  107. /* we start some action by calling perform right away */
  108. while(CURLM_CALL_MULTI_PERFORM ==
  109. curl_multi_perform(multi_handle, &still_running));
  110. while(still_running) {
  111. struct timeval timeout;
  112. int rc; /* select() return code */
  113. fd_set fdread;
  114. fd_set fdwrite;
  115. fd_set fdexcep;
  116. int maxfd;
  117. FD_ZERO(&fdread);
  118. FD_ZERO(&fdwrite);
  119. FD_ZERO(&fdexcep);
  120. /* set a suitable timeout to play around with */
  121. timeout.tv_sec = 1;
  122. timeout.tv_usec = 0;
  123. /* get file descriptors from the transfers */
  124. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  125. /* In a real-world program you OF COURSE check the return code of the
  126. function calls, *and* you make sure that maxfd is bigger than -1
  127. so that the call to select() below makes sense! */
  128. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  129. switch(rc) {
  130. case -1:
  131. /* select error */
  132. still_running = 0;
  133. printf("select() returns error, this is badness\n");
  134. break;
  135. case 0:
  136. default:
  137. /* timeout or readable/writable sockets */
  138. while(CURLM_CALL_MULTI_PERFORM ==
  139. curl_multi_perform(multi_handle, &still_running));
  140. break;
  141. }
  142. }
  143. curl_multi_cleanup(multi_handle);
  144. curl_easy_cleanup(http_handle);
  145. return 0;
  146. }