1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 http://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
31 /* somewhat unix-specific */
36 #include <curl/curl.h>
38 #ifndef CURLPIPE_MULTIPLEX
39 /* This little trick will just make sure that we don't enable pipelining for
40 libcurls old enough to not have this symbol. It is _not_ defined to zero in
41 a recent libcurl header. */
42 #define CURLPIPE_MULTIPLEX 0
45 #define NUM_HANDLES 1000
47 void *curl_hnd[NUM_HANDLES];
50 /* a handle to number lookup, highly ineffective when we do many
52 static int hnd2num(CURL *hnd)
55 for(i=0; i< num_transfers; i++) {
56 if(curl_hnd[i] == hnd)
59 return 0; /* weird, but just a fail-safe */
63 void dump(const char *text, int num, unsigned char *ptr, size_t size,
68 unsigned int width=0x10;
71 /* without the hex output, we can fit more on screen */
74 fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
75 num, text, (long)size, (long)size);
77 for(i=0; i<size; i+= width) {
79 fprintf(stderr, "%4.4lx: ", (long)i);
82 /* hex not disabled, show it */
83 for(c = 0; c < width; c++)
85 fprintf(stderr, "%02x ", ptr[i+c]);
90 for(c = 0; (c < width) && (i+c < size); c++) {
91 /* check for 0D0A; if found, skip past and start a new line of output */
92 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
97 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
98 /* check again for 0D0A, to avoid an extra \n if it's at width */
99 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
104 fputc('\n', stderr); /* newline */
109 int my_trace(CURL *handle, curl_infotype type,
110 char *data, size_t size,
115 int num = hnd2num(handle);
116 static time_t epoch_offset;
117 static int known_offset;
122 (void)handle; /* prevent compiler warning */
125 gettimeofday(&tv, NULL);
127 epoch_offset = time(NULL) - tv.tv_sec;
130 secs = epoch_offset + tv.tv_sec;
131 now = localtime(&secs); /* not thread safe but we don't care */
132 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
133 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
137 fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
138 default: /* in case a new one is introduced to shock us */
141 case CURLINFO_HEADER_OUT:
142 text = "=> Send header";
144 case CURLINFO_DATA_OUT:
145 text = "=> Send data";
147 case CURLINFO_SSL_DATA_OUT:
148 text = "=> Send SSL data";
150 case CURLINFO_HEADER_IN:
151 text = "<= Recv header";
153 case CURLINFO_DATA_IN:
154 text = "<= Recv data";
156 case CURLINFO_SSL_DATA_IN:
157 text = "<= Recv SSL data";
161 dump(text, num, (unsigned char *)data, size, 1);
167 size_t bytes_read; /* count up */
171 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
173 struct input *i = userp;
174 size_t retcode = fread(ptr, size, nmemb, i->in);
175 i->bytes_read += retcode;
179 struct input indata[NUM_HANDLES];
181 static void setup(CURL *hnd, int num, const char *upload)
186 struct stat file_info;
187 curl_off_t uploadsize;
189 sprintf(filename, "dl-%d", num);
190 out = fopen(filename, "wb");
192 sprintf(url, "https://localhost:8443/upload-%d", num);
194 /* get the file size of the local file */
195 stat(upload, &file_info);
196 uploadsize = file_info.st_size;
198 indata[num].in = fopen(upload, "rb");
199 indata[num].hnd = hnd;
201 /* write to this file */
202 curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
204 /* we want to use our own read function */
205 curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
206 /* read from this file */
207 curl_easy_setopt(hnd, CURLOPT_READDATA, &indata[num]);
208 /* provide the size of the upload */
209 curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
211 /* send in the URL to store the upload as */
212 curl_easy_setopt(hnd, CURLOPT_URL, url);
215 curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
217 /* send it verbose for max debuggaility */
218 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
219 curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
222 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
224 /* we use a self-signed test server, skip verification during debugging */
225 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
226 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
228 #if (CURLPIPE_MULTIPLEX > 0)
229 /* wait for pipe connection to confirm */
230 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
237 * Upload all files over HTTP/2, using the same physical connection!
239 int main(int argc, char **argv)
241 CURL *easy[NUM_HANDLES];
244 int still_running; /* keep number of running handles */
245 const char *filename = "index.html";
248 /* if given a number, do that many transfers */
249 num_transfers = atoi(argv[1]);
252 /* if given a file name, upload this! */
255 if(!num_transfers || (num_transfers > NUM_HANDLES))
256 num_transfers = 3; /* a suitable low default */
258 /* init a multi stack */
259 multi_handle = curl_multi_init();
261 for(i=0; i<num_transfers; i++) {
262 easy[i] = curl_easy_init();
264 setup(easy[i], i, filename);
266 /* add the individual transfer */
267 curl_multi_add_handle(multi_handle, easy[i]);
270 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
272 /* We do HTTP/2 so let's stick to one connection per host */
273 curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
275 /* we start some action by calling perform right away */
276 curl_multi_perform(multi_handle, &still_running);
279 struct timeval timeout;
280 int rc; /* select() return code */
281 CURLMcode mc; /* curl_multi_fdset() return code */
288 long curl_timeo = -1;
294 /* set a suitable timeout to play around with */
298 curl_multi_timeout(multi_handle, &curl_timeo);
299 if(curl_timeo >= 0) {
300 timeout.tv_sec = curl_timeo / 1000;
301 if(timeout.tv_sec > 1)
304 timeout.tv_usec = (curl_timeo % 1000) * 1000;
307 /* get file descriptors from the transfers */
308 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]);