Disk ARchive: mycurl_easyhandle_node.hpp Source File

| Disk ARchive 2.8.1 Full featured and portable backup and archiving tool

Loading...
Searching...
No Matches
mycurl_easyhandle_node.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2025 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
31extern "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
47namespace 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 { CURLOPT_NEW_DIRECTORY_PERMS, type_long },
181 { CURLOPT_FTP_CREATE_MISSING_DIRS, type_long },
182 // eolist is needed to flag the end of list, option type does not matter
183 { CURLOPT_APPEND, eolist }
184 };
185
186 static opttype get_opt_type(CURLoption opt);
187
188 static bool defaults_initialized;
189 static mycurl_param_list defaults;
190
191 template <class T> void check_for_type(CURLoption opt, const T & val)
192 {
193 switch(get_opt_type(opt))
194 {
195 case type_string:
196 if(typeid(val) != typeid(std::string))
197 throw SRC_BUG;
198 break;
199 case type_secu_string:
200 if(typeid(val) != typeid(secu_string))
201 throw SRC_BUG;
202 break;
203 case type_pointer:
204 if(typeid(val) != typeid(void *))
205 throw SRC_BUG;
206 break;
207 case type_long:
208 if(typeid(val) != typeid(long))
209 throw SRC_BUG;
210 break;
211 case type_mycurl_slist:
212 if(typeid(val) != typeid(mycurl_slist))
213 throw SRC_BUG;
214 break;
215 case type_curl_off_t:
216 if(typeid(val) != typeid(curl_off_t))
217 throw SRC_BUG;
218 break;
219 case eolist:
220 throw SRC_BUG;
221 default:
222 throw SRC_BUG;
223 }
224 }
225 };
226
227#endif
228
230
231} // end of namespace
232
233#endif
libdar::tools_printf
std::string tools_printf(const char *format,...)
make printf-like formating to a std::string
my_config.h
include macro defined by the configure script and some specific additional ones
mycurl_param_list.hpp
wrapper for element a CURL* object can receive as parameter in order to be put in etherogeneous list
mycurl_slist.hpp
wrapper of the curl_slist struct to allow usual copy/move on C++ object
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47
tools.hpp
a set of general purpose routines
user_interaction.hpp
defines the interaction interface between libdar and users.