Annotation of embedaddon/curl/docs/examples/debug.c, revision 1.1.1.1

1.1       misho       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:  * Show how CURLOPT_DEBUGFUNCTION can be used.
                     24:  * </DESC>
                     25:  */
                     26: #include <stdio.h>
                     27: #include <curl/curl.h>
                     28: 
                     29: struct data {
                     30:   char trace_ascii; /* 1 or 0 */
                     31: };
                     32: 
                     33: static
                     34: void dump(const char *text,
                     35:           FILE *stream, unsigned char *ptr, size_t size,
                     36:           char nohex)
                     37: {
                     38:   size_t i;
                     39:   size_t c;
                     40: 
                     41:   unsigned int width = 0x10;
                     42: 
                     43:   if(nohex)
                     44:     /* without the hex output, we can fit more on screen */
                     45:     width = 0x40;
                     46: 
                     47:   fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
                     48:           text, (unsigned long)size, (unsigned long)size);
                     49: 
                     50:   for(i = 0; i<size; i += width) {
                     51: 
                     52:     fprintf(stream, "%4.4lx: ", (unsigned long)i);
                     53: 
                     54:     if(!nohex) {
                     55:       /* hex not disabled, show it */
                     56:       for(c = 0; c < width; c++)
                     57:         if(i + c < size)
                     58:           fprintf(stream, "%02x ", ptr[i + c]);
                     59:         else
                     60:           fputs("   ", stream);
                     61:     }
                     62: 
                     63:     for(c = 0; (c < width) && (i + c < size); c++) {
                     64:       /* check for 0D0A; if found, skip past and start a new line of output */
                     65:       if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
                     66:          ptr[i + c + 1] == 0x0A) {
                     67:         i += (c + 2 - width);
                     68:         break;
                     69:       }
                     70:       fprintf(stream, "%c",
                     71:               (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
                     72:       /* check again for 0D0A, to avoid an extra \n if it's at width */
                     73:       if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
                     74:          ptr[i + c + 2] == 0x0A) {
                     75:         i += (c + 3 - width);
                     76:         break;
                     77:       }
                     78:     }
                     79:     fputc('\n', stream); /* newline */
                     80:   }
                     81:   fflush(stream);
                     82: }
                     83: 
                     84: static
                     85: int my_trace(CURL *handle, curl_infotype type,
                     86:              char *data, size_t size,
                     87:              void *userp)
                     88: {
                     89:   struct data *config = (struct data *)userp;
                     90:   const char *text;
                     91:   (void)handle; /* prevent compiler warning */
                     92: 
                     93:   switch(type) {
                     94:   case CURLINFO_TEXT:
                     95:     fprintf(stderr, "== Info: %s", data);
                     96:     /* FALLTHROUGH */
                     97:   default: /* in case a new one is introduced to shock us */
                     98:     return 0;
                     99: 
                    100:   case CURLINFO_HEADER_OUT:
                    101:     text = "=> Send header";
                    102:     break;
                    103:   case CURLINFO_DATA_OUT:
                    104:     text = "=> Send data";
                    105:     break;
                    106:   case CURLINFO_SSL_DATA_OUT:
                    107:     text = "=> Send SSL data";
                    108:     break;
                    109:   case CURLINFO_HEADER_IN:
                    110:     text = "<= Recv header";
                    111:     break;
                    112:   case CURLINFO_DATA_IN:
                    113:     text = "<= Recv data";
                    114:     break;
                    115:   case CURLINFO_SSL_DATA_IN:
                    116:     text = "<= Recv SSL data";
                    117:     break;
                    118:   }
                    119: 
                    120:   dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
                    121:   return 0;
                    122: }
                    123: 
                    124: int main(void)
                    125: {
                    126:   CURL *curl;
                    127:   CURLcode res;
                    128:   struct data config;
                    129: 
                    130:   config.trace_ascii = 1; /* enable ascii tracing */
                    131: 
                    132:   curl = curl_easy_init();
                    133:   if(curl) {
                    134:     curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
                    135:     curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
                    136: 
                    137:     /* the DEBUGFUNCTION has no effect until we enable VERBOSE */
                    138:     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
                    139: 
                    140:     /* example.com is redirected, so we tell libcurl to follow redirection */
                    141:     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
                    142: 
                    143:     curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
                    144:     res = curl_easy_perform(curl);
                    145:     /* Check for errors */
                    146:     if(res != CURLE_OK)
                    147:       fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    148:               curl_easy_strerror(res));
                    149: 
                    150:     /* always cleanup */
                    151:     curl_easy_cleanup(curl);
                    152:   }
                    153:   return 0;
                    154: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>