http2-upload.c 9.5 KB

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