00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _http_cache_h
00027 #define _http_cache_h
00028
00029 #include <pthread.h>
00030
00031 #ifdef WIN32
00032 #include <io.h>
00033 #endif
00034
00035 #include <string>
00036 #include <vector>
00037 #include <map>
00038
00039 #ifndef _error_h
00040 #include "Error.h"
00041 #endif
00042
00043 #ifndef _internalerr_h
00044 #include "InternalErr.h"
00045 #endif
00046
00047
00048
00049
00050
00051
00052
00053 #ifndef _response_too_big_err_h
00054 #include "ResponseTooBigErr.h"
00055 #endif
00056
00057 #ifndef _http_cache_disconnected_mode_h
00058 #include "HTTPCacheDisconnectedMode.h"
00059 #endif
00060
00061 #ifndef _signal_handler_registered_err_h
00062 #include "SignalHandlerRegisteredErr.h"
00063 #endif
00064
00065 const int CACHE_TABLE_SIZE = 1499;
00066
00067 using namespace std;
00068
00069 namespace libdap
00070 {
00071
00129 class HTTPCache
00130 {
00131 public:
00143 struct CacheEntry
00144 {
00145 string url;
00146 int hash;
00147 int hits;
00148
00149 string cachename;
00150
00151 string etag;
00152 time_t lm;
00153 time_t expires;
00154 time_t date;
00155 time_t age;
00156 time_t max_age;
00157
00158 unsigned long size;
00159 bool range;
00160
00161
00162 time_t freshness_lifetime;
00163 time_t response_time;
00164 time_t corrected_initial_age;
00165
00166 bool must_revalidate;
00167 bool no_cache;
00168
00169 int locked;
00170 pthread_mutex_t lock ;
00171
00172 CacheEntry() : url(""), hash(-1), hits(0), cachename(""),
00173 etag(""), lm(-1),
00174 expires(-1), date(-1), age(-1), max_age(-1), size(0),
00175 range(false), freshness_lifetime(0), response_time(0),
00176 corrected_initial_age(0), must_revalidate(false),
00177 no_cache(false), locked(0)
00178 {}
00179 };
00180
00181 #ifdef WIN32
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 unsigned long d_max_entry_size;
00192
00193 void remove_cache_entry(CacheEntry *entry);
00194 bool stopGC() const;
00195 #endif
00196
00197 private:
00198 string d_cache_root;
00199 string d_cache_index;
00200 FILE *d_locked_open_file;
00201
00202 bool d_cache_enabled;
00203 bool d_cache_protected;
00204 CacheDisconnectedMode d_cache_disconnected;
00205 bool d_expire_ignored;
00206 bool d_always_validate;
00207
00208 unsigned long d_total_size;
00209 unsigned long d_folder_size;
00210 unsigned long d_gc_buffer;
00211 #ifndef WIN32 // Declared public above for win32
00212 unsigned long d_max_entry_size;
00213 #endif
00214 unsigned long d_current_size;
00215 int d_default_expiration;
00216 unsigned int d_block_size;
00217
00218 vector<string> d_cache_control;
00219
00220
00221
00222
00223 time_t d_max_age;
00224 time_t d_max_stale;
00225 time_t d_min_fresh;
00226
00227 int d_new_entries;
00228
00229
00230 pthread_mutex_t d_cache_mutex;
00231
00232
00233
00234
00235
00236
00237 typedef vector<CacheEntry *> CachePointers;
00238 typedef CachePointers::iterator CachePointersIter;
00239
00240
00241
00242
00243 typedef CachePointers *CacheTable[CACHE_TABLE_SIZE];
00244
00245 CacheTable d_cache_table;
00246
00247 map<FILE *, CacheEntry *> d_locked_entries;
00248 vector<string> d_open_files;
00249
00250 static HTTPCache *_instance;
00251
00252 friend class HTTPCacheTest;
00253 friend class HTTPCacheInterruptHandler;
00254
00255
00256
00257 friend class DeleteExpired;
00258 friend class DeleteByHits;
00259 friend class DeleteCacheEntry;
00260 friend class DeleteUnlockedCacheEntry;
00261 friend class WriteOneCacheEntry;
00262
00263
00264
00265 void clone(const HTTPCache &)
00266 {}
00267
00268 HTTPCache(const HTTPCache &cache)
00269 {
00270 clone(cache);
00271 }
00272
00273 HTTPCache()
00274 {}
00275
00276 HTTPCache(string cache_root, bool force) throw(Error);
00277
00278 HTTPCache &operator=(const HTTPCache &rhs)
00279 {
00280 if (this != &rhs)
00281 clone(rhs);
00282 return *this;
00283 }
00284
00285 static void delete_instance();
00286
00287 CacheEntry *cache_index_parse_line(const char *line);
00288 bool cache_index_read();
00289 bool cache_index_delete();
00290
00291 void set_cache_root(const string &root = "");
00292
00293 bool get_single_user_lock(bool force = false);
00294 void release_single_user_lock();
00295
00296 void add_entry_to_cache_table(CacheEntry *e);
00297 void remove_entry_from_cache_table(const string &url);
00298 void parse_headers(CacheEntry *entry, const vector<string> &headers);
00299 void calculate_time(CacheEntry *entry, time_t request_time);
00300 #ifndef WIN32 // Declared public above for win32
00301 void remove_cache_entry(CacheEntry *entry);
00302 #endif
00303 CacheEntry *get_entry_from_cache_table(const string &url) const;
00304 CacheEntry *get_entry_from_cache_table(int hash, const string &url) const;
00305
00306
00307
00308
00309 void write_metadata(const string &cachename, const vector<string> &headers);
00310 void read_metadata(const string &cachename, vector<string> &headers);
00311 int write_body(const string &cachename, const FILE *src);
00312 FILE *open_body(const string &cachename);
00313
00314 void create_cache_root(const string &cache_root);
00315
00316 string create_hash_directory(int hash);
00317 void create_location(CacheEntry *entry);
00318
00319 #ifndef WIN32 // Declared public above for win32
00320 bool stopGC() const;
00321 #endif
00322 bool startGC() const;
00323
00324 void cache_index_write();
00325
00326 void perform_garbage_collection();
00327 void expired_gc();
00328 void hits_gc();
00329
00330 public:
00331 static HTTPCache *instance(const string &cache_root, bool force = false);
00332 virtual ~HTTPCache();
00333
00334 string get_cache_root() const;
00335
00336 void set_cache_enabled(bool mode);
00337 bool is_cache_enabled() const;
00338
00339 void set_cache_protected(bool mode);
00340 bool is_cache_protected() const;
00341
00342 void set_cache_disconnected(CacheDisconnectedMode mode);
00343 CacheDisconnectedMode get_cache_disconnected() const;
00344
00345 void set_expire_ignored(bool mode);
00346 bool is_expire_ignored() const;
00347
00348 void set_max_size(unsigned long size);
00349 unsigned long get_max_size() const;
00350
00351 void set_max_entry_size(unsigned long size);
00352 unsigned long get_max_entry_size() const;
00353
00354 void set_default_expiration(int exp_time);
00355 int get_default_expiration() const;
00356
00357 void set_always_validate(bool validate);
00358 bool get_always_validate() const;
00359
00360 void set_cache_control(const vector<string> &cc);
00361 vector<string> get_cache_control();
00362
00363 bool cache_response(const string &url, time_t request_time,
00364 const vector<string> &headers, const FILE *body);
00365 vector<string> get_conditional_request_headers(const string &url);
00366 void update_response(const string &url, time_t request_time,
00367 const vector<string> &headers);
00368
00369 bool is_url_in_cache(const string &url);
00370 bool is_url_valid(const string &url);
00371 FILE *get_cached_response(const string &url, vector<string> &headers);
00372 FILE *get_cached_response(const string &url, vector<string> &headers,
00373 string &cacheName);
00374 FILE *get_cached_response_body(const string &url);
00375 void release_cached_response(FILE *response);
00376
00377 void purge_cache();
00378 };
00379
00380 }
00381
00382 #endif // _http_cache_h