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