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