CURLOPT_HEADERFUNCTION.3 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. .\"
  25. .TH CURLOPT_HEADERFUNCTION 3 "January 02, 2023" "libcurl 7.88.1" "curl_easy_setopt options"
  26. .SH NAME
  27. CURLOPT_HEADERFUNCTION \- callback that receives header data
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/curl.h>
  31. size_t header_callback(char *buffer,
  32. size_t size,
  33. size_t nitems,
  34. void *userdata);
  35. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION,
  36. header_callback);
  37. .fi
  38. .SH DESCRIPTION
  39. Pass a pointer to your callback function, which should match the prototype
  40. shown above.
  41. This function gets called by libcurl as soon as it has received header
  42. data. The header callback will be called once for each header and only
  43. complete header lines are passed on to the callback. Parsing headers is easy
  44. to do using this callback. \fIbuffer\fP points to the delivered data, and the
  45. size of that data is \fInitems\fP; \fIsize\fP is always 1. Do not assume that
  46. the header line is null-terminated!
  47. The pointer named \fIuserdata\fP is the one you set with the
  48. \fICURLOPT_HEADERDATA(3)\fP option.
  49. Your callback should return the number of bytes actually taken care of. If
  50. that amount differs from the amount passed to your callback function, it will
  51. signal an error condition to the library. This will cause the transfer to get
  52. aborted and the libcurl function used will return \fICURLE_WRITE_ERROR\fP.
  53. You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0)
  54. A complete HTTP header that is passed to this function can be up to
  55. \fICURL_MAX_HTTP_HEADER\fP (100K) bytes and includes the final line terminator.
  56. If this option is not set, or if it is set to NULL, but
  57. \fICURLOPT_HEADERDATA(3)\fP is set to anything but NULL, the function used to
  58. accept response data will be used instead. That is, it will be the function
  59. specified with \fICURLOPT_WRITEFUNCTION(3)\fP, or if it is not specified or
  60. NULL - the default, stream-writing function.
  61. It's important to note that the callback will be invoked for the headers of
  62. all responses received after initiating a request and not just the final
  63. response. This includes all responses which occur during authentication
  64. negotiation. If you need to operate on only the headers from the final
  65. response, you will need to collect headers in the callback yourself and use
  66. HTTP status lines, for example, to delimit response boundaries.
  67. For an HTTP transfer, the status line and the blank line preceding the response
  68. body are both included as headers and passed to this function.
  69. When a server sends a chunked encoded transfer, it may contain a trailer. That
  70. trailer is identical to an HTTP header and if such a trailer is received it is
  71. passed to the application using this callback as well. There are several ways
  72. to detect it being a trailer and not an ordinary header: 1) it comes after the
  73. response-body. 2) it comes after the final header line (CR LF) 3) a Trailer:
  74. header among the regular response-headers mention what header(s) to expect in
  75. the trailer.
  76. For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function will get
  77. called with the server responses to the commands that libcurl sends.
  78. .SH LIMITATIONS
  79. libcurl does not unfold HTTP "folded headers" (deprecated since RFC 7230). A
  80. folded header is a header that continues on a subsequent line and starts with
  81. a whitespace. Such folds will be passed to the header callback as a separate
  82. one, although strictly it is just a continuation of the previous line.
  83. .SH DEFAULT
  84. Nothing.
  85. .SH PROTOCOLS
  86. Used for all protocols with headers or meta-data concept: HTTP, FTP, POP3,
  87. IMAP, SMTP and more.
  88. .SH EXAMPLE
  89. .nf
  90. static size_t header_callback(char *buffer, size_t size,
  91. size_t nitems, void *userdata)
  92. {
  93. /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
  94. /* 'userdata' is set with CURLOPT_HEADERDATA */
  95. return nitems * size;
  96. }
  97. CURL *curl = curl_easy_init();
  98. if(curl) {
  99. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  100. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
  101. curl_easy_perform(curl);
  102. }
  103. .fi
  104. .SH AVAILABILITY
  105. Always
  106. .SH RETURN VALUE
  107. Returns CURLE_OK
  108. .SH "SEE ALSO"
  109. .BR curl_easy_header "(3), "
  110. .BR CURLOPT_HEADERDATA "(3), " CURLOPT_WRITEFUNCTION "(3), "