Disk ARchive  2.7.15
Full featured and portable backup and archiving tool
mycurl_easyhandle_node.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2024 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // to contact the author, see the AUTHOR file
20 /*********************************************************************/
21 
25 
26 #ifndef MYCURL_EASYHANDLE_NODE_HPP
27 #define MYCURL_EASYHANDLE_NODE_HPP
28 
29 #include "../my_config.h"
30 
31 extern "C"
32 {
33 #if LIBCURL_AVAILABLE
34 #if HAVE_CURL_CURL_H
35 #include <curl/curl.h>
36 #endif
37 #endif
38 } // end extern "C"
39 
40 #include <string>
41 #include <typeinfo>
42 #include "mycurl_param_list.hpp"
43 #include "user_interaction.hpp"
44 #include "mycurl_slist.hpp"
45 #include "tools.hpp"
46 
47 namespace libdar
48 {
51 
52 #if LIBCURL_AVAILABLE
53 
54 
56 
57  class mycurl_easyhandle_node
58  {
59  public:
61  mycurl_easyhandle_node() { init(); };
62 
64  mycurl_easyhandle_node(const mycurl_easyhandle_node & ref);
65 
67  mycurl_easyhandle_node(mycurl_easyhandle_node && ref) noexcept;
68 
70  mycurl_easyhandle_node & operator = (const mycurl_easyhandle_node & ref);
71 
73  mycurl_easyhandle_node & operator = (mycurl_easyhandle_node && ref) noexcept;
74 
76  ~mycurl_easyhandle_node() { if(handle != nullptr) curl_easy_cleanup(handle); };
77 
79  template<class T> void setopt(CURLoption opt, const T & val) { check_for_type(opt, val); wanted.add(opt, val); }
80 
82  void setopt_list(const mycurl_param_list & listing) { (void) wanted.update_with(listing); };
83 
85  void setopt_default(CURLoption opt);
86 
88  void setopt_all_default();
89 
91  void apply(const std::shared_ptr<user_interaction> & dialog,
92  U_I wait_seconds,
93  const bool & end_anyway = false);
94 
96 
97  template<class T> void getinfo(CURLINFO info, T* val)
98  {
99  CURLcode err = curl_easy_getinfo(handle, info, val);
100  if(err != CURLE_OK)
101  throw Erange("mycurl_easyhandle_node::getinfo",
102  tools_printf(gettext("Error met while fetching info %d: %s"),
103  (S_I)info,
104  curl_easy_strerror(err)));
105  }
106 
107  static void init_defaults(); // must be called once libgcrypt has been initialized (due to secu_string presence in the defaults)
108  static void release_defaults() { defaults.clear(); }; // must be called before libgcrypt is cleaned up
109 
110  private:
111 
113  // object level fields and methods
114  //
115 
116  enum opttype
117  {
118  type_string,
119  type_secu_string,
120  type_pointer,
121  type_long,
122  type_mycurl_slist,
123  type_curl_off_t,
124  eolist
125  };
126 
127  CURL *handle;
128  mycurl_param_list current;
129  mycurl_param_list wanted;
130 
131  void init();
132  template<class T>void set_to_default(CURLoption opt)
133  {
134  const T* ptr;
135 
136  if(current.get_val(opt, ptr))
137  {
138  if(defaults.get_val(opt, ptr))
139  wanted.add(opt, *ptr);
140  else
141  throw SRC_BUG;
142  }
143  else
144  wanted.clear(opt);
145  }
146 
147 
149  // class level fields and methods
150  //
151 
152  struct opt_asso
153  {
154  CURLoption opt;
155  opttype cast;
156  };
157 
158  static constexpr const opt_asso association[] =
159  {
160  { CURLOPT_APPEND, type_long },
161  { CURLOPT_DIRLISTONLY, type_long },
162  { CURLOPT_NETRC, type_long },
163  { CURLOPT_NOBODY, type_long },
164  { CURLOPT_SSH_KNOWNHOSTS, type_string },
165  { CURLOPT_SSH_PUBLIC_KEYFILE, type_string },
166  { CURLOPT_SSH_PRIVATE_KEYFILE, type_string },
167  { CURLOPT_SSH_AUTH_TYPES, type_long },
168  { CURLOPT_QUOTE, type_mycurl_slist },
169  { CURLOPT_RANGE, type_string },
170  { CURLOPT_READDATA, type_pointer },
171  { CURLOPT_READFUNCTION, type_pointer },
172  { CURLOPT_RESUME_FROM_LARGE, type_curl_off_t },
173  { CURLOPT_UPLOAD, type_long },
174  { CURLOPT_URL, type_string },
175  { CURLOPT_USERNAME, type_string },
176  { CURLOPT_USERPWD, type_secu_string },
177  { CURLOPT_VERBOSE, type_long },
178  { CURLOPT_WRITEDATA, type_pointer },
179  { CURLOPT_WRITEFUNCTION, type_pointer },
180  // eolist is needed to flag the end of list, option type does not matter
181  { CURLOPT_APPEND, eolist }
182  };
183 
184  static opttype get_opt_type(CURLoption opt);
185 
186  static bool defaults_initialized;
187  static mycurl_param_list defaults;
188 
189  template <class T> void check_for_type(CURLoption opt, const T & val)
190  {
191  switch(get_opt_type(opt))
192  {
193  case type_string:
194  if(typeid(val) != typeid(std::string))
195  throw SRC_BUG;
196  break;
197  case type_secu_string:
198  if(typeid(val) != typeid(secu_string))
199  throw SRC_BUG;
200  break;
201  case type_pointer:
202  if(typeid(val) != typeid(void *))
203  throw SRC_BUG;
204  break;
205  case type_long:
206  if(typeid(val) != typeid(long))
207  throw SRC_BUG;
208  break;
209  case type_mycurl_slist:
210  if(typeid(val) != typeid(mycurl_slist))
211  throw SRC_BUG;
212  break;
213  case type_curl_off_t:
214  if(typeid(val) != typeid(curl_off_t))
215  throw SRC_BUG;
216  break;
217  case eolist:
218  throw SRC_BUG;
219  default:
220  throw SRC_BUG;
221  }
222  }
223  };
224 
225 #endif
226 
228 
229 } // end of namespace
230 
231 #endif
std::string tools_printf(const char *format,...)
make printf-like formating to a std::string
wrapper for element a CURL* object can receive as parameter in order to be put in etherogeneous list
wrapper of the curl_slist struct to allow usual copy/move on C++ object
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
a set of general purpose routines
defines the interaction interface between libdar and users.