| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2019 |
| 4 |
* Author: Georgios Bitzes <[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 "CurlSession.hpp" |
| 23 |
#include <curl/curl.h> |
| 24 |
#include <params/davixrequestparams.hpp> |
| 25 |
#include <mutex> |
| 26 |
|
| 27 |
#define DBG(message) std::cerr << __FILE__ << ":" << __LINE__ << " -- " << #message << " = " << message << std::endl; |
| 28 |
|
| 29 |
namespace Davix { |
| 30 |
|
| 31 |
static std::once_flag curl_once; |
| 32 |
|
| 33 |
void init_curl() { |
| 34 |
curl_global_init(CURL_GLOBAL_ALL); |
| 35 |
} |
| 36 |
|
| 37 |
//------------------------------------------------------------------------------ |
| 38 |
// CurlHandle: Destructor |
| 39 |
//------------------------------------------------------------------------------ |
| 40 |
CurlHandle::~CurlHandle() { |
| 41 |
if(handle) { |
| 42 |
curl_easy_cleanup(handle); |
| 43 |
} |
| 44 |
|
| 45 |
if(mhandle) { |
| 46 |
curl_multi_cleanup(mhandle); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
//------------------------------------------------------------------------------ |
| 51 |
// CurlHandle: Constructor |
| 52 |
//------------------------------------------------------------------------------ |
| 53 |
CurlHandle::CurlHandle(const std::string &k, CURLM *mh, CURL *h) : key(k), mhandle(mh), handle(h) { |
| 54 |
curl_multi_add_handle(mhandle, handle); |
| 55 |
} |
| 56 |
|
| 57 |
//------------------------------------------------------------------------------ |
| 58 |
// Renew curl handle |
| 59 |
//------------------------------------------------------------------------------ |
| 60 |
void CurlHandle::renewHandle() { |
| 61 |
if(handle) { |
| 62 |
curl_easy_cleanup(handle); |
| 63 |
} |
| 64 |
|
| 65 |
handle = curl_easy_init(); |
| 66 |
curl_multi_add_handle(mhandle, handle); |
| 67 |
} |
| 68 |
|
| 69 |
//------------------------------------------------------------------------------ |
| 70 |
// Constructor |
| 71 |
//------------------------------------------------------------------------------ |
| 72 |
CurlSession::CurlSession(CurlSessionFactory &f, CurlHandlePtr h, const Uri & uri, const RequestParams & p, Status &st) |
| 73 |
: _factory(f), _handle(h) { |
| 74 |
|
| 75 |
configureSession(p, st); |
| 76 |
} |
| 77 |
|
| 78 |
//------------------------------------------------------------------------------ |
| 79 |
// Destructor |
| 80 |
//------------------------------------------------------------------------------ |
| 81 |
CurlSession::~CurlSession() {} |
| 82 |
|
| 83 |
//------------------------------------------------------------------------------ |
| 84 |
// Configure session |
| 85 |
//------------------------------------------------------------------------------ |
| 86 |
void CurlSession::configureSession(const RequestParams ¶ms, Status &st) { |
| 87 |
if(!params.getSSLCACheck()) { |
| 88 |
curl_easy_setopt(_handle->handle, CURLOPT_SSL_VERIFYPEER, 0L); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
} |