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