http2-serverpush.c 8.5 KB

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