Revert "Update to 7.40.1"
[platform/upstream/curl.git] / docs / examples / multi-debugcallback.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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 http://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 /* This is an example showing the multi interface and the debug callback. */
23
24 #include <stdio.h>
25 #include <string.h>
26
27 /* somewhat unix-specific */
28 #include <sys/time.h>
29 #include <unistd.h>
30
31 /* curl stuff */
32 #include <curl/curl.h>
33
34 typedef char bool;
35 #define TRUE 1
36
37 static
38 void dump(const char *text,
39           FILE *stream, unsigned char *ptr, size_t size,
40           bool nohex)
41 {
42   size_t i;
43   size_t c;
44
45   unsigned int width=0x10;
46
47   if(nohex)
48     /* without the hex output, we can fit more on screen */
49     width = 0x40;
50
51   fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
52           text, (long)size, (long)size);
53
54   for(i=0; i<size; i+= width) {
55
56     fprintf(stream, "%4.4lx: ", (long)i);
57
58     if(!nohex) {
59       /* hex not disabled, show it */
60       for(c = 0; c < width; c++)
61         if(i+c < size)
62           fprintf(stream, "%02x ", ptr[i+c]);
63         else
64           fputs("   ", stream);
65     }
66
67     for(c = 0; (c < width) && (i+c < size); c++) {
68       /* check for 0D0A; if found, skip past and start a new line of output */
69       if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
70         i+=(c+2-width);
71         break;
72       }
73       fprintf(stream, "%c",
74               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
75       /* check again for 0D0A, to avoid an extra \n if it's at width */
76       if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && 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
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   default: /* in case a new one is introduced to shock us */
100     return 0;
101
102   case CURLINFO_HEADER_OUT:
103     text = "=> Send header";
104     break;
105   case CURLINFO_DATA_OUT:
106     text = "=> Send data";
107     break;
108   case CURLINFO_HEADER_IN:
109     text = "<= Recv header";
110     break;
111   case CURLINFO_DATA_IN:
112     text = "<= Recv data";
113     break;
114   }
115
116   dump(text, stderr, data, size, TRUE);
117   return 0;
118 }
119
120 /*
121  * Simply download a HTTP file.
122  */
123 int main(void)
124 {
125   CURL *http_handle;
126   CURLM *multi_handle;
127
128   int still_running; /* keep number of running handles */
129
130   http_handle = curl_easy_init();
131
132   /* set the options (I left out a few, you'll get the point anyway) */
133   curl_easy_setopt(http_handle, CURLOPT_URL, "http://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   /* we start some action by calling perform right away */
145   curl_multi_perform(multi_handle, &still_running);
146
147   do {
148     struct timeval timeout;
149     int rc; /* select() return code */
150
151     fd_set fdread;
152     fd_set fdwrite;
153     fd_set fdexcep;
154     int maxfd = -1;
155
156     long curl_timeo = -1;
157
158     FD_ZERO(&fdread);
159     FD_ZERO(&fdwrite);
160     FD_ZERO(&fdexcep);
161
162     /* set a suitable timeout to play around with */
163     timeout.tv_sec = 1;
164     timeout.tv_usec = 0;
165
166     curl_multi_timeout(multi_handle, &curl_timeo);
167     if(curl_timeo >= 0) {
168       timeout.tv_sec = curl_timeo / 1000;
169       if(timeout.tv_sec > 1)
170         timeout.tv_sec = 1;
171       else
172         timeout.tv_usec = (curl_timeo % 1000) * 1000;
173     }
174
175     /* get file descriptors from the transfers */
176     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
177
178     /* In a real-world program you OF COURSE check the return code of the
179        function calls.  On success, the value of maxfd is guaranteed to be
180        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
181        case of (maxfd == -1), we call select(0, ...), which is basically equal
182        to sleep. */
183
184     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
185
186     switch(rc) {
187     case -1:
188       /* select error */
189       still_running = 0;
190       printf("select() returns error, this is badness\n");
191       break;
192     case 0:
193     default:
194       /* timeout or readable/writable sockets */
195       curl_multi_perform(multi_handle, &still_running);
196       break;
197     }
198   } while(still_running);
199
200   curl_multi_cleanup(multi_handle);
201
202   curl_easy_cleanup(http_handle);
203
204   return 0;
205 }