2
0

sepheaders.c 2.7 KB

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