• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /***************************************************************************
2   *                                  _   _ ____  _
3   *  Project                     ___| | | |  _ \| |
4   *                             / __| | | | |_) | |
5   *                            | (__| |_| |  _ <| |___
6   *                             \___|\___/|_| \_\_____|
7   *
8   * Copyright (C) 1998 - 2016, 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.haxx.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  
32  /* somewhat unix-specific */
33  #include <sys/time.h>
34  #include <unistd.h>
35  
36  /* curl stuff */
37  #include <curl/curl.h>
38  
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
44  #endif
45  
46  #define NUM_HANDLES 1000
47  
48  void *curl_hnd[NUM_HANDLES];
49  int num_transfers;
50  
51  /* a handle to number lookup, highly ineffective when we do many
52     transfers... */
hnd2num(CURL * hnd)53  static int hnd2num(CURL *hnd)
54  {
55    int i;
56    for(i=0; i< num_transfers; i++) {
57      if(curl_hnd[i] == hnd)
58        return i;
59    }
60    return 0; /* weird, but just a fail-safe */
61  }
62  
63  static
dump(const char * text,int num,unsigned char * ptr,size_t size,char nohex)64  void dump(const char *text, int num, unsigned char *ptr, size_t size,
65            char nohex)
66  {
67    size_t i;
68    size_t c;
69    unsigned int width=0x10;
70  
71    if(nohex)
72      /* without the hex output, we can fit more on screen */
73      width = 0x40;
74  
75    fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n",
76            num, text, (long)size, (long)size);
77  
78    for(i=0; i<size; i+= width) {
79  
80      fprintf(stderr, "%4.4lx: ", (long)i);
81  
82      if(!nohex) {
83        /* hex not disabled, show it */
84        for(c = 0; c < width; c++)
85          if(i+c < size)
86            fprintf(stderr, "%02x ", ptr[i+c]);
87          else
88            fputs("   ", stderr);
89      }
90  
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) {
94          i+=(c+2-width);
95          break;
96        }
97        fprintf(stderr, "%c",
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) {
101          i+=(c+3-width);
102          break;
103        }
104      }
105      fputc('\n', stderr); /* newline */
106    }
107  }
108  
109  static
my_trace(CURL * handle,curl_infotype type,char * data,size_t size,void * userp)110  int my_trace(CURL *handle, curl_infotype type,
111               char *data, size_t size,
112               void *userp)
113  {
114    char timebuf[20];
115    const char *text;
116    int num = hnd2num(handle);
117    static time_t epoch_offset;
118    static int    known_offset;
119    struct timeval tv;
120    time_t secs;
121    struct tm *now;
122  
123    (void)handle; /* prevent compiler warning */
124    (void)userp;
125  
126    gettimeofday(&tv, NULL);
127    if(!known_offset) {
128      epoch_offset = time(NULL) - tv.tv_sec;
129      known_offset = 1;
130    }
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);
135  
136    switch (type) {
137    case CURLINFO_TEXT:
138      fprintf(stderr, "%s [%d] Info: %s", timebuf, num, data);
139    default: /* in case a new one is introduced to shock us */
140      return 0;
141  
142    case CURLINFO_HEADER_OUT:
143      text = "=> Send header";
144      break;
145    case CURLINFO_DATA_OUT:
146      text = "=> Send data";
147      break;
148    case CURLINFO_SSL_DATA_OUT:
149      text = "=> Send SSL data";
150      break;
151    case CURLINFO_HEADER_IN:
152      text = "<= Recv header";
153      break;
154    case CURLINFO_DATA_IN:
155      text = "<= Recv data";
156      break;
157    case CURLINFO_SSL_DATA_IN:
158      text = "<= Recv SSL data";
159      break;
160    }
161  
162    dump(text, num, (unsigned char *)data, size, 1);
163    return 0;
164  }
165  
166  struct input {
167    FILE *in;
168    size_t bytes_read; /* count up */
169    CURL *hnd;
170  };
171  
read_callback(void * ptr,size_t size,size_t nmemb,void * userp)172  static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
173  {
174    struct input *i = userp;
175    size_t retcode = fread(ptr, size, nmemb, i->in);
176    i->bytes_read += retcode;
177    return retcode;
178  }
179  
180  struct input indata[NUM_HANDLES];
181  
setup(CURL * hnd,int num,const char * upload)182  static void setup(CURL *hnd, int num, const char *upload)
183  {
184    FILE *out;
185    char url[256];
186    char filename[128];
187    struct stat file_info;
188    curl_off_t uploadsize;
189  
190    snprintf(filename, 128, "dl-%d", num);
191    out = fopen(filename, "wb");
192  
193    snprintf(url, 256, "https://localhost:8443/upload-%d", num);
194  
195    /* get the file size of the local file */
196    stat(upload, &file_info);
197    uploadsize = file_info.st_size;
198  
199    indata[num].in = fopen(upload, "rb");
200    indata[num].hnd = hnd;
201  
202    /* write to this file */
203    curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
204  
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);
211  
212    /* send in the URL to store the upload as */
213    curl_easy_setopt(hnd, CURLOPT_URL, url);
214  
215    /* upload please */
216    curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
217  
218    /* send it verbose for max debuggaility */
219    curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
220    curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
221  
222    /* HTTP/2 please */
223    curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
224  
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);
228  
229  #if (CURLPIPE_MULTIPLEX > 0)
230    /* wait for pipe connection to confirm */
231    curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
232  #endif
233  
234    curl_hnd[num] = hnd;
235  }
236  
237  /*
238   * Upload all files over HTTP/2, using the same physical connection!
239   */
main(int argc,char ** argv)240  int main(int argc, char **argv)
241  {
242    CURL *easy[NUM_HANDLES];
243    CURLM *multi_handle;
244    int i;
245    int still_running; /* keep number of running handles */
246    const char *filename = "index.html";
247  
248    if(argc > 1)
249      /* if given a number, do that many transfers */
250      num_transfers = atoi(argv[1]);
251  
252    if(argc > 2)
253      /* if given a file name, upload this! */
254      filename = argv[2];
255  
256    if(!num_transfers || (num_transfers > NUM_HANDLES))
257      num_transfers = 3; /* a suitable low default */
258  
259    /* init a multi stack */
260    multi_handle = curl_multi_init();
261  
262    for(i=0; i<num_transfers; i++) {
263      easy[i] = curl_easy_init();
264      /* set options */
265      setup(easy[i], i, filename);
266  
267      /* add the individual transfer */
268      curl_multi_add_handle(multi_handle, easy[i]);
269    }
270  
271    curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
272  
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);
275  
276    /* we start some action by calling perform right away */
277    curl_multi_perform(multi_handle, &still_running);
278  
279    do {
280      struct timeval timeout;
281      int rc; /* select() return code */
282      CURLMcode mc; /* curl_multi_fdset() return code */
283  
284      fd_set fdread;
285      fd_set fdwrite;
286      fd_set fdexcep;
287      int maxfd = -1;
288  
289      long curl_timeo = -1;
290  
291      FD_ZERO(&fdread);
292      FD_ZERO(&fdwrite);
293      FD_ZERO(&fdexcep);
294  
295      /* set a suitable timeout to play around with */
296      timeout.tv_sec = 1;
297      timeout.tv_usec = 0;
298  
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)
303          timeout.tv_sec = 1;
304        else
305          timeout.tv_usec = (curl_timeo % 1000) * 1000;
306      }
307  
308      /* get file descriptors from the transfers */
309      mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
310  
311      if(mc != CURLM_OK) {
312        fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
313        break;
314      }
315  
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. */
321  
322      if(maxfd == -1) {
323  #ifdef _WIN32
324        Sleep(100);
325        rc = 0;
326  #else
327        /* Portable sleep for platforms other than Windows. */
328        struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
329        rc = select(0, NULL, NULL, NULL, &wait);
330  #endif
331      }
332      else {
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);
336      }
337  
338      switch(rc) {
339      case -1:
340        /* select error */
341        break;
342      case 0:
343      default:
344        /* timeout or readable/writable sockets */
345        curl_multi_perform(multi_handle, &still_running);
346        break;
347      }
348    } while(still_running);
349  
350    curl_multi_cleanup(multi_handle);
351  
352    for(i=0; i<num_transfers; i++)
353      curl_easy_cleanup(easy[i]);
354  
355    return 0;
356  }
357