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