curl_url_get.3 7.0 KB

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