2024-01-17 18:32:44 +08:00
|
|
|
---
|
2024-02-28 18:28:10 +08:00
|
|
|
c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2024-01-17 18:32:44 +08:00
|
|
|
SPDX-License-Identifier: curl
|
|
|
|
Title: curl_easy_header
|
|
|
|
Section: 3
|
|
|
|
Source: libcurl
|
|
|
|
See-also:
|
|
|
|
- CURLINFO_CONTENT_TYPE (3)
|
|
|
|
- CURLOPT_HEADERFUNCTION (3)
|
|
|
|
- curl_easy_nextheader (3)
|
|
|
|
- curl_easy_perform (3)
|
|
|
|
- libcurl-errors (3)
|
2024-03-21 18:50:20 +08:00
|
|
|
Protocol:
|
|
|
|
- HTTP
|
2024-01-17 18:32:44 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
# NAME
|
|
|
|
|
2022-03-29 19:58:11 +08:00
|
|
|
curl_easy_header - get an HTTP header
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# SYNOPSIS
|
|
|
|
|
|
|
|
~~~c
|
2022-03-17 17:20:19 +08:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
CURLHcode curl_easy_header(CURL *easy,
|
|
|
|
const char *name,
|
|
|
|
size_t index,
|
|
|
|
unsigned int origin,
|
|
|
|
int request,
|
|
|
|
struct curl_header **hout);
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# DESCRIPTION
|
|
|
|
|
|
|
|
curl_easy_header(3) returns a pointer to a "curl_header" struct in **hout**
|
|
|
|
with data for the HTTP response header *name*. The case insensitive
|
|
|
|
null-terminated header name should be specified without colon.
|
|
|
|
|
|
|
|
*index* 0 means asking for the first instance of the header. If the returned
|
|
|
|
header struct has **amount** set larger than 1, it means there are more
|
|
|
|
instances of the same header name available to get. Asking for a too big index
|
|
|
|
makes **CURLHE_BADINDEX** get returned.
|
|
|
|
|
|
|
|
The *origin* argument is for specifying which headers to receive, as a single
|
|
|
|
HTTP transfer might provide headers from several different places and they may
|
|
|
|
then have different importance to the user and headers using the same name
|
|
|
|
might be used. The *origin* is a bitmask for what header sources you want. See
|
|
|
|
the descriptions below.
|
|
|
|
|
|
|
|
The *request* argument tells libcurl from which request you want headers
|
2022-03-17 17:20:19 +08:00
|
|
|
from. A single transfer might consist of a series of HTTP requests and this
|
2022-04-11 17:25:13 +08:00
|
|
|
argument lets you specify which particular individual request you want the
|
2022-03-17 17:20:19 +08:00
|
|
|
headers from. 0 being the first request and then the number increases for
|
|
|
|
further redirects or when multi-state authentication is used. Passing in -1 is
|
|
|
|
a shortcut to "the last" request in the series, independently of the actual
|
|
|
|
amount of requests used.
|
|
|
|
|
|
|
|
libcurl stores and provides the actually used "correct" headers. If for
|
|
|
|
example two headers with the same name arrive and the latter overrides the
|
2023-08-22 23:40:39 +08:00
|
|
|
former, then only the latter is provided. If the first header survives the
|
|
|
|
second, then only the first one is provided. An application using this API
|
|
|
|
does not have to bother about multiple headers used wrongly.
|
2022-03-17 17:20:19 +08:00
|
|
|
|
|
|
|
The memory for the returned struct is associated with the easy handle and
|
2024-01-17 18:32:44 +08:00
|
|
|
subsequent calls to curl_easy_header(3) clobber the struct used in the
|
2023-08-22 23:40:39 +08:00
|
|
|
previous calls for the same easy handle. Applications need to copy the data if
|
|
|
|
it wants to keep it around. The memory used for the struct gets freed with
|
2024-01-17 18:32:44 +08:00
|
|
|
calling curl_easy_cleanup(3) of the easy handle.
|
2022-03-17 17:20:19 +08:00
|
|
|
|
2022-03-29 19:58:11 +08:00
|
|
|
The first line in an HTTP response is called the status line. It is not
|
2022-03-17 17:20:19 +08:00
|
|
|
considered a header by this function. Headers are the "name: value" lines
|
|
|
|
following the status.
|
|
|
|
|
|
|
|
This function can be used before (all) headers have been received and is fine
|
2023-08-22 23:40:39 +08:00
|
|
|
to call from within libcurl callbacks. It returns the state of the headers at
|
|
|
|
the time it is called.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# The header struct
|
|
|
|
|
|
|
|
~~~c
|
2022-03-17 17:20:19 +08:00
|
|
|
struct curl_header {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
size_t amount;
|
|
|
|
size_t index;
|
|
|
|
unsigned int origin;
|
|
|
|
void *anchor;
|
|
|
|
};
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
2022-03-17 17:20:19 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
The data **name** field points to, is the same as the requested name, but
|
2023-08-22 23:40:39 +08:00
|
|
|
might have a different case.
|
2022-03-17 17:20:19 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
The data **value** field points to, comes exactly as delivered over the
|
2022-03-17 17:20:19 +08:00
|
|
|
network but with leading and trailing whitespace and newlines stripped
|
2022-09-17 23:32:21 +08:00
|
|
|
off. The `value` data is null-terminated. For legacy HTTP/1 "folded headers",
|
2022-05-25 05:33:35 +08:00
|
|
|
this API provides the full single value in an unfolded manner with a single
|
|
|
|
whitespace between the lines.
|
2022-03-17 17:20:19 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
**amount** is how many headers using this name that exist, within the origin
|
2022-03-17 17:20:19 +08:00
|
|
|
and request scope asked for.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
**index** is the zero based entry number of this particular header, which in
|
2022-03-17 17:20:19 +08:00
|
|
|
case this header was used more than once in the requested scope can be larger
|
2024-01-17 18:32:44 +08:00
|
|
|
than 0 but is always less than **amount**.
|
2022-03-17 17:20:19 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
The **origin** field in the "curl_header" struct has one of the origin bits
|
2022-03-17 17:20:19 +08:00
|
|
|
set, indicating where from the header originates. At the time of this writing,
|
|
|
|
there are 5 bits with defined use. The undocumented 27 remaining bits are
|
|
|
|
reserved for future use and must not be assumed to have any particular value.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
**anchor** is a private handle used by libcurl internals. Do not modify.
|
|
|
|
|
|
|
|
# ORIGINS
|
|
|
|
|
|
|
|
## CURLH_HEADER
|
|
|
|
|
2022-03-17 17:20:19 +08:00
|
|
|
The header arrived as a header from the server.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLH_TRAILER
|
|
|
|
|
2022-03-17 17:20:19 +08:00
|
|
|
The header arrived as a trailer. A header that arrives after the body.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLH_CONNECT
|
|
|
|
|
2022-03-17 17:20:19 +08:00
|
|
|
The header arrived in a CONNECT response. A CONNECT request is being done to
|
2022-03-29 19:58:11 +08:00
|
|
|
setup a transfer "through" an HTTP(S) proxy.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLH_1XX
|
|
|
|
|
2022-03-29 19:58:11 +08:00
|
|
|
The header arrived in an HTTP 1xx response. A 1xx response is an "intermediate"
|
2022-03-17 17:20:19 +08:00
|
|
|
response that might happen before the "real" response.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
## CURLH_PSEUDO
|
|
|
|
|
2022-03-29 19:58:11 +08:00
|
|
|
The header is an HTTP/2 or HTTP/3 pseudo header
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# EXAMPLE
|
|
|
|
|
|
|
|
~~~c
|
2023-12-04 17:50:42 +08:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
struct curl_header *type;
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
if(curl) {
|
|
|
|
CURLHcode h;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
|
|
|
|
curl_easy_perform(curl);
|
|
|
|
h = curl_easy_header(curl, "Content-Type", 0, CURLH_HEADER, -1, &type);
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 18:32:44 +08:00
|
|
|
~~~
|
|
|
|
|
|
|
|
# AVAILABILITY
|
|
|
|
|
2022-05-30 20:13:48 +08:00
|
|
|
Added in 7.83.0. Officially supported since 7.84.0.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# RETURN VALUE
|
|
|
|
|
2022-04-11 17:25:13 +08:00
|
|
|
This function returns a CURLHcode indicating success or error.
|