curl-cpp
static c++17 wrapper for curl with -fno-exceptions support
curl_url.hpp
1 #ifndef __curl_cpp_curl_url_HPP__
2 # define __curl_cpp_curl_url_HPP__
3 
4 # include "curl.hpp"
5 
6 namespace curl {
7 /**
8  * @pre for using this class: curl_t::has_CURLU()
9  * @example curl_url.cc
10  *
11  * Why make Url_ref_t RAII-less?
12  *
13  * Since url can be used by multiple Easy_ref_t, users might want to
14  * manage it using std::shared_ptr.
15  *
16  * It Url_ref_t is RAII-ed, then user would have to write
17  * std::shared_ptr<Url_ref_t>, introducing another layer of indirection
18  * while it doesn't have to.
19  *
20  * Url_ref_t's member function cannot be called in multiple threads simultaneously.
21  *
22  * "<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<fragment>"
23  */
24 class Url_ref_t {
25 protected:
26  static void check_url(int code);
27 
28 public:
29  /**
30  * If url == nullptr, then calling any member function has
31  * undefined behavior.
32  */
33  char *url;
34 
35  enum class set_code {
36  ok,
37  /**
38  * For url, it could be:
39  * - url is longer than 8000000 bytes (7MiB)
40  * - scheme too long (newest libcurl support up to 40 bytes)
41  * - url does not match standard syntax
42  * - lack necessary part, e.g. scheme, host
43  * - contain junk character <= 0x1f || == 0x7f
44  */
46  bad_port_number,
47  unsupported_scheme,
48  };
49 
50  /**
51  * @return all of set_code
52  */
53  auto set_url(const char *url_arg) noexcept -> Ret_except<set_code, std::bad_alloc>;
54 
55  /**
56  * @return only set_code::unsupported_scheme, malform_input, ok
57  */
58  auto set_scheme(const char *scheme) noexcept -> Ret_except<set_code, std::bad_alloc>;
59  /**
60  * @return only set_code::malform_input, ok
61  */
62  auto set_options(const char *options) noexcept -> Ret_except<set_code, std::bad_alloc>;
63  /**
64  * @return only set_code::malform_input, ok
65  */
66  auto set_query(const char *query) noexcept -> Ret_except<set_code, std::bad_alloc>;
67 
68  /**
69  * Deleter for curl::Url_ref_t::string.
70  */
72  void operator () (char *p) const noexcept;
73  };
74  /**
75  * std::unique_ptr for any libcurl-returned string
76  * that requires deallocation.
77  */
78  using string = std::unique_ptr<char, curl_string_deleter>;
79 
80  /**
81  * get_code::no_* can be returned by respective get_*() function.
82  */
83  enum class get_code {
84  no_scheme,
85  no_user,
86  no_passwd,
87  no_options,
88  no_host,
89  no_port,
90  no_query,
91  no_fragment,
92  };
93 
94  /**
95  * @return no_scheme or no_host
96  */
97  auto get_url() const noexcept -> Ret_except<string, get_code, std::bad_alloc>;
98 
99  auto get_scheme() const noexcept -> Ret_except<string, get_code, std::bad_alloc>;
100  auto get_options() const noexcept -> Ret_except<string, get_code, std::bad_alloc>;
101  auto get_query() const noexcept -> Ret_except<string, get_code, std::bad_alloc>;
102 };
103 } /* namespace curl */
104 
105 #endif
curl::Url_ref_t::set_code
set_code
Definition: curl_url.hpp:35
curl::Url_ref_t::curl_string_deleter
Definition: curl_url.hpp:71
curl::Url_ref_t::set_url
auto set_url(const char *url_arg) noexcept -> Ret_except< set_code, std::bad_alloc >
Definition: curl_url.cc:49
curl::Url_ref_t
Definition: curl_url.hpp:24
curl::Url_ref_t::set_options
auto set_options(const char *options) noexcept -> Ret_except< set_code, std::bad_alloc >
Definition: curl_url.cc:57
curl::Url_ref_t::set_query
auto set_query(const char *query) noexcept -> Ret_except< set_code, std::bad_alloc >
Definition: curl_url.cc:61
curl::Url_ref_t::set_code::malform_input
@ malform_input
curl::Url_ref_t::set_scheme
auto set_scheme(const char *scheme) noexcept -> Ret_except< set_code, std::bad_alloc >
Definition: curl_url.cc:53
curl::Url_ref_t::get_code
get_code
Definition: curl_url.hpp:83
curl::Url_ref_t::url
char * url
Definition: curl_url.hpp:33
curl::Url_ref_t::get_url
auto get_url() const noexcept -> Ret_except< string, get_code, std::bad_alloc >
Definition: curl_url.cc:104