xref: /curl/docs/examples/multi-debugcallback.c (revision 53b4dfe4)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 /* <DESC>
25  * multi interface and debug callback
26  * </DESC>
27  */
28 
29 #include <stdio.h>
30 #include <string.h>
31 
32 /* curl stuff */
33 #include <curl/curl.h>
34 
35 #define TRUE 1
36 
dump(const char * text,FILE * stream,unsigned char * ptr,size_t size,char nohex)37 static void dump(const char *text, FILE *stream, unsigned char *ptr,
38                  size_t size, char nohex)
39 {
40   size_t i;
41   size_t c;
42 
43   unsigned int width = 0x10;
44 
45   if(nohex)
46     /* without the hex output, we can fit more on screen */
47     width = 0x40;
48 
49   fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n",
50           text, (unsigned long)size, (unsigned long)size);
51 
52   for(i = 0; i<size; i += width) {
53 
54     fprintf(stream, "%4.4lx: ", (unsigned long)i);
55 
56     if(!nohex) {
57       /* hex not disabled, show it */
58       for(c = 0; c < width; c++)
59         if(i + c < size)
60           fprintf(stream, "%02x ", ptr[i + c]);
61         else
62           fputs("   ", stream);
63     }
64 
65     for(c = 0; (c < width) && (i + c < size); c++) {
66       /* check for 0D0A; if found, skip past and start a new line of output */
67       if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
68          ptr[i + c + 1] == 0x0A) {
69         i += (c + 2 - width);
70         break;
71       }
72       fprintf(stream, "%c",
73               (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
74       /* check again for 0D0A, to avoid an extra \n if it's at width */
75       if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
76          ptr[i + c + 2] == 0x0A) {
77         i += (c + 3 - width);
78         break;
79       }
80     }
81     fputc('\n', stream); /* newline */
82   }
83   fflush(stream);
84 }
85 
86 static
my_trace(CURL * handle,curl_infotype type,unsigned char * data,size_t size,void * userp)87 int my_trace(CURL *handle, curl_infotype type,
88              unsigned char *data, size_t size,
89              void *userp)
90 {
91   const char *text;
92 
93   (void)userp;
94   (void)handle; /* prevent compiler warning */
95 
96   switch(type) {
97   case CURLINFO_TEXT:
98     fprintf(stderr, "== Info: %s", data);
99     return 0;
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_HEADER_IN:
107     text = "<= Recv header";
108     break;
109   case CURLINFO_DATA_IN:
110     text = "<= Recv data";
111     break;
112   default: /* in case a new one is introduced to shock us */
113     return 0;
114   }
115 
116   dump(text, stderr, data, size, TRUE);
117   return 0;
118 }
119 
120 /*
121  * Simply download an HTTP file.
122  */
main(void)123 int main(void)
124 {
125   CURL *http_handle;
126   CURLM *multi_handle;
127 
128   int still_running = 0; /* keep number of running handles */
129 
130   http_handle = curl_easy_init();
131 
132   /* set the options (I left out a few, you get the point anyway) */
133   curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
134 
135   curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
136   curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
137 
138   /* init a multi stack */
139   multi_handle = curl_multi_init();
140 
141   /* add the individual transfers */
142   curl_multi_add_handle(multi_handle, http_handle);
143 
144   do {
145     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
146 
147     if(still_running)
148       /* wait for activity, timeout or "nothing" */
149       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
150 
151     if(mc)
152       break;
153 
154   } while(still_running);
155 
156   curl_multi_cleanup(multi_handle);
157 
158   curl_easy_cleanup(http_handle);
159 
160   return 0;
161 }
162