2
0

http2-pushinmemory.c 5.0 KB

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