Debian Codegraph @

src/params/davixrequestparams.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2013
4 * Author: Adrien Devresse <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include <davix_internal.hpp>
23
24 #include <utils/davix_logger_internal.hpp>
25 #include <params/davixrequestparams.hpp>
26 #include <libs/time_utils.h>
27 #include <utils/davix_gcloud_utils.hpp>
28
29
30
31 namespace Davix {
32
33
34
35 const char * default_agent = "libdavix/" DAVIX_VERSION_STRING;
36 volatile int state_value =0;
37 std::mutex state_value_mtx;
38
39 inline int get_requeste_uid(){
40 std::lock_guard<std::mutex> lock(state_value_mtx);
41 state_value +=1;
42 return state_value;
43 }
44
45
46 #define SESSION_FLAG_KEEP_ALIVE 0x01
47
48 struct X509Data{
49 X509Data() : _pair(static_cast<authCallbackClientCertX509>(NULL),static_cast<void*>(NULL)), _x509_fun(), _cred(){}
50
51 std::pair<authCallbackClientCertX509, void*> _pair;
52
53 authFunctionClientCertX509 _x509_fun;
54
55 X509Credential _cred;
56
57 static X509Data* instance(std::shared_ptr<X509Data> & cred_ptr){
58 if(cred_ptr.get() == NULL){
59 cred_ptr.reset(new X509Data());
60 }
61 return cred_ptr.get();
62 }
63
64 static X509Data* reset(std::shared_ptr<X509Data> & cred_ptr){
65 cred_ptr.reset(new X509Data());
66 return cred_ptr.get();
67 }
68
69 int cred_callback(const SessionInfo & info, X509Credential& cert){
70 (void) info;
71 cert = _cred;
72 return 0;
73 }
74
75 int c_callback(const SessionInfo & info, X509Credential& cert){
76 DavixError* tmp_err=NULL;
77 int ret = -1;
78 if(_pair.first){
79 ret = _pair.first(_pair.second, info, &cert, &tmp_err);
80 }
81 Davix::checkDavixError(&tmp_err);
82 return ret;
83 }
84 };
85
86 struct RequestParamsInternal{
87 RequestParamsInternal() :
88 _ssl_check(true),
89 _redirection(true),
90 _recursive_mode(false),
91 _s3_listing_mode(S3ListingMode::Hierarchical),
92 _swift_listing_mode(SwiftListingMode::Hierarchical),
93 _s3_max_key_entries(10000),
94 _ca_path(),
95 _x509_data(),
96 _idlogpass(),
97 _call_loginpswwd(NULL),
98 _call_loginpswwd_userdata(NULL),
99 _aws_cred(),
100 _aws_region(),
101 _aws_token(),
102 _aws_alternate(false),
103 _azure_key(),
104 _gcloud_creds(),
105 _os_token(),
106 _os_project_id(),
107 _swift_account(),
108 ops_timeout(),
109 connexion_timeout(),
110 agent_string(default_agent),
111 _proto(RequestProtocol::Auto),
112 _metalink_mode(MetalinkMode::Auto),
113 _customhdr(),
114 _proxy_server(),
115 _session_flag(SESSION_FLAG_KEEP_ALIVE),
116 _state_uid(get_requeste_uid()),
117 _transferCb(),
118 retry_number(default_retry_number),
119 retry_delay(),
120 _copy_mode(CopyMode::Push),
121 _support_100continue(true),
122 _accepted_retry(180), // wait for half an hour by default
123 _accepted_delay(10)
124 {
125 timespec_clear(&connexion_timeout);
126 timespec_clear(&ops_timeout);
127 connexion_timeout.tv_sec = DAVIX_DEFAULT_CONN_TIMEOUT;
128 ops_timeout.tv_sec = DAVIX_DEFAULT_OPS_TIMEOUT;
129 }
130
131 virtual ~RequestParamsInternal(){
132 if(_idlogpass.second.empty() == false){
133 // eradicate password from memory
134 for(std::string::iterator it = _idlogpass.second.begin(); it < _idlogpass.second.end(); ++it)
135 *it = 'a';
136 }
137
138 }
139 RequestParamsInternal(const RequestParamsInternal & param_private):
140 _ssl_check(param_private._ssl_check),
141 _redirection(param_private._redirection),
142 _recursive_mode(param_private._recursive_mode),
143 _s3_listing_mode(param_private._s3_listing_mode),
144 _swift_listing_mode(param_private._swift_listing_mode),
145 _s3_max_key_entries(param_private._s3_max_key_entries),
146 _ca_path(param_private._ca_path),
147 _x509_data(param_private._x509_data),
148 _idlogpass(param_private._idlogpass),
149 _call_loginpswwd(param_private._call_loginpswwd),
150 _call_loginpswwd_userdata(param_private._call_loginpswwd_userdata),
151 _aws_cred(param_private._aws_cred),
152 _aws_region(param_private._aws_region),
153 _aws_token(param_private._aws_token),
154 _aws_alternate(param_private._aws_alternate),
155 _azure_key(param_private._azure_key),
156 _gcloud_creds(param_private._gcloud_creds),
157 _os_token(param_private._os_token),
158 _os_project_id(param_private._os_project_id),
159 _swift_account(param_private._swift_account),
160 ops_timeout(),
161 connexion_timeout(),
162 agent_string(param_private.agent_string),
163 _proto(param_private._proto),
164 _metalink_mode(param_private._metalink_mode),
165 _customhdr(param_private._customhdr),
166 _proxy_server(param_private._proxy_server),
167 _session_flag(param_private._session_flag),
168 _state_uid(param_private._state_uid),
169 _transferCb(param_private._transferCb),
170 retry_number(param_private.retry_number),
171 retry_delay(param_private.retry_delay),
172 _copy_mode(param_private._copy_mode),
173 _support_100continue(param_private._support_100continue),
174 _accepted_retry(param_private._accepted_retry),
175 _accepted_delay(param_private._accepted_delay) {
176
177 timespec_copy(&(connexion_timeout), &(param_private.connexion_timeout));
178 timespec_copy(&(ops_timeout), &(param_private.ops_timeout));
179 }
180 bool _ssl_check; // ssl CA check
181 bool _redirection; // redirection support
182 bool _recursive_mode; // recursive mode for get/put collections
183
184 // s3 bucket listing mode
185 S3ListingMode::S3ListingMode _s3_listing_mode;
186
187 // swift listing mode
188 SwiftListingMode::SwiftListingMode _swift_listing_mode;
189
190 // Max number of keys returned by a S3 list bucket request
191 unsigned long _s3_max_key_entries;
192
193 // CA management
194 std::vector<std::string> _ca_path;
195
196 // auth info
197 std::shared_ptr<X509Data> _x509_data;
198
199 std::pair<std::string,std::string> _idlogpass;
200 authCallbackLoginPasswordBasic _call_loginpswwd;
201 void* _call_loginpswwd_userdata;
202 std::pair<AwsSecretKey, AwsAccessKey> _aws_cred;
203 AwsRegion _aws_region;
204 AwsToken _aws_token;
205 bool _aws_alternate;
206 AzureSecretKey _azure_key;
207 gcloud::Credentials _gcloud_creds;
208 OSToken _os_token;
209 OSProjectID _os_project_id;
210 SwiftAccount _swift_account;
211
212 // timeout management
213 struct timespec ops_timeout;
214 struct timespec connexion_timeout;
215
216 // user agent
217 std::string agent_string;
218
219 // proto
220 RequestProtocol::Protocol _proto;
221 MetalinkMode::MetalinkMode _metalink_mode;
222
223 // additional custom header lines
224 HeaderVec _customhdr;
225
226 // Proxy server URI
227 std::shared_ptr<Uri> _proxy_server;
228
229 // session flag
230 int _session_flag;
231
232 // ssl state value, a state uid is used to check if two copy of a requestParam struct are equal
233 int _state_uid;
234
235 // transfer cb
236 TransferMonitorCB _transferCb;
237
238 // retry attempts
239 int retry_number;
240
241 // delay in seconds between retry attempts
242 int retry_delay;
243
244 // 3rd party copy mode
245 CopyMode::CopyMode _copy_mode;
246
247 // whether server has support for 100-Continue
248 bool _support_100continue;
249
250 // number of retries in case davix receives 202-Accepted
251 int _accepted_retry;
252
253 // delay in seconds between retries in case davix receives 202-Accepted
254 int _accepted_delay;
255
256 // method
257 inline void regenerateStateUid(){
258 _state_uid = get_requeste_uid();
259 }
260
261 private:
262 RequestParamsInternal & operator=(const RequestParamsInternal & params);
263 };
264
265
266 RequestParams::RequestParams() :
267 d_ptr(new RequestParamsInternal())
268 {
269
270 }
271
272 RequestParams::RequestParams(const RequestParams& params) :
273 d_ptr(new RequestParamsInternal(*(params.d_ptr))){
274
275 }
276
277
278
279
280 RequestParams::~RequestParams(){
281 delete d_ptr;
282 }
283
284 RequestParams::RequestParams(const RequestParams* params) :
285 d_ptr( ((params)?(new RequestParamsInternal(*(params->d_ptr))):(new RequestParamsInternal())) ){
286
287 }
288
289
290 RequestParams & RequestParams::operator=(const RequestParams & orig){
291 if(d_ptr != orig.d_ptr)
292 delete d_ptr;
293 d_ptr = new RequestParamsInternal(*(orig.d_ptr));
294 return *this;
295 }
296
297
298 bool RequestParams::getSSLCACheck() const{
299 return d_ptr->_ssl_check;
300 }
301
302 void RequestParams::setSSLCAcheck(bool chk){
303 d_ptr->regenerateStateUid();
304 d_ptr->_ssl_check = chk;
305 }
306
307
308 void RequestParams::setClientCertX509(const X509Credential & cli_cert){
309 using namespace std;
310 d_ptr->regenerateStateUid();
311 X509Data* x509 = X509Data::reset(d_ptr->_x509_data);
312 x509->_cred = cli_cert;
313 x509->_x509_fun = std::bind(&X509Data::cred_callback, x509, std::placeholders::_1, std::placeholders::_2);
314 }
315
316 void RequestParams::setClientLoginPassword(const std::string & login, const std::string & password){
317 d_ptr->regenerateStateUid();
318 d_ptr->_idlogpass = std::make_pair(login, password);
319 }
320
321 const std::pair<std::string, std::string> & RequestParams::getClientLoginPassword() const{
322 return d_ptr->_idlogpass;
323 }
324
325 const X509Credential & RequestParams::getClientCertX509() const{
326 X509Data* x509 = X509Data::instance(d_ptr->_x509_data);
327 return x509->_cred;
328 }
329
330 /// set a callback for X509 client side dynamic authentication
331 /// this function overwrite \ref setClientCertX509
332 void RequestParams::setClientCertCallbackX509(authCallbackClientCertX509 callback, void* userdata){
333 using namespace std;
334 d_ptr->regenerateStateUid();
335 X509Data* x509 = X509Data::reset(d_ptr->_x509_data);
336 x509->_pair = std::make_pair(callback, userdata);
337 x509->_x509_fun = std::bind(&X509Data::c_callback, x509, std::placeholders::_1, std::placeholders::_2);
338 }
339
340
341 void RequestParams::setClientCertFunctionX509(const authFunctionClientCertX509 &callback){
342 d_ptr->regenerateStateUid();
343 X509Data* x509 = X509Data::reset(d_ptr->_x509_data);
344 x509->_x509_fun= callback;
345 }
346
347 const authFunctionClientCertX509 & RequestParams::getClientCertFunctionX509() const{
348 X509Data* x509 = X509Data::instance(d_ptr->_x509_data);
349 return x509->_x509_fun;
350 }
351
352 /// return the current client side callback for authentication with the current user data
353 std::pair<authCallbackClientCertX509,void*> RequestParams::getClientCertCallbackX509() const{
354 X509Data* x509 = X509Data::instance(d_ptr->_x509_data);
355 return x509->_pair;
356 }
357
358 /// set a callback for X509 client side dynamic authentication
359 /// this function overwrite \ref setClientCertX509
360 void RequestParams::setClientLoginPasswordCallback(authCallbackLoginPasswordBasic callback, void* userdata){
361 d_ptr->regenerateStateUid();
362 d_ptr->_call_loginpswwd = callback;
363 d_ptr->_call_loginpswwd_userdata = userdata;
364 }
365
366 /// return the current client side callback for authentication with the current user data
367 std::pair<authCallbackLoginPasswordBasic,void*> RequestParams::getClientLoginPasswordCallback() const{
368 return std::pair<authCallbackLoginPasswordBasic,void*>(d_ptr->_call_loginpswwd, d_ptr->_call_loginpswwd_userdata);
369 }
370
371
372 void RequestParams::setAwsAuthorizationKeys(const std::string &secret_key, const std::string &access_key){
373 d_ptr->_aws_cred = std::pair<AwsSecretKey, AwsAccessKey>(secret_key,access_key);
374 }
375
376 const std::pair<AwsSecretKey, AwsAccessKey> & RequestParams::getAwsAutorizationKeys() const{
377 return d_ptr->_aws_cred;
378 }
379
380 void RequestParams::setAwsRegion(const AwsRegion &region) {
381 d_ptr->_aws_region = region;
382 }
383
384 const AwsRegion & RequestParams::getAwsRegion() const {
385 return d_ptr->_aws_region;
386 }
387
388 void RequestParams::setAwsToken(const AwsToken &token) {
389 d_ptr->_aws_token = token;
390 }
391
392 const AwsToken & RequestParams::getAwsToken() const {
393 return d_ptr->_aws_token;
394 }
395
396 void RequestParams::setAwsAlternate(const bool &alternate) {
397 d_ptr->_aws_alternate = alternate;
398 }
399
400 const bool & RequestParams::getAwsAlternate() const {
401 return d_ptr->_aws_alternate;
402 }
403
404 void RequestParams::setAzureKey(const AzureSecretKey &key) {
405 d_ptr->_azure_key = key;
406 }
407
408 const AzureSecretKey & RequestParams::getAzureKey() const {
409 return d_ptr->_azure_key;
410 }
411
412 void RequestParams::setGcloudCredentials(const gcloud::Credentials &creds) {
413 d_ptr->_gcloud_creds = creds;
414 }
415
416 const gcloud::Credentials & RequestParams::getGcloudCredentials() const {
417 return d_ptr->_gcloud_creds;
418 }
419
420 void RequestParams::setOSToken(const OSToken &token) {
421 d_ptr->_os_token = token;
422 }
423
424 const OSToken & RequestParams::getOSToken() const {
425 return d_ptr->_os_token;
426 }
427
428 void RequestParams::setOSProjectID(const OSProjectID &id) {
429 d_ptr->_os_project_id = id;
430 }
431
432 const OSProjectID & RequestParams::getOSProjectID() const {
433 return d_ptr->_os_project_id;
434 }
435
436 void RequestParams::setSwiftAccount(const SwiftAccount &account) {
437 d_ptr->_swift_account = account;
438 }
439
440 const SwiftAccount & RequestParams::getSwiftAccount() const {
441 return d_ptr->_swift_account;
442 }
443
444 void RequestParams::setS3ListingMode(const S3ListingMode::S3ListingMode s3_listing_mode){
445 d_ptr->_s3_listing_mode = s3_listing_mode;
446 }
447
448 S3ListingMode::S3ListingMode RequestParams::getS3ListingMode() const{
449 return d_ptr->_s3_listing_mode;
450 }
451
452 void RequestParams::setSwiftListingMode(const SwiftListingMode::SwiftListingMode swift_listing_mode){
453 d_ptr->_swift_listing_mode = swift_listing_mode;
454 }
455
456 SwiftListingMode::SwiftListingMode RequestParams::getSwiftListingMode() const{
457 return d_ptr->_swift_listing_mode;
458 }
459
460 void RequestParams::setS3MaxKey(const unsigned long s3_max_key_entries){
461 d_ptr->_s3_max_key_entries = s3_max_key_entries;
462 }
463
464 unsigned long RequestParams::getS3MaxKey() const{
465 return d_ptr->_s3_max_key_entries;
466 }
467
468 void RequestParams::addCertificateAuthorityPath(const std::string &path){
469 d_ptr->regenerateStateUid();
470 d_ptr->_ca_path.push_back(path);
471 }
472
473 const std::vector<std::string> & RequestParams::listCertificateAuthorityPath() const{
474 return d_ptr->_ca_path;
475 }
476
477
478 void RequestParams::setConnectionTimeout(struct timespec *conn_timeout1){
479 timespec_copy(&(d_ptr->connexion_timeout),conn_timeout1);
480 }
481
482 void RequestParams::setOperationTimeout(struct timespec *ops_timeout1){
483 timespec_copy(&(d_ptr->ops_timeout), ops_timeout1);
484 }
485
486 const struct timespec* RequestParams::getConnectionTimeout() const {
487 return &d_ptr->connexion_timeout;
488 }
489
490 const struct timespec* RequestParams::getOperationTimeout() const {
491 return &d_ptr->ops_timeout;
492 }
493
494 void RequestParams::setTransparentRedirectionSupport(bool redirection){
495 d_ptr->regenerateStateUid();
496 d_ptr->_redirection = redirection;
497 }
498
499
500 bool RequestParams::getTransparentRedirectionSupport() const{
501 return d_ptr->_redirection;
502 }
503
504 void RequestParams::setOperationRetry(int number_retry){
505 d_ptr->retry_number = number_retry;
506 }
507
508 int RequestParams::getOperationRetry() const{
509 return d_ptr->retry_number;
510 }
511
512 void RequestParams::setOperationRetryDelay(int delay_retry){
513 d_ptr->retry_delay = delay_retry;
514 }
515
516 int RequestParams::getOperationRetryDelay() const{
517 return d_ptr->retry_delay;
518 }
519
520 void RequestParams::setTransfertMonitorCb(const TransferMonitorCB &cb){
521 d_ptr->_transferCb = cb;
522 }
523
524 const TransferMonitorCB & RequestParams::getTransferMonitorCb() const{
525 return d_ptr->_transferCb;
526 }
527
528 const std::string & RequestParams::getUserAgent() const{
529 return d_ptr->agent_string;
530 }
531
532 void RequestParams::setUserAgent(const std::string &user_agent){
533 d_ptr->regenerateStateUid();
534 d_ptr->agent_string = user_agent;
535 }
536
537
538 RequestProtocol::Protocol RequestParams::getProtocol() const {
539 return d_ptr->_proto;
540 }
541
542 void RequestParams::setProtocol(const RequestProtocol::Protocol proto){
543 d_ptr->_proto = proto;
544 }
545
546 MetalinkMode::MetalinkMode RequestParams::getMetalinkMode() const{
547 return d_ptr->_metalink_mode;
548 }
549
550 void RequestParams::setMetalinkMode(const MetalinkMode::MetalinkMode mode){
551 d_ptr->_metalink_mode = mode;
552 }
553
554
555 void RequestParams::setKeepAlive(const bool keep_alive_flag){
556 d_ptr->regenerateStateUid();
557 if(keep_alive_flag)
558 d_ptr->_session_flag |= SESSION_FLAG_KEEP_ALIVE;
559 else
560 d_ptr->_session_flag &= ~(SESSION_FLAG_KEEP_ALIVE);
561 }
562
563
564 bool RequestParams::getKeepAlive() const{
565 return d_ptr->_session_flag & SESSION_FLAG_KEEP_ALIVE;
566 }
567
568
569
570 void RequestParams::addHeader(const std::string &key, const std::string &val) {
571
572 d_ptr->_customhdr.push_back( std::pair<std::string,std::string>(key, val) );
573 }
574
575 const HeaderVec & RequestParams::getHeaders() const{
576 return d_ptr->_customhdr;
577 }
578
579 void RequestParams::setProxyServer(const Uri &proxy_url){
580 d_ptr->_proxy_server.reset(new Uri(proxy_url));
581 }
582
583 const Uri* RequestParams::getProxyServer() const{
584 return d_ptr->_proxy_server.get();
585 }
586
587
588 void RequestParams::setCopyMode(const CopyMode::CopyMode copy_mode){
589 d_ptr->_copy_mode = copy_mode;
590 }
591
592 CopyMode::CopyMode RequestParams::getCopyMode() const{
593 return d_ptr->_copy_mode;
594 }
595
596 void RequestParams::setRecursiveMode(const bool recursive_mode){
597 d_ptr->_recursive_mode = recursive_mode;
598 }
599
600 bool RequestParams::getRecursiveMode() const{
601 return d_ptr->_recursive_mode;
602 }
603
604 void RequestParams::set100ContinueSupport(const bool enabled) {
605 d_ptr->_support_100continue = enabled;
606 }
607
608 bool RequestParams::get100ContinueSupport() const {
609 return d_ptr->_support_100continue;
610 }
611
612 int RequestParams::getAcceptedRetry() const {
613 return d_ptr->_accepted_retry;
614 }
615
616 void RequestParams::setAcceptedRetry(int retries) {
617 d_ptr->_accepted_retry = retries;
618 }
619
620 int RequestParams::getAcceptedRetryDelay() const {
621 return d_ptr->_accepted_delay;
622 }
623
624 void RequestParams::setAcceptedRetryDelay(int delay) {
625 d_ptr->_accepted_delay = delay;
626 }
627
628 // suppress useless warning
629 #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
630 void* RequestParams::getParmState() const{
631
632 return (void*) (d_ptr->_state_uid);
633 }
634
635 void RequestParams::swap(RequestParams & p){
636 std::swap(d_ptr, p.d_ptr);
637 }
638
639
640 } // namespace Davix