http2-serverpush.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * HTTP/2 server push
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.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. #error "too old libcurl, cannot do HTTP/2 server push!"
  38. #endif
  39. static
  40. void dump(const char *text, unsigned char *ptr, size_t size,
  41. char nohex)
  42. {
  43. size_t i;
  44. size_t c;
  45. unsigned int width = 0x10;
  46. if(nohex)
  47. /* without the hex output, we can fit more on screen */
  48. width = 0x40;
  49. fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
  50. text, (unsigned long)size, (unsigned long)size);
  51. for(i = 0; i<size; i += width) {
  52. fprintf(stderr, "%4.4lx: ", (unsigned long)i);
  53. if(!nohex) {
  54. /* hex not disabled, show it */
  55. for(c = 0; c < width; c++)
  56. if(i + c < size)
  57. fprintf(stderr, "%02x ", ptr[i + c]);
  58. else
  59. fputs(" ", stderr);
  60. }
  61. for(c = 0; (c < width) && (i + c < size); c++) {
  62. /* check for 0D0A; if found, skip past and start a new line of output */
  63. if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
  64. ptr[i + c + 1] == 0x0A) {
  65. i += (c + 2 - width);
  66. break;
  67. }
  68. fprintf(stderr, "%c",
  69. (ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
  70. /* check again for 0D0A, to avoid an extra \n if it's at width */
  71. if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
  72. ptr[i + c + 2] == 0x0A) {
  73. i += (c + 3 - width);
  74. break;
  75. }
  76. }
  77. fputc('\n', stderr); /* newline */
  78. }
  79. }
  80. static
  81. int my_trace(CURL *handle, curl_infotype type,
  82. char *data, size_t size,
  83. void *userp)
  84. {
  85. const char *text;
  86. (void)handle; /* prevent compiler warning */
  87. (void)userp;
  88. switch(type) {
  89. case CURLINFO_TEXT:
  90. fprintf(stderr, "== Info: %s", data);
  91. /* FALLTHROUGH */
  92. default: /* in case a new one is introduced to shock us */
  93. return 0;
  94. case CURLINFO_HEADER_OUT:
  95. text = "=> Send header";
  96. break;
  97. case CURLINFO_DATA_OUT:
  98. text = "=> Send data";
  99. break;
  100. case CURLINFO_SSL_DATA_OUT:
  101. text = "=> Send SSL data";
  102. break;
  103. case CURLINFO_HEADER_IN:
  104. text = "<= Recv header";
  105. break;
  106. case CURLINFO_DATA_IN:
  107. text = "<= Recv data";
  108. break;
  109. case CURLINFO_SSL_DATA_IN:
  110. text = "<= Recv SSL data";
  111. break;
  112. }
  113. dump(text, (unsigned char *)data, size, 1);
  114. return 0;
  115. }
  116. #define OUTPUTFILE "dl"
  117. static int setup(CURL *hnd, const char *url)
  118. {
  119. FILE *out = fopen(OUTPUTFILE, "wb");
  120. if(!out)
  121. /* failed */
  122. return 1;
  123. /* write to this file */
  124. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
  125. /* set the same URL */
  126. curl_easy_setopt(hnd, CURLOPT_URL, url);
  127. /* please be verbose */
  128. curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  129. curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
  130. /* HTTP/2 please */
  131. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  132. /* we use a self-signed test server, skip verification during debugging */
  133. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  134. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  135. #if (CURLPIPE_MULTIPLEX > 0)
  136. /* wait for pipe connection to confirm */
  137. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  138. #endif
  139. return 0; /* all is good */
  140. }
  141. /* called when there's an incoming push */
  142. static int server_push_callback(CURL *parent,
  143. CURL *easy,
  144. size_t num_headers,
  145. struct curl_pushheaders *headers,
  146. void *userp)
  147. {
  148. char *headp;
  149. size_t i;
  150. int *transfers = (int *)userp;
  151. char filename[128];
  152. FILE *out;
  153. static unsigned int count = 0;
  154. (void)parent; /* we have no use for this */
  155. snprintf(filename, 128, "push%u", count++);
  156. /* here's a new stream, save it in a new file for each new push */
  157. out = fopen(filename, "wb");
  158. if(!out) {
  159. /* if we cannot save it, deny it */
  160. fprintf(stderr, "Failed to create output file for push\n");
  161. return CURL_PUSH_DENY;
  162. }
  163. /* write to this file */
  164. curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
  165. fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
  166. count, (unsigned long)num_headers);
  167. for(i = 0; i<num_headers; i++) {
  168. headp = curl_pushheader_bynum(headers, i);
  169. fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
  170. }
  171. headp = curl_pushheader_byname(headers, ":path");
  172. if(headp) {
  173. fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */);
  174. }
  175. (*transfers)++; /* one more */
  176. return CURL_PUSH_OK;
  177. }
  178. /*
  179. * Download a file over HTTP/2, take care of server push.
  180. */
  181. int main(int argc, char *argv[])
  182. {
  183. CURL *easy;
  184. CURLM *multi_handle;
  185. int transfers = 1; /* we start with one */
  186. struct CURLMsg *m;
  187. const char *url = "https://localhost:8443/index.html";
  188. if(argc == 2)
  189. url = argv[1];
  190. /* init a multi stack */
  191. multi_handle = curl_multi_init();
  192. easy = curl_easy_init();
  193. /* set options */
  194. if(setup(easy, url)) {
  195. fprintf(stderr, "failed\n");
  196. return 1;
  197. }
  198. /* add the easy transfer */
  199. curl_multi_add_handle(multi_handle, easy);
  200. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  201. curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
  202. curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
  203. do {
  204. int still_running; /* keep number of running handles */
  205. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  206. if(still_running)
  207. /* wait for activity, timeout or "nothing" */
  208. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  209. if(mc)
  210. break;
  211. /*
  212. * A little caution when doing server push is that libcurl itself has
  213. * created and added one or more easy handles but we need to clean them up
  214. * when we are done.
  215. */
  216. do {
  217. int msgq = 0;
  218. m = curl_multi_info_read(multi_handle, &msgq);
  219. if(m && (m->msg == CURLMSG_DONE)) {
  220. CURL *e = m->easy_handle;
  221. transfers--;
  222. curl_multi_remove_handle(multi_handle, e);
  223. curl_easy_cleanup(e);
  224. }
  225. } while(m);
  226. } while(transfers); /* as long as we have transfers going */
  227. curl_multi_cleanup(multi_handle);
  228. return 0;
  229. }