| 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 <string> |
| 23 |
#include <algorithm> |
| 24 |
#include <mutex> |
| 25 |
#include <davix_internal.hpp> |
| 26 |
#include "neonsessionfactory.hpp" |
| 27 |
#include <backend/SessionFactory.hpp> |
| 28 |
|
| 29 |
#include <utils/davix_logger_internal.hpp> |
| 30 |
|
| 31 |
#define SSTR(message) static_cast<std::ostringstream&>(std::ostringstream().flush() << message).str() |
| 32 |
|
| 33 |
namespace Davix { |
| 34 |
|
| 35 |
NeonHandle::~NeonHandle() { |
| 36 |
if(session) { |
| 37 |
ne_session_destroy(session); |
| 38 |
session = NULL; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
//------------------------------------------------------------------------------ |
| 43 |
// Check if session caching is disabled from environment variables |
| 44 |
//------------------------------------------------------------------------------ |
| 45 |
static bool isSessionCachingDisabled(){ |
| 46 |
return ( getenv("DAVIX_DISABLE_SESSION_CACHING") != NULL); |
| 47 |
} |
| 48 |
|
| 49 |
static std::once_flag neon_once; |
| 50 |
|
| 51 |
static void init_neon(){ |
| 52 |
ne_sock_init(); |
| 53 |
} |
| 54 |
|
| 55 |
NEONSessionFactory::NEONSessionFactory() : _session_caching(!isSessionCachingDisabled()) { |
| 56 |
std::call_once(neon_once, &init_neon); |
| 57 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "HTTP/SSL Session caching {}", (_session_caching?"ENABLED":"DISABLED")); |
| 58 |
} |
| 59 |
|
| 60 |
NEONSessionFactory::~NEONSessionFactory(){ |
| 61 |
_session_pool.clear(); |
| 62 |
} |
| 63 |
|
| 64 |
//------------------------------------------------------------------------------ |
| 65 |
// Create a NEONSession. |
| 66 |
//------------------------------------------------------------------------------ |
| 67 |
std::unique_ptr<NEONSession> NEONSessionFactory::provideNEONSession(const Uri &uri, const RequestParams ¶ms, DavixError **err) { |
| 68 |
NeonHandlePtr internal_session = createNeonSession(params, uri, err); |
| 69 |
if(!internal_session) { |
| 70 |
return {}; |
| 71 |
} |
| 72 |
|
| 73 |
return std::unique_ptr<NEONSession>(new NEONSession(*this, std::move(internal_session), uri, params, err)); |
| 74 |
} |
| 75 |
|
| 76 |
NeonHandlePtr NEONSessionFactory::createNeonSession(const RequestParams & params, const Uri & uri, DavixError **err){ |
| 77 |
if(uri.getStatus() == StatusCode::OK){ |
| 78 |
std::string scheme = SessionFactory::httpizeProtocol(uri.getProtocol()); |
| 79 |
if(scheme.size() > 0){ |
| 80 |
return create_recycled_session(params, scheme, uri.getHost(), httpUriGetPort(uri)); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
DavixError::setupError(err, davix_scope_http_request(), StatusCode::UriParsingError, fmt::format("impossible to parse {}, not a valid HTTP, S3 or Webdav URL", uri.getString())); |
| 85 |
return NeonHandlePtr(); |
| 86 |
} |
| 87 |
|
| 88 |
void NEONSessionFactory::storeNeonSession(NeonHandlePtr sess){ |
| 89 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "add old session to cache {}", sess->key.c_str()); |
| 90 |
_session_pool.insert(sess->key, sess); |
| 91 |
} |
| 92 |
|
| 93 |
NeonHandlePtr NEONSessionFactory::create_session(const RequestParams & params, const std::string & protocol, const std::string &host, unsigned int port){ |
| 94 |
ne_session *se; |
| 95 |
se = ne_session_create(protocol.c_str(), host.c_str(), (int) port); |
| 96 |
|
| 97 |
const Uri* proxy = params.getProxyServer(); |
| 98 |
if(se != NULL && proxy != NULL){ |
| 99 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, " configure mandatory proxy to {}", proxy->getString().c_str()); |
| 100 |
const enum ne_sock_sversion version = ((proxy->getProtocol().compare("socks5") ==0)?NE_SOCK_SOCKSV5:NE_SOCK_SOCKSV4); |
| 101 |
const int port_proxy = ((proxy->getPort() ==0)?1080:(proxy->getPort())); |
| 102 |
const std::string & userinfo = proxy->getUserInfo(); |
| 103 |
std::string user, password; |
| 104 |
std::string::const_iterator delimiter = std::find(userinfo.begin(), userinfo.end(), ':'); |
| 105 |
|
| 106 |
if(delimiter != userinfo.end()){ |
| 107 |
user.assign(std::string(userinfo.begin(), delimiter)); |
| 108 |
password.assign((delimiter+1), userinfo.end()); |
| 109 |
ne_session_socks_proxy(se, version, proxy->getHost().c_str(), port_proxy, user.c_str(), password.c_str()); |
| 110 |
}else{ |
| 111 |
ne_session_socks_proxy(se, version, proxy->getHost().c_str(), port_proxy, NULL, NULL); |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
//ne_ssl_trust_default_ca(se); not stable in neon on epel 5 |
| 117 |
return NeonHandlePtr(new NeonHandle(create_map_keys_from_URL(protocol, host, port), se)); |
| 118 |
} |
| 119 |
|
| 120 |
NeonHandlePtr NEONSessionFactory::create_recycled_session(const RequestParams & params, const std::string &protocol, const std::string &host, unsigned int port){ |
| 121 |
|
| 122 |
if(params.getKeepAlive()){ |
| 123 |
NeonHandlePtr out; |
| 124 |
if(_session_pool.retrieve(create_map_keys_from_URL(protocol, host, port), out)) { |
| 125 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "cached ne_session found ! taken from cache "); |
| 126 |
return out; |
| 127 |
} |
| 128 |
} |
| 129 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "no cached ne_session, create a new one "); |
| 130 |
return create_session(params, protocol, host, port); |
| 131 |
} |
| 132 |
|
| 133 |
std::string create_map_keys_from_URL(const std::string & protocol, const std::string &host, unsigned int port){ |
| 134 |
return SSTR(protocol << host << ":" << port); |
| 135 |
} |
| 136 |
|
| 137 |
//------------------------------------------------------------------------------ |
| 138 |
// Set caching on or off |
| 139 |
//------------------------------------------------------------------------------ |
| 140 |
void NEONSessionFactory::setSessionCaching(bool caching) { |
| 141 |
std::lock_guard<std::mutex> lock(_session_caching_mtx); |
| 142 |
_session_caching = caching && !isSessionCachingDisabled(); |
| 143 |
} |
| 144 |
|
| 145 |
//------------------------------------------------------------------------------ |
| 146 |
// Get caching status |
| 147 |
//------------------------------------------------------------------------------ |
| 148 |
bool NEONSessionFactory::getSessionCaching() const { |
| 149 |
std::lock_guard<std::mutex> lock(_session_caching_mtx); |
| 150 |
return _session_caching; |
| 151 |
} |
| 152 |
|
| 153 |
} // namespace Davix |