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