getinmemory.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * Shows how the write callback function can be used to download data into a
  26. * chunk of memory instead of storing it in a file.
  27. * </DESC>
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <curl/curl.h>
  33. struct MemoryStruct {
  34. char *memory;
  35. size_t size;
  36. };
  37. static size_t
  38. WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  39. {
  40. size_t realsize = size * nmemb;
  41. struct MemoryStruct *mem = (struct MemoryStruct *)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. int main(void)
  55. {
  56. CURL *curl_handle;
  57. CURLcode res;
  58. struct MemoryStruct chunk;
  59. chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
  60. chunk.size = 0; /* no data at this point */
  61. curl_global_init(CURL_GLOBAL_ALL);
  62. /* init the curl session */
  63. curl_handle = curl_easy_init();
  64. /* specify URL to get */
  65. curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.example.com/");
  66. /* send all data to this function */
  67. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  68. /* we pass our 'chunk' struct to the callback function */
  69. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  70. /* some servers do not like requests that are made without a user-agent
  71. field, so we provide one */
  72. curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  73. /* get it! */
  74. res = curl_easy_perform(curl_handle);
  75. /* check for errors */
  76. if(res != CURLE_OK) {
  77. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  78. curl_easy_strerror(res));
  79. }
  80. else {
  81. /*
  82. * Now, our chunk.memory points to a memory block that is chunk.size
  83. * bytes big and contains the remote file.
  84. *
  85. * Do something nice with it!
  86. */
  87. printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
  88. }
  89. /* cleanup curl stuff */
  90. curl_easy_cleanup(curl_handle);
  91. free(chunk.memory);
  92. /* we are done with libcurl, so clean it up */
  93. curl_global_cleanup();
  94. return 0;
  95. }