113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* <DESC>
2513498266Sopenharmony_ci * Shows how the write callback function can be used to download data into a
2613498266Sopenharmony_ci * chunk of memory instead of storing it in a file.
2713498266Sopenharmony_ci * </DESC>
2813498266Sopenharmony_ci */
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#include <stdio.h>
3113498266Sopenharmony_ci#include <stdlib.h>
3213498266Sopenharmony_ci#include <string.h>
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci#include <curl/curl.h>
3513498266Sopenharmony_ci
3613498266Sopenharmony_cistruct MemoryStruct {
3713498266Sopenharmony_ci  char *memory;
3813498266Sopenharmony_ci  size_t size;
3913498266Sopenharmony_ci};
4013498266Sopenharmony_ci
4113498266Sopenharmony_cistatic size_t
4213498266Sopenharmony_ciWriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
4313498266Sopenharmony_ci{
4413498266Sopenharmony_ci  size_t realsize = size * nmemb;
4513498266Sopenharmony_ci  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci  char *ptr = realloc(mem->memory, mem->size + realsize + 1);
4813498266Sopenharmony_ci  if(!ptr) {
4913498266Sopenharmony_ci    /* out of memory! */
5013498266Sopenharmony_ci    printf("not enough memory (realloc returned NULL)\n");
5113498266Sopenharmony_ci    return 0;
5213498266Sopenharmony_ci  }
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci  mem->memory = ptr;
5513498266Sopenharmony_ci  memcpy(&(mem->memory[mem->size]), contents, realsize);
5613498266Sopenharmony_ci  mem->size += realsize;
5713498266Sopenharmony_ci  mem->memory[mem->size] = 0;
5813498266Sopenharmony_ci
5913498266Sopenharmony_ci  return realsize;
6013498266Sopenharmony_ci}
6113498266Sopenharmony_ci
6213498266Sopenharmony_ciint main(void)
6313498266Sopenharmony_ci{
6413498266Sopenharmony_ci  CURL *curl_handle;
6513498266Sopenharmony_ci  CURLcode res;
6613498266Sopenharmony_ci
6713498266Sopenharmony_ci  struct MemoryStruct chunk;
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
7013498266Sopenharmony_ci  chunk.size = 0;    /* no data at this point */
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci  curl_global_init(CURL_GLOBAL_ALL);
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci  /* init the curl session */
7513498266Sopenharmony_ci  curl_handle = curl_easy_init();
7613498266Sopenharmony_ci
7713498266Sopenharmony_ci  /* specify URL to get */
7813498266Sopenharmony_ci  curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.example.com/");
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci  /* send all data to this function  */
8113498266Sopenharmony_ci  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
8213498266Sopenharmony_ci
8313498266Sopenharmony_ci  /* we pass our 'chunk' struct to the callback function */
8413498266Sopenharmony_ci  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
8513498266Sopenharmony_ci
8613498266Sopenharmony_ci  /* some servers do not like requests that are made without a user-agent
8713498266Sopenharmony_ci     field, so we provide one */
8813498266Sopenharmony_ci  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci  /* get it! */
9113498266Sopenharmony_ci  res = curl_easy_perform(curl_handle);
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci  /* check for errors */
9413498266Sopenharmony_ci  if(res != CURLE_OK) {
9513498266Sopenharmony_ci    fprintf(stderr, "curl_easy_perform() failed: %s\n",
9613498266Sopenharmony_ci            curl_easy_strerror(res));
9713498266Sopenharmony_ci  }
9813498266Sopenharmony_ci  else {
9913498266Sopenharmony_ci    /*
10013498266Sopenharmony_ci     * Now, our chunk.memory points to a memory block that is chunk.size
10113498266Sopenharmony_ci     * bytes big and contains the remote file.
10213498266Sopenharmony_ci     *
10313498266Sopenharmony_ci     * Do something nice with it!
10413498266Sopenharmony_ci     */
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci    printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
10713498266Sopenharmony_ci  }
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci  /* cleanup curl stuff */
11013498266Sopenharmony_ci  curl_easy_cleanup(curl_handle);
11113498266Sopenharmony_ci
11213498266Sopenharmony_ci  free(chunk.memory);
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci  /* we are done with libcurl, so clean it up */
11513498266Sopenharmony_ci  curl_global_cleanup();
11613498266Sopenharmony_ci
11713498266Sopenharmony_ci  return 0;
11813498266Sopenharmony_ci}
119