curl_url_get.3 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "January 02, 2023" "libcurl 7.88.1" "libcurl Manual"
  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 what,
  32. char **part,
  33. unsigned int flags)
  34. .fi
  35. .SH DESCRIPTION
  36. Given the \fIurl\fP handle of an already parsed URL, this function lets the
  37. user extract individual pieces from it.
  38. The \fIwhat\fP argument should be the particular part to extract (see list
  39. below) and \fIpart\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 part 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 will make \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 will make
  50. \fIcurl_url_get(3)\fP 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. will not attempt to decode the scheme, the port number or the full URL.
  57. The query component will also get plus-to-space conversion as a bonus when
  58. this bit is set.
  59. Note that this URL decoding is charset unaware and you will get a zero
  60. terminated string back with data that could be intended for a particular
  61. encoding.
  62. If there's any byte values lower than 32 in the decoded string, the get
  63. operation will return an error instead.
  64. .IP CURLU_URLENCODE
  65. If set, will make \fIcurl_url_get(3)\fP URL encode the host name part when a
  66. full URL is retrieved. If not set (default), libcurl returns the URL with the
  67. host name "raw" to support IDN names to appear as-is. IDN host names are
  68. typically using non-ASCII bytes that otherwise will be percent-encoded.
  69. Note that even when not asking for URL encoding, the '%' (byte 37) will be URL
  70. encoded to make sure the host name remains valid.
  71. .IP CURLU_PUNYCODE
  72. If set and \fICURLU_URLENCODE\fP is not set, and asked to retrieve the
  73. \fBCURLUPART_HOST\fP or \fBCURLUPART_URL\fP parts, libcurl returns the host
  74. name in its punycode version if it contains any non-ASCII octets (and is an
  75. IDN name).
  76. If libcurl is built without IDN capabilities, using this bit will make
  77. \fIcurl_url_get(3)\fP return \fICURLUE_LACKS_IDN\fP if the host name contains
  78. anything outside the ASCII range.
  79. (Added in curl 7.88.0)
  80. .SH PARTS
  81. .IP CURLUPART_URL
  82. When asked to return the full URL, \fIcurl_url_get(3)\fP will return a
  83. normalized and possibly cleaned up version of what was previously parsed.
  84. .IP CURLUPART_SCHEME
  85. Scheme cannot be URL decoded on get.
  86. .IP CURLUPART_USER
  87. .IP CURLUPART_PASSWORD
  88. .IP CURLUPART_OPTIONS
  89. .IP CURLUPART_HOST
  90. The host name. If it is an IPv6 numeric address, the zone id will not be part
  91. of it but is provided separately in \fICURLUPART_ZONEID\fP. IPv6 numerical
  92. addresses are returned within brackets ([]).
  93. .IP CURLUPART_ZONEID
  94. If the host name is a numeric IPv6 address, this field might also be set.
  95. .IP CURLUPART_PORT
  96. Port cannot be URL decoded on get.
  97. .IP CURLUPART_PATH
  98. \fIpart\fP will be '/' even if no path is supplied in the URL.
  99. .IP CURLUPART_QUERY
  100. The initial question mark that denotes the beginning of the query part is
  101. a delimiter only.
  102. It is not part of the query contents.
  103. A not-present query will lead \fIpart\fP to be set to NULL.
  104. A zero-length query will lead \fIpart\fP to be set to a zero-length string.
  105. The query part will also get pluses converted to space when asked to URL
  106. decode on get with the CURLU_URLDECODE bit.
  107. .IP CURLUPART_FRAGMENT
  108. .SH EXAMPLE
  109. .nf
  110. CURLUcode rc;
  111. CURLU *url = curl_url();
  112. rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
  113. if(!rc) {
  114. char *scheme;
  115. rc = curl_url_get(url, CURLUPART_SCHEME, &scheme, 0);
  116. if(!rc) {
  117. printf("the scheme is %s\\n", scheme);
  118. curl_free(scheme);
  119. }
  120. curl_url_cleanup(url);
  121. }
  122. .fi
  123. .SH AVAILABILITY
  124. Added in 7.62.0. CURLUPART_ZONEID was added in 7.65.0.
  125. .SH RETURN VALUE
  126. Returns a CURLUcode error value, which is CURLUE_OK (0) if everything went
  127. fine. See the \fIlibcurl-errors(3)\fP man page for the full list with
  128. descriptions.
  129. If this function returns an error, no URL part is returned.
  130. .SH "SEE ALSO"
  131. .BR curl_url_cleanup "(3), " curl_url "(3), " curl_url_set "(3), "
  132. .BR curl_url_dup "(3), " curl_url_strerror "(3), " CURLOPT_CURLU "(3)"