1--- 2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al. 3SPDX-License-Identifier: curl 4Title: CURLOPT_HEADERFUNCTION 5Section: 3 6Source: libcurl 7See-also: 8 - CURLOPT_HEADERDATA (3) 9 - CURLOPT_WRITEFUNCTION (3) 10 - curl_easy_header (3) 11--- 12 13# NAME 14 15CURLOPT_HEADERFUNCTION - callback that receives header data 16 17# SYNOPSIS 18 19~~~c 20#include <curl/curl.h> 21 22size_t header_callback(char *buffer, 23 size_t size, 24 size_t nitems, 25 void *userdata); 26 27CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION, 28 header_callback); 29~~~ 30 31# DESCRIPTION 32 33Pass a pointer to your callback function, which should match the prototype 34shown above. 35 36This callback function gets invoked by libcurl as soon as it has received 37header data. The header callback is called once for each header and only 38complete header lines are passed on to the callback. Parsing headers is easy 39to do using this callback. *buffer* points to the delivered data, and the 40size of that data is *nitems*; *size* is always 1. The provide header 41line is not null-terminated! 42 43The pointer named *userdata* is the one you set with the 44CURLOPT_HEADERDATA(3) option. 45 46Your callback should return the number of bytes actually taken care of. If 47that amount differs from the amount passed to your callback function, it 48signals an error condition to the library. This causes the transfer to get 49aborted and the libcurl function used returns *CURLE_WRITE_ERROR*. 50 51You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0) 52 53A complete HTTP header that is passed to this function can be up to 54*CURL_MAX_HTTP_HEADER* (100K) bytes and includes the final line terminator. 55 56If this option is not set, or if it is set to NULL, but 57CURLOPT_HEADERDATA(3) is set to anything but NULL, the function used to 58accept response data is used instead. That is the function specified with 59CURLOPT_WRITEFUNCTION(3), or if it is not specified or NULL - the 60default, stream-writing function. 61 62It is important to note that the callback is invoked for the headers of all 63responses received after initiating a request and not just the final 64response. This includes all responses which occur during authentication 65negotiation. If you need to operate on only the headers from the final 66response, you need to collect headers in the callback yourself and use HTTP 67status lines, for example, to delimit response boundaries. 68 69For an HTTP transfer, the status line and the blank line preceding the response 70body are both included as headers and passed to this function. 71 72When a server sends a chunked encoded transfer, it may contain a trailer. That 73trailer is identical to an HTTP header and if such a trailer is received it is 74passed to the application using this callback as well. There are several ways 75to detect it being a trailer and not an ordinary header: 1) it comes after the 76response-body. 2) it comes after the final header line (CR LF) 3) a Trailer: 77header among the regular response-headers mention what header(s) to expect in 78the trailer. 79 80For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function gets called 81with the server responses to the commands that libcurl sends. 82 83A more convenient way to get HTTP headers might be to use 84curl_easy_header(3). 85 86# LIMITATIONS 87 88libcurl does not unfold HTTP "folded headers" (deprecated since RFC 7230). A 89folded header is a header that continues on a subsequent line and starts with 90a whitespace. Such folds are passed to the header callback as separate ones, 91although strictly they are just continuations of the previous lines. 92 93# DEFAULT 94 95Nothing. 96 97# PROTOCOLS 98 99Used for all protocols with headers or meta-data concept: HTTP, FTP, POP3, 100IMAP, SMTP and more. 101 102# EXAMPLE 103 104~~~c 105static size_t header_callback(char *buffer, size_t size, 106 size_t nitems, void *userdata) 107{ 108 /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */ 109 /* 'userdata' is set with CURLOPT_HEADERDATA */ 110 return nitems * size; 111} 112 113int main(void) 114{ 115 CURL *curl = curl_easy_init(); 116 if(curl) { 117 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 118 119 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback); 120 121 curl_easy_perform(curl); 122 } 123} 124~~~ 125 126# AVAILABILITY 127 128Always 129 130# RETURN VALUE 131 132Returns CURLE_OK 133