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