http2-pushinmemory.c 5.0 KB

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