curl_url_get.3 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. .\" generated by cd2nroff 0.1 from curl_url_get.md
  2. .TH curl_url_get 3 "2025-01-17" libcurl
  3. .SH NAME
  4. curl_url_get \- extract a part from a URL
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLUcode curl_url_get(const CURLU *url,
  9. CURLUPart part,
  10. char **content,
  11. unsigned int flags);
  12. .fi
  13. .SH DESCRIPTION
  14. Given a \fIurl\fP handle of a URL object, this function extracts an individual
  15. piece or the full URL from it.
  16. The \fIpart\fP argument specifies which part to extract (see list below) and
  17. \fIcontent\fP points to a \(aqchar *\(aq to get updated to point to a newly
  18. allocated string with the contents.
  19. The \fIflags\fP argument is a bitmask with individual features.
  20. The returned content pointer must be freed with \fIcurl_free(3)\fP after use.
  21. .SH FLAGS
  22. The flags argument is zero, one or more bits set in a bitmask.
  23. .IP CURLU_DEFAULT_PORT
  24. If the handle has no port stored, this option makes \fIcurl_url_get(3)\fP
  25. return the default port for the used scheme.
  26. .IP CURLU_DEFAULT_SCHEME
  27. If the handle has no scheme stored, this option makes \fIcurl_url_get(3)\fP
  28. return the default scheme instead of error.
  29. .IP CURLU_NO_DEFAULT_PORT
  30. Instructs \fIcurl_url_get(3)\fP to not return a port number if it matches the
  31. default port for the scheme.
  32. .IP CURLU_URLDECODE
  33. Asks \fIcurl_url_get(3)\fP to URL decode the contents before returning it. It
  34. does not decode the scheme, the port number or the full URL.
  35. The query component also gets plus\-to\-space conversion as a bonus when this
  36. bit is set.
  37. Note that this URL decoding is charset unaware and you get a zero terminated
  38. string back with data that could be intended for a particular encoding.
  39. If there are byte values lower than 32 in the decoded string, the get
  40. operation returns an error instead.
  41. .IP CURLU_URLENCODE
  42. If set, \fIcurl_url_get(3)\fP URL encodes the hostname part when a full URL is
  43. retrieved. If not set (default), libcurl returns the URL with the hostname raw
  44. to support IDN names to appear as\-is. IDN hostnames are typically using
  45. non\-ASCII bytes that otherwise gets percent\-encoded.
  46. Note that even when not asking for URL encoding, the \(aq%\(aq (byte 37) is URL
  47. encoded to make sure the hostname remains valid.
  48. .IP CURLU_PUNYCODE
  49. If set and \fICURLU_URLENCODE\fP is not set, and asked to retrieve the
  50. \fBCURLUPART_HOST\fP or \fBCURLUPART_URL\fP parts, libcurl returns the host
  51. name in its punycode version if it contains any non\-ASCII octets (and is an
  52. IDN name).
  53. If libcurl is built without IDN capabilities, using this bit makes
  54. \fIcurl_url_get(3)\fP return \fICURLUE_LACKS_IDN\fP if the hostname contains
  55. anything outside the ASCII range.
  56. (Added in curl 7.88.0)
  57. .IP CURLU_PUNY2IDN
  58. If set and asked to retrieve the \fBCURLUPART_HOST\fP or \fBCURLUPART_URL\fP
  59. parts, libcurl returns the hostname in its IDN (International Domain Name)
  60. UTF\-8 version if it otherwise is a punycode version. If the punycode name
  61. cannot be converted to IDN correctly, libcurl returns
  62. \fICURLUE_BAD_HOSTNAME\fP.
  63. If libcurl is built without IDN capabilities, using this bit makes
  64. \fIcurl_url_get(3)\fP return \fICURLUE_LACKS_IDN\fP if the hostname is using
  65. punycode.
  66. (Added in curl 8.3.0)
  67. .IP CURLU_GET_EMPTY
  68. When this flag is used in curl_url_get(), it makes the function return empty
  69. query and fragments parts or when used in the full URL. By default, libcurl
  70. otherwise considers empty parts non\-existing.
  71. An empty query part is one where this is nothing following the question mark
  72. (before the possible fragment). An empty fragments part is one where there is
  73. nothing following the hash sign.
  74. (Added in curl 8.8.0)
  75. .IP CURLU_NO_GUESS_SCHEME
  76. When this flag is used in curl_url_get(), it treats the scheme as non\-existing
  77. if it was set as a result of a previous guess; when CURLU_GUESS_SCHEME was
  78. used parsing a URL.
  79. Using this flag when getting CURLUPART_SCHEME if the scheme was set as the
  80. result of a guess makes curl_url_get() return CURLUE_NO_SCHEME.
  81. Using this flag when getting CURLUPART_URL if the scheme was set as the result
  82. of a guess makes curl_url_get() return the full URL without the scheme
  83. component. Such a URL can then only be parsed with curl_url_set() if
  84. CURLU_GUESS_SCHEME is used.
  85. (Added in curl 8.9.0)
  86. .SH PARTS
  87. .IP CURLUPART_URL
  88. When asked to return the full URL, \fIcurl_url_get(3)\fP returns a normalized and
  89. possibly cleaned up version using all available URL parts.
  90. We advise using the \fICURLU_PUNYCODE\fP option to get the URL as "normalized" as
  91. possible since IDN allows hostnames to be written in many different ways that
  92. still end up the same punycode version.
  93. Zero\-length queries and fragments are excluded from the URL unless
  94. CURLU_GET_EMPTY is set.
  95. .IP CURLUPART_SCHEME
  96. Scheme cannot be URL decoded on get.
  97. .IP CURLUPART_USER
  98. .IP CURLUPART_PASSWORD
  99. .IP CURLUPART_OPTIONS
  100. The options field is an optional field that might follow the password in the
  101. userinfo part. It is only recognized/used when parsing URLs for the following
  102. schemes: pop3, smtp and imap. The URL API still allows users to set and get
  103. this field independently of scheme when not parsing full URLs.
  104. .IP CURLUPART_HOST
  105. The hostname. If it is an IPv6 numeric address, the zone id is not part of it
  106. but is provided separately in \fICURLUPART_ZONEID\fP. IPv6 numerical addresses
  107. are returned within brackets ([]).
  108. IPv6 names are normalized when set, which should make them as short as
  109. possible while maintaining correct syntax.
  110. .IP CURLUPART_ZONEID
  111. If the hostname is a numeric IPv6 address, this field might also be set.
  112. .IP CURLUPART_PORT
  113. A port cannot be URL decoded on get. This number is returned in a string just
  114. like all other parts. That string is guaranteed to hold a valid port number in
  115. ASCII using base 10.
  116. .IP CURLUPART_PATH
  117. The \fIpart\fP is always at least a slash (\(aq/\(aq) even if no path was supplied
  118. in the URL. A URL path always starts with a slash.
  119. .IP CURLUPART_QUERY
  120. The initial question mark that denotes the beginning of the query part is a
  121. delimiter only. It is not part of the query contents.
  122. A not\-present query returns \fIpart\fP set to NULL.
  123. A zero\-length query returns \fIpart\fP as NULL unless CURLU_GET_EMPTY is set.
  124. The query part gets pluses converted to space when asked to URL decode on get
  125. with the CURLU_URLDECODE bit.
  126. .IP CURLUPART_FRAGMENT
  127. The initial hash sign that denotes the beginning of the fragment is a
  128. delimiter only. It is not part of the fragment contents.
  129. A not\-present fragment returns \fIpart\fP set to NULL.
  130. A zero\-length fragment returns \fIpart\fP as NULL unless CURLU_GET_EMPTY is set.
  131. .SH PROTOCOLS
  132. This functionality affects all supported protocols
  133. .SH EXAMPLE
  134. .nf
  135. int main(void)
  136. {
  137. CURLUcode rc;
  138. CURLU *url = curl_url();
  139. rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
  140. if(!rc) {
  141. char *scheme;
  142. rc = curl_url_get(url, CURLUPART_SCHEME, &scheme, 0);
  143. if(!rc) {
  144. printf("the scheme is %s\\n", scheme);
  145. curl_free(scheme);
  146. }
  147. curl_url_cleanup(url);
  148. }
  149. }
  150. .fi
  151. .SH AVAILABILITY
  152. Added in curl 7.62.0
  153. .SH RETURN VALUE
  154. Returns a CURLUcode error value, which is CURLUE_OK (0) if everything went
  155. fine. See the \fIlibcurl\-errors(3)\fP man page for the full list with
  156. descriptions.
  157. If this function returns an error, no URL part is returned.
  158. .SH SEE ALSO
  159. .BR CURLOPT_CURLU (3),
  160. .BR curl_url (3),
  161. .BR curl_url_cleanup (3),
  162. .BR curl_url_dup (3),
  163. .BR curl_url_set (3),
  164. .BR curl_url_strerror (3)