xref: /curl/docs/examples/http2-upload.c (revision fb711b50)
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  * Multiplexed HTTP/2 uploads over a single connection
26  * </DESC>
27  */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 
35 /* somewhat Unix-specific */
36 #ifndef _MSC_VER
37 #include <sys/time.h>
38 #include <unistd.h>
39 #endif
40 
41 /* curl stuff */
42 #include <curl/curl.h>
43 #include <curl/mprintf.h>
44 
45 #ifndef CURLPIPE_MULTIPLEX
46 /* This little trick makes sure that we do not enable pipelining for libcurls
47    old enough to not have this symbol. It is _not_ defined to zero in a recent
48    libcurl header. */
49 #define CURLPIPE_MULTIPLEX 0
50 #endif
51 
52 #define NUM_HANDLES 1000
53 
54 #ifdef _MSC_VER
55 #define gettimeofday(a, b) my_gettimeofday((a), (b))
56 static
my_gettimeofday(struct timeval * tp,void * tzp)57 int my_gettimeofday(struct timeval *tp, void *tzp)
58 {
59   (void)tzp;
60   if(tp) {
61     /* Offset between 1601-01-01 and 1970-01-01 in 100 nanosec units */
62     #define _WIN32_FT_OFFSET (116444736000000000)
63     union {
64       CURL_TYPEOF_CURL_OFF_T ns100; /* time since 1 Jan 1601 in 100ns units */
65       FILETIME ft;
66     } _now;
67     GetSystemTimeAsFileTime(&_now.ft);
68     tp->tv_usec = (long)((_now.ns100 / 10) % 1000000);
69     tp->tv_sec = (long)((_now.ns100 - _WIN32_FT_OFFSET) / 10000000);
70   }
71   return 0;
72 }
73 #endif
74 
75 struct input {
76   FILE *in;
77   size_t bytes_read; /* count up */
78   CURL *hnd;
79   int num;
80 };
81 
82 static
dump(const char * text,int num,unsigned char * ptr,size_t size,char nohex)83 void dump(const char *text, int num, unsigned char *ptr, size_t size,
84           char nohex)
85 {
86   size_t i;
87   size_t c;
88   unsigned int width = 0x10;
89 
90   if(nohex)
91     /* without the hex output, we can fit more on screen */
92     width = 0x40;
93 
94   fprintf(stderr, "%d %s, %lu bytes (0x%lx)\n",
95           num, text, (unsigned long)size, (unsigned long)size);
96 
97   for(i = 0; i < size; i += width) {
98 
99     fprintf(stderr, "%4.4lx: ", (unsigned long)i);
100 
101     if(!nohex) {
102       /* hex not disabled, show it */
103       for(c = 0; c < width; c++)
104         if(i + c < size)
105           fprintf(stderr, "%02x ", ptr[i + c]);
106         else
107           fputs("   ", stderr);
108     }
109 
110     for(c = 0; (c < width) && (i + c < size); c++) {
111       /* check for 0D0A; if found, skip past and start a new line of output */
112       if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
113          ptr[i + c + 1] == 0x0A) {
114         i += (c + 2 - width);
115         break;
116       }
117       fprintf(stderr, "%c",
118               (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
119       /* check again for 0D0A, to avoid an extra \n if it's at width */
120       if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
121          ptr[i + c + 2] == 0x0A) {
122         i += (c + 3 - width);
123         break;
124       }
125     }
126     fputc('\n', stderr); /* newline */
127   }
128 }
129 
130 static
my_trace(CURL * handle,curl_infotype type,char * data,size_t size,void * userp)131 int my_trace(CURL *handle, curl_infotype type,
132              char *data, size_t size,
133              void *userp)
134 {
135   char timebuf[60];
136   const char *text;
137   struct input *i = (struct input *)userp;
138   int num = i->num;
139   static time_t epoch_offset;
140   static int    known_offset;
141   struct timeval tv;
142   time_t secs;
143   struct tm *now;
144   (void)handle; /* prevent compiler warning */
145 
146   gettimeofday(&tv, NULL);
147   if(!known_offset) {
148     epoch_offset = time(NULL) - tv.tv_sec;
149     known_offset = 1;
150   }
151   secs = epoch_offset + tv.tv_sec;
152   now = localtime(&secs);  /* not thread safe but we do not care */
153   curl_msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
154                  now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
155 
156   switch(type) {
157   case CURLINFO_TEXT:
158     fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
159     return 0;
160   case CURLINFO_HEADER_OUT:
161     text = "=> Send header";
162     break;
163   case CURLINFO_DATA_OUT:
164     text = "=> Send data";
165     break;
166   case CURLINFO_SSL_DATA_OUT:
167     text = "=> Send SSL data";
168     break;
169   case CURLINFO_HEADER_IN:
170     text = "<= Recv header";
171     break;
172   case CURLINFO_DATA_IN:
173     text = "<= Recv data";
174     break;
175   case CURLINFO_SSL_DATA_IN:
176     text = "<= Recv SSL data";
177     break;
178   default: /* in case a new one is introduced to shock us */
179     return 0;
180   }
181 
182   dump(text, num, (unsigned char *)data, size, 1);
183   return 0;
184 }
185 
read_callback(char * ptr,size_t size,size_t nmemb,void * userp)186 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
187 {
188   struct input *i = userp;
189   size_t retcode = fread(ptr, size, nmemb, i->in);
190   i->bytes_read += retcode;
191   return retcode;
192 }
193 
setup(struct input * i,int num,const char * upload)194 static void setup(struct input *i, int num, const char *upload)
195 {
196   FILE *out;
197   char url[256];
198   char filename[128];
199   struct stat file_info;
200   curl_off_t uploadsize;
201   CURL *hnd;
202 
203   hnd = i->hnd = curl_easy_init();
204   i->num = num;
205   curl_msnprintf(filename, 128, "dl-%d", num);
206   out = fopen(filename, "wb");
207   if(!out) {
208     fprintf(stderr, "error: could not open file %s for writing: %s\n", upload,
209             strerror(errno));
210     exit(1);
211   }
212 
213   curl_msnprintf(url, 256, "https://localhost:8443/upload-%d", num);
214 
215   /* get the file size of the local file */
216   if(stat(upload, &file_info)) {
217     fprintf(stderr, "error: could not stat file %s: %s\n", upload,
218             strerror(errno));
219     exit(1);
220   }
221 
222   uploadsize = file_info.st_size;
223 
224   i->in = fopen(upload, "rb");
225   if(!i->in) {
226     fprintf(stderr, "error: could not open file %s for reading: %s\n", upload,
227             strerror(errno));
228     exit(1);
229   }
230 
231   /* write to this file */
232   curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
233 
234   /* we want to use our own read function */
235   curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
236   /* read from this file */
237   curl_easy_setopt(hnd, CURLOPT_READDATA, i);
238   /* provide the size of the upload */
239   curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
240 
241   /* send in the URL to store the upload as */
242   curl_easy_setopt(hnd, CURLOPT_URL, url);
243 
244   /* upload please */
245   curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
246 
247   /* please be verbose */
248   curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
249   curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
250   curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, i);
251 
252   /* HTTP/2 please */
253   curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
254 
255   /* we use a self-signed test server, skip verification during debugging */
256   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
257   curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
258 
259 #if (CURLPIPE_MULTIPLEX > 0)
260   /* wait for pipe connection to confirm */
261   curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
262 #endif
263 }
264 
265 /*
266  * Upload all files over HTTP/2, using the same physical connection!
267  */
main(int argc,char ** argv)268 int main(int argc, char **argv)
269 {
270   struct input trans[NUM_HANDLES];
271   CURLM *multi_handle;
272   int i;
273   int still_running = 0; /* keep number of running handles */
274   const char *filename = "index.html";
275   int num_transfers;
276 
277   if(argc > 1) {
278     /* if given a number, do that many transfers */
279     num_transfers = atoi(argv[1]);
280 
281     if(!num_transfers || (num_transfers > NUM_HANDLES))
282       num_transfers = 3; /* a suitable low default */
283 
284     if(argc > 2)
285       /* if given a file name, upload this! */
286       filename = argv[2];
287   }
288   else
289     num_transfers = 3;
290 
291   /* init a multi stack */
292   multi_handle = curl_multi_init();
293 
294   for(i = 0; i < num_transfers; i++) {
295     setup(&trans[i], i, filename);
296 
297     /* add the individual transfer */
298     curl_multi_add_handle(multi_handle, trans[i].hnd);
299   }
300 
301   curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
302 
303   /* We do HTTP/2 so let's stick to one connection per host */
304   curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
305 
306   do {
307     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
308 
309     if(still_running)
310       /* wait for activity, timeout or "nothing" */
311       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
312 
313     if(mc)
314       break;
315 
316   } while(still_running);
317 
318   curl_multi_cleanup(multi_handle);
319 
320   for(i = 0; i < num_transfers; i++) {
321     curl_multi_remove_handle(multi_handle, trans[i].hnd);
322     curl_easy_cleanup(trans[i].hnd);
323   }
324 
325   return 0;
326 }
327