xref: /curl/docs/examples/getinmemory.c (revision f540e43b)
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 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <curl/curl.h>
35 
36 struct MemoryStruct {
37   char *memory;
38   size_t size;
39 };
40 
41 static size_t
WriteMemoryCallback(void * contents,size_t size,size_t nmemb,void * userp)42 WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
43 {
44   size_t realsize = size * nmemb;
45   struct MemoryStruct *mem = (struct MemoryStruct *)userp;
46 
47   char *ptr = realloc(mem->memory, mem->size + realsize + 1);
48   if(!ptr) {
49     /* out of memory! */
50     printf("not enough memory (realloc returned NULL)\n");
51     return 0;
52   }
53 
54   mem->memory = ptr;
55   memcpy(&(mem->memory[mem->size]), contents, realsize);
56   mem->size += realsize;
57   mem->memory[mem->size] = 0;
58 
59   return realsize;
60 }
61 
main(void)62 int main(void)
63 {
64   CURL *curl_handle;
65   CURLcode res;
66 
67   struct MemoryStruct chunk;
68 
69   chunk.memory = malloc(1);  /* grown as needed by the realloc above */
70   chunk.size = 0;    /* no data at this point */
71 
72   curl_global_init(CURL_GLOBAL_ALL);
73 
74   /* init the curl session */
75   curl_handle = curl_easy_init();
76 
77   /* specify URL to get */
78   curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.example.com/");
79 
80   /* send all data to this function  */
81   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
82 
83   /* we pass our 'chunk' struct to the callback function */
84   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
85 
86   /* some servers do not like requests that are made without a user-agent
87      field, so we provide one */
88   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
89 
90   /* get it! */
91   res = curl_easy_perform(curl_handle);
92 
93   /* check for errors */
94   if(res != CURLE_OK) {
95     fprintf(stderr, "curl_easy_perform() failed: %s\n",
96             curl_easy_strerror(res));
97   }
98   else {
99     /*
100      * Now, our chunk.memory points to a memory block that is chunk.size
101      * bytes big and contains the remote file.
102      *
103      * Do something nice with it!
104      */
105 
106     printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
107   }
108 
109   /* cleanup curl stuff */
110   curl_easy_cleanup(curl_handle);
111 
112   free(chunk.memory);
113 
114   /* we are done with libcurl, so clean it up */
115   curl_global_cleanup();
116 
117   return 0;
118 }
119