http2-upload.c 9.4 KB

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