• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Simple HTTP GET that stores the headers in a separate file
26  * </DESC>
27  */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 
32 #include <curl/curl.h>
33 
write_data(void * ptr,size_t size,size_t nmemb,void * stream)34 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
35 {
36   size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
37   return written;
38 }
39 
main(void)40 int main(void)
41 {
42   CURL *curl_handle;
43   static const char *headerfilename = "head.out";
44   FILE *headerfile;
45   static const char *bodyfilename = "body.out";
46   FILE *bodyfile;
47 
48   curl_global_init(CURL_GLOBAL_ALL);
49 
50   /* init the curl session */
51   curl_handle = curl_easy_init();
52 
53   /* set URL to get */
54   curl_easy_setopt(curl_handle, CURLOPT_URL, "https://example.com");
55 
56   /* no progress meter please */
57   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
58 
59   /* send all data to this function  */
60   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
61 
62   /* open the header file */
63   headerfile = fopen(headerfilename, "wb");
64   if(!headerfile) {
65     curl_easy_cleanup(curl_handle);
66     return -1;
67   }
68 
69   /* open the body file */
70   bodyfile = fopen(bodyfilename, "wb");
71   if(!bodyfile) {
72     curl_easy_cleanup(curl_handle);
73     fclose(headerfile);
74     return -1;
75   }
76 
77   /* we want the headers be written to this file handle */
78   curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
79 
80   /* we want the body be written to this file handle instead of stdout */
81   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
82 
83   /* get it! */
84   curl_easy_perform(curl_handle);
85 
86   /* close the header file */
87   fclose(headerfile);
88 
89   /* close the body file */
90   fclose(bodyfile);
91 
92   /* cleanup curl stuff */
93   curl_easy_cleanup(curl_handle);
94 
95   return 0;
96 }
97