h2-serverpush.c 7.1 KB

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