• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_url_set
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_CURLU (3)
9  - curl_url (3)
10  - curl_url_cleanup (3)
11  - curl_url_dup (3)
12  - curl_url_get (3)
13  - curl_url_strerror (3)
14---
15
16# NAME
17
18curl_url_set - set a URL part
19
20# SYNOPSIS
21
22~~~c
23#include <curl/curl.h>
24
25CURLUcode curl_url_set(CURLU *url,
26                       CURLUPart part,
27                       const char *content,
28                       unsigned int flags);
29~~~
30
31# DESCRIPTION
32
33The *url* handle to work on, passed in as the first argument, must be a
34handle previously created by curl_url(3) or curl_url_dup(3).
35
36This function sets or updates individual URL components, or parts, held by the
37URL object the handle identifies.
38
39The *part* argument should identify the particular URL part (see list
40below) to set or change, with *content* pointing to a null-terminated
41string with the new contents for that URL part. The contents should be in the
42form and encoding they would use in a URL: URL encoded.
43
44When setting part in the URL object that was previously already set, it
45replaces the data that was previously stored for that part with the new
46*content*.
47
48The caller does not have to keep *content* around after a successful call
49as this function copies the content.
50
51Setting a part to a NULL pointer removes that part's contents from the
52*CURLU* handle.
53
54By default, this API only accepts URLs using schemes for protocols that are
55supported built-in. To make libcurl parse URLs generically even for schemes it
56does not know about, the **CURLU_NON_SUPPORT_SCHEME** flags bit must be
57set. Otherwise, this function returns *CURLUE_UNSUPPORTED_SCHEME* for URL
58schemes it does not recognize.
59
60This function has an 8 MB maximum length limit for all provided input strings.
61In the real world, excessively long fields in URLs cause problems even if this
62API accepts them.
63
64When setting or updating contents of individual URL parts, this API might
65accept data that would not be otherwise possible to set in the string when it
66gets populated as a result of a full URL parse. Beware. If done so, extracting
67a full URL later on from such components might render an invalid URL.
68
69The *flags* argument is a bitmask with independent features.
70
71# PARTS
72
73## CURLUPART_URL
74
75Allows the full URL of the handle to be replaced. If the handle already is
76populated with a URL, the new URL can be relative to the previous.
77
78When successfully setting a new URL, relative or absolute, the handle contents
79is replaced with the components of the newly set URL.
80
81Pass a pointer to a null-terminated string to the *url* parameter. The
82string must point to a correctly formatted "RFC 3986+" URL or be a NULL
83pointer.
84
85Unless *CURLU_NO_AUTHORITY* is set, a blank hostname is not allowed in
86the URL.
87
88## CURLUPART_SCHEME
89
90Scheme cannot be URL decoded on set. libcurl only accepts setting schemes up
91to 40 bytes long.
92
93## CURLUPART_USER
94
95## CURLUPART_PASSWORD
96
97## CURLUPART_OPTIONS
98
99The options field is an optional field that might follow the password in the
100userinfo part. It is only recognized/used when parsing URLs for the following
101schemes: pop3, smtp and imap. This function however allows users to
102independently set this field.
103
104## CURLUPART_HOST
105
106The hostname. If it is International Domain Name (IDN) the string must then be
107encoded as your locale says or UTF-8 (when WinIDN is used). If it is a
108bracketed IPv6 numeric address it may contain a zone id (or you can use
109*CURLUPART_ZONEID*).
110
111Unless *CURLU_NO_AUTHORITY* is set, a blank hostname is not allowed to set.
112
113## CURLUPART_ZONEID
114
115If the hostname is a numeric IPv6 address, this field can also be set.
116
117## CURLUPART_PORT
118
119The port number cannot be URL encoded on set. The given port number is
120provided as a string and the decimal number in it must be between 0 and
12165535. Anything else returns an error.
122
123## CURLUPART_PATH
124
125If a path is set in the URL without a leading slash, a slash is prepended
126automatically.
127
128## CURLUPART_QUERY
129
130The query part gets spaces converted to pluses when asked to URL encode on set
131with the *CURLU_URLENCODE* bit.
132
133If used together with the *CURLU_APPENDQUERY* bit, the provided part is
134appended on the end of the existing query.
135
136The question mark in the URL is not part of the actual query contents.
137
138## CURLUPART_FRAGMENT
139
140The hash sign in the URL is not part of the actual fragment contents.
141
142# FLAGS
143
144The flags argument is zero, one or more bits set in a bitmask.
145
146## CURLU_APPENDQUERY
147
148Can be used when setting the *CURLUPART_QUERY* component. The provided new
149part is then appended at the end of the existing query - and if the previous
150part did not end with an ampersand (&), an ampersand gets inserted before the
151new appended part.
152
153When *CURLU_APPENDQUERY* is used together with *CURLU_URLENCODE*, the
154first '=' symbol is not URL encoded.
155
156## CURLU_NON_SUPPORT_SCHEME
157
158If set, allows curl_url_set(3) to set a non-supported scheme.
159
160## CURLU_URLENCODE
161
162When set, curl_url_set(3) URL encodes the part on entry, except for
163scheme, port and URL.
164
165When setting the path component with URL encoding enabled, the slash character
166is be skipped.
167
168The query part gets space-to-plus conversion before the URL conversion.
169
170This URL encoding is charset unaware and converts the input in a byte-by-byte
171manner.
172
173## CURLU_DEFAULT_SCHEME
174
175If set, allows the URL to be set without a scheme and then sets that to the
176default scheme: HTTPS. Overrides the *CURLU_GUESS_SCHEME* option if both
177are set.
178
179## CURLU_GUESS_SCHEME
180
181If set, allows the URL to be set without a scheme and it instead "guesses"
182which scheme that was intended based on the hostname. If the outermost
183subdomain name matches DICT, FTP, IMAP, LDAP, POP3 or SMTP then that scheme is
184used, otherwise it picks HTTP. Conflicts with the *CURLU_DEFAULT_SCHEME*
185option which takes precedence if both are set.
186
187## CURLU_NO_AUTHORITY
188
189If set, skips authority checks. The RFC allows individual schemes to omit the
190host part (normally the only mandatory part of the authority), but libcurl
191cannot know whether this is permitted for custom schemes. Specifying the flag
192permits empty authority sections, similar to how file scheme is handled.
193
194## CURLU_PATH_AS_IS
195
196When set for **CURLUPART_URL**, this skips the normalization of the
197path. That is the procedure where libcurl otherwise removes sequences of
198dot-slash and dot-dot etc. The same option used for transfers is called
199CURLOPT_PATH_AS_IS(3).
200
201## CURLU_ALLOW_SPACE
202
203If set, the URL parser allows space (ASCII 32) where possible. The URL syntax
204does normally not allow spaces anywhere, but they should be encoded as %20
205or '+'. When spaces are allowed, they are still not allowed in the scheme.
206When space is used and allowed in a URL, it is stored as-is unless
207*CURLU_URLENCODE* is also set, which then makes libcurl URL encode the
208space before stored. This affects how the URL is constructed when
209curl_url_get(3) is subsequently used to extract the full URL or
210individual parts. (Added in 7.78.0)
211
212## CURLU_DISALLOW_USER
213
214If set, the URL parser does not accept embedded credentials for the
215**CURLUPART_URL**, and instead returns **CURLUE_USER_NOT_ALLOWED** for
216such URLs.
217
218# EXAMPLE
219
220~~~c
221int main(void)
222{
223  CURLUcode rc;
224  CURLU *url = curl_url();
225  rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
226  if(!rc) {
227    /* change it to an FTP URL */
228    rc = curl_url_set(url, CURLUPART_SCHEME, "ftp", 0);
229  }
230  curl_url_cleanup(url);
231}
232~~~
233
234# AVAILABILITY
235
236Added in 7.62.0. CURLUPART_ZONEID was added in 7.65.0.
237
238# RETURN VALUE
239
240Returns a *CURLUcode* error value, which is CURLUE_OK (0) if everything
241went fine. See the libcurl-errors(3) man page for the full list with
242descriptions.
243
244The input string passed to curl_url_set(3) must be shorter than eight
245million bytes. Otherwise this function returns **CURLUE_MALFORMED_INPUT**.
246
247If this function returns an error, no URL part is set.
248