1e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/***************************************************************************
29bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels *                                  _   _ ____  _
39bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels *  Project                     ___| | | |  _ \| |
49bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels *                             / __| | | | |_) | |
59bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels *                            | (__| |_| |  _ <| |___
69bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels *                             \___|\___/|_| \_\_____|
79bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels *
8d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
10e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is licensed as described in the file COPYING, which
11e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * you should have received as part of this distribution. The terms
12d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * are also available at https://curl.haxx.se/docs/copyright.html.
13e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
14e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * copies of the Software, and permit persons to whom the Software is
16e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * furnished to do so, under the terms of the COPYING file.
17e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
18e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * KIND, either express or implied.
20e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
21e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET ***************************************************************************/
22d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo/* <DESC>
23d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * Simple HTTP GET that stores the headers in a separate file
24d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * </DESC>
25d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo */
269bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels#include <stdio.h>
279bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels#include <stdlib.h>
289bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels#include <unistd.h>
299bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
309bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels#include <curl/curl.h>
319bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
329bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckelsstatic size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
339bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels{
34d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
359bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  return written;
369bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels}
379bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
38e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETint main(void)
399bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels{
409bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  CURL *curl_handle;
419bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  static const char *headerfilename = "head.out";
429bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  FILE *headerfile;
439bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  static const char *bodyfilename = "body.out";
449bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  FILE *bodyfile;
459bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
469bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  curl_global_init(CURL_GLOBAL_ALL);
479bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
489bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  /* init the curl session */
499bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  curl_handle = curl_easy_init();
509bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
519bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  /* set URL to get */
529bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");
539bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
549bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  /* no progress meter please */
559bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
569bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
579bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  /* send all data to this function  */
589bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
599bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
60e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* open the header file */
61e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  headerfile = fopen(headerfilename, "wb");
62e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  if(!headerfile) {
639bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    curl_easy_cleanup(curl_handle);
649bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    return -1;
659bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  }
66e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
67e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* open the body file */
68e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  bodyfile = fopen(bodyfilename, "wb");
69e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  if(!bodyfile) {
709bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    curl_easy_cleanup(curl_handle);
71e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fclose(headerfile);
729bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels    return -1;
739bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  }
749bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
75e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* we want the headers be written to this file handle */
76e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
779bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
78e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* we want the body be written to this file handle instead of stdout */
79e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
809bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
819bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  /* get it! */
829bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  curl_easy_perform(curl_handle);
839bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
849bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  /* close the header file */
859bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  fclose(headerfile);
869bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
87e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* close the body file */
88e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  fclose(bodyfile);
89e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
909bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  /* cleanup curl stuff */
919bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  curl_easy_cleanup(curl_handle);
929bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels
939bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels  return 0;
949bd90e6e25f1e55f50201c87a1b5837de7e5b64aLucas Eckels}
95