| 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 |
#include <davixcontext.hpp> |
| 24 |
|
| 25 |
#include <utils/stringutils.hpp> |
| 26 |
#include <utils/davix_uri.hpp> |
| 27 |
#include <modules/modules_profiles.hpp> |
| 28 |
#include <backend/SessionFactory.hpp> |
| 29 |
#include <davix_context_internal.hpp> |
| 30 |
#include <core/RedirectionResolver.hpp> |
| 31 |
|
| 32 |
#include <curl/curl.h> |
| 33 |
|
| 34 |
#include <set> |
| 35 |
#include <mutex> |
| 36 |
|
| 37 |
namespace Davix{ |
| 38 |
|
| 39 |
struct LibPath; |
| 40 |
|
| 41 |
|
| 42 |
static LibPath lib_path; |
| 43 |
|
| 44 |
|
| 45 |
static bool redirCachingDisabled(){ |
| 46 |
return ( getenv("DAVIX_DISABLE_REDIRECT_CACHING") != NULL); |
| 47 |
} |
| 48 |
|
| 49 |
/// Implementation f the core logic in davix |
| 50 |
struct ContextInternal |
| 51 |
{ |
| 52 |
ContextInternal(): |
| 53 |
_fsess(new SessionFactory()), |
| 54 |
_redirectionResolver(new RedirectionResolver(!redirCachingDisabled())), |
| 55 |
_hook_list() |
| 56 |
{ |
| 57 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_CORE, "libdavix path {}, version: {}", getLibPath(), version()); |
| 58 |
} |
| 59 |
|
| 60 |
ContextInternal(const ContextInternal & orig) : |
| 61 |
_fsess(new SessionFactory()), |
| 62 |
_redirectionResolver(new RedirectionResolver(!redirCachingDisabled())), |
| 63 |
_hook_list(orig._hook_list) |
| 64 |
{ |
| 65 |
} |
| 66 |
|
| 67 |
virtual ~ContextInternal(){} |
| 68 |
|
| 69 |
// implementation of getSessionFactory |
| 70 |
inline SessionFactory* getSessionFactory(){ |
| 71 |
return _fsess.get(); |
| 72 |
} |
| 73 |
|
| 74 |
inline RedirectionResolver* getRedirectionResolver() { |
| 75 |
return _redirectionResolver.get(); |
| 76 |
} |
| 77 |
|
| 78 |
std::unique_ptr<SessionFactory> _fsess; |
| 79 |
std::unique_ptr<RedirectionResolver> _redirectionResolver; |
| 80 |
HookList _hook_list; |
| 81 |
}; |
| 82 |
|
| 83 |
/////////////////////////////////////////////////////////////// |
| 84 |
////////////////////////////////////////////////////////////// |
| 85 |
////////////////////////////////////////////////////////////// |
| 86 |
|
| 87 |
|
| 88 |
Context::Context() : |
| 89 |
_intern(new ContextInternal()) |
| 90 |
{ |
| 91 |
} |
| 92 |
|
| 93 |
Context::Context(const Context &c) : |
| 94 |
_intern(new ContextInternal(*(c._intern))){ |
| 95 |
} |
| 96 |
|
| 97 |
Context & Context::operator=(const Context & c){ |
| 98 |
if( this != &c ){ |
| 99 |
if( _intern != NULL) |
| 100 |
delete _intern; |
| 101 |
_intern = new ContextInternal(*(c._intern)); |
| 102 |
} |
| 103 |
return *this; |
| 104 |
} |
| 105 |
|
| 106 |
Context::~Context(){ |
| 107 |
delete _intern; |
| 108 |
} |
| 109 |
|
| 110 |
Context* Context::clone(){ |
| 111 |
return new Context(*this); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void Context::setSessionCaching(bool caching){ |
| 116 |
_intern->_fsess->setSessionCaching(caching); |
| 117 |
} |
| 118 |
|
| 119 |
bool Context::getSessionCaching() const{ |
| 120 |
return _intern->_fsess->getSessionCaching(); |
| 121 |
} |
| 122 |
|
| 123 |
void Context::clearCache() { |
| 124 |
_intern->_fsess.reset(new SessionFactory()); |
| 125 |
} |
| 126 |
|
| 127 |
HttpRequest* Context::createRequest(const std::string & url, DavixError** err){ |
| 128 |
return new HttpRequest(*this, Uri(url), err); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
HttpRequest* Context::createRequest(const Uri &uri, DavixError **err){ |
| 133 |
return new HttpRequest(*this, uri, err); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
void Context::loadModule(const std::string &name){ |
| 138 |
if( StrUtil::compare_ncase("grid",name) == 0){ |
| 139 |
loadGridProfile(*this); |
| 140 |
return; |
| 141 |
} |
| 142 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_CORE, "No module named {} found", name); |
| 143 |
} |
| 144 |
|
| 145 |
HookList & Context::getHookList(){ |
| 146 |
return _intern->_hook_list; |
| 147 |
} |
| 148 |
|
| 149 |
SessionFactory& ContextExplorer::SessionFactoryFromContext(Context & c){ |
| 150 |
return *(c._intern->getSessionFactory()); |
| 151 |
} |
| 152 |
|
| 153 |
RedirectionResolver & ContextExplorer::RedirectionResolverFromContext(Context &c) { |
| 154 |
return *c._intern->getRedirectionResolver(); |
| 155 |
} |
| 156 |
|
| 157 |
LibPath::LibPath(){ |
| 158 |
Dl_info shared_lib_infos; |
| 159 |
|
| 160 |
// do an address resolution on a local function |
| 161 |
// get this resolution to determine davix shared library path at runtime |
| 162 |
if( dladdr((void*) &version, &shared_lib_infos) != 0){ |
| 163 |
path = shared_lib_infos.dli_fname; |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
const std::string & version(){ |
| 169 |
static const std::string _version = DAVIX_VERSION_STRING; |
| 170 |
return _version; |
| 171 |
} |
| 172 |
|
| 173 |
const std::string backendRuntimeVersion() { |
| 174 |
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); |
| 175 |
|
| 176 |
std::ostringstream ss; |
| 177 |
ss << data->version << " " << data->ssl_version; |
| 178 |
|
| 179 |
return ss.str(); |
| 180 |
} |
| 181 |
|
| 182 |
const std::string backendHeadersVersion() { |
| 183 |
std::ostringstream ss; |
| 184 |
ss << LIBCURL_VERSION; |
| 185 |
|
| 186 |
return ss.str(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
const std::string & getLibPath(){ |
| 191 |
return lib_path.path; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
} // End Davix |