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