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 /* <DESC>
25 * Basic URL API use.
26 * </DESC>
27 */
28 #include <stdio.h>
29 #include <curl/curl.h>
30
31 #if !CURL_AT_LEAST_VERSION(7, 62, 0)
32 #error "this example requires curl 7.62.0 or later"
33 #endif
34
main(void)35 int main(void)
36 {
37 CURLU *h;
38 CURLUcode uc;
39 char *host;
40 char *path;
41
42 h = curl_url(); /* get a handle to work with */
43 if(!h)
44 return 1;
45
46 /* parse a full URL */
47 uc = curl_url_set(h, CURLUPART_URL, "http://example.com/path/index.html", 0);
48 if(uc)
49 goto fail;
50
51 /* extract hostname from the parsed URL */
52 uc = curl_url_get(h, CURLUPART_HOST, &host, 0);
53 if(!uc) {
54 printf("Host name: %s\n", host);
55 curl_free(host);
56 }
57
58 /* extract the path from the parsed URL */
59 uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
60 if(!uc) {
61 printf("Path: %s\n", path);
62 curl_free(path);
63 }
64
65 /* redirect with a relative URL */
66 uc = curl_url_set(h, CURLUPART_URL, "../another/second.html", 0);
67 if(uc)
68 goto fail;
69
70 /* extract the new, updated path */
71 uc = curl_url_get(h, CURLUPART_PATH, &path, 0);
72 if(!uc) {
73 printf("Path: %s\n", path);
74 curl_free(path);
75 }
76
77 fail:
78 curl_url_cleanup(h); /* free URL handle */
79 return 0;
80 }
81