Imported Upstream version 7.44.0
[platform/upstream/curl.git] / docs / examples / http2-download.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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 /* <DESC>
23  * Multiplexed HTTP/2 downloads over a single connection
24  * </DESC>
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 /* somewhat unix-specific */
30 #include <sys/time.h>
31 #include <unistd.h>
32
33 /* curl stuff */
34 #include <curl/curl.h>
35
36 #ifndef CURLPIPE_MULTIPLEX
37 /* This little trick will just make sure that we don't enable pipelining for
38    libcurls old enough to not have this symbol. It is _not_ defined to zero in
39    a recent libcurl header. */
40 #define CURLPIPE_MULTIPLEX 0
41 #endif
42
43 #define NUM_HANDLES 1000
44
45 void *curl_hnd[NUM_HANDLES];
46 int num_transfers;
47
48 /* a handle to number lookup, highly ineffective when we do many
49    transfers... */
50 static int hnd2num(CURL *hnd)
51 {
52   int i;
53   for(i=0; i< num_transfers; i++) {
54     if(curl_hnd[i] == hnd)
55       return i;
56   }
57   return 0; /* weird, but just a fail-safe */
58 }
59
60 static
61 void dump(const char *text, int num, unsigned char *ptr, size_t size,
62           char nohex)
63 {
64   size_t i;
65   size_t c;
66
67   unsigned int width=0x10;
68
69   if(nohex)
70     /* without the hex output, we can fit more on screen */
71     width = 0x40;
72
73   fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
74           num, text, (long)size, (long)size);
75
76   for(i=0; i<size; i+= width) {
77
78     fprintf(stderr, "%4.4lx: ", (long)i);
79
80     if(!nohex) {
81       /* hex not disabled, show it */
82       for(c = 0; c < width; c++)
83         if(i+c < size)
84           fprintf(stderr, "%02x ", ptr[i+c]);
85         else
86           fputs("   ", stderr);
87     }
88
89     for(c = 0; (c < width) && (i+c < size); c++) {
90       /* check for 0D0A; if found, skip past and start a new line of output */
91       if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
92         i+=(c+2-width);
93         break;
94       }
95       fprintf(stderr, "%c",
96               (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
97       /* check again for 0D0A, to avoid an extra \n if it's at width */
98       if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
99         i+=(c+3-width);
100         break;
101       }
102     }
103     fputc('\n', stderr); /* newline */
104   }
105 }
106
107 static
108 int my_trace(CURL *handle, curl_infotype type,
109              char *data, size_t size,
110              void *userp)
111 {
112   const char *text;
113   int num = hnd2num(handle);
114   (void)handle; /* prevent compiler warning */
115   (void)userp;
116   switch (type) {
117   case CURLINFO_TEXT:
118     fprintf(stderr, "== %d Info: %s", num, data);
119   default: /* in case a new one is introduced to shock us */
120     return 0;
121
122   case CURLINFO_HEADER_OUT:
123     text = "=> Send header";
124     break;
125   case CURLINFO_DATA_OUT:
126     text = "=> Send data";
127     break;
128   case CURLINFO_SSL_DATA_OUT:
129     text = "=> Send SSL data";
130     break;
131   case CURLINFO_HEADER_IN:
132     text = "<= Recv header";
133     break;
134   case CURLINFO_DATA_IN:
135     text = "<= Recv data";
136     break;
137   case CURLINFO_SSL_DATA_IN:
138     text = "<= Recv SSL data";
139     break;
140   }
141
142   dump(text, num, (unsigned char *)data, size, 1);
143   return 0;
144 }
145
146 static void setup(CURL *hnd, int num)
147 {
148   FILE *out;
149   char filename[128];
150
151   sprintf(filename, "dl-%d", num);
152
153   out = fopen(filename, "wb");
154
155   /* write to this file */
156   curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
157
158   /* set the same URL */
159   curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
160
161   /* send it verbose for max debuggaility */
162   curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
163   curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
164
165   /* HTTP/2 please */
166   curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
167
168   /* we use a self-signed test server, skip verification during debugging */
169   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
170   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
171
172 #if (CURLPIPE_MULTIPLEX > 0)
173   /* wait for pipe connection to confirm */
174   curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
175 #endif
176
177   curl_hnd[num] = hnd;
178 }
179
180 /*
181  * Simply download two files over HTTP/2, using the same physical connection!
182  */
183 int main(int argc, char **argv)
184 {
185   CURL *easy[NUM_HANDLES];
186   CURLM *multi_handle;
187   int i;
188   int still_running; /* keep number of running handles */
189
190   if(argc > 1)
191     /* if given a number, do that many transfers */
192     num_transfers = atoi(argv[1]);
193
194   if(!num_transfers || (num_transfers > NUM_HANDLES))
195     num_transfers = 3; /* a suitable low default */
196
197   /* init a multi stack */
198   multi_handle = curl_multi_init();
199
200   for(i=0; i<num_transfers; i++) {
201     easy[i] = curl_easy_init();
202     /* set options */
203     setup(easy[i], i);
204
205     /* add the individual transfer */
206     curl_multi_add_handle(multi_handle, easy[i]);
207   }
208
209   curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
210
211   /* we start some action by calling perform right away */
212   curl_multi_perform(multi_handle, &still_running);
213
214   do {
215     struct timeval timeout;
216     int rc; /* select() return code */
217     CURLMcode mc; /* curl_multi_fdset() return code */
218
219     fd_set fdread;
220     fd_set fdwrite;
221     fd_set fdexcep;
222     int maxfd = -1;
223
224     long curl_timeo = -1;
225
226     FD_ZERO(&fdread);
227     FD_ZERO(&fdwrite);
228     FD_ZERO(&fdexcep);
229
230     /* set a suitable timeout to play around with */
231     timeout.tv_sec = 1;
232     timeout.tv_usec = 0;
233
234     curl_multi_timeout(multi_handle, &curl_timeo);
235     if(curl_timeo >= 0) {
236       timeout.tv_sec = curl_timeo / 1000;
237       if(timeout.tv_sec > 1)
238         timeout.tv_sec = 1;
239       else
240         timeout.tv_usec = (curl_timeo % 1000) * 1000;
241     }
242
243     /* get file descriptors from the transfers */
244     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
245
246     if(mc != CURLM_OK)
247     {
248       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
249       break;
250     }
251
252     /* On success the value of maxfd is guaranteed to be >= -1. We call
253        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
254        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
255        to sleep 100ms, which is the minimum suggested value in the
256        curl_multi_fdset() doc. */
257
258     if(maxfd == -1) {
259 #ifdef _WIN32
260       Sleep(100);
261       rc = 0;
262 #else
263       /* Portable sleep for platforms other than Windows. */
264       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
265       rc = select(0, NULL, NULL, NULL, &wait);
266 #endif
267     }
268     else {
269       /* Note that on some platforms 'timeout' may be modified by select().
270          If you need access to the original value save a copy beforehand. */
271       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
272     }
273
274     switch(rc) {
275     case -1:
276       /* select error */
277       break;
278     case 0:
279     default:
280       /* timeout or readable/writable sockets */
281       curl_multi_perform(multi_handle, &still_running);
282       break;
283     }
284   } while(still_running);
285
286   curl_multi_cleanup(multi_handle);
287
288   for(i=0; i<num_transfers; i++)
289     curl_easy_cleanup(easy[i]);
290
291   return 0;
292 }