1*6236dae4SAndroid Build Coastguard Worker--- 2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al. 3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl 4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_HEADERFUNCTION 5*6236dae4SAndroid Build Coastguard WorkerSection: 3 6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl 7*6236dae4SAndroid Build Coastguard WorkerSee-also: 8*6236dae4SAndroid Build Coastguard Worker - CURLOPT_HEADERDATA (3) 9*6236dae4SAndroid Build Coastguard Worker - CURLOPT_WRITEFUNCTION (3) 10*6236dae4SAndroid Build Coastguard Worker - curl_easy_header (3) 11*6236dae4SAndroid Build Coastguard WorkerProtocol: 12*6236dae4SAndroid Build Coastguard Worker - HTTP 13*6236dae4SAndroid Build Coastguard Worker - FTP 14*6236dae4SAndroid Build Coastguard Worker - POP3 15*6236dae4SAndroid Build Coastguard Worker - IMAP 16*6236dae4SAndroid Build Coastguard Worker - SMTP 17*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.7.2 18*6236dae4SAndroid Build Coastguard Worker--- 19*6236dae4SAndroid Build Coastguard Worker 20*6236dae4SAndroid Build Coastguard Worker# NAME 21*6236dae4SAndroid Build Coastguard Worker 22*6236dae4SAndroid Build Coastguard WorkerCURLOPT_HEADERFUNCTION - callback that receives header data 23*6236dae4SAndroid Build Coastguard Worker 24*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS 25*6236dae4SAndroid Build Coastguard Worker 26*6236dae4SAndroid Build Coastguard Worker~~~c 27*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h> 28*6236dae4SAndroid Build Coastguard Worker 29*6236dae4SAndroid Build Coastguard Workersize_t header_callback(char *buffer, 30*6236dae4SAndroid Build Coastguard Worker size_t size, 31*6236dae4SAndroid Build Coastguard Worker size_t nitems, 32*6236dae4SAndroid Build Coastguard Worker void *userdata); 33*6236dae4SAndroid Build Coastguard Worker 34*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION, 35*6236dae4SAndroid Build Coastguard Worker header_callback); 36*6236dae4SAndroid Build Coastguard Worker~~~ 37*6236dae4SAndroid Build Coastguard Worker 38*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION 39*6236dae4SAndroid Build Coastguard Worker 40*6236dae4SAndroid Build Coastguard WorkerPass a pointer to your callback function, which should match the prototype 41*6236dae4SAndroid Build Coastguard Workershown above. 42*6236dae4SAndroid Build Coastguard Worker 43*6236dae4SAndroid Build Coastguard WorkerThis callback function gets invoked by libcurl as soon as it has received 44*6236dae4SAndroid Build Coastguard Workerheader data. The header callback is called once for each header and only 45*6236dae4SAndroid Build Coastguard Workercomplete header lines are passed on to the callback. Parsing headers is easy 46*6236dae4SAndroid Build Coastguard Workerto do using this callback. *buffer* points to the delivered data, and the size 47*6236dae4SAndroid Build Coastguard Workerof that data is *nitems*; *size* is always 1. The provided header line is not 48*6236dae4SAndroid Build Coastguard Workernull-terminated. Do not modify the passed in buffer. 49*6236dae4SAndroid Build Coastguard Worker 50*6236dae4SAndroid Build Coastguard WorkerThe pointer named *userdata* is the one you set with the CURLOPT_HEADERDATA(3) 51*6236dae4SAndroid Build Coastguard Workeroption. 52*6236dae4SAndroid Build Coastguard Worker 53*6236dae4SAndroid Build Coastguard WorkerYour callback should return the number of bytes actually taken care of. If 54*6236dae4SAndroid Build Coastguard Workerthat amount differs from the amount passed to your callback function, it 55*6236dae4SAndroid Build Coastguard Workersignals an error condition to the library. This causes the transfer to get 56*6236dae4SAndroid Build Coastguard Workeraborted and the libcurl function used returns *CURLE_WRITE_ERROR*. 57*6236dae4SAndroid Build Coastguard Worker 58*6236dae4SAndroid Build Coastguard WorkerYou can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0) 59*6236dae4SAndroid Build Coastguard Worker 60*6236dae4SAndroid Build Coastguard WorkerA complete HTTP header that is passed to this function can be up to 61*6236dae4SAndroid Build Coastguard Worker*CURL_MAX_HTTP_HEADER* (100K) bytes and includes the final line terminator. 62*6236dae4SAndroid Build Coastguard Worker 63*6236dae4SAndroid Build Coastguard WorkerIf this option is not set, or if it is set to NULL, but 64*6236dae4SAndroid Build Coastguard WorkerCURLOPT_HEADERDATA(3) is set to anything but NULL, the function used to 65*6236dae4SAndroid Build Coastguard Workeraccept response data is used instead. That is the function specified with 66*6236dae4SAndroid Build Coastguard WorkerCURLOPT_WRITEFUNCTION(3), or if it is not specified or NULL - the 67*6236dae4SAndroid Build Coastguard Workerdefault, stream-writing function. 68*6236dae4SAndroid Build Coastguard Worker 69*6236dae4SAndroid Build Coastguard WorkerIt is important to note that the callback is invoked for the headers of all 70*6236dae4SAndroid Build Coastguard Workerresponses received after initiating a request and not just the final 71*6236dae4SAndroid Build Coastguard Workerresponse. This includes all responses which occur during authentication 72*6236dae4SAndroid Build Coastguard Workernegotiation. If you need to operate on only the headers from the final 73*6236dae4SAndroid Build Coastguard Workerresponse, you need to collect headers in the callback yourself and use HTTP 74*6236dae4SAndroid Build Coastguard Workerstatus lines, for example, to delimit response boundaries. 75*6236dae4SAndroid Build Coastguard Worker 76*6236dae4SAndroid Build Coastguard WorkerFor an HTTP transfer, the status line and the blank line preceding the response 77*6236dae4SAndroid Build Coastguard Workerbody are both included as headers and passed to this function. 78*6236dae4SAndroid Build Coastguard Worker 79*6236dae4SAndroid Build Coastguard WorkerWhen a server sends a chunked encoded transfer, it may contain a trailer. That 80*6236dae4SAndroid Build Coastguard Workertrailer is identical to an HTTP header and if such a trailer is received it is 81*6236dae4SAndroid Build Coastguard Workerpassed to the application using this callback as well. There are several ways 82*6236dae4SAndroid Build Coastguard Workerto detect it being a trailer and not an ordinary header: 1) it comes after the 83*6236dae4SAndroid Build Coastguard Workerresponse-body. 2) it comes after the final header line (CR LF) 3) a Trailer: 84*6236dae4SAndroid Build Coastguard Workerheader among the regular response-headers mention what header(s) to expect in 85*6236dae4SAndroid Build Coastguard Workerthe trailer. 86*6236dae4SAndroid Build Coastguard Worker 87*6236dae4SAndroid Build Coastguard WorkerFor non-HTTP protocols like FTP, POP3, IMAP and SMTP this function gets called 88*6236dae4SAndroid Build Coastguard Workerwith the server responses to the commands that libcurl sends. 89*6236dae4SAndroid Build Coastguard Worker 90*6236dae4SAndroid Build Coastguard WorkerA more convenient way to get HTTP headers might be to use 91*6236dae4SAndroid Build Coastguard Workercurl_easy_header(3). 92*6236dae4SAndroid Build Coastguard Worker 93*6236dae4SAndroid Build Coastguard Worker# LIMITATIONS 94*6236dae4SAndroid Build Coastguard Worker 95*6236dae4SAndroid Build Coastguard Workerlibcurl does not unfold HTTP "folded headers" (deprecated since RFC 7230). A 96*6236dae4SAndroid Build Coastguard Workerfolded header is a header that continues on a subsequent line and starts with 97*6236dae4SAndroid Build Coastguard Workera whitespace. Such folds are passed to the header callback as separate ones, 98*6236dae4SAndroid Build Coastguard Workeralthough strictly they are just continuations of the previous lines. 99*6236dae4SAndroid Build Coastguard Worker 100*6236dae4SAndroid Build Coastguard Worker# DEFAULT 101*6236dae4SAndroid Build Coastguard Worker 102*6236dae4SAndroid Build Coastguard WorkerNothing. 103*6236dae4SAndroid Build Coastguard Worker 104*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS% 105*6236dae4SAndroid Build Coastguard Worker 106*6236dae4SAndroid Build Coastguard Worker# EXAMPLE 107*6236dae4SAndroid Build Coastguard Worker 108*6236dae4SAndroid Build Coastguard Worker~~~c 109*6236dae4SAndroid Build Coastguard Workerstatic size_t header_callback(char *buffer, size_t size, 110*6236dae4SAndroid Build Coastguard Worker size_t nitems, void *userdata) 111*6236dae4SAndroid Build Coastguard Worker{ 112*6236dae4SAndroid Build Coastguard Worker /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */ 113*6236dae4SAndroid Build Coastguard Worker /* 'userdata' is set with CURLOPT_HEADERDATA */ 114*6236dae4SAndroid Build Coastguard Worker return nitems * size; 115*6236dae4SAndroid Build Coastguard Worker} 116*6236dae4SAndroid Build Coastguard Worker 117*6236dae4SAndroid Build Coastguard Workerint main(void) 118*6236dae4SAndroid Build Coastguard Worker{ 119*6236dae4SAndroid Build Coastguard Worker CURL *curl = curl_easy_init(); 120*6236dae4SAndroid Build Coastguard Worker if(curl) { 121*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 122*6236dae4SAndroid Build Coastguard Worker 123*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback); 124*6236dae4SAndroid Build Coastguard Worker 125*6236dae4SAndroid Build Coastguard Worker curl_easy_perform(curl); 126*6236dae4SAndroid Build Coastguard Worker } 127*6236dae4SAndroid Build Coastguard Worker} 128*6236dae4SAndroid Build Coastguard Worker~~~ 129*6236dae4SAndroid Build Coastguard Worker 130*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY% 131*6236dae4SAndroid Build Coastguard Worker 132*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE 133*6236dae4SAndroid Build Coastguard Worker 134*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK 135