http2-serverpush.c 7.1 KB

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