libcurl-url.3 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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. .TH libcurl 3 "May 17, 2022" "libcurl 7.85.0" "libcurl URL interface"
  25. .SH NAME
  26. libcurl-url \- URL interface overview
  27. .SH DESCRIPTION
  28. The URL interface provides functions for parsing and generating URLs.
  29. .SH INCLUDE
  30. You still only include <curl/curl.h> in your code.
  31. .SH CREATE
  32. Create a handle that holds URL info and resources with \fIcurl_url(3)\fP:
  33. CURLU *h = curl_url();
  34. .SH CLEANUP
  35. When done with it, clean it up with \fIcurl_url_cleanup(3)\fP:
  36. curl_url_cleanup(h);
  37. .SH DUPLICATE
  38. When you need a copy of a handle, just duplicate it with \fIcurl_url_dup(3)\fP:
  39. CURLU *nh = curl_url_dup(h);
  40. .SH PARSING
  41. By "setting" a URL to the handle with \fIcurl_url_set(3)\fP, the URL is parsed
  42. and stored in the handle. If the URL is not syntactically correct it will
  43. return an error instead.
  44. .nf
  45. rc = curl_url_set(h, CURLUPART_URL,
  46. "https://example.com:449/foo/bar?name=moo", 0);
  47. .fi
  48. The zero in the fourth argument is a bitmask for changing specific features.
  49. If successful, this stores the URL in its individual parts within the handle.
  50. .SH REDIRECT
  51. When a handle already contains info about a URL, setting a relative URL will
  52. make it "redirect" to adapt to it.
  53. rc = curl_url_set(h, CURLUPART_URL, "../test?another", 0);
  54. .SH "GET URL"
  55. The `CURLU` handle represents a URL and you can easily extract that with
  56. \fIcurl_url_get(3)\fP:
  57. char *url;
  58. rc = curl_url_get(h, CURLUPART_URL, &url, 0);
  59. curl_free(url);
  60. The zero in the fourth argument is a bitmask for changing specific features.
  61. .SH "GET PARTS"
  62. When a URL has been parsed or parts have been set, you can extract those
  63. pieces from the handle at any time.
  64. .nf
  65. rc = curl_url_get(h, CURLUPART_HOST, &host, 0);
  66. rc = curl_url_get(h, CURLUPART_SCHEME, &scheme, 0);
  67. rc = curl_url_get(h, CURLUPART_USER, &user, 0);
  68. rc = curl_url_get(h, CURLUPART_PASSWORD, &password, 0);
  69. rc = curl_url_get(h, CURLUPART_PORT, &port, 0);
  70. rc = curl_url_get(h, CURLUPART_PATH, &path, 0);
  71. rc = curl_url_get(h, CURLUPART_QUERY, &query, 0);
  72. rc = curl_url_get(h, CURLUPART_FRAGMENT, &fragment, 0);
  73. .fi
  74. Extracted parts are not URL decoded unless the user also asks for it with the
  75. CURLU_URLDECODE flag set in the fourth bitmask argument.
  76. Remember to free the returned string with \fIcurl_free(3)\fP when you are done
  77. with it!
  78. .SH "SET PARTS"
  79. A user set individual URL parts, either after having parsed a full URL or
  80. instead of parsing such.
  81. .nf
  82. rc = curl_url_set(urlp, CURLUPART_HOST, "www.example.com", 0);
  83. rc = curl_url_set(urlp, CURLUPART_SCHEME, "https", 0);
  84. rc = curl_url_set(urlp, CURLUPART_USER, "john", 0);
  85. rc = curl_url_set(urlp, CURLUPART_PASSWORD, "doe", 0);
  86. rc = curl_url_set(urlp, CURLUPART_PORT, "443", 0);
  87. rc = curl_url_set(urlp, CURLUPART_PATH, "/index.html", 0);
  88. rc = curl_url_set(urlp, CURLUPART_QUERY, "name=john", 0);
  89. rc = curl_url_set(urlp, CURLUPART_FRAGMENT, "anchor", 0);
  90. .fi
  91. Set parts are not URL encoded unless the user asks for it with the
  92. `CURLU_URLENCODE` flag.
  93. .SH "APPENDQUERY"
  94. An application can append a string to the right end of the query part with the
  95. `CURLU_APPENDQUERY` flag to \fIcurl_url_set(3)\fP.
  96. Imagine a handle that holds the URL `https://example.com/?shoes=2`. An
  97. application can then add the string `hat=1` to the query part like this:
  98. .nf
  99. rc = curl_url_set(urlp, CURLUPART_QUERY, "hat=1", CURLU_APPENDQUERY);
  100. .fi
  101. It will even notice the lack of an ampersand (`&`) separator so it will inject
  102. one too, and the handle's full URL will then equal
  103. `https://example.com/?shoes=2&hat=1`.
  104. The appended string can of course also get URL encoded on add, and if asked to
  105. URL encode, the encoding process will skip the '=' character. For example,
  106. append `candy=N&N` to what we already have, and URL encode it to deal with the
  107. ampersand in the data:
  108. .nf
  109. rc = curl_url_set(urlp, CURLUPART_QUERY, "candy=N&N",
  110. CURLU_APPENDQUERY | CURLU_URLENCODE);
  111. .fi
  112. Now the URL looks like
  113. .nf
  114. https://example.com/?shoes=2&hat=1&candy=N%26N`
  115. .fi
  116. .SH AVALABILITY
  117. The URL API was introduced in libcurl 7.62.0.
  118. .SH "SEE ALSO"
  119. .BR curl_url "(3), " curl_url_cleanup "(3), " curl_url_get "(3), "
  120. .BR curl_url_dup "(3), " curl_url_set "(3), " curl_url_strerror "(3), "
  121. .BR CURLOPT_URL "(3)"