2018-09-10 16:09:18 +08:00
|
|
|
.\" **************************************************************************
|
|
|
|
.\" * _ _ ____ _
|
|
|
|
.\" * Project ___| | | | _ \| |
|
|
|
|
.\" * / __| | | | |_) | |
|
|
|
|
.\" * | (__| |_| | _ <| |___
|
|
|
|
.\" * \___|\___/|_| \_\_____|
|
|
|
|
.\" *
|
2022-03-29 19:58:11 +08:00
|
|
|
.\" * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2018-09-10 16:09:18 +08:00
|
|
|
.\" *
|
|
|
|
.\" * This software is licensed as described in the file COPYING, which
|
|
|
|
.\" * you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
.\" * are also available at https://curl.se/docs/copyright.html.
|
2018-09-10 16:09:18 +08:00
|
|
|
.\" *
|
|
|
|
.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
.\" * copies of the Software, and permit persons to whom the Software is
|
|
|
|
.\" * furnished to do so, under the terms of the COPYING file.
|
|
|
|
.\" *
|
|
|
|
.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
.\" * KIND, either express or implied.
|
|
|
|
.\" *
|
2022-05-17 17:16:50 +08:00
|
|
|
.\" * SPDX-License-Identifier: curl
|
|
|
|
.\" *
|
2018-09-10 16:09:18 +08:00
|
|
|
.\" **************************************************************************
|
2022-03-29 19:58:11 +08:00
|
|
|
.TH libcurl 3 "10 Sep 2018" "libcurl" "libcurl URL interface"
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH NAME
|
|
|
|
libcurl-url \- URL interface overview
|
|
|
|
.SH DESCRIPTION
|
2021-05-05 15:17:24 +08:00
|
|
|
The URL interface provides functions for parsing and generating URLs.
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH INCLUDE
|
2021-05-05 15:17:24 +08:00
|
|
|
You still only include <curl/curl.h> in your code.
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH CREATE
|
|
|
|
Create a handle that holds URL info and resources with \fIcurl_url(3)\fP:
|
2022-09-21 05:30:19 +08:00
|
|
|
.nf
|
2018-09-10 16:09:18 +08:00
|
|
|
CURLU *h = curl_url();
|
2022-09-21 05:30:19 +08:00
|
|
|
.fi
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH CLEANUP
|
2022-09-21 05:30:19 +08:00
|
|
|
When done with it, clean it up with \fIcurl_url_cleanup(3)\fP
|
|
|
|
.nf
|
2018-09-10 16:09:18 +08:00
|
|
|
curl_url_cleanup(h);
|
2022-09-21 05:30:19 +08:00
|
|
|
.fi
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH DUPLICATE
|
|
|
|
When you need a copy of a handle, just duplicate it with \fIcurl_url_dup(3)\fP:
|
2022-09-21 05:30:19 +08:00
|
|
|
.nf
|
2018-09-10 16:09:18 +08:00
|
|
|
CURLU *nh = curl_url_dup(h);
|
2022-09-21 05:30:19 +08:00
|
|
|
.fi
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH PARSING
|
2022-09-21 05:30:19 +08:00
|
|
|
By setting a URL to the handle with \fIcurl_url_set(3)\fP, the URL is parsed
|
2018-09-10 16:09:18 +08:00
|
|
|
and stored in the handle. If the URL is not syntactically correct it will
|
|
|
|
return an error instead.
|
|
|
|
.nf
|
|
|
|
rc = curl_url_set(h, CURLUPART_URL,
|
|
|
|
"https://example.com:449/foo/bar?name=moo", 0);
|
|
|
|
.fi
|
|
|
|
|
|
|
|
The zero in the fourth argument is a bitmask for changing specific features.
|
|
|
|
|
|
|
|
If successful, this stores the URL in its individual parts within the handle.
|
|
|
|
.SH REDIRECT
|
|
|
|
When a handle already contains info about a URL, setting a relative URL will
|
|
|
|
make it "redirect" to adapt to it.
|
2022-09-21 05:30:19 +08:00
|
|
|
.nf
|
2018-09-10 16:09:18 +08:00
|
|
|
rc = curl_url_set(h, CURLUPART_URL, "../test?another", 0);
|
2022-09-21 05:30:19 +08:00
|
|
|
.fi
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH "GET URL"
|
2022-09-21 05:30:19 +08:00
|
|
|
The \fBCURLU\fP handle represents a URL and you can easily extract that with
|
2018-09-10 16:09:18 +08:00
|
|
|
\fIcurl_url_get(3)\fP:
|
2022-09-21 05:30:19 +08:00
|
|
|
.nf
|
2018-09-10 16:09:18 +08:00
|
|
|
char *url;
|
|
|
|
rc = curl_url_get(h, CURLUPART_URL, &url, 0);
|
|
|
|
curl_free(url);
|
2022-09-21 05:30:19 +08:00
|
|
|
.fi
|
2018-09-10 16:09:18 +08:00
|
|
|
The zero in the fourth argument is a bitmask for changing specific features.
|
|
|
|
.SH "GET PARTS"
|
|
|
|
When a URL has been parsed or parts have been set, you can extract those
|
|
|
|
pieces from the handle at any time.
|
|
|
|
|
|
|
|
.nf
|
|
|
|
rc = curl_url_get(h, CURLUPART_HOST, &host, 0);
|
|
|
|
rc = curl_url_get(h, CURLUPART_SCHEME, &scheme, 0);
|
|
|
|
rc = curl_url_get(h, CURLUPART_USER, &user, 0);
|
|
|
|
rc = curl_url_get(h, CURLUPART_PASSWORD, &password, 0);
|
|
|
|
rc = curl_url_get(h, CURLUPART_PORT, &port, 0);
|
|
|
|
rc = curl_url_get(h, CURLUPART_PATH, &path, 0);
|
|
|
|
rc = curl_url_get(h, CURLUPART_QUERY, &query, 0);
|
|
|
|
rc = curl_url_get(h, CURLUPART_FRAGMENT, &fragment, 0);
|
|
|
|
.fi
|
|
|
|
|
|
|
|
Extracted parts are not URL decoded unless the user also asks for it with the
|
2022-09-21 05:30:19 +08:00
|
|
|
\fICURLU_URLDECODE\fP flag set in the fourth bitmask argument.
|
2018-09-10 16:09:18 +08:00
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
Remember to free the returned string with \fIcurl_free(3)\fP when you are done
|
2018-09-10 16:09:18 +08:00
|
|
|
with it!
|
|
|
|
.SH "SET PARTS"
|
|
|
|
A user set individual URL parts, either after having parsed a full URL or
|
|
|
|
instead of parsing such.
|
|
|
|
|
|
|
|
.nf
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_HOST, "www.example.com", 0);
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_SCHEME, "https", 0);
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_USER, "john", 0);
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_PASSWORD, "doe", 0);
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_PORT, "443", 0);
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_PATH, "/index.html", 0);
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_QUERY, "name=john", 0);
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_FRAGMENT, "anchor", 0);
|
|
|
|
.fi
|
|
|
|
|
|
|
|
Set parts are not URL encoded unless the user asks for it with the
|
2022-09-21 05:30:19 +08:00
|
|
|
\fICURLU_URLENCODE\fP flag.
|
|
|
|
.SH "CURLU_APPENDQUERY"
|
2018-09-10 16:09:18 +08:00
|
|
|
An application can append a string to the right end of the query part with the
|
2022-09-21 05:30:19 +08:00
|
|
|
\fICURLU_APPENDQUERY\fP flag to \fIcurl_url_set(3)\fP.
|
2018-09-10 16:09:18 +08:00
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
Imagine a handle that holds the URL "https://example.com/?shoes=2". An
|
|
|
|
application can then add the string "hat=1" to the query part like this:
|
2018-09-10 16:09:18 +08:00
|
|
|
|
|
|
|
.nf
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_QUERY, "hat=1", CURLU_APPENDQUERY);
|
|
|
|
.fi
|
|
|
|
|
2022-09-21 05:30:19 +08:00
|
|
|
It will even notice the lack of an ampersand (&) separator so it will inject
|
2018-09-10 16:09:18 +08:00
|
|
|
one too, and the handle's full URL will then equal
|
2022-09-21 05:30:19 +08:00
|
|
|
"https://example.com/?shoes=2&hat=1".
|
2018-09-10 16:09:18 +08:00
|
|
|
|
|
|
|
The appended string can of course also get URL encoded on add, and if asked to
|
|
|
|
URL encode, the encoding process will skip the '=' character. For example,
|
2022-09-21 05:30:19 +08:00
|
|
|
append "candy=N&N" to what we already have, and URL encode it to deal with the
|
2018-09-10 16:09:18 +08:00
|
|
|
ampersand in the data:
|
|
|
|
.nf
|
|
|
|
rc = curl_url_set(urlp, CURLUPART_QUERY, "candy=N&N",
|
|
|
|
CURLU_APPENDQUERY | CURLU_URLENCODE);
|
|
|
|
.fi
|
|
|
|
|
|
|
|
Now the URL looks like
|
|
|
|
.nf
|
2022-09-21 05:30:19 +08:00
|
|
|
https://example.com/?shoes=2&hat=1&candy=N%26N
|
2018-09-10 16:09:18 +08:00
|
|
|
.fi
|
2022-09-21 05:30:19 +08:00
|
|
|
.SH AVAILABILITY
|
2021-05-05 15:17:24 +08:00
|
|
|
The URL API was introduced in libcurl 7.62.0.
|
2018-09-10 16:09:18 +08:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR curl_url "(3), " curl_url_cleanup "(3), " curl_url_get "(3), "
|
2021-09-27 14:22:54 +08:00
|
|
|
.BR curl_url_dup "(3), " curl_url_set "(3), " curl_url_strerror "(3), "
|
|
|
|
.BR CURLOPT_URL "(3)"
|