lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2017, 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 | static void *curl_hnd[NUM_HANDLES]; |
| 49 | static int num_transfers; |
| 50 | |
| 51 | /* a handle to number lookup, highly ineffective when we do many |
| 52 | transfers... */ |
| 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 |
| 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 |
| 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 | /* FALLTHROUGH */ |
| 140 | default: /* in case a new one is introduced to shock us */ |
| 141 | return 0; |
| 142 | |
| 143 | case CURLINFO_HEADER_OUT: |
| 144 | text = "=> Send header"; |
| 145 | break; |
| 146 | case CURLINFO_DATA_OUT: |
| 147 | text = "=> Send data"; |
| 148 | break; |
| 149 | case CURLINFO_SSL_DATA_OUT: |
| 150 | text = "=> Send SSL data"; |
| 151 | break; |
| 152 | case CURLINFO_HEADER_IN: |
| 153 | text = "<= Recv header"; |
| 154 | break; |
| 155 | case CURLINFO_DATA_IN: |
| 156 | text = "<= Recv data"; |
| 157 | break; |
| 158 | case CURLINFO_SSL_DATA_IN: |
| 159 | text = "<= Recv SSL data"; |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | dump(text, num, (unsigned char *)data, size, 1); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | struct input { |
| 168 | FILE *in; |
| 169 | size_t bytes_read; /* count up */ |
| 170 | CURL *hnd; |
| 171 | }; |
| 172 | |
| 173 | static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp) |
| 174 | { |
| 175 | struct input *i = userp; |
| 176 | size_t retcode = fread(ptr, size, nmemb, i->in); |
| 177 | i->bytes_read += retcode; |
| 178 | return retcode; |
| 179 | } |
| 180 | |
| 181 | static struct input indata[NUM_HANDLES]; |
| 182 | |
| 183 | static void setup(CURL *hnd, int num, const char *upload) |
| 184 | { |
| 185 | FILE *out; |
| 186 | char url[256]; |
| 187 | char filename[128]; |
| 188 | struct stat file_info; |
| 189 | curl_off_t uploadsize; |
| 190 | |
| 191 | snprintf(filename, 128, "dl-%d", num); |
| 192 | out = fopen(filename, "wb"); |
| 193 | |
| 194 | snprintf(url, 256, "https://localhost:8443/upload-%d", num); |
| 195 | |
| 196 | /* get the file size of the local file */ |
| 197 | stat(upload, &file_info); |
| 198 | uploadsize = file_info.st_size; |
| 199 | |
| 200 | indata[num].in = fopen(upload, "rb"); |
| 201 | indata[num].hnd = hnd; |
| 202 | |
| 203 | /* write to this file */ |
| 204 | curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out); |
| 205 | |
| 206 | /* we want to use our own read function */ |
| 207 | curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback); |
| 208 | /* read from this file */ |
| 209 | curl_easy_setopt(hnd, CURLOPT_READDATA, &indata[num]); |
| 210 | /* provide the size of the upload */ |
| 211 | curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize); |
| 212 | |
| 213 | /* send in the URL to store the upload as */ |
| 214 | curl_easy_setopt(hnd, CURLOPT_URL, url); |
| 215 | |
| 216 | /* upload please */ |
| 217 | curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L); |
| 218 | |
| 219 | /* please be verbose */ |
| 220 | curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L); |
| 221 | curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace); |
| 222 | |
| 223 | /* HTTP/2 please */ |
| 224 | curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
| 225 | |
| 226 | /* we use a self-signed test server, skip verification during debugging */ |
| 227 | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L); |
| 228 | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L); |
| 229 | |
| 230 | #if (CURLPIPE_MULTIPLEX > 0) |
| 231 | /* wait for pipe connection to confirm */ |
| 232 | curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L); |
| 233 | #endif |
| 234 | |
| 235 | curl_hnd[num] = hnd; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Upload all files over HTTP/2, using the same physical connection! |
| 240 | */ |
| 241 | int main(int argc, char **argv) |
| 242 | { |
| 243 | CURL *easy[NUM_HANDLES]; |
| 244 | CURLM *multi_handle; |
| 245 | int i; |
| 246 | int still_running; /* keep number of running handles */ |
| 247 | const char *filename = "index.html"; |
| 248 | |
| 249 | if(argc > 1) |
| 250 | /* if given a number, do that many transfers */ |
| 251 | num_transfers = atoi(argv[1]); |
| 252 | |
| 253 | if(argc > 2) |
| 254 | /* if given a file name, upload this! */ |
| 255 | filename = argv[2]; |
| 256 | |
| 257 | if(!num_transfers || (num_transfers > NUM_HANDLES)) |
| 258 | num_transfers = 3; /* a suitable low default */ |
| 259 | |
| 260 | /* init a multi stack */ |
| 261 | multi_handle = curl_multi_init(); |
| 262 | |
| 263 | for(i=0; i<num_transfers; i++) { |
| 264 | easy[i] = curl_easy_init(); |
| 265 | /* set options */ |
| 266 | setup(easy[i], i, filename); |
| 267 | |
| 268 | /* add the individual transfer */ |
| 269 | curl_multi_add_handle(multi_handle, easy[i]); |
| 270 | } |
| 271 | |
| 272 | curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
| 273 | |
| 274 | /* We do HTTP/2 so let's stick to one connection per host */ |
| 275 | curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L); |
| 276 | |
| 277 | /* we start some action by calling perform right away */ |
| 278 | curl_multi_perform(multi_handle, &still_running); |
| 279 | |
| 280 | do { |
| 281 | struct timeval timeout; |
| 282 | int rc; /* select() return code */ |
| 283 | CURLMcode mc; /* curl_multi_fdset() return code */ |
| 284 | |
| 285 | fd_set fdread; |
| 286 | fd_set fdwrite; |
| 287 | fd_set fdexcep; |
| 288 | int maxfd = -1; |
| 289 | |
| 290 | long curl_timeo = -1; |
| 291 | |
| 292 | FD_ZERO(&fdread); |
| 293 | FD_ZERO(&fdwrite); |
| 294 | FD_ZERO(&fdexcep); |
| 295 | |
| 296 | /* set a suitable timeout to play around with */ |
| 297 | timeout.tv_sec = 1; |
| 298 | timeout.tv_usec = 0; |
| 299 | |
| 300 | curl_multi_timeout(multi_handle, &curl_timeo); |
| 301 | if(curl_timeo >= 0) { |
| 302 | timeout.tv_sec = curl_timeo / 1000; |
| 303 | if(timeout.tv_sec > 1) |
| 304 | timeout.tv_sec = 1; |
| 305 | else |
| 306 | timeout.tv_usec = (curl_timeo % 1000) * 1000; |
| 307 | } |
| 308 | |
| 309 | /* get file descriptors from the transfers */ |
| 310 | mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); |
| 311 | |
| 312 | if(mc != CURLM_OK) { |
| 313 | fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | /* On success the value of maxfd is guaranteed to be >= -1. We call |
| 318 | select(maxfd + 1, ...); specially in case of (maxfd == -1) there are |
| 319 | no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- |
| 320 | to sleep 100ms, which is the minimum suggested value in the |
| 321 | curl_multi_fdset() doc. */ |
| 322 | |
| 323 | if(maxfd == -1) { |
| 324 | #ifdef _WIN32 |
| 325 | Sleep(100); |
| 326 | rc = 0; |
| 327 | #else |
| 328 | /* Portable sleep for platforms other than Windows. */ |
| 329 | struct timeval wait = { 0, 100 * 1000 }; /* 100ms */ |
| 330 | rc = select(0, NULL, NULL, NULL, &wait); |
| 331 | #endif |
| 332 | } |
| 333 | else { |
| 334 | /* Note that on some platforms 'timeout' may be modified by select(). |
| 335 | If you need access to the original value save a copy beforehand. */ |
| 336 | rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); |
| 337 | } |
| 338 | |
| 339 | switch(rc) { |
| 340 | case -1: |
| 341 | /* select error */ |
| 342 | break; |
| 343 | case 0: |
| 344 | default: |
| 345 | /* timeout or readable/writable sockets */ |
| 346 | curl_multi_perform(multi_handle, &still_running); |
| 347 | break; |
| 348 | } |
| 349 | } while(still_running); |
| 350 | |
| 351 | curl_multi_cleanup(multi_handle); |
| 352 | |
| 353 | for(i=0; i<num_transfers; i++) |
| 354 | curl_easy_cleanup(easy[i]); |
| 355 | |
| 356 | return 0; |
| 357 | } |