curl_easy_nextheader.md (3291B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: curl_easy_nextheader 5 Section: 3 6 Source: libcurl 7 See-also: 8 - curl_easy_header (3) 9 - curl_easy_perform (3) 10 Protocol: 11 - HTTP 12 Added-in: 7.83.0 13 --- 14 15 # NAME 16 17 curl_easy_nextheader - get the next HTTP header 18 19 # SYNOPSIS 20 21 ~~~c 22 #include <curl/curl.h> 23 24 struct curl_header *curl_easy_nextheader(CURL *easy, 25 unsigned int origin, 26 int request, 27 struct curl_header *prev); 28 ~~~ 29 30 # DESCRIPTION 31 32 This function lets an application iterate over all previously received HTTP 33 headers. 34 35 The *origin* argument is for specifying which headers to receive, as a single 36 HTTP transfer might provide headers from several different places and they may 37 then have different importance to the user and headers using the same name 38 might be used. The *origin* is a bitmask for what header sources you want. See 39 the curl_easy_header(3) man page for the origin descriptions. 40 41 The *request* argument tells libcurl from which request you want headers 42 from. A single transfer might consist of a series of HTTP requests and this 43 argument lets you specify which particular individual request you want the 44 headers from. 0 being the first request and then the number increases for 45 further redirects or when multi-state authentication is used. Passing in -1 is 46 a shortcut to "the last" request in the series, independently of the actual 47 amount of requests used. 48 49 It is suggested that you pass in the same **origin** and **request** when 50 iterating over a range of headers as changing the value mid-loop might give 51 you unexpected results. 52 53 If *prev* is NULL, this function returns a pointer to the first header stored 54 within the given scope (origin + request). 55 56 If *prev* is a pointer to a previously returned header struct, 57 curl_easy_nextheader(3) returns a pointer the next header stored within the 58 given scope. This way, an application can iterate over all available headers. 59 60 The memory for the struct this points to, is owned and managed by libcurl and 61 is associated with the easy handle. Applications must copy the data if they 62 want it to survive subsequent API calls or the life-time of the easy handle. 63 64 # %PROTOCOLS% 65 66 # EXAMPLE 67 68 ~~~c 69 int main(void) 70 { 71 struct curl_header *prev = NULL; 72 struct curl_header *h; 73 74 CURL *curl = curl_easy_init(); 75 if(curl) { 76 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 77 curl_easy_perform(curl); 78 79 /* extract the normal headers from the first request */ 80 while((h = curl_easy_nextheader(curl, CURLH_HEADER, 0, prev))) { 81 printf("%s: %s\n", h->name, h->value); 82 prev = h; 83 } 84 85 /* extract the normal headers + 1xx + trailers from the last request */ 86 unsigned int origin = CURLH_HEADER| CURLH_1XX | CURLH_TRAILER; 87 while((h = curl_easy_nextheader(curl, origin, -1, prev))) { 88 printf("%s: %s\n", h->name, h->value); 89 prev = h; 90 } 91 } 92 } 93 ~~~ 94 95 # %AVAILABILITY% 96 97 # RETURN VALUE 98 99 This function returns the next header, or NULL when there are no more 100 (matching) headers or an error occurred. 101 102 If this function returns NULL when *prev* was set to NULL, then there are no 103 headers available within the scope to return.