2
0

http2-pushinmemory.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. Receive all data in memory.
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. /* curl stuff */
  32. #include <curl/curl.h>
  33. struct Memory {
  34. char *memory;
  35. size_t size;
  36. };
  37. static size_t
  38. write_cb(void *contents, size_t size, size_t nmemb, void *userp)
  39. {
  40. size_t realsize = size * nmemb;
  41. struct Memory *mem = (struct Memory *)userp;
  42. char *ptr = realloc(mem->memory, mem->size + realsize + 1);
  43. if(!ptr) {
  44. /* out of memory! */
  45. printf("not enough memory (realloc returned NULL)\n");
  46. return 0;
  47. }
  48. mem->memory = ptr;
  49. memcpy(&(mem->memory[mem->size]), contents, realsize);
  50. mem->size += realsize;
  51. mem->memory[mem->size] = 0;
  52. return realsize;
  53. }
  54. #define MAX_FILES 10
  55. static struct Memory files[MAX_FILES];
  56. static int pushindex = 1;
  57. static void init_memory(struct Memory *chunk)
  58. {
  59. chunk->memory = malloc(1); /* grown as needed with realloc */
  60. chunk->size = 0; /* no data at this point */
  61. }
  62. static void setup(CURL *hnd)
  63. {
  64. /* set the same URL */
  65. curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
  66. /* HTTP/2 please */
  67. curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  68. /* we use a self-signed test server, skip verification during debugging */
  69. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  70. curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  71. /* write data to a struct */
  72. curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_cb);
  73. init_memory(&files[0]);
  74. curl_easy_setopt(hnd, CURLOPT_WRITEDATA, &files[0]);
  75. /* wait for pipe connection to confirm */
  76. curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
  77. }
  78. /* called when there is an incoming push */
  79. static int server_push_callback(CURL *parent,
  80. CURL *easy,
  81. size_t num_headers,
  82. struct curl_pushheaders *headers,
  83. void *userp)
  84. {
  85. char *headp;
  86. int *transfers = (int *)userp;
  87. (void)parent; /* we have no use for this */
  88. (void)num_headers; /* unused */
  89. if(pushindex == MAX_FILES)
  90. /* cannot fit anymore */
  91. return CURL_PUSH_DENY;
  92. /* write to this buffer */
  93. init_memory(&files[pushindex]);
  94. curl_easy_setopt(easy, CURLOPT_WRITEDATA, &files[pushindex]);
  95. pushindex++;
  96. headp = curl_pushheader_byname(headers, ":path");
  97. if(headp)
  98. fprintf(stderr, "* Pushed :path '%s'\n", headp /* skip :path + colon */);
  99. (*transfers)++; /* one more */
  100. return CURL_PUSH_OK;
  101. }
  102. /*
  103. * Download a file over HTTP/2, take care of server push.
  104. */
  105. int main(void)
  106. {
  107. CURL *easy;
  108. CURLM *multi;
  109. int still_running; /* keep number of running handles */
  110. int transfers = 1; /* we start with one */
  111. int i;
  112. struct CURLMsg *m;
  113. /* init a multi stack */
  114. multi = curl_multi_init();
  115. easy = curl_easy_init();
  116. /* set options */
  117. setup(easy);
  118. /* add the easy transfer */
  119. curl_multi_add_handle(multi, easy);
  120. curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  121. curl_multi_setopt(multi, CURLMOPT_PUSHFUNCTION, server_push_callback);
  122. curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &transfers);
  123. while(transfers) {
  124. int rc;
  125. CURLMcode mcode = curl_multi_perform(multi, &still_running);
  126. if(mcode)
  127. break;
  128. mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
  129. if(mcode)
  130. break;
  131. /*
  132. * When doing server push, libcurl itself created and added one or more
  133. * easy handles but *we* need to clean them up when they are done.
  134. */
  135. do {
  136. int msgq = 0;
  137. m = curl_multi_info_read(multi, &msgq);
  138. if(m && (m->msg == CURLMSG_DONE)) {
  139. CURL *e = m->easy_handle;
  140. transfers--;
  141. curl_multi_remove_handle(multi, e);
  142. curl_easy_cleanup(e);
  143. }
  144. } while(m);
  145. }
  146. curl_multi_cleanup(multi);
  147. /* 'pushindex' is now the number of received transfers */
  148. for(i = 0; i < pushindex; i++) {
  149. /* do something fun with the data, and then free it when done */
  150. free(files[i].memory);
  151. }
  152. return 0;
  153. }