getinmemory.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * Example source code to show how the callback function can be used to
  11. * download data into a chunk of memory instead of storing it in a file.
  12. *
  13. * This exact source code has not been verified to work.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <curl/curl.h>
  19. #include <curl/types.h>
  20. #include <curl/easy.h>
  21. struct MemoryStruct {
  22. char *memory;
  23. size_t size;
  24. };
  25. static void *myrealloc(void *ptr, size_t size)
  26. {
  27. /* There might be a realloc() out there that doesn't like reallocing
  28. NULL pointers, so we take care of it here */
  29. if(ptr)
  30. return realloc(ptr, size);
  31. else
  32. return malloc(size);
  33. }
  34. static size_t
  35. WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
  36. {
  37. size_t realsize = size * nmemb;
  38. struct MemoryStruct *mem = (struct MemoryStruct *)data;
  39. mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
  40. if (mem->memory) {
  41. memcpy(&(mem->memory[mem->size]), ptr, realsize);
  42. mem->size += realsize;
  43. mem->memory[mem->size] = 0;
  44. }
  45. return realsize;
  46. }
  47. int main(int argc, char **argv)
  48. {
  49. CURL *curl_handle;
  50. struct MemoryStruct chunk;
  51. chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
  52. chunk.size = 0; /* no data at this point */
  53. curl_global_init(CURL_GLOBAL_ALL);
  54. /* init the curl session */
  55. curl_handle = curl_easy_init();
  56. /* specify URL to get */
  57. curl_easy_setopt(curl_handle, CURLOPT_URL, "http://cool.haxx.se/");
  58. /* send all data to this function */
  59. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  60. /* we pass our 'chunk' struct to the callback function */
  61. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  62. /* some servers don't like requests that are made without a user-agent
  63. field, so we provide one */
  64. curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  65. /* get it! */
  66. curl_easy_perform(curl_handle);
  67. /* cleanup curl stuff */
  68. curl_easy_cleanup(curl_handle);
  69. /*
  70. * Now, our chunk.memory points to a memory block that is chunk.size
  71. * bytes big and contains the remote file.
  72. *
  73. * Do something nice with it!
  74. *
  75. * You should be aware of the fact that at this point we might have an
  76. * allocated data block, and nothing has yet deallocated that data. So when
  77. * you're done with it, you should free() it as a nice application.
  78. */
  79. if(chunk.memory)
  80. free(chunk.memory);
  81. /* we're done with libcurl, so clean it up */
  82. curl_global_cleanup();
  83. return 0;
  84. }