1e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/***************************************************************************
2e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                                  _   _ ____  _
3e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *  Project                     ___| | | |  _ \| |
4e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                             / __| | | | |_) | |
5e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                            | (__| |_| |  _ <| |___
6e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                             \___|\___/|_| \_\_____|
7e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
8d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
10e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is licensed as described in the file COPYING, which
11e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * you should have received as part of this distribution. The terms
12d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * are also available at https://curl.haxx.se/docs/copyright.html.
13e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
14e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * copies of the Software, and permit persons to whom the Software is
16e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * furnished to do so, under the terms of the COPYING file.
17e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
18e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * KIND, either express or implied.
20e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
21e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET ***************************************************************************/
22d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo/* <DESC>
23d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * Multiplexed HTTP/2 uploads over a single connection
24d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * </DESC>
25d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo */
26e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <stdio.h>
27e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <stdlib.h>
28d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo#include <string.h>
29e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <fcntl.h>
30e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <sys/stat.h>
31e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
32e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/* somewhat unix-specific */
33e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <sys/time.h>
34e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <unistd.h>
35e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
36e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/* curl stuff */
37e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <curl/curl.h>
38e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
39e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#ifndef CURLPIPE_MULTIPLEX
40e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/* This little trick will just make sure that we don't enable pipelining for
41e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   libcurls old enough to not have this symbol. It is _not_ defined to zero in
42e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   a recent libcurl header. */
43e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#define CURLPIPE_MULTIPLEX 0
44e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#endif
45e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
46e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#define NUM_HANDLES 1000
47e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
48e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETvoid *curl_hnd[NUM_HANDLES];
49e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETint num_transfers;
50e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
51e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/* a handle to number lookup, highly ineffective when we do many
52e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   transfers... */
53e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstatic int hnd2num(CURL *hnd)
54e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
55e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  int i;
56e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  for(i=0; i< num_transfers; i++) {
57e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    if(curl_hnd[i] == hnd)
58e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      return i;
59e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  }
60e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  return 0; /* weird, but just a fail-safe */
61e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
62e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
63e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstatic
64e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETvoid dump(const char *text, int num, unsigned char *ptr, size_t size,
65e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET          char nohex)
66e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
67e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  size_t i;
68e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  size_t c;
69e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  unsigned int width=0x10;
70e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
71e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  if(nohex)
72e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* without the hex output, we can fit more on screen */
73e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    width = 0x40;
74e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
75e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
76e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET          num, text, (long)size, (long)size);
77e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
78e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  for(i=0; i<size; i+= width) {
79e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
80e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fprintf(stderr, "%4.4lx: ", (long)i);
81e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
82e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    if(!nohex) {
83e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      /* hex not disabled, show it */
84e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      for(c = 0; c < width; c++)
85e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        if(i+c < size)
86e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET          fprintf(stderr, "%02x ", ptr[i+c]);
87e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        else
88e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET          fputs("   ", stderr);
89e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
90e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
91e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    for(c = 0; (c < width) && (i+c < size); c++) {
92e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      /* check for 0D0A; if found, skip past and start a new line of output */
93d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo      if(nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
94e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        i+=(c+2-width);
95e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        break;
96e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      }
97e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      fprintf(stderr, "%c",
98e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET              (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
99e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      /* check again for 0D0A, to avoid an extra \n if it's at width */
100d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo      if(nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
101e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        i+=(c+3-width);
102e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        break;
103e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      }
104e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
105e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fputc('\n', stderr); /* newline */
106e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  }
107e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
108e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
109e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstatic
110e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETint my_trace(CURL *handle, curl_infotype type,
111e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET             char *data, size_t size,
112e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET             void *userp)
113e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
114e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  char timebuf[20];
115e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  const char *text;
116e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  int num = hnd2num(handle);
117e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  static time_t epoch_offset;
118e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  static int    known_offset;
119e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  struct timeval tv;
120e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  time_t secs;
121e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  struct tm *now;
122e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
123e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  (void)handle; /* prevent compiler warning */
124e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  (void)userp;
125e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
126e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  gettimeofday(&tv, NULL);
127e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  if(!known_offset) {
128e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    epoch_offset = time(NULL) - tv.tv_sec;
129e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    known_offset = 1;
130e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  }
131e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  secs = epoch_offset + tv.tv_sec;
132e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  now = localtime(&secs);  /* not thread safe but we don't care */
133e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
134e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET           now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
135e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
136e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  switch (type) {
137e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  case CURLINFO_TEXT:
138e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
139e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  default: /* in case a new one is introduced to shock us */
140e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    return 0;
141e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
142e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  case CURLINFO_HEADER_OUT:
143e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    text = "=> Send header";
144e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    break;
145e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  case CURLINFO_DATA_OUT:
146e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    text = "=> Send data";
147e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    break;
148e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  case CURLINFO_SSL_DATA_OUT:
149e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    text = "=> Send SSL data";
150e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    break;
151e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  case CURLINFO_HEADER_IN:
152e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    text = "<= Recv header";
153e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    break;
154e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  case CURLINFO_DATA_IN:
155e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    text = "<= Recv data";
156e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    break;
157e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  case CURLINFO_SSL_DATA_IN:
158e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    text = "<= Recv SSL data";
159e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    break;
160e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  }
161e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
162e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  dump(text, num, (unsigned char *)data, size, 1);
163e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  return 0;
164e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
165e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
166e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstruct input {
167e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  FILE *in;
168e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  size_t bytes_read; /* count up */
169e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  CURL *hnd;
170e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET};
171e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
172e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstatic size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
173e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
174e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  struct input *i = userp;
175e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  size_t retcode = fread(ptr, size, nmemb, i->in);
176e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  i->bytes_read += retcode;
177e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  return retcode;
178e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
179e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
180e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstruct input indata[NUM_HANDLES];
181e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
182e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstatic void setup(CURL *hnd, int num, const char *upload)
183e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
184e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  FILE *out;
185e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  char url[256];
186e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  char filename[128];
187e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  struct stat file_info;
188e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_off_t uploadsize;
189e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
190d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo  snprintf(filename, 128, "dl-%d", num);
191e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  out = fopen(filename, "wb");
192e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
193d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo  snprintf(url, 256, "https://localhost:8443/upload-%d", num);
194e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
195e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* get the file size of the local file */
196e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  stat(upload, &file_info);
197e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  uploadsize = file_info.st_size;
198e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
199e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  indata[num].in = fopen(upload, "rb");
200e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  indata[num].hnd = hnd;
201e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
202e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* write to this file */
203e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
204e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
205e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* we want to use our own read function */
206e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback);
207e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* read from this file */
208e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_READDATA, &indata[num]);
209e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* provide the size of the upload */
210e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize);
211e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
212e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* send in the URL to store the upload as */
213e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_URL, url);
214e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
215e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* upload please */
216e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
217e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
218e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* send it verbose for max debuggaility */
219e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
220e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
221e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
222e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* HTTP/2 please */
223e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
224e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
225e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* we use a self-signed test server, skip verification during debugging */
226e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
227e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
228e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
229e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#if (CURLPIPE_MULTIPLEX > 0)
230e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* wait for pipe connection to confirm */
231e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
232e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#endif
233e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
234e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_hnd[num] = hnd;
235e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
236e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
237e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/*
238e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * Upload all files over HTTP/2, using the same physical connection!
239e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET */
240e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETint main(int argc, char **argv)
241e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
242e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  CURL *easy[NUM_HANDLES];
243e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  CURLM *multi_handle;
244e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  int i;
245e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  int still_running; /* keep number of running handles */
246e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  const char *filename = "index.html";
247e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
248e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  if(argc > 1)
249e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* if given a number, do that many transfers */
250e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    num_transfers = atoi(argv[1]);
251e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
252e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  if(argc > 2)
253e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* if given a file name, upload this! */
254e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    filename = argv[2];
255e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
256e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  if(!num_transfers || (num_transfers > NUM_HANDLES))
257e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    num_transfers = 3; /* a suitable low default */
258e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
259e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* init a multi stack */
260e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  multi_handle = curl_multi_init();
261e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
262e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  for(i=0; i<num_transfers; i++) {
263e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    easy[i] = curl_easy_init();
264e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* set options */
265e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    setup(easy[i], i, filename);
266e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
267e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* add the individual transfer */
268e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    curl_multi_add_handle(multi_handle, easy[i]);
269e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  }
270e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
271e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
272e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
273e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* We do HTTP/2 so let's stick to one connection per host */
274e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L);
275e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
276e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* we start some action by calling perform right away */
277e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_multi_perform(multi_handle, &still_running);
278e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
279e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  do {
280e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    struct timeval timeout;
281e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    int rc; /* select() return code */
282e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    CURLMcode mc; /* curl_multi_fdset() return code */
283e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
284e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fd_set fdread;
285e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fd_set fdwrite;
286e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fd_set fdexcep;
287e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    int maxfd = -1;
288e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
289e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    long curl_timeo = -1;
290e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
291e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    FD_ZERO(&fdread);
292e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    FD_ZERO(&fdwrite);
293e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    FD_ZERO(&fdexcep);
294e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
295e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* set a suitable timeout to play around with */
296e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    timeout.tv_sec = 1;
297e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    timeout.tv_usec = 0;
298e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
299e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    curl_multi_timeout(multi_handle, &curl_timeo);
300e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    if(curl_timeo >= 0) {
301e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      timeout.tv_sec = curl_timeo / 1000;
302e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      if(timeout.tv_sec > 1)
303e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        timeout.tv_sec = 1;
304e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      else
305e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET        timeout.tv_usec = (curl_timeo % 1000) * 1000;
306e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
307e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
308e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* get file descriptors from the transfers */
309e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
310e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
311d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo    if(mc != CURLM_OK) {
312e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
313e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      break;
314e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
315e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
316e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* On success the value of maxfd is guaranteed to be >= -1. We call
317e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET       select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
318e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET       no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
319e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET       to sleep 100ms, which is the minimum suggested value in the
320e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET       curl_multi_fdset() doc. */
321e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
322e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    if(maxfd == -1) {
323e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#ifdef _WIN32
324e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      Sleep(100);
325e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      rc = 0;
326e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#else
327e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      /* Portable sleep for platforms other than Windows. */
328e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
329e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      rc = select(0, NULL, NULL, NULL, &wait);
330e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#endif
331e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
332e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    else {
333e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      /* Note that on some platforms 'timeout' may be modified by select().
334e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET         If you need access to the original value save a copy beforehand. */
335e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
336e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
337e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
338e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    switch(rc) {
339e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    case -1:
340e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      /* select error */
341e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      break;
342e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    case 0:
343e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    default:
344e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      /* timeout or readable/writable sockets */
345e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      curl_multi_perform(multi_handle, &still_running);
346e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      break;
347e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
348e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  } while(still_running);
349e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
350e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_multi_cleanup(multi_handle);
351e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
352e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  for(i=0; i<num_transfers; i++)
353e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    curl_easy_cleanup(easy[i]);
354e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
355e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  return 0;
356e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
357