libcurl-url.3 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. .\" generated by cd2nroff 0.1 from libcurl-url.md
  2. .TH libcurl-url 3 "2025-01-17" libcurl
  3. .SH NAME
  4. libcurl\-url \- URL interface overview
  5. .SH DESCRIPTION
  6. The URL interface provides functions for parsing and generating URLs.
  7. .SH INCLUDE
  8. You still only include <curl/curl.h> in your code.
  9. .SH CREATE
  10. Create a handle that holds URL info and resources with \fIcurl_url(3)\fP:
  11. .nf
  12. CURLU *h = curl_url();
  13. .fi
  14. .SH CLEANUP
  15. When done with it, clean it up with \fIcurl_url_cleanup(3)\fP
  16. .nf
  17. curl_url_cleanup(h);
  18. .fi
  19. .SH DUPLICATE
  20. When you need a copy of a handle, just duplicate it with \fIcurl_url_dup(3)\fP:
  21. .nf
  22. CURLU *nh = curl_url_dup(h);
  23. .fi
  24. .SH PARSING
  25. By setting a URL to the handle with \fIcurl_url_set(3)\fP, the URL is parsed
  26. and stored in the handle. If the URL is not syntactically correct it returns
  27. an error instead.
  28. .nf
  29. rc = curl_url_set(h, CURLUPART_URL,
  30. "https://example.com:449/foo/bar?name=moo", 0);
  31. .fi
  32. The zero in the fourth argument is a bitmask for changing specific features.
  33. If successful, this stores the URL in its individual parts within the handle.
  34. .SH REDIRECT
  35. When a handle already contains info about a URL, setting a relative URL makes
  36. it "redirect" to that.
  37. .nf
  38. rc = curl_url_set(h, CURLUPART_URL, "../test?another", 0);
  39. .fi
  40. .SH GET URL
  41. The \fBCURLU\fP handle represents a URL and you can easily extract that with
  42. \fIcurl_url_get(3)\fP:
  43. .nf
  44. char *url;
  45. rc = curl_url_get(h, CURLUPART_URL, &url, 0);
  46. curl_free(url);
  47. .fi
  48. The zero in the fourth argument is a bitmask for changing specific features.
  49. .SH GET PARTS
  50. When a URL has been parsed or parts have been set, you can extract those
  51. pieces from the handle at any time.
  52. .nf
  53. rc = curl_url_get(h, CURLUPART_FRAGMENT, &fragment, 0);
  54. rc = curl_url_get(h, CURLUPART_HOST, &host, 0);
  55. rc = curl_url_get(h, CURLUPART_PASSWORD, &password, 0);
  56. rc = curl_url_get(h, CURLUPART_PATH, &path, 0);
  57. rc = curl_url_get(h, CURLUPART_PORT, &port, 0);
  58. rc = curl_url_get(h, CURLUPART_QUERY, &query, 0);
  59. rc = curl_url_get(h, CURLUPART_SCHEME, &scheme, 0);
  60. rc = curl_url_get(h, CURLUPART_USER, &user, 0);
  61. rc = curl_url_get(h, CURLUPART_ZONEID, &zoneid, 0);
  62. .fi
  63. Extracted parts are not URL decoded unless the user also asks for it with the
  64. \fICURLU_URLDECODE\fP flag set in the fourth bitmask argument.
  65. Remember to free the returned string with \fIcurl_free(3)\fP when you are done
  66. with it!
  67. .SH SET PARTS
  68. A user set individual URL parts, either after having parsed a full URL or
  69. instead of parsing such.
  70. .nf
  71. rc = curl_url_set(urlp, CURLUPART_FRAGMENT, "anchor", 0);
  72. rc = curl_url_set(urlp, CURLUPART_HOST, "www.example.com", 0);
  73. rc = curl_url_set(urlp, CURLUPART_PASSWORD, "doe", 0);
  74. rc = curl_url_set(urlp, CURLUPART_PATH, "/index.html", 0);
  75. rc = curl_url_set(urlp, CURLUPART_PORT, "443", 0);
  76. rc = curl_url_set(urlp, CURLUPART_QUERY, "name=john", 0);
  77. rc = curl_url_set(urlp, CURLUPART_SCHEME, "https", 0);
  78. rc = curl_url_set(urlp, CURLUPART_USER, "john", 0);
  79. rc = curl_url_set(urlp, CURLUPART_ZONEID, "eth0", 0);
  80. .fi
  81. Set parts are not URL encoded unless the user asks for it with the
  82. \fICURLU_URLENCODE\fP flag.
  83. .SH CURLU_APPENDQUERY
  84. An application can append a string to the right end of the query part with the
  85. \fICURLU_APPENDQUERY\fP flag to \fIcurl_url_set(3)\fP.
  86. Imagine a handle that holds the URL "https://example.com/?shoes=2". An
  87. application can then add the string "hat=1" to the query part like this:
  88. .nf
  89. rc = curl_url_set(urlp, CURLUPART_QUERY, "hat=1", CURLU_APPENDQUERY);
  90. .fi
  91. It notices the lack of an ampersand (&) separator and injects one, and the
  92. handle\(aqs full URL then equals "https://example.com/?shoes=2&hat=1".
  93. The appended string can of course also get URL encoded on add, and if asked to
  94. URL encode, the encoding process skips the \(aq=\(aq character. For example, append
  95. \&"candy=N&N" to what we already have, and URL encode it to deal with the
  96. ampersand in the data:
  97. .nf
  98. rc = curl_url_set(urlp, CURLUPART_QUERY, "candy=N&N",
  99. CURLU_APPENDQUERY | CURLU_URLENCODE);
  100. .fi
  101. Now the URL looks like
  102. .nf
  103. https://example.com/?shoes=2&hat=1&candy=N%26N
  104. .fi
  105. .SH NOTES
  106. A URL with a literal IPv6 address can be parsed even when IPv6 support is not
  107. enabled.
  108. .SH SEE ALSO
  109. .BR CURLOPT_URL (3),
  110. .BR curl_url (3),
  111. .BR curl_url_cleanup (3),
  112. .BR curl_url_dup (3),
  113. .BR curl_url_get (3),
  114. .BR curl_url_set (3),
  115. .BR curl_url_strerror (3)