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