curl_easy_header.3 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .TH curl_easy_header 3 "13 March 2022" "libcurl" "libcurl"
  25. .SH NAME
  26. curl_easy_header - get an HTTP header
  27. .SH SYNOPSIS
  28. .nf
  29. #include <curl/curl.h>
  30. CURLHcode curl_easy_header(CURL *easy,
  31. const char *name,
  32. size_t index,
  33. unsigned int origin,
  34. int request,
  35. struct curl_header **hout);
  36. .fi
  37. .SH DESCRIPTION
  38. \fIcurl_easy_header(3)\fP returns a pointer to a "curl_header" struct in
  39. \fBhout\fP with data for the HTTP response header \fIname\fP. The case
  40. insensitive null-terminated header name should be specified without colon.
  41. \fIindex\fP 0 means asking for the first instance of the header. If the
  42. returned header struct has \fBamount\fP set larger than 1, it means there are
  43. more instances of the same header name available to get. Asking for a too big
  44. index makes \fBCURLHE_BADINDEX\fP get returned.
  45. The \fIorigin\fP argument is for specifying which headers to receive, as a
  46. single HTTP transfer might provide headers from several different places and
  47. they may then have different importance to the user and headers using the same
  48. name might be used. The \fIorigin\fP is a bitmask for what header sources you
  49. want. See the descriptions below.
  50. The \fIrequest\fP argument tells libcurl from which request you want headers
  51. from. A single transfer might consist of a series of HTTP requests and this
  52. argument lets you specify which particular individual request you want the
  53. headers from. 0 being the first request and then the number increases for
  54. further redirects or when multi-state authentication is used. Passing in -1 is
  55. a shortcut to "the last" request in the series, independently of the actual
  56. amount of requests used.
  57. libcurl stores and provides the actually used "correct" headers. If for
  58. example two headers with the same name arrive and the latter overrides the
  59. former, then only the latter is provided. If the first header survives the
  60. second, then only the first one is provided. An application using this API
  61. does not have to bother about multiple headers used wrongly.
  62. The memory for the returned struct is associated with the easy handle and
  63. subsequent calls to \fIcurl_easy_header(3)\fP clobber the struct used in the
  64. previous calls for the same easy handle. Applications need to copy the data if
  65. it wants to keep it around. The memory used for the struct gets freed with
  66. calling \fIcurl_easy_cleanup(3)\fP of the easy handle.
  67. The first line in an HTTP response is called the status line. It is not
  68. considered a header by this function. Headers are the "name: value" lines
  69. following the status.
  70. This function can be used before (all) headers have been received and is fine
  71. to call from within libcurl callbacks. It returns the state of the headers at
  72. the time it is called.
  73. .SH "The header struct"
  74. .nf
  75. struct curl_header {
  76. char *name;
  77. char *value;
  78. size_t amount;
  79. size_t index;
  80. unsigned int origin;
  81. void *anchor;
  82. };
  83. .fi
  84. The data \fBname\fP field points to, is the same as the requested name, but
  85. might have a different case.
  86. The data \fBvalue\fP field points to, comes exactly as delivered over the
  87. network but with leading and trailing whitespace and newlines stripped
  88. off. The `value` data is null-terminated. For legacy HTTP/1 "folded headers",
  89. this API provides the full single value in an unfolded manner with a single
  90. whitespace between the lines.
  91. \fBamount\fP is how many headers using this name that exist, within the origin
  92. and request scope asked for.
  93. \fBindex\fP is the zero based entry number of this particular header, which in
  94. case this header was used more than once in the requested scope can be larger
  95. than 0 but is always less than \fBamount\fP.
  96. The \fBorigin\fP field in the "curl_header" struct has one of the origin bits
  97. set, indicating where from the header originates. At the time of this writing,
  98. there are 5 bits with defined use. The undocumented 27 remaining bits are
  99. reserved for future use and must not be assumed to have any particular value.
  100. \fBanchor\fP is a private handle used by libcurl internals. Do not modify.
  101. .SH ORIGINS
  102. .IP CURLH_HEADER
  103. The header arrived as a header from the server.
  104. .IP CURLH_TRAILER
  105. The header arrived as a trailer. A header that arrives after the body.
  106. .IP CURLH_CONNECT
  107. The header arrived in a CONNECT response. A CONNECT request is being done to
  108. setup a transfer "through" an HTTP(S) proxy.
  109. .IP CURLH_1XX
  110. The header arrived in an HTTP 1xx response. A 1xx response is an "intermediate"
  111. response that might happen before the "real" response.
  112. .IP CURLH_PSEUDO
  113. The header is an HTTP/2 or HTTP/3 pseudo header
  114. .SH EXAMPLE
  115. .nf
  116. int main(void)
  117. {
  118. struct curl_header *type;
  119. CURL *curl = curl_easy_init();
  120. if(curl) {
  121. CURLHcode h;
  122. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  123. curl_easy_perform(curl);
  124. h = curl_easy_header(curl, "Content-Type", 0, CURLH_HEADER, -1, &type);
  125. curl_easy_cleanup(curl);
  126. }
  127. }
  128. .fi
  129. .SH AVAILABILITY
  130. Added in 7.83.0. Officially supported since 7.84.0.
  131. .SH RETURN VALUE
  132. This function returns a CURLHcode indicating success or error.
  133. .SH "SEE ALSO"
  134. .BR curl_easy_nextheader (3),
  135. .BR curl_easy_perform (3),
  136. .BR CURLINFO_CONTENT_TYPE (3),
  137. .BR CURLOPT_HEADERFUNCTION (3),
  138. .BR libcurl-errors (3)