113498266Sopenharmony_ci---
213498266Sopenharmony_cic: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
313498266Sopenharmony_ciSPDX-License-Identifier: curl
413498266Sopenharmony_ciTitle: curl_easy_nextheader
513498266Sopenharmony_ciSection: 3
613498266Sopenharmony_ciSource: libcurl
713498266Sopenharmony_ciSee-also:
813498266Sopenharmony_ci  - curl_easy_header (3)
913498266Sopenharmony_ci  - curl_easy_perform (3)
1013498266Sopenharmony_ci---
1113498266Sopenharmony_ci
1213498266Sopenharmony_ci# NAME
1313498266Sopenharmony_ci
1413498266Sopenharmony_cicurl_easy_nextheader - get the next HTTP header
1513498266Sopenharmony_ci
1613498266Sopenharmony_ci# SYNOPSIS
1713498266Sopenharmony_ci
1813498266Sopenharmony_ci~~~c
1913498266Sopenharmony_ci#include <curl/curl.h>
2013498266Sopenharmony_ci
2113498266Sopenharmony_cistruct curl_header *curl_easy_nextheader(CURL *easy,
2213498266Sopenharmony_ci                                         unsigned int origin,
2313498266Sopenharmony_ci                                         int request,
2413498266Sopenharmony_ci                                         struct curl_header *prev);
2513498266Sopenharmony_ci~~~
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci# DESCRIPTION
2813498266Sopenharmony_ci
2913498266Sopenharmony_ciThis function lets an application iterate over all previously received HTTP
3013498266Sopenharmony_ciheaders.
3113498266Sopenharmony_ci
3213498266Sopenharmony_ciThe *origin* argument is for specifying which headers to receive, as a single
3313498266Sopenharmony_ciHTTP transfer might provide headers from several different places and they may
3413498266Sopenharmony_cithen have different importance to the user and headers using the same name
3513498266Sopenharmony_cimight be used. The *origin* is a bitmask for what header sources you want. See
3613498266Sopenharmony_cithe curl_easy_header(3) man page for the origin descriptions.
3713498266Sopenharmony_ci
3813498266Sopenharmony_ciThe *request* argument tells libcurl from which request you want headers
3913498266Sopenharmony_cifrom. A single transfer might consist of a series of HTTP requests and this
4013498266Sopenharmony_ciargument lets you specify which particular individual request you want the
4113498266Sopenharmony_ciheaders from. 0 being the first request and then the number increases for
4213498266Sopenharmony_cifurther redirects or when multi-state authentication is used. Passing in -1 is
4313498266Sopenharmony_cia shortcut to "the last" request in the series, independently of the actual
4413498266Sopenharmony_ciamount of requests used.
4513498266Sopenharmony_ci
4613498266Sopenharmony_ciIt is suggested that you pass in the same **origin** and **request** when
4713498266Sopenharmony_ciiterating over a range of headers as changing the value mid-loop might give
4813498266Sopenharmony_ciyou unexpected results.
4913498266Sopenharmony_ci
5013498266Sopenharmony_ciIf *prev* is NULL, this function returns a pointer to the first header stored
5113498266Sopenharmony_ciwithin the given scope (origin + request).
5213498266Sopenharmony_ci
5313498266Sopenharmony_ciIf *prev* is a pointer to a previously returned header struct,
5413498266Sopenharmony_cicurl_easy_nextheader(3) returns a pointer the next header stored within the
5513498266Sopenharmony_cigiven scope. This way, an application can iterate over all available headers.
5613498266Sopenharmony_ci
5713498266Sopenharmony_ciThe memory for the struct this points to, is owned and managed by libcurl and
5813498266Sopenharmony_ciis associated with the easy handle. Applications must copy the data if they
5913498266Sopenharmony_ciwant it to survive subsequent API calls or the life-time of the easy handle.
6013498266Sopenharmony_ci
6113498266Sopenharmony_ci# EXAMPLE
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci~~~c
6413498266Sopenharmony_ciint main(void)
6513498266Sopenharmony_ci{
6613498266Sopenharmony_ci  struct curl_header *prev = NULL;
6713498266Sopenharmony_ci  struct curl_header *h;
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci  CURL *curl = curl_easy_init();
7013498266Sopenharmony_ci  if(curl) {
7113498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
7213498266Sopenharmony_ci    curl_easy_perform(curl);
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci    /* extract the normal headers from the first request */
7513498266Sopenharmony_ci    while((h = curl_easy_nextheader(curl, CURLH_HEADER, 0, prev))) {
7613498266Sopenharmony_ci      printf("%s: %s\n", h->name, h->value);
7713498266Sopenharmony_ci      prev = h;
7813498266Sopenharmony_ci    }
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci    /* extract the normal headers + 1xx + trailers from the last request */
8113498266Sopenharmony_ci    unsigned int origin = CURLH_HEADER| CURLH_1XX | CURLH_TRAILER;
8213498266Sopenharmony_ci    while((h = curl_easy_nextheader(curl, origin, -1, prev))) {
8313498266Sopenharmony_ci      printf("%s: %s\n", h->name, h->value);
8413498266Sopenharmony_ci      prev = h;
8513498266Sopenharmony_ci    }
8613498266Sopenharmony_ci  }
8713498266Sopenharmony_ci}
8813498266Sopenharmony_ci~~~
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci# AVAILABILITY
9113498266Sopenharmony_ci
9213498266Sopenharmony_ciAdded in 7.83.0. Officially supported since 7.84.0.
9313498266Sopenharmony_ci
9413498266Sopenharmony_ci# RETURN VALUE
9513498266Sopenharmony_ci
9613498266Sopenharmony_ciThis function returns the next header, or NULL when there are no more
9713498266Sopenharmony_ci(matching) headers or an error occurred.
9813498266Sopenharmony_ci
9913498266Sopenharmony_ciIf this function returns NULL when *prev* was set to NULL, then there are no
10013498266Sopenharmony_ciheaders available within the scope to return.
101