Revert "Imported Upstream version 7.53.1"
[platform/upstream/curl.git] / docs / examples / multi-debugcallback.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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  * 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
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.10ld bytes (0x%8.8lx)\n",
55           text, (long)size, (long)size);
56
57   for(i=0; i<size; i+= width) {
58
59     fprintf(stream, "%4.4lx: ", (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 && 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 && ptr[i+c+2]==0x0A) {
80         i+=(c+3-width);
81         break;
82       }
83     }
84     fputc('\n', stream); /* newline */
85   }
86   fflush(stream);
87 }
88
89 static
90 int my_trace(CURL *handle, curl_infotype type,
91              unsigned char *data, size_t size,
92              void *userp)
93 {
94   const char *text;
95
96   (void)userp;
97   (void)handle; /* prevent compiler warning */
98
99   switch (type) {
100   case CURLINFO_TEXT:
101     fprintf(stderr, "== Info: %s", data);
102   default: /* in case a new one is introduced to shock us */
103     return 0;
104
105   case CURLINFO_HEADER_OUT:
106     text = "=> Send header";
107     break;
108   case CURLINFO_DATA_OUT:
109     text = "=> Send data";
110     break;
111   case CURLINFO_HEADER_IN:
112     text = "<= Recv header";
113     break;
114   case CURLINFO_DATA_IN:
115     text = "<= Recv data";
116     break;
117   }
118
119   dump(text, stderr, data, size, TRUE);
120   return 0;
121 }
122
123 /*
124  * Simply download a HTTP file.
125  */
126 int main(void)
127 {
128   CURL *http_handle;
129   CURLM *multi_handle;
130
131   int still_running; /* keep number of running handles */
132
133   http_handle = curl_easy_init();
134
135   /* set the options (I left out a few, you'll get the point anyway) */
136   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
137
138   curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace);
139   curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L);
140
141   /* init a multi stack */
142   multi_handle = curl_multi_init();
143
144   /* add the individual transfers */
145   curl_multi_add_handle(multi_handle, http_handle);
146
147   /* we start some action by calling perform right away */
148   curl_multi_perform(multi_handle, &still_running);
149
150   do {
151     struct timeval timeout;
152     int rc; /* select() return code */
153     CURLMcode mc; /* curl_multi_fdset() return code */
154
155     fd_set fdread;
156     fd_set fdwrite;
157     fd_set fdexcep;
158     int maxfd = -1;
159
160     long curl_timeo = -1;
161
162     FD_ZERO(&fdread);
163     FD_ZERO(&fdwrite);
164     FD_ZERO(&fdexcep);
165
166     /* set a suitable timeout to play around with */
167     timeout.tv_sec = 1;
168     timeout.tv_usec = 0;
169
170     curl_multi_timeout(multi_handle, &curl_timeo);
171     if(curl_timeo >= 0) {
172       timeout.tv_sec = curl_timeo / 1000;
173       if(timeout.tv_sec > 1)
174         timeout.tv_sec = 1;
175       else
176         timeout.tv_usec = (curl_timeo % 1000) * 1000;
177     }
178
179     /* get file descriptors from the transfers */
180     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
181
182     if(mc != CURLM_OK) {
183       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
184       break;
185     }
186
187     /* On success the value of maxfd is guaranteed to be >= -1. We call
188        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
189        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
190        to sleep 100ms, which is the minimum suggested value in the
191        curl_multi_fdset() doc. */
192
193     if(maxfd == -1) {
194 #ifdef _WIN32
195       Sleep(100);
196       rc = 0;
197 #else
198       /* Portable sleep for platforms other than Windows. */
199       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
200       rc = select(0, NULL, NULL, NULL, &wait);
201 #endif
202     }
203     else {
204       /* Note that on some platforms 'timeout' may be modified by select().
205          If you need access to the original value save a copy beforehand. */
206       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
207     }
208
209     switch(rc) {
210     case -1:
211       /* select error */
212       still_running = 0;
213       printf("select() returns error, this is badness\n");
214       break;
215     case 0:
216     default:
217       /* timeout or readable/writable sockets */
218       curl_multi_perform(multi_handle, &still_running);
219       break;
220     }
221   } while(still_running);
222
223   curl_multi_cleanup(multi_handle);
224
225   curl_easy_cleanup(http_handle);
226
227   return 0;
228 }