xref: /aosp_15_r20/external/curl/docs/libcurl/curl_url_get.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: curl_url_get
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_CURLU (3)
9*6236dae4SAndroid Build Coastguard Worker  - curl_url (3)
10*6236dae4SAndroid Build Coastguard Worker  - curl_url_cleanup (3)
11*6236dae4SAndroid Build Coastguard Worker  - curl_url_dup (3)
12*6236dae4SAndroid Build Coastguard Worker  - curl_url_set (3)
13*6236dae4SAndroid Build Coastguard Worker  - curl_url_strerror (3)
14*6236dae4SAndroid Build Coastguard WorkerProtocol:
15*6236dae4SAndroid Build Coastguard Worker  - All
16*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.62.0
17*6236dae4SAndroid Build Coastguard Worker---
18*6236dae4SAndroid Build Coastguard Worker
19*6236dae4SAndroid Build Coastguard Worker# NAME
20*6236dae4SAndroid Build Coastguard Worker
21*6236dae4SAndroid Build Coastguard Workercurl_url_get - extract a part from a URL
22*6236dae4SAndroid Build Coastguard Worker
23*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
24*6236dae4SAndroid Build Coastguard Worker
25*6236dae4SAndroid Build Coastguard Worker~~~c
26*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
27*6236dae4SAndroid Build Coastguard Worker
28*6236dae4SAndroid Build Coastguard WorkerCURLUcode curl_url_get(const CURLU *url,
29*6236dae4SAndroid Build Coastguard Worker                       CURLUPart part,
30*6236dae4SAndroid Build Coastguard Worker                       char **content,
31*6236dae4SAndroid Build Coastguard Worker                       unsigned int flags);
32*6236dae4SAndroid Build Coastguard Worker~~~
33*6236dae4SAndroid Build Coastguard Worker
34*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
35*6236dae4SAndroid Build Coastguard Worker
36*6236dae4SAndroid Build Coastguard WorkerGiven a *url* handle of a URL object, this function extracts an individual
37*6236dae4SAndroid Build Coastguard Workerpiece or the full URL from it.
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard WorkerThe *part* argument specifies which part to extract (see list below) and
40*6236dae4SAndroid Build Coastguard Worker*content* points to a 'char *' to get updated to point to a newly
41*6236dae4SAndroid Build Coastguard Workerallocated string with the contents.
42*6236dae4SAndroid Build Coastguard Worker
43*6236dae4SAndroid Build Coastguard WorkerThe *flags* argument is a bitmask with individual features.
44*6236dae4SAndroid Build Coastguard Worker
45*6236dae4SAndroid Build Coastguard WorkerThe returned content pointer must be freed with curl_free(3) after use.
46*6236dae4SAndroid Build Coastguard Worker
47*6236dae4SAndroid Build Coastguard Worker# FLAGS
48*6236dae4SAndroid Build Coastguard Worker
49*6236dae4SAndroid Build Coastguard WorkerThe flags argument is zero, one or more bits set in a bitmask.
50*6236dae4SAndroid Build Coastguard Worker
51*6236dae4SAndroid Build Coastguard Worker## CURLU_DEFAULT_PORT
52*6236dae4SAndroid Build Coastguard Worker
53*6236dae4SAndroid Build Coastguard WorkerIf the handle has no port stored, this option makes curl_url_get(3)
54*6236dae4SAndroid Build Coastguard Workerreturn the default port for the used scheme.
55*6236dae4SAndroid Build Coastguard Worker
56*6236dae4SAndroid Build Coastguard Worker## CURLU_DEFAULT_SCHEME
57*6236dae4SAndroid Build Coastguard Worker
58*6236dae4SAndroid Build Coastguard WorkerIf the handle has no scheme stored, this option makes curl_url_get(3)
59*6236dae4SAndroid Build Coastguard Workerreturn the default scheme instead of error.
60*6236dae4SAndroid Build Coastguard Worker
61*6236dae4SAndroid Build Coastguard Worker## CURLU_NO_DEFAULT_PORT
62*6236dae4SAndroid Build Coastguard Worker
63*6236dae4SAndroid Build Coastguard WorkerInstructs curl_url_get(3) to not return a port number if it matches the
64*6236dae4SAndroid Build Coastguard Workerdefault port for the scheme.
65*6236dae4SAndroid Build Coastguard Worker
66*6236dae4SAndroid Build Coastguard Worker## CURLU_URLDECODE
67*6236dae4SAndroid Build Coastguard Worker
68*6236dae4SAndroid Build Coastguard WorkerAsks curl_url_get(3) to URL decode the contents before returning it. It
69*6236dae4SAndroid Build Coastguard Workerdoes not decode the scheme, the port number or the full URL.
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard WorkerThe query component also gets plus-to-space conversion as a bonus when this
72*6236dae4SAndroid Build Coastguard Workerbit is set.
73*6236dae4SAndroid Build Coastguard Worker
74*6236dae4SAndroid Build Coastguard WorkerNote that this URL decoding is charset unaware and you get a zero terminated
75*6236dae4SAndroid Build Coastguard Workerstring back with data that could be intended for a particular encoding.
76*6236dae4SAndroid Build Coastguard Worker
77*6236dae4SAndroid Build Coastguard WorkerIf there are byte values lower than 32 in the decoded string, the get
78*6236dae4SAndroid Build Coastguard Workeroperation returns an error instead.
79*6236dae4SAndroid Build Coastguard Worker
80*6236dae4SAndroid Build Coastguard Worker## CURLU_URLENCODE
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard WorkerIf set, curl_url_get(3) URL encodes the hostname part when a full URL is
83*6236dae4SAndroid Build Coastguard Workerretrieved. If not set (default), libcurl returns the URL with the hostname raw
84*6236dae4SAndroid Build Coastguard Workerto support IDN names to appear as-is. IDN hostnames are typically using
85*6236dae4SAndroid Build Coastguard Workernon-ASCII bytes that otherwise gets percent-encoded.
86*6236dae4SAndroid Build Coastguard Worker
87*6236dae4SAndroid Build Coastguard WorkerNote that even when not asking for URL encoding, the '%' (byte 37) is URL
88*6236dae4SAndroid Build Coastguard Workerencoded to make sure the hostname remains valid.
89*6236dae4SAndroid Build Coastguard Worker
90*6236dae4SAndroid Build Coastguard Worker## CURLU_PUNYCODE
91*6236dae4SAndroid Build Coastguard Worker
92*6236dae4SAndroid Build Coastguard WorkerIf set and *CURLU_URLENCODE* is not set, and asked to retrieve the
93*6236dae4SAndroid Build Coastguard Worker**CURLUPART_HOST** or **CURLUPART_URL** parts, libcurl returns the host
94*6236dae4SAndroid Build Coastguard Workername in its punycode version if it contains any non-ASCII octets (and is an
95*6236dae4SAndroid Build Coastguard WorkerIDN name).
96*6236dae4SAndroid Build Coastguard Worker
97*6236dae4SAndroid Build Coastguard WorkerIf libcurl is built without IDN capabilities, using this bit makes
98*6236dae4SAndroid Build Coastguard Workercurl_url_get(3) return *CURLUE_LACKS_IDN* if the hostname contains
99*6236dae4SAndroid Build Coastguard Workeranything outside the ASCII range.
100*6236dae4SAndroid Build Coastguard Worker
101*6236dae4SAndroid Build Coastguard Worker(Added in curl 7.88.0)
102*6236dae4SAndroid Build Coastguard Worker
103*6236dae4SAndroid Build Coastguard Worker## CURLU_PUNY2IDN
104*6236dae4SAndroid Build Coastguard Worker
105*6236dae4SAndroid Build Coastguard WorkerIf set and asked to retrieve the **CURLUPART_HOST** or **CURLUPART_URL**
106*6236dae4SAndroid Build Coastguard Workerparts, libcurl returns the hostname in its IDN (International Domain Name)
107*6236dae4SAndroid Build Coastguard WorkerUTF-8 version if it otherwise is a punycode version. If the punycode name
108*6236dae4SAndroid Build Coastguard Workercannot be converted to IDN correctly, libcurl returns
109*6236dae4SAndroid Build Coastguard Worker*CURLUE_BAD_HOSTNAME*.
110*6236dae4SAndroid Build Coastguard Worker
111*6236dae4SAndroid Build Coastguard WorkerIf libcurl is built without IDN capabilities, using this bit makes
112*6236dae4SAndroid Build Coastguard Workercurl_url_get(3) return *CURLUE_LACKS_IDN* if the hostname is using
113*6236dae4SAndroid Build Coastguard Workerpunycode.
114*6236dae4SAndroid Build Coastguard Worker
115*6236dae4SAndroid Build Coastguard Worker(Added in curl 8.3.0)
116*6236dae4SAndroid Build Coastguard Worker
117*6236dae4SAndroid Build Coastguard Worker## CURLU_GET_EMPTY
118*6236dae4SAndroid Build Coastguard Worker
119*6236dae4SAndroid Build Coastguard WorkerWhen this flag is used in curl_url_get(), it makes the function return empty
120*6236dae4SAndroid Build Coastguard Workerquery and fragments parts or when used in the full URL. By default, libcurl
121*6236dae4SAndroid Build Coastguard Workerotherwise considers empty parts non-existing.
122*6236dae4SAndroid Build Coastguard Worker
123*6236dae4SAndroid Build Coastguard WorkerAn empty query part is one where this is nothing following the question mark
124*6236dae4SAndroid Build Coastguard Worker(before the possible fragment). An empty fragments part is one where there is
125*6236dae4SAndroid Build Coastguard Workernothing following the hash sign.
126*6236dae4SAndroid Build Coastguard Worker
127*6236dae4SAndroid Build Coastguard Worker(Added in curl 8.8.0)
128*6236dae4SAndroid Build Coastguard Worker
129*6236dae4SAndroid Build Coastguard Worker## CURLU_NO_GUESS_SCHEME
130*6236dae4SAndroid Build Coastguard Worker
131*6236dae4SAndroid Build Coastguard WorkerWhen this flag is used in curl_url_get(), it treats the scheme as non-existing
132*6236dae4SAndroid Build Coastguard Workerif it was set as a result of a previous guess; when CURLU_GUESS_SCHEME was
133*6236dae4SAndroid Build Coastguard Workerused parsing a URL.
134*6236dae4SAndroid Build Coastguard Worker
135*6236dae4SAndroid Build Coastguard WorkerUsing this flag when getting CURLUPART_SCHEME if the scheme was set as the
136*6236dae4SAndroid Build Coastguard Workerresult of a guess makes curl_url_get() return CURLUE_NO_SCHEME.
137*6236dae4SAndroid Build Coastguard Worker
138*6236dae4SAndroid Build Coastguard WorkerUsing this flag when getting CURLUPART_URL if the scheme was set as the result
139*6236dae4SAndroid Build Coastguard Workerof a guess makes curl_url_get() return the full URL without the scheme
140*6236dae4SAndroid Build Coastguard Workercomponent. Such a URL can then only be parsed with curl_url_set() if
141*6236dae4SAndroid Build Coastguard WorkerCURLU_GUESS_SCHEME is used.
142*6236dae4SAndroid Build Coastguard Worker
143*6236dae4SAndroid Build Coastguard Worker(Added in curl 8.9.0)
144*6236dae4SAndroid Build Coastguard Worker
145*6236dae4SAndroid Build Coastguard Worker# PARTS
146*6236dae4SAndroid Build Coastguard Worker
147*6236dae4SAndroid Build Coastguard Worker## CURLUPART_URL
148*6236dae4SAndroid Build Coastguard Worker
149*6236dae4SAndroid Build Coastguard WorkerWhen asked to return the full URL, curl_url_get(3) returns a normalized and
150*6236dae4SAndroid Build Coastguard Workerpossibly cleaned up version using all available URL parts.
151*6236dae4SAndroid Build Coastguard Worker
152*6236dae4SAndroid Build Coastguard WorkerWe advise using the *CURLU_PUNYCODE* option to get the URL as "normalized" as
153*6236dae4SAndroid Build Coastguard Workerpossible since IDN allows hostnames to be written in many different ways that
154*6236dae4SAndroid Build Coastguard Workerstill end up the same punycode version.
155*6236dae4SAndroid Build Coastguard Worker
156*6236dae4SAndroid Build Coastguard WorkerZero-length queries and fragments are excluded from the URL unless
157*6236dae4SAndroid Build Coastguard WorkerCURLU_GET_EMPTY is set.
158*6236dae4SAndroid Build Coastguard Worker
159*6236dae4SAndroid Build Coastguard Worker## CURLUPART_SCHEME
160*6236dae4SAndroid Build Coastguard Worker
161*6236dae4SAndroid Build Coastguard WorkerScheme cannot be URL decoded on get.
162*6236dae4SAndroid Build Coastguard Worker
163*6236dae4SAndroid Build Coastguard Worker## CURLUPART_USER
164*6236dae4SAndroid Build Coastguard Worker
165*6236dae4SAndroid Build Coastguard Worker## CURLUPART_PASSWORD
166*6236dae4SAndroid Build Coastguard Worker
167*6236dae4SAndroid Build Coastguard Worker## CURLUPART_OPTIONS
168*6236dae4SAndroid Build Coastguard Worker
169*6236dae4SAndroid Build Coastguard WorkerThe options field is an optional field that might follow the password in the
170*6236dae4SAndroid Build Coastguard Workeruserinfo part. It is only recognized/used when parsing URLs for the following
171*6236dae4SAndroid Build Coastguard Workerschemes: pop3, smtp and imap. The URL API still allows users to set and get
172*6236dae4SAndroid Build Coastguard Workerthis field independently of scheme when not parsing full URLs.
173*6236dae4SAndroid Build Coastguard Worker
174*6236dae4SAndroid Build Coastguard Worker## CURLUPART_HOST
175*6236dae4SAndroid Build Coastguard Worker
176*6236dae4SAndroid Build Coastguard WorkerThe hostname. If it is an IPv6 numeric address, the zone id is not part of it
177*6236dae4SAndroid Build Coastguard Workerbut is provided separately in *CURLUPART_ZONEID*. IPv6 numerical addresses
178*6236dae4SAndroid Build Coastguard Workerare returned within brackets ([]).
179*6236dae4SAndroid Build Coastguard Worker
180*6236dae4SAndroid Build Coastguard WorkerIPv6 names are normalized when set, which should make them as short as
181*6236dae4SAndroid Build Coastguard Workerpossible while maintaining correct syntax.
182*6236dae4SAndroid Build Coastguard Worker
183*6236dae4SAndroid Build Coastguard Worker## CURLUPART_ZONEID
184*6236dae4SAndroid Build Coastguard Worker
185*6236dae4SAndroid Build Coastguard WorkerIf the hostname is a numeric IPv6 address, this field might also be set.
186*6236dae4SAndroid Build Coastguard Worker
187*6236dae4SAndroid Build Coastguard Worker## CURLUPART_PORT
188*6236dae4SAndroid Build Coastguard Worker
189*6236dae4SAndroid Build Coastguard WorkerA port cannot be URL decoded on get. This number is returned in a string just
190*6236dae4SAndroid Build Coastguard Workerlike all other parts. That string is guaranteed to hold a valid port number in
191*6236dae4SAndroid Build Coastguard WorkerASCII using base 10.
192*6236dae4SAndroid Build Coastguard Worker
193*6236dae4SAndroid Build Coastguard Worker## CURLUPART_PATH
194*6236dae4SAndroid Build Coastguard Worker
195*6236dae4SAndroid Build Coastguard WorkerThe *part* is always at least a slash ('/') even if no path was supplied
196*6236dae4SAndroid Build Coastguard Workerin the URL. A URL path always starts with a slash.
197*6236dae4SAndroid Build Coastguard Worker
198*6236dae4SAndroid Build Coastguard Worker## CURLUPART_QUERY
199*6236dae4SAndroid Build Coastguard Worker
200*6236dae4SAndroid Build Coastguard WorkerThe initial question mark that denotes the beginning of the query part is a
201*6236dae4SAndroid Build Coastguard Workerdelimiter only. It is not part of the query contents.
202*6236dae4SAndroid Build Coastguard Worker
203*6236dae4SAndroid Build Coastguard WorkerA not-present query returns *part* set to NULL.
204*6236dae4SAndroid Build Coastguard Worker
205*6236dae4SAndroid Build Coastguard WorkerA zero-length query returns *part* as NULL unless CURLU_GET_EMPTY is set.
206*6236dae4SAndroid Build Coastguard Worker
207*6236dae4SAndroid Build Coastguard WorkerThe query part gets pluses converted to space when asked to URL decode on get
208*6236dae4SAndroid Build Coastguard Workerwith the CURLU_URLDECODE bit.
209*6236dae4SAndroid Build Coastguard Worker
210*6236dae4SAndroid Build Coastguard Worker## CURLUPART_FRAGMENT
211*6236dae4SAndroid Build Coastguard Worker
212*6236dae4SAndroid Build Coastguard WorkerThe initial hash sign that denotes the beginning of the fragment is a
213*6236dae4SAndroid Build Coastguard Workerdelimiter only. It is not part of the fragment contents.
214*6236dae4SAndroid Build Coastguard Worker
215*6236dae4SAndroid Build Coastguard WorkerA not-present fragment returns *part* set to NULL.
216*6236dae4SAndroid Build Coastguard Worker
217*6236dae4SAndroid Build Coastguard WorkerA zero-length fragment returns *part* as NULL unless CURLU_GET_EMPTY is set.
218*6236dae4SAndroid Build Coastguard Worker
219*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
220*6236dae4SAndroid Build Coastguard Worker
221*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
222*6236dae4SAndroid Build Coastguard Worker
223*6236dae4SAndroid Build Coastguard Worker~~~c
224*6236dae4SAndroid Build Coastguard Workerint main(void)
225*6236dae4SAndroid Build Coastguard Worker{
226*6236dae4SAndroid Build Coastguard Worker  CURLUcode rc;
227*6236dae4SAndroid Build Coastguard Worker  CURLU *url = curl_url();
228*6236dae4SAndroid Build Coastguard Worker  rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
229*6236dae4SAndroid Build Coastguard Worker  if(!rc) {
230*6236dae4SAndroid Build Coastguard Worker    char *scheme;
231*6236dae4SAndroid Build Coastguard Worker    rc = curl_url_get(url, CURLUPART_SCHEME, &scheme, 0);
232*6236dae4SAndroid Build Coastguard Worker    if(!rc) {
233*6236dae4SAndroid Build Coastguard Worker      printf("the scheme is %s\n", scheme);
234*6236dae4SAndroid Build Coastguard Worker      curl_free(scheme);
235*6236dae4SAndroid Build Coastguard Worker    }
236*6236dae4SAndroid Build Coastguard Worker    curl_url_cleanup(url);
237*6236dae4SAndroid Build Coastguard Worker  }
238*6236dae4SAndroid Build Coastguard Worker}
239*6236dae4SAndroid Build Coastguard Worker~~~
240*6236dae4SAndroid Build Coastguard Worker
241*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
242*6236dae4SAndroid Build Coastguard Worker
243*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
244*6236dae4SAndroid Build Coastguard Worker
245*6236dae4SAndroid Build Coastguard WorkerReturns a CURLUcode error value, which is CURLUE_OK (0) if everything went
246*6236dae4SAndroid Build Coastguard Workerfine. See the libcurl-errors(3) man page for the full list with
247*6236dae4SAndroid Build Coastguard Workerdescriptions.
248*6236dae4SAndroid Build Coastguard Worker
249*6236dae4SAndroid Build Coastguard WorkerIf this function returns an error, no URL part is returned.
250