multi-debugcallback.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * multi interface and debug callback
00024  * </DESC>
00025  */
00026 
00027 #include <stdio.h>
00028 #include <string.h>
00029 
00030 /* somewhat unix-specific */
00031 #include <sys/time.h>
00032 #include <unistd.h>
00033 
00034 /* curl stuff */
00035 #include <curl/curl.h>
00036 
00037 typedef char bool;
00038 #define TRUE 1
00039 
00040 static
00041 void dump(const char *text,
00042           FILE *stream, unsigned char *ptr, size_t size,
00043           bool nohex)
00044 {
00045   size_t i;
00046   size_t c;
00047 
00048   unsigned int width=0x10;
00049 
00050   if(nohex)
00051     /* without the hex output, we can fit more on screen */
00052     width = 0x40;
00053 
00054   fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
00055           text, (long)size, (long)size);
00056 
00057   for(i=0; i<size; i+= width) {
00058 
00059     fprintf(stream, "%4.4lx: ", (long)i);
00060 
00061     if(!nohex) {
00062       /* hex not disabled, show it */
00063       for(c = 0; c < width; c++)
00064         if(i+c < size)
00065           fprintf(stream, "%02x ", ptr[i+c]);
00066         else
00067           fputs("   ", stream);
00068     }
00069 
00070     for(c = 0; (c < width) && (i+c < size); c++) {
00071       /* check for 0D0A; if found, skip past and start a new line of output */
00072       if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
00073         i+=(c+2-width);
00074         break;
00075       }
00076       fprintf(stream, "%c",
00077               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
00078       /* check again for 0D0A, to avoid an extra \n if it's at width */
00079       if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
00080         i+=(c+3-width);
00081         break;
00082       }
00083     }
00084     fputc('\n', stream); /* newline */
00085   }
00086   fflush(stream);
00087 }
00088 
00089 static
00090 int my_trace(CURL *handle, curl_infotype type,
00091              unsigned char *data, size_t size,
00092              void *userp)
00093 {
00094   const char *text;
00095 
00096   (void)userp;
00097   (void)handle; /* prevent compiler warning */
00098 
00099   switch(type) {
00100   case CURLINFO_TEXT:
00101     fprintf(stderr, "== Info: %s", data);
00102   default: /* in case a new one is introduced to shock us */
00103     return 0;
00104 
00105   case CURLINFO_HEADER_OUT:
00106     text = "=> Send header";
00107     break;
00108   case CURLINFO_DATA_OUT:
00109     text = "=> Send data";
00110     break;
00111   case CURLINFO_HEADER_IN:
00112     text = "<= Recv header";
00113     break;
00114   case CURLINFO_DATA_IN:
00115     text = "<= Recv data";
00116     break;
00117   }
00118 
00119   dump(text, stderr, data, size, TRUE);
00120   return 0;
00121 }
00122 
00123 /*
00124  * Simply download a HTTP file.
00125  */
00126 int main(void)
00127 {
00128   CURL *http_handle;
00129   CURLM *multi_handle;
00130 
00131   int still_running; /* keep number of running handles */
00132 
00133   http_handle = curl_easy_init();
00134 
00135   /* set the options (I left out a few, you'll get the point anyway) */
00136   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
00137 
00138   curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
00139   curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
00140 
00141   /* init a multi stack */
00142   multi_handle = curl_multi_init();
00143 
00144   /* add the individual transfers */
00145   curl_multi_add_handle(multi_handle, http_handle);
00146 
00147   /* we start some action by calling perform right away */
00148   curl_multi_perform(multi_handle, &still_running);
00149 
00150   do {
00151     struct timeval timeout;
00152     int rc; /* select() return code */
00153     CURLMcode mc; /* curl_multi_fdset() return code */
00154 
00155     fd_set fdread;
00156     fd_set fdwrite;
00157     fd_set fdexcep;
00158     int maxfd = -1;
00159 
00160     long curl_timeo = -1;
00161 
00162     FD_ZERO(&fdread);
00163     FD_ZERO(&fdwrite);
00164     FD_ZERO(&fdexcep);
00165 
00166     /* set a suitable timeout to play around with */
00167     timeout.tv_sec = 1;
00168     timeout.tv_usec = 0;
00169 
00170     curl_multi_timeout(multi_handle, &curl_timeo);
00171     if(curl_timeo >= 0) {
00172       timeout.tv_sec = curl_timeo / 1000;
00173       if(timeout.tv_sec > 1)
00174         timeout.tv_sec = 1;
00175       else
00176         timeout.tv_usec = (curl_timeo % 1000) * 1000;
00177     }
00178 
00179     /* get file descriptors from the transfers */
00180     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
00181 
00182     if(mc != CURLM_OK) {
00183       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00184       break;
00185     }
00186 
00187     /* On success the value of maxfd is guaranteed to be >= -1. We call
00188        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
00189        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
00190        to sleep 100ms, which is the minimum suggested value in the
00191        curl_multi_fdset() doc. */
00192 
00193     if(maxfd == -1) {
00194 #ifdef _WIN32
00195       Sleep(100);
00196       rc = 0;
00197 #else
00198       /* Portable sleep for platforms other than Windows. */
00199       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
00200       rc = select(0, NULL, NULL, NULL, &wait);
00201 #endif
00202     }
00203     else {
00204       /* Note that on some platforms 'timeout' may be modified by select().
00205          If you need access to the original value save a copy beforehand. */
00206       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00207     }
00208 
00209     switch(rc) {
00210     case -1:
00211       /* select error */
00212       still_running = 0;
00213       printf("select() returns error, this is badness\n");
00214       break;
00215     case 0:
00216     default:
00217       /* timeout or readable/writable sockets */
00218       curl_multi_perform(multi_handle, &still_running);
00219       break;
00220     }
00221   } while(still_running);
00222 
00223   curl_multi_cleanup(multi_handle);
00224 
00225   curl_easy_cleanup(http_handle);
00226 
00227   return 0;
00228 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05