Artifactspublic-v0

curl

master
ply4 git calls218 ms289 KiB2 cachedprotocol v2
141 lines4.5 KiBRaw

NAME

CURLOPT_HEADERFUNCTION - callback that receives header data

SYNOPSIS

c
#include <curl/curl.h>

size_t header_callback(char *buffer,
                       size_t size,
                       size_t nitems,
                       void *userdata);

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION,
                          header_callback);

DESCRIPTION

Pass a pointer to your callback function, which should match the prototype shown above.

This callback function gets invoked by libcurl as soon as it has received header data. The header callback is called once for each header and only complete header lines are passed on to the callback. Parsing headers is easy to do using this callback. buffer points to the delivered data, and the size of that data is nitems; size is always 1. The provided header line is not null-terminated. Do not modify the passed in buffer.

The pointer named userdata is the one you set with the CURLOPT_HEADERDATA(3) option.

Your callback should return the number of bytes actually taken care of. If that amount differs from the amount passed to your callback function, it signals an error condition to the library. This causes the transfer to get aborted and the libcurl function used returns CURLE_WRITE_ERROR.

You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0)

A complete HTTP header that is passed to this function can be up to CURL_MAX_HTTP_HEADER (100K) bytes and includes the final line terminator.

If this option is not set, or if it is set to NULL, but CURLOPT_HEADERDATA(3) is set to anything but NULL, the function used to accept response data is used instead. That is the function specified with CURLOPT_WRITEFUNCTION(3), or if it is not specified or NULL - the default, stream-writing function.

It is important to note that the callback is invoked for the headers of all responses received after initiating a request and not the final response. This includes all responses which occur during authentication negotiation. If you need to operate on only the headers from the final response, you need to collect headers in the callback yourself and use HTTP status lines, for example, to delimit response boundaries.

For an HTTP transfer, the status line and the blank line preceding the response body are both included as headers and passed to this function.

When a server sends a chunked encoded transfer, it may contain a trailer. That trailer is identical to an HTTP header and if such a trailer is received it is passed to the application using this callback as well. There are several ways to detect it being a trailer and not an ordinary header: 1) it comes after the response-body. 2) it comes after the final header line (CR LF) 3) a Trailer: header among the regular response-headers mention what header(s) to expect in the trailer.

For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function gets called with the server responses to the commands that libcurl sends.

A more convenient way to get HTTP headers might be to use curl_easy_header(3).

LIMITATIONS

For legacy HTTP/1 "folded headers" (deprecated since RFC 7230), libcurl unfolds the lines before calling the header callback. A folded header is a header that continues on a subsequent line and starts with whitespace. The callback gets the full single header with one whitespace between the lines. This unfolding is done since 8.18.0.

DEFAULT

Nothing.

%PROTOCOLS%

EXAMPLE

c
static size_t header_callback(char *buffer, size_t size,
                              size_t nitems, void *userdata)
{
  /* received header is 'nitems' bytes in 'buffer' NOT null-terminated */
  /* 'userdata' is set with CURLOPT_HEADERDATA */
  return nitems;
}

int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    CURLcode result;
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");

    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);

    result = curl_easy_perform(curl);

    curl_easy_cleanup(curl);
  }
}

%AVAILABILITY%

RETURN VALUE

curl_easy_setopt(3) returns a CURLcode indicating success or error.

CURLE_OK (0) means everything was OK, non-zero means an error occurred, see libcurl-errors(3).

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
17Added-in: 7.7.2
18---
20# NAME
22CURLOPT_HEADERFUNCTION - callback that receives header data
24# SYNOPSIS
26~~~c
27#include <curl/curl.h>
29size_t header_callback(char *buffer,
30 size_t size,
31 size_t nitems,
32 void *userdata);
34CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION,
35 header_callback);
36~~~
38# DESCRIPTION
40Pass a pointer to your callback function, which should match the prototype
41shown above.
43This callback function gets invoked by libcurl as soon as it has received
44header data. The header callback is called once for each header and only
45complete header lines are passed on to the callback. Parsing headers is easy
46to do using this callback. *buffer* points to the delivered data, and the size
47of that data is *nitems*; *size* is always 1. The provided header line is not
48null-terminated. Do not modify the passed in buffer.
50The pointer named *userdata* is the one you set with the CURLOPT_HEADERDATA(3)
51option.
53Your callback should return the number of bytes actually taken care of. If
54that amount differs from the amount passed to your callback function, it
55signals an error condition to the library. This causes the transfer to get
56aborted and the libcurl function used returns *CURLE_WRITE_ERROR*.
58You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0)
60A 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.
63If this option is not set, or if it is set to NULL, but
64CURLOPT_HEADERDATA(3) is set to anything but NULL, the function used to
65accept response data is used instead. That is the function specified with
66CURLOPT_WRITEFUNCTION(3), or if it is not specified or NULL - the
67default, stream-writing function.
69It is important to note that the callback is invoked for the headers of all
70responses received after initiating a request and not the final response. This
71includes all responses which occur during authentication negotiation. If you
72need to operate on only the headers from the final response, you need to
73collect headers in the callback yourself and use HTTP status lines, for
74example, to delimit response boundaries.
76For an HTTP transfer, the status line and the blank line preceding the response
77body are both included as headers and passed to this function.
79When a server sends a chunked encoded transfer, it may contain a trailer. That
80trailer is identical to an HTTP header and if such a trailer is received it is
81passed to the application using this callback as well. There are several ways
82to detect it being a trailer and not an ordinary header: 1) it comes after the
83response-body. 2) it comes after the final header line (CR LF) 3) a Trailer:
84header among the regular response-headers mention what header(s) to expect in
85the trailer.
87For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function gets called
88with the server responses to the commands that libcurl sends.
90A more convenient way to get HTTP headers might be to use
91curl_easy_header(3).
93# LIMITATIONS
95For legacy HTTP/1 "folded headers" (deprecated since RFC 7230), libcurl
96unfolds the lines before calling the header callback. A folded header is a
97header that continues on a subsequent line and starts with whitespace. The
98callback gets the full single header with one whitespace between the lines.
99This unfolding is done since 8.18.0.
101# DEFAULT
103Nothing.
105# %PROTOCOLS%
107# EXAMPLE
109~~~c
110static size_t header_callback(char *buffer, size_t size,
111 size_t nitems, void *userdata)
113 /* received header is 'nitems' bytes in 'buffer' NOT null-terminated */
114 /* 'userdata' is set with CURLOPT_HEADERDATA */
115 return nitems;
118int main(void)
120 CURL *curl = curl_easy_init();
121 if(curl) {
122 CURLcode result;
123 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
125 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
127 result = curl_easy_perform(curl);
129 curl_easy_cleanup(curl);
130 }
132~~~
134# %AVAILABILITY%
136# RETURN VALUE
138curl_easy_setopt(3) returns a CURLcode indicating success or error.
140CURLE_OK (0) means everything was OK, non-zero means an error occurred, see
141libcurl-errors(3).
Diagnostics4 git calls · 218 ms
CallStatusSizeTime0218 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=213
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2002.1 KiB213 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2134
git.response.cache=MISS
git.response.duration_ms=213
git.fetch.wants=130f78f30bd24f42797370c72bfe7485ec41992c
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2134
git.pkt.payload.max=2113
git.sideband.band1.bytes=2112
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e3523f0b85224d
Request
3ca96adb…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format