CURLOPT_HEADERFUNCTION.3 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. .\" generated by cd2nroff 0.1 from CURLOPT_HEADERFUNCTION.md
  2. .TH CURLOPT_HEADERFUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_HEADERFUNCTION \- callback that receives header data
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. size_t header_callback(char *buffer,
  9. size_t size,
  10. size_t nitems,
  11. void *userdata);
  12. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION,
  13. header_callback);
  14. .fi
  15. .SH DESCRIPTION
  16. Pass a pointer to your callback function, which should match the prototype
  17. shown above.
  18. This callback function gets invoked by libcurl as soon as it has received
  19. header data. The header callback is called once for each header and only
  20. complete header lines are passed on to the callback. Parsing headers is easy
  21. to do using this callback. \fIbuffer\fP points to the delivered data, and the
  22. size of that data is \fInitems\fP; \fIsize\fP is always 1. The provide header
  23. line is not null\-terminated!
  24. The pointer named \fIuserdata\fP is the one you set with the
  25. \fICURLOPT_HEADERDATA(3)\fP option.
  26. Your callback should return the number of bytes actually taken care of. If
  27. that amount differs from the amount passed to your callback function, it
  28. signals an error condition to the library. This causes the transfer to get
  29. aborted and the libcurl function used returns \fICURLE_WRITE_ERROR\fP.
  30. You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0)
  31. A complete HTTP header that is passed to this function can be up to
  32. \fICURL_MAX_HTTP_HEADER\fP (100K) bytes and includes the final line terminator.
  33. If this option is not set, or if it is set to NULL, but
  34. \fICURLOPT_HEADERDATA(3)\fP is set to anything but NULL, the function used to
  35. accept response data is used instead. That is the function specified with
  36. \fICURLOPT_WRITEFUNCTION(3)\fP, or if it is not specified or NULL \- the
  37. default, stream\-writing function.
  38. It is important to note that the callback is invoked for the headers of all
  39. responses received after initiating a request and not just the final
  40. response. This includes all responses which occur during authentication
  41. negotiation. If you need to operate on only the headers from the final
  42. response, you need to collect headers in the callback yourself and use HTTP
  43. status lines, for example, to delimit response boundaries.
  44. For an HTTP transfer, the status line and the blank line preceding the response
  45. body are both included as headers and passed to this function.
  46. When a server sends a chunked encoded transfer, it may contain a trailer. That
  47. trailer is identical to an HTTP header and if such a trailer is received it is
  48. passed to the application using this callback as well. There are several ways
  49. to detect it being a trailer and not an ordinary header: 1) it comes after the
  50. response\-body. 2) it comes after the final header line (CR LF) 3) a Trailer:
  51. header among the regular response\-headers mention what header(s) to expect in
  52. the trailer.
  53. For non\-HTTP protocols like FTP, POP3, IMAP and SMTP this function gets called
  54. with the server responses to the commands that libcurl sends.
  55. A more convenient way to get HTTP headers might be to use
  56. \fIcurl_easy_header(3)\fP.
  57. .SH LIMITATIONS
  58. libcurl does not unfold HTTP "folded headers" (deprecated since RFC 7230). A
  59. folded header is a header that continues on a subsequent line and starts with
  60. a whitespace. Such folds are passed to the header callback as separate ones,
  61. although strictly they are just continuations of the previous lines.
  62. .SH DEFAULT
  63. Nothing.
  64. .SH PROTOCOLS
  65. This functionality affects ftp, http, imap, pop3 and smtp
  66. .SH EXAMPLE
  67. .nf
  68. static size_t header_callback(char *buffer, size_t size,
  69. size_t nitems, void *userdata)
  70. {
  71. /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
  72. /* 'userdata' is set with CURLOPT_HEADERDATA */
  73. return nitems * size;
  74. }
  75. int main(void)
  76. {
  77. CURL *curl = curl_easy_init();
  78. if(curl) {
  79. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  80. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
  81. curl_easy_perform(curl);
  82. }
  83. }
  84. .fi
  85. .SH AVAILABILITY
  86. Added in curl 7.7.2
  87. .SH RETURN VALUE
  88. Returns CURLE_OK
  89. .SH SEE ALSO
  90. .BR CURLOPT_HEADERDATA (3),
  91. .BR CURLOPT_WRITEFUNCTION (3),
  92. .BR curl_easy_header (3)