getinmemory.c 3.2 KB

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