sepheaders.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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  * Simple HTTP GET that stores the headers in a separate file
24  * </DESC>
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 
30 #include <curl/curl.h>
31 
32 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
33 {
34  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
35  return written;
36 }
37 
38 int main(void)
39 {
41  static const char *headerfilename = "head.out";
42  FILE *headerfile;
43  static const char *bodyfilename = "body.out";
44  FILE *bodyfile;
45 
47 
48  /* init the curl session */
49  curl_handle = curl_easy_init();
50 
51  /* set URL to get */
52  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");
53 
54  /* no progress meter please */
55  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
56 
57  /* send all data to this function */
58  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
59 
60  /* open the header file */
61  headerfile = fopen(headerfilename, "wb");
62  if(!headerfile) {
63  curl_easy_cleanup(curl_handle);
64  return -1;
65  }
66 
67  /* open the body file */
68  bodyfile = fopen(bodyfilename, "wb");
69  if(!bodyfile) {
70  curl_easy_cleanup(curl_handle);
71  fclose(headerfile);
72  return -1;
73  }
74 
75  /* we want the headers be written to this file handle */
76  curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
77 
78  /* we want the body be written to this file handle instead of stdout */
79  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
80 
81  /* get it! */
82  curl_easy_perform(curl_handle);
83 
84  /* close the header file */
85  fclose(headerfile);
86 
87  /* close the body file */
88  fclose(bodyfile);
89 
90  /* cleanup curl stuff */
91  curl_easy_cleanup(curl_handle);
92 
93  return 0;
94 }
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
Definition: sepheaders.c:32
UNITTEST_START char * ptr
Definition: unit1330.c:38
int main(void)
Definition: sepheaders.c:38
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURLM * curl_handle
Definition: multi-uv.c:41
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURL
Definition: curl.h:102
size_t size
Definition: unit1302.c:52
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
size_t fwrite(const void *, size_t, size_t, FILE *)
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16