http2-serverpush.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.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. * HTTP/2 server push
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. /* somewhat unix-specific */
  30. #include <sys/time.h>
  31. #include <unistd.h>
  32. /* curl stuff */
  33. #include <curl/curl.h>
  34. #ifndef CURLPIPE_MULTIPLEX
  35. #error "too old libcurl, can't do HTTP/2 server push!"
  36. #endif
  37. static
  38. void dump(const char *text, unsigned char *ptr, size_t size,
  39. char nohex)
  40. {
  41. size_t i;
  42. size_t c;
  43. unsigned int width = 0x10;
  44. if(nohex)
  45. /* without the hex output, we can fit more on screen */
  46. width = 0x40;
  47. fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
  48. text, (unsigned long)size, (unsigned long)size);
  49. for(i = 0; i<size; i += width) {
  50. fprintf(stderr, "%4.4lx: ", (unsigned long)i);
  51. if(!nohex) {
  52. /* hex not disabled, show it */
  53. for(c = 0; c < width; c++)
  54. if(i + c < size)
  55. fprintf(stderr, "%02x ", ptr[i + c]);
  56. else
  57. fputs(" ", stderr);
  58. }
  59. for(c = 0; (c < width) && (i + c < size); c++) {
  60. /* check for 0D0A; if found, skip past and start a new line of output */
  61. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  62. ptr[i + c + 1] == 0x0A) {
  63. i += (c + 2 - width);
  64. break;
  65. }
  66. fprintf(stderr, "%c",
  67. (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
  68. /* check again for 0D0A, to avoid an extra \n if it's at width */
  69. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  70. ptr[i + c + 2] == 0x0A) {
  71. i += (c + 3 - width);
  72. break;
  73. }
  74. }
  75. fputc('\n', stderr); /* newline */
  76. }
  77. }
  78. static
  79. int my_trace(CURL *handle, curl_infotype type,
  80. char *data, size_t size,
  81. void *userp)
  82. {
  83. const char *text;
  84. (void)handle; /* prevent compiler warning */
  85. (void)userp;
  86. switch(type) {
  87. case CURLINFO_TEXT:
  88. fprintf(stderr, "== Info: %s", data);
  89. /* FALLTHROUGH */
  90. default: /* in case a new one is introduced to shock us */
  91. return 0;
  92. case CURLINFO_HEADER_OUT:
  93. text = "=> Send header";
  94. break;
  95. case CURLINFO_DATA_OUT:
  96. text = "=> Send data";
  97. break;
  98. case CURLINFO_SSL_DATA_OUT:
  99. text = "=> Send SSL data";
  100. break;
  101. case CURLINFO_HEADER_IN:
  102. text = "<= Recv header";
  103. break;
  104. case CURLINFO_DATA_IN:
  105. text = "<= Recv data";
  106. break;
  107. case CURLINFO_SSL_DATA_IN:
  108. text = "<= Recv SSL data";
  109. break;
  110. }
  111. dump(text, (unsigned char *)data, size, 1);
  112. return 0;
  113. }
  114. #define OUTPUTFILE "dl"
  115. static int setup(CURL *hnd)
  116. {
  117. FILE *out = fopen(OUTPUTFILE, "wb");
  118. if(!out)
  119. /* failed */
  120. return 1;
  121. /* write to this file */
  122. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
  123. /* set the same URL */
  124. curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
  125. /* please be verbose */
  126. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  127. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
  128. /* HTTP/2 please */
  129. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  130. /* we use a self-signed test server, skip verification during debugging */
  131. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  132. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  133. #if (CURLPIPE_MULTIPLEX > 0)
  134. /* wait for pipe connection to confirm */
  135. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  136. #endif
  137. return 0; /* all is good */
  138. }
  139. /* called when there's an incoming push */
  140. static int server_push_callback(CURL *parent,
  141. CURL *easy,
  142. size_t num_headers,
  143. struct curl_pushheaders *headers,
  144. void *userp)
  145. {
  146. char *headp;
  147. size_t i;
  148. int *transfers = (int *)userp;
  149. char filename[128];
  150. FILE *out;
  151. static unsigned int count = 0;
  152. (void)parent; /* we have no use for this */
  153. snprintf(filename, 128, "push%u", count++);
  154. /* here's a new stream, save it in a new file for each new push */
  155. out = fopen(filename, "wb");
  156. if(!out) {
  157. /* if we can't save it, deny it */
  158. fprintf(stderr, "Failed to create output file for push\n");
  159. return CURL_PUSH_DENY;
  160. }
  161. /* write to this file */
  162. curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
  163. fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
  164. count, (unsigned long)num_headers);
  165. for(i = 0; i<num_headers; i++) {
  166. headp = curl_pushheader_bynum(headers, i);
  167. fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
  168. }
  169. headp = curl_pushheader_byname(headers, ":path");
  170. if(headp) {
  171. fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
  172. }
  173. (*transfers)++; /* one more */
  174. return CURL_PUSH_OK;
  175. }
  176. /*
  177. * Download a file over HTTP/2, take care of server push.
  178. */
  179. int main(void)
  180. {
  181. CURL *easy;
  182. CURLM *multi_handle;
  183. int still_running; /* keep number of running handles */
  184. int transfers = 1; /* we start with one */
  185. struct CURLMsg *m;
  186. /* init a multi stack */
  187. multi_handle = curl_multi_init();
  188. easy = curl_easy_init();
  189. /* set options */
  190. if(setup(easy)) {
  191. fprintf(stderr, "failed\n");
  192. return 1;
  193. }
  194. /* add the easy transfer */
  195. curl_multi_add_handle(multi_handle, easy);
  196. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  197. curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
  198. curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
  199. /* we start some action by calling perform right away */
  200. curl_multi_perform(multi_handle, &still_running);
  201. do {
  202. struct timeval timeout;
  203. int rc; /* select() return code */
  204. CURLMcode mc; /* curl_multi_fdset() return code */
  205. fd_set fdread;
  206. fd_set fdwrite;
  207. fd_set fdexcep;
  208. int maxfd = -1;
  209. long curl_timeo = -1;
  210. FD_ZERO(&fdread);
  211. FD_ZERO(&fdwrite);
  212. FD_ZERO(&fdexcep);
  213. /* set a suitable timeout to play around with */
  214. timeout.tv_sec = 1;
  215. timeout.tv_usec = 0;
  216. curl_multi_timeout(multi_handle, &curl_timeo);
  217. if(curl_timeo >= 0) {
  218. timeout.tv_sec = curl_timeo / 1000;
  219. if(timeout.tv_sec > 1)
  220. timeout.tv_sec = 1;
  221. else
  222. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  223. }
  224. /* get file descriptors from the transfers */
  225. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  226. if(mc != CURLM_OK) {
  227. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  228. break;
  229. }
  230. /* On success the value of maxfd is guaranteed to be >= -1. We call
  231. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  232. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  233. to sleep 100ms, which is the minimum suggested value in the
  234. curl_multi_fdset() doc. */
  235. if(maxfd == -1) {
  236. #ifdef _WIN32
  237. Sleep(100);
  238. rc = 0;
  239. #else
  240. /* Portable sleep for platforms other than Windows. */
  241. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  242. rc = select(0, NULL, NULL, NULL, &wait);
  243. #endif
  244. }
  245. else {
  246. /* Note that on some platforms 'timeout' may be modified by select().
  247. If you need access to the original value save a copy beforehand. */
  248. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  249. }
  250. switch(rc) {
  251. case -1:
  252. /* select error */
  253. break;
  254. case 0:
  255. default:
  256. /* timeout or readable/writable sockets */
  257. curl_multi_perform(multi_handle, &still_running);
  258. break;
  259. }
  260. /*
  261. * A little caution when doing server push is that libcurl itself has
  262. * created and added one or more easy handles but we need to clean them up
  263. * when we are done.
  264. */
  265. do {
  266. int msgq = 0;;
  267. m = curl_multi_info_read(multi_handle, &msgq);
  268. if(m && (m->msg == CURLMSG_DONE)) {
  269. CURL *e = m->easy_handle;
  270. transfers--;
  271. curl_multi_remove_handle(multi_handle, e);
  272. curl_easy_cleanup(e);
  273. }
  274. } while(m);
  275. } while(transfers); /* as long as we have transfers going */
  276. curl_multi_cleanup(multi_handle);
  277. return 0;
  278. }