39 #include "PicoSHA2/picosha2.h"
41 #include <BESInternalError.h>
44 #include <TheBESKeys.h>
46 #include "HttpCache.h"
47 #include "HttpUtils.h"
48 #include "HttpNames.h"
52 #define AT_EXIT(x) atexit((x))
56 #define prolog string("HttpCache::").append(__func__).append("() - ")
62 using 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 string HttpCache::getCacheDirFromConfig() {
95 msg << prolog <<
"The BES Key " << HTTP_CACHE_DIR_KEY <<
" is not set.";
96 BESDEBUG(HTTP_MODULE, msg.str() << endl);
103 string HttpCache::getCachePrefixFromConfig() {
112 msg << prolog <<
"The BES Key " << HTTP_CACHE_PREFIX_KEY <<
" is not set.";
113 BESDEBUG(HTTP_MODULE, msg.str() << endl);
120 HttpCache::HttpCache() {
121 BESDEBUG(HTTP_MODULE, prolog <<
"BEGIN" << endl);
123 string cacheDir = getCacheDirFromConfig();
124 string cachePrefix = getCachePrefixFromConfig();
125 unsigned long cacheSizeMbytes = getCacheSizeFromConfig();
127 BESDEBUG(HTTP_MODULE, prolog <<
"Cache configuration params: " << cacheDir <<
", " << cachePrefix <<
", "
128 << cacheSizeMbytes << endl);
129 initialize(cacheDir, cachePrefix, cacheSizeMbytes);
131 BESDEBUG(HTTP_MODULE, prolog <<
"END" << endl);
135 HttpCache::HttpCache(
const string &cache_dir,
const string &prefix,
unsigned long long size) {
137 BESDEBUG(HTTP_MODULE, prolog <<
"BEGIN" << endl);
141 BESDEBUG(HTTP_MODULE, prolog <<
"END" << endl);
147 unsigned long long max_cache_size) {
148 if (d_enabled && d_instance == 0) {
150 d_instance =
new HttpCache(cache_dir, cache_file_prefix, max_cache_size);
155 BESDEBUG(HTTP_MODULE,
"HttpCache::" << __func__ <<
"() - " <<
"Cache is DISABLED" << endl);
157 AT_EXIT(delete_instance);
159 BESDEBUG(HTTP_MODULE,
"HttpCache::" << __func__ <<
"() - " <<
"Cache is ENABLED" << endl);
173 if (d_enabled && d_instance == 0) {
180 BESDEBUG(HTTP_MODULE, prolog <<
"Cache is DISABLED" << endl);
182 AT_EXIT(delete_instance);
184 BESDEBUG(HTTP_MODULE, prolog <<
"Cache is ENABLED" << endl);
188 BESDEBUG(HTTP_MODULE,
189 "[ERROR] HttpCache::get_instance(): Failed to obtain cache! msg: " << bie.
get_message()
197 #if HASH_CACHE_FILENAME
200 HttpCache::get_hash(
const string &s)
203 string msg =
"You cannot hash the empty string.";
204 BESDEBUG(HTTP_MODULE, prolog << msg << endl);
207 return picosha2::hash256_hex_string(s[0] ==
'/' ? s :
"/" + s);
211 bool is_url(
const string &candidate){
212 size_t index = candidate.find(HTTP_PROTOCOL);
214 index = candidate.find(HTTPS_PROTOCOL);
231 string real_name_extension;
235 if(is_url(identifier)) {
240 path_part = target_url.path();
243 path_part = identifier;
246 vector<string> path_elements;
249 if(!path_elements.empty()){
250 string last = path_elements.back();
251 if(last != path_part)
252 real_name_extension =
"#" + last;
254 return real_name_extension;
267 stringstream cache_filename;
269 string real_name_extension;
273 uid_part = uid +
"_";
276 hashed_part = get_hash(src_id);
279 hashed_part = src_id;
virtual std::string get_message()
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)