getinmemory.c 3.2 KB

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