sepheaders.c 2.7 KB

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