39#include "PicoSHA2/picosha2.h"
41#include <BESInternalError.h>
44#include <TheBESKeys.h>
52#define AT_EXIT(x) atexit((x))
56#define prolog string("HttpCache::").append(__func__).append("() - ")
62using std::stringstream;
66 HttpCache *HttpCache::d_instance = 0;
67 bool HttpCache::d_enabled =
true;
69 unsigned long HttpCache::getCacheSizeFromConfig() {
72 unsigned long size_in_megabytes = 0;
76 std::istringstream iss(size);
77 iss >> size_in_megabytes;
80 msg << prolog <<
"The BES Key " << HTTP_CACHE_SIZE_KEY <<
" is not set.";
81 BESDEBUG(HTTP_MODULE, msg.str() << endl);
85 return size_in_megabytes;
88 unsigned long HttpCache::getCacheExpiresTime() {
91 unsigned long time_in_seconds = 0;
95 std::istringstream iss(time);
96 iss >> time_in_seconds;
98 time_in_seconds = REMOTE_RESOURCE_DEFAULT_EXPIRED_INTERVAL;
101 return time_in_seconds;
104 string HttpCache::getCacheDirFromConfig() {
111 msg << prolog <<
"The BES Key " << HTTP_CACHE_DIR_KEY <<
" is not set.";
112 BESDEBUG(HTTP_MODULE, msg.str() << endl);
119 string HttpCache::getCachePrefixFromConfig() {
128 msg << prolog <<
"The BES Key " << HTTP_CACHE_PREFIX_KEY <<
" is not set.";
129 BESDEBUG(HTTP_MODULE, msg.str() << endl);
136 HttpCache::HttpCache() {
137 BESDEBUG(HTTP_MODULE, prolog <<
"BEGIN" << endl);
139 string cacheDir = getCacheDirFromConfig();
140 string cachePrefix = getCachePrefixFromConfig();
141 unsigned long cacheSizeMbytes = getCacheSizeFromConfig();
143 BESDEBUG(HTTP_MODULE, prolog <<
"Cache configuration params: " << cacheDir <<
", " << cachePrefix <<
", "
144 << cacheSizeMbytes << endl);
145 initialize(cacheDir, cachePrefix, cacheSizeMbytes);
147 BESDEBUG(HTTP_MODULE, prolog <<
"END" << endl);
151 HttpCache::HttpCache(
const string &cache_dir,
const string &prefix,
unsigned long long size) {
153 BESDEBUG(HTTP_MODULE, prolog <<
"BEGIN" << endl);
157 BESDEBUG(HTTP_MODULE, prolog <<
"END" << endl);
163 unsigned long long max_cache_size) {
164 if (d_enabled && d_instance == 0) {
166 d_instance =
new HttpCache(cache_dir, cache_file_prefix, max_cache_size);
171 BESDEBUG(HTTP_MODULE,
"HttpCache::" << __func__ <<
"() - " <<
"Cache is DISABLED" << endl);
173 AT_EXIT(delete_instance);
175 BESDEBUG(HTTP_MODULE,
"HttpCache::" << __func__ <<
"() - " <<
"Cache is ENABLED" << endl);
189 if (d_enabled && d_instance == 0) {
196 BESDEBUG(HTTP_MODULE, prolog <<
"Cache is DISABLED" << endl);
198 AT_EXIT(delete_instance);
200 BESDEBUG(HTTP_MODULE, prolog <<
"Cache is ENABLED" << endl);
204 BESDEBUG(HTTP_MODULE,
205 "[ERROR] HttpCache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message()
213#if HASH_CACHE_FILENAME
216 HttpCache::get_hash(
const string &s)
219 string msg =
"You cannot hash the empty string.";
220 BESDEBUG(HTTP_MODULE, prolog << msg << endl);
223 return picosha2::hash256_hex_string(s[0] ==
'/' ? s :
"/" + s);
227 bool is_url(
const string &candidate){
228 size_t index = candidate.find(HTTP_PROTOCOL);
230 index = candidate.find(HTTPS_PROTOCOL);
247 string real_name_extension;
251 if(is_url(identifier)) {
256 path_part = target_url.path();
259 path_part = identifier;
262 vector<string> path_elements;
265 if(!path_elements.empty()){
266 string last = path_elements.back();
267 if(last != path_part)
268 real_name_extension =
"#" + last;
270 return real_name_extension;
283 stringstream cache_filename;
285 string real_name_extension;
289 uid_part = uid +
"_";
292 hashed_part = get_hash(src_id);
295 hashed_part = src_id;
std::string get_message() const
get the error message for this exception
bool cache_enabled() const
void initialize(const std::string &cache_dir, const std::string &prefix, unsigned long long size)
Initialize an instance of FileLockingCache.
const std::string get_cache_directory()
static bool dir_exists(const std::string &dir)
const std::string get_cache_file_prefix()
exception thrown if internal error encountered
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters="/")
static std::string lowercase(const std::string &s)
static std::string assemblePath(const std::string &firstPart, const std::string &secondPart, bool leadingSlash=false, bool trailingSlash=false)
Assemble path fragments making sure that they are separated by a single '/' character.
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
A cache for content accessed via HTTP.
static HttpCache * get_instance()
virtual std::string get_cache_file_name(const std::string &uid, const std::string &src, bool mangle=true)
utility class for the HTTP catalog module
string get_real_name_extension(const string &identifier)