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 downloads over a single connection |
| 24 | * </DESC> |
| 25 | */ |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | /* somewhat unix-specific */ |
| 31 | #include <sys/time.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | /* curl stuff */ |
| 35 | #include <curl/curl.h> |
| 36 | |
| 37 | #ifndef CURLPIPE_MULTIPLEX |
| 38 | /* This little trick will just make sure that we don't enable pipelining for |
| 39 | libcurls old enough to not have this symbol. It is _not_ defined to zero in |
| 40 | a recent libcurl header. */ |
| 41 | #define CURLPIPE_MULTIPLEX 0 |
| 42 | #endif |
| 43 | |
| 44 | #define NUM_HANDLES 1000 |
| 45 | |
| 46 | static void *curl_hnd[NUM_HANDLES]; |
| 47 | static int num_transfers; |
| 48 | |
| 49 | /* a handle to number lookup, highly ineffective when we do many |
| 50 | transfers... */ |
| 51 | static int hnd2num(CURL *hnd) |
| 52 | { |
| 53 | int i; |
| 54 | for(i=0; i< num_transfers; i++) { |
| 55 | if(curl_hnd[i] == hnd) |
| 56 | return i; |
| 57 | } |
| 58 | return 0; /* weird, but just a fail-safe */ |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | void dump(const char *text, int num, unsigned char *ptr, size_t size, |
| 63 | char nohex) |
| 64 | { |
| 65 | size_t i; |
| 66 | size_t c; |
| 67 | |
| 68 | unsigned int width=0x10; |
| 69 | |
| 70 | if(nohex) |
| 71 | /* without the hex output, we can fit more on screen */ |
| 72 | width = 0x40; |
| 73 | |
| 74 | fprintf(stderr, "%d %s, %ld bytes (0x%lx)\n", |
| 75 | num, text, (long)size, (long)size); |
| 76 | |
| 77 | for(i=0; i<size; i+= width) { |
| 78 | |
| 79 | fprintf(stderr, "%4.4lx: ", (long)i); |
| 80 | |
| 81 | if(!nohex) { |
| 82 | /* hex not disabled, show it */ |
| 83 | for(c = 0; c < width; c++) |
| 84 | if(i+c < size) |
| 85 | fprintf(stderr, "%02x ", ptr[i+c]); |
| 86 | else |
| 87 | fputs(" ", stderr); |
| 88 | } |
| 89 | |
| 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) { |
| 93 | i+=(c+2-width); |
| 94 | break; |
| 95 | } |
| 96 | fprintf(stderr, "%c", |
| 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) { |
| 100 | i+=(c+3-width); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | fputc('\n', stderr); /* newline */ |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static |
| 109 | int my_trace(CURL *handle, curl_infotype type, |
| 110 | char *data, size_t size, |
| 111 | void *userp) |
| 112 | { |
| 113 | const char *text; |
| 114 | int num = hnd2num(handle); |
| 115 | (void)handle; /* prevent compiler warning */ |
| 116 | (void)userp; |
| 117 | switch(type) { |
| 118 | case CURLINFO_TEXT: |
| 119 | fprintf(stderr, "== %d Info: %s", num, data); |
| 120 | /* FALLTHROUGH */ |
| 121 | default: /* in case a new one is introduced to shock us */ |
| 122 | return 0; |
| 123 | |
| 124 | case CURLINFO_HEADER_OUT: |
| 125 | text = "=> Send header"; |
| 126 | break; |
| 127 | case CURLINFO_DATA_OUT: |
| 128 | text = "=> Send data"; |
| 129 | break; |
| 130 | case CURLINFO_SSL_DATA_OUT: |
| 131 | text = "=> Send SSL data"; |
| 132 | break; |
| 133 | case CURLINFO_HEADER_IN: |
| 134 | text = "<= Recv header"; |
| 135 | break; |
| 136 | case CURLINFO_DATA_IN: |
| 137 | text = "<= Recv data"; |
| 138 | break; |
| 139 | case CURLINFO_SSL_DATA_IN: |
| 140 | text = "<= Recv SSL data"; |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | dump(text, num, (unsigned char *)data, size, 1); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static void setup(CURL *hnd, int num) |
| 149 | { |
| 150 | FILE *out; |
| 151 | char filename[128]; |
| 152 | |
| 153 | snprintf(filename, 128, "dl-%d", num); |
| 154 | |
| 155 | out = fopen(filename, "wb"); |
| 156 | |
| 157 | /* write to this file */ |
| 158 | curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out); |
| 159 | |
| 160 | /* set the same URL */ |
| 161 | curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html"); |
| 162 | |
| 163 | /* please be verbose */ |
| 164 | curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L); |
| 165 | curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace); |
| 166 | |
| 167 | /* HTTP/2 please */ |
| 168 | curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
| 169 | |
| 170 | /* we use a self-signed test server, skip verification during debugging */ |
| 171 | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L); |
| 172 | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L); |
| 173 | |
| 174 | #if (CURLPIPE_MULTIPLEX > 0) |
| 175 | /* wait for pipe connection to confirm */ |
| 176 | curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L); |
| 177 | #endif |
| 178 | |
| 179 | curl_hnd[num] = hnd; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Simply download two files over HTTP/2, using the same physical connection! |
| 184 | */ |
| 185 | int main(int argc, char **argv) |
| 186 | { |
| 187 | CURL *easy[NUM_HANDLES]; |
| 188 | CURLM *multi_handle; |
| 189 | int i; |
| 190 | int still_running; /* keep number of running handles */ |
| 191 | |
| 192 | if(argc > 1) |
| 193 | /* if given a number, do that many transfers */ |
| 194 | num_transfers = atoi(argv[1]); |
| 195 | |
| 196 | if(!num_transfers || (num_transfers > NUM_HANDLES)) |
| 197 | num_transfers = 3; /* a suitable low default */ |
| 198 | |
| 199 | /* init a multi stack */ |
| 200 | multi_handle = curl_multi_init(); |
| 201 | |
| 202 | for(i=0; i<num_transfers; i++) { |
| 203 | easy[i] = curl_easy_init(); |
| 204 | /* set options */ |
| 205 | setup(easy[i], i); |
| 206 | |
| 207 | /* add the individual transfer */ |
| 208 | curl_multi_add_handle(multi_handle, easy[i]); |
| 209 | } |
| 210 | |
| 211 | curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
| 212 | |
| 213 | /* we start some action by calling perform right away */ |
| 214 | curl_multi_perform(multi_handle, &still_running); |
| 215 | |
| 216 | do { |
| 217 | struct timeval timeout; |
| 218 | int rc; /* select() return code */ |
| 219 | CURLMcode mc; /* curl_multi_fdset() return code */ |
| 220 | |
| 221 | fd_set fdread; |
| 222 | fd_set fdwrite; |
| 223 | fd_set fdexcep; |
| 224 | int maxfd = -1; |
| 225 | |
| 226 | long curl_timeo = -1; |
| 227 | |
| 228 | FD_ZERO(&fdread); |
| 229 | FD_ZERO(&fdwrite); |
| 230 | FD_ZERO(&fdexcep); |
| 231 | |
| 232 | /* set a suitable timeout to play around with */ |
| 233 | timeout.tv_sec = 1; |
| 234 | timeout.tv_usec = 0; |
| 235 | |
| 236 | curl_multi_timeout(multi_handle, &curl_timeo); |
| 237 | if(curl_timeo >= 0) { |
| 238 | timeout.tv_sec = curl_timeo / 1000; |
| 239 | if(timeout.tv_sec > 1) |
| 240 | timeout.tv_sec = 1; |
| 241 | else |
| 242 | timeout.tv_usec = (curl_timeo % 1000) * 1000; |
| 243 | } |
| 244 | |
| 245 | /* get file descriptors from the transfers */ |
| 246 | mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); |
| 247 | |
| 248 | if(mc != CURLM_OK) { |
| 249 | fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | /* On success the value of maxfd is guaranteed to be >= -1. We call |
| 254 | select(maxfd + 1, ...); specially in case of (maxfd == -1) there are |
| 255 | no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- |
| 256 | to sleep 100ms, which is the minimum suggested value in the |
| 257 | curl_multi_fdset() doc. */ |
| 258 | |
| 259 | if(maxfd == -1) { |
| 260 | #ifdef _WIN32 |
| 261 | Sleep(100); |
| 262 | rc = 0; |
| 263 | #else |
| 264 | /* Portable sleep for platforms other than Windows. */ |
| 265 | struct timeval wait = { 0, 100 * 1000 }; /* 100ms */ |
| 266 | rc = select(0, NULL, NULL, NULL, &wait); |
| 267 | #endif |
| 268 | } |
| 269 | else { |
| 270 | /* Note that on some platforms 'timeout' may be modified by select(). |
| 271 | If you need access to the original value save a copy beforehand. */ |
| 272 | rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); |
| 273 | } |
| 274 | |
| 275 | switch(rc) { |
| 276 | case -1: |
| 277 | /* select error */ |
| 278 | break; |
| 279 | case 0: |
| 280 | default: |
| 281 | /* timeout or readable/writable sockets */ |
| 282 | curl_multi_perform(multi_handle, &still_running); |
| 283 | break; |
| 284 | } |
| 285 | } while(still_running); |
| 286 | |
| 287 | curl_multi_cleanup(multi_handle); |
| 288 | |
| 289 | for(i=0; i<num_transfers; i++) |
| 290 | curl_easy_cleanup(easy[i]); |
| 291 | |
| 292 | return 0; |
| 293 | } |