debug.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  * Show how CURLOPT_DEBUGFUNCTION can be used.
00024  * </DESC>
00025  */
00026 #include <stdio.h>
00027 #include <curl/curl.h>
00028 
00029 struct data {
00030   char trace_ascii; /* 1 or 0 */
00031 };
00032 
00033 static
00034 void dump(const char *text,
00035           FILE *stream, unsigned char *ptr, size_t size,
00036           char nohex)
00037 {
00038   size_t i;
00039   size_t c;
00040 
00041   unsigned int width=0x10;
00042 
00043   if(nohex)
00044     /* without the hex output, we can fit more on screen */
00045     width = 0x40;
00046 
00047   fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
00048           text, (long)size, (long)size);
00049 
00050   for(i=0; i<size; i+= width) {
00051 
00052     fprintf(stream, "%4.4lx: ", (long)i);
00053 
00054     if(!nohex) {
00055       /* hex not disabled, show it */
00056       for(c = 0; c < width; c++)
00057         if(i+c < size)
00058           fprintf(stream, "%02x ", ptr[i+c]);
00059         else
00060           fputs("   ", stream);
00061     }
00062 
00063     for(c = 0; (c < width) && (i+c < size); c++) {
00064       /* check for 0D0A; if found, skip past and start a new line of output */
00065       if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
00066         i+=(c+2-width);
00067         break;
00068       }
00069       fprintf(stream, "%c",
00070               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
00071       /* check again for 0D0A, to avoid an extra \n if it's at width */
00072       if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
00073         i+=(c+3-width);
00074         break;
00075       }
00076     }
00077     fputc('\n', stream); /* newline */
00078   }
00079   fflush(stream);
00080 }
00081 
00082 static
00083 int my_trace(CURL *handle, curl_infotype type,
00084              char *data, size_t size,
00085              void *userp)
00086 {
00087   struct data *config = (struct data *)userp;
00088   const char *text;
00089   (void)handle; /* prevent compiler warning */
00090 
00091   switch(type) {
00092   case CURLINFO_TEXT:
00093     fprintf(stderr, "== Info: %s", data);
00094   default: /* in case a new one is introduced to shock us */
00095     return 0;
00096 
00097   case CURLINFO_HEADER_OUT:
00098     text = "=> Send header";
00099     break;
00100   case CURLINFO_DATA_OUT:
00101     text = "=> Send data";
00102     break;
00103   case CURLINFO_SSL_DATA_OUT:
00104     text = "=> Send SSL data";
00105     break;
00106   case CURLINFO_HEADER_IN:
00107     text = "<= Recv header";
00108     break;
00109   case CURLINFO_DATA_IN:
00110     text = "<= Recv data";
00111     break;
00112   case CURLINFO_SSL_DATA_IN:
00113     text = "<= Recv SSL data";
00114     break;
00115   }
00116 
00117   dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
00118   return 0;
00119 }
00120 
00121 int main(void)
00122 {
00123   CURL *curl;
00124   CURLcode res;
00125   struct data config;
00126 
00127   config.trace_ascii = 1; /* enable ascii tracing */
00128 
00129   curl = curl_easy_init();
00130   if(curl) {
00131     curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
00132     curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
00133 
00134     /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
00135     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
00136 
00137     /* example.com is redirected, so we tell libcurl to follow redirection */
00138     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
00139 
00140     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
00141     res = curl_easy_perform(curl);
00142     /* Check for errors */
00143     if(res != CURLE_OK)
00144       fprintf(stderr, "curl_easy_perform() failed: %s\n",
00145               curl_easy_strerror(res));
00146 
00147     /* always cleanup */
00148     curl_easy_cleanup(curl);
00149   }
00150   return 0;
00151 }


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