2
0

sepheaders.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <curl/curl.h>
  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. int main(void)
  38. {
  39. CURL *curl_handle;
  40. static const char *headerfilename = "head.out";
  41. FILE *headerfile;
  42. static const char *bodyfilename = "body.out";
  43. FILE *bodyfile;
  44. curl_global_init(CURL_GLOBAL_ALL);
  45. /* init the curl session */
  46. curl_handle = curl_easy_init();
  47. /* set URL to get */
  48. curl_easy_setopt(curl_handle, CURLOPT_URL, "https://example.com");
  49. /* no progress meter please */
  50. curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
  51. /* send all data to this function */
  52. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
  53. /* open the header file */
  54. headerfile = fopen(headerfilename, "wb");
  55. if(!headerfile) {
  56. curl_easy_cleanup(curl_handle);
  57. return -1;
  58. }
  59. /* open the body file */
  60. bodyfile = fopen(bodyfilename, "wb");
  61. if(!bodyfile) {
  62. curl_easy_cleanup(curl_handle);
  63. fclose(headerfile);
  64. return -1;
  65. }
  66. /* we want the headers be written to this file handle */
  67. curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
  68. /* we want the body be written to this file handle instead of stdout */
  69. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
  70. /* get it! */
  71. curl_easy_perform(curl_handle);
  72. /* close the header file */
  73. fclose(headerfile);
  74. /* close the body file */
  75. fclose(bodyfile);
  76. /* cleanup curl stuff */
  77. curl_easy_cleanup(curl_handle);
  78. return 0;
  79. }