1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2014, 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 http://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 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include <curl/curl.h>
27
write_data(void * ptr,size_t size,size_t nmemb,void * stream)28 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
29 {
30 int written = fwrite(ptr, size, nmemb, (FILE *)stream);
31 return written;
32 }
33
main(void)34 int main(void)
35 {
36 CURL *curl_handle;
37 static const char *headerfilename = "head.out";
38 FILE *headerfile;
39 static const char *bodyfilename = "body.out";
40 FILE *bodyfile;
41
42 curl_global_init(CURL_GLOBAL_ALL);
43
44 /* init the curl session */
45 curl_handle = curl_easy_init();
46
47 /* set URL to get */
48 curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");
49
50 /* no progress meter please */
51 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
52
53 /* send all data to this function */
54 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
55
56 /* open the header file */
57 headerfile = fopen(headerfilename, "wb");
58 if(!headerfile) {
59 curl_easy_cleanup(curl_handle);
60 return -1;
61 }
62
63 /* open the body file */
64 bodyfile = fopen(bodyfilename, "wb");
65 if(!bodyfile) {
66 curl_easy_cleanup(curl_handle);
67 fclose(headerfile);
68 return -1;
69 }
70
71 /* we want the headers be written to this file handle */
72 curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
73
74 /* we want the body be written to this file handle instead of stdout */
75 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
76
77 /* get it! */
78 curl_easy_perform(curl_handle);
79
80 /* close the header file */
81 fclose(headerfile);
82
83 /* close the body file */
84 fclose(bodyfile);
85
86 /* cleanup curl stuff */
87 curl_easy_cleanup(curl_handle);
88
89 return 0;
90 }
91