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