1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Multiplexed HTTP/2 uploads over a single connection
32 /* somewhat unix-specific */
37 #include <curl/curl.h>
39 #ifndef CURLPIPE_MULTIPLEX
40 /* This little trick will just make sure that we don't enable pipelining for
41 libcurls old enough to not have this symbol. It is _not_ defined to zero in
42 a recent libcurl header. */
43 #define CURLPIPE_MULTIPLEX 0
46 #define NUM_HANDLES 1000
48 void *curl_hnd[NUM_HANDLES];
51 /* a handle to number lookup, highly ineffective when we do many
53 static int hnd2num(CURL *hnd)
56 for(i=0; i< num_transfers; i++) {
57 if(curl_hnd[i] == hnd)
60 return 0; /* weird, but just a fail-safe */
64 void dump(const char *text, int num, unsigned char *ptr, size_t size,
69 unsigned int width=0x10;
72 /* without the hex output, we can fit more on screen */
75 fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
76 num, text, (long)size, (long)size);
78 for(i=0; i<size; i+= width) {
80 fprintf(stderr, "%4.4lx: ", (long)i);
83 /* hex not disabled, show it */
84 for(c = 0; c < width; c++)
86 fprintf(stderr, "%02x ", ptr[i+c]);
91 for(c = 0; (c < width) && (i+c < size); c++) {
92 /* check for 0D0A; if found, skip past and start a new line of output */
93 if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
98 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
99 /* check again for 0D0A, to avoid an extra \n if it's at width */
100 if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
105 fputc('\n', stderr); /* newline */
110 int my_trace(CURL *handle, curl_infotype type,
111 char *data, size_t size,
116 int num = hnd2num(handle);
117 static time_t epoch_offset;
118 static int known_offset;
123 (void)handle; /* prevent compiler warning */
126 gettimeofday(&tv, NULL);
128 epoch_offset = time(NULL) - tv.tv_sec;
131 secs = epoch_offset + tv.tv_sec;
132 now = localtime(&secs); /* not thread safe but we don't care */
133 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
134 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
138 fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
139 default: /* in case a new one is introduced to shock us */
142 case CURLINFO_HEADER_OUT:
143 text = "=> Send header";
145 case CURLINFO_DATA_OUT:
146 text = "=> Send data";
148 case CURLINFO_SSL_DATA_OUT:
149 text = "=> Send SSL data";
151 case CURLINFO_HEADER_IN:
152 text = "<= Recv header";
154 case CURLINFO_DATA_IN:
155 text = "<= Recv data";
157 case CURLINFO_SSL_DATA_IN:
158 text = "<= Recv SSL data";
162 dump(text, num, (unsigned char *)data, size, 1);
168 size_t bytes_read; /* count up */
172 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
174 struct input *i = userp;
175 size_t retcode = fread(ptr, size, nmemb, i->in);
176 i->bytes_read += retcode;
180 struct input indata[NUM_HANDLES];
182 static void setup(CURL *hnd, int num, const char *upload)
187 struct stat file_info;
188 curl_off_t uploadsize;
190 snprintf(filename, 128, "dl-%d", num);
191 out = fopen(filename, "wb");
193 snprintf(url, 256, "https://localhost:8443/upload-%d", num);
195 /* get the file size of the local file */
196 stat(upload, &file_info);
197 uploadsize = file_info.st_size;
199 indata[num].in = fopen(upload, "rb");
200 indata[num].hnd = hnd;
202 /* write to this file */
203 curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
205 /* we want to use our own read function */
206 curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
207 /* read from this file */
208 curl_easy_setopt(hnd, CURLOPT_READDATA, &indata[num]);
209 /* provide the size of the upload */
210 curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
212 /* send in the URL to store the upload as */
213 curl_easy_setopt(hnd, CURLOPT_URL, url);
216 curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
218 /* send it verbose for max debuggaility */
219 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
220 curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
223 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
225 /* we use a self-signed test server, skip verification during debugging */
226 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
227 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
229 #if (CURLPIPE_MULTIPLEX > 0)
230 /* wait for pipe connection to confirm */
231 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
238 * Upload all files over HTTP/2, using the same physical connection!
240 int main(int argc, char **argv)
242 CURL *easy[NUM_HANDLES];
245 int still_running; /* keep number of running handles */
246 const char *filename = "index.html";
249 /* if given a number, do that many transfers */
250 num_transfers = atoi(argv[1]);
253 /* if given a file name, upload this! */
256 if(!num_transfers || (num_transfers > NUM_HANDLES))
257 num_transfers = 3; /* a suitable low default */
259 /* init a multi stack */
260 multi_handle = curl_multi_init();
262 for(i=0; i<num_transfers; i++) {
263 easy[i] = curl_easy_init();
265 setup(easy[i], i, filename);
267 /* add the individual transfer */
268 curl_multi_add_handle(multi_handle, easy[i]);
271 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
273 /* We do HTTP/2 so let's stick to one connection per host */
274 curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
276 /* we start some action by calling perform right away */
277 curl_multi_perform(multi_handle, &still_running);
280 struct timeval timeout;
281 int rc; /* select() return code */
282 CURLMcode mc; /* curl_multi_fdset() return code */
289 long curl_timeo = -1;
295 /* set a suitable timeout to play around with */
299 curl_multi_timeout(multi_handle, &curl_timeo);
300 if(curl_timeo >= 0) {
301 timeout.tv_sec = curl_timeo / 1000;
302 if(timeout.tv_sec > 1)
305 timeout.tv_usec = (curl_timeo % 1000) * 1000;
308 /* get file descriptors from the transfers */
309 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
312 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
316 /* On success the value of maxfd is guaranteed to be >= -1. We call
317 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
318 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
319 to sleep 100ms, which is the minimum suggested value in the
320 curl_multi_fdset() doc. */
327 /* Portable sleep for platforms other than Windows. */
328 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
329 rc = select(0, NULL, NULL, NULL, &wait);
333 /* Note that on some platforms 'timeout' may be modified by select().
334 If you need access to the original value save a copy beforehand. */
335 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
344 /* timeout or readable/writable sockets */
345 curl_multi_perform(multi_handle, &still_running);
348 } while(still_running);
350 curl_multi_cleanup(multi_handle);
352 for(i=0; i<num_transfers; i++)
353 curl_easy_cleanup(easy[i]);