HTTPCache.h
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2008 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#ifndef _http_cache_h
#define _http_cache_h
#include <pthread.h>
#ifdef WIN32
#include <io.h> // stat for win32? 09/05/02 jhrg
#endif
#include <string>
#include <vector>
#include <map>
#include "HTTPCacheTable.h" // included for macros
#include "HTTPCacheDisconnectedMode.h"
//using namespace std;
namespace libdap
{
class HTTPCacheTabe;
// This function is exported so the test code can use it too.
bool is_hop_by_hop_header(const string &header);
class HTTPCache
{
private:
string d_cache_root;
FILE *d_locked_open_file; // Lock for single process use.
bool d_cache_enabled;
bool d_cache_protected;
CacheDisconnectedMode d_cache_disconnected;
bool d_expire_ignored;
bool d_always_validate;
unsigned long d_total_size; // How much can we store?
unsigned long d_folder_size; // How much of that is meta data?
unsigned long d_gc_buffer; // How much memory needed as buffer?
unsigned long d_max_entry_size; // Max individual entry size.
int d_default_expiration;
vector<string> d_cache_control;
// these are values read from a request-directive Cache-Control header.
// Not to be confused with values read from the response or a cached
// response (e.g., CacheEntry has a max_age field, too). These fields are
// set when the set_cache_control method is called.
time_t d_max_age;
time_t d_max_stale; // -1: not set, 0:any response, >0 max time.
time_t d_min_fresh;
// Lock non-const methods (also ones that use the STL).
pthread_mutex_t d_cache_mutex;
HTTPCacheTable *d_http_cache_table;
// d_open_files is used by the interrupt handler to clean up
vector<string> d_open_files;
static HTTPCache *_instance;
friend class HTTPCacheTest; // Unit tests
friend class HTTPConnectTest;
friend class HTTPCacheInterruptHandler;
// Private methods
HTTPCache(const HTTPCache &);
HTTPCache();
HTTPCache &operator=(const HTTPCache &);
HTTPCache(string cache_root, bool force);
static void delete_instance(); // Run by atexit (hence static)
void set_cache_root(const string &root = "");
void create_cache_root(const string &cache_root);
// These will go away when the cache can be used by multiple processes.
bool get_single_user_lock(bool force = false);
void release_single_user_lock();
bool is_url_in_cache(const string &url);
// I made these four methods so they could be tested by HTTPCacheTest.
// Otherwise they would be static functions. jhrg 10/01/02
void write_metadata(const string &cachename, const vector<string> &headers);
void read_metadata(const string &cachename, vector<string> &headers);
int write_body(const string &cachename, const FILE *src);
FILE *open_body(const string &cachename);
bool stopGC() const;
bool startGC() const;
void perform_garbage_collection();
void too_big_gc();
void expired_gc();
void hits_gc();
public:
static HTTPCache *instance(const string &cache_root, bool force = false);
virtual ~HTTPCache();
string get_cache_root() const;
void set_cache_enabled(bool mode);
bool is_cache_enabled() const;
void set_cache_disconnected(CacheDisconnectedMode mode);
CacheDisconnectedMode get_cache_disconnected() const;
void set_expire_ignored(bool mode);
bool is_expire_ignored() const;
void set_max_size(unsigned long size);
unsigned long get_max_size() const;
void set_max_entry_size(unsigned long size);
unsigned long get_max_entry_size() const;
void set_default_expiration(int exp_time);
int get_default_expiration() const;
void set_always_validate(bool validate);
bool get_always_validate() const;
void set_cache_control(const vector<string> &cc);
vector<string> get_cache_control();
void lock_cache_interface() {
DBG(cerr << "Locking interface... ");
LOCK(&d_cache_mutex);
DBGN(cerr << "Done" << endl);
}
void unlock_cache_interface() {
DBG(cerr << "Unlocking interface... " );
UNLOCK(&d_cache_mutex);
DBGN(cerr << "Done" << endl);
}
// This must lock for writing
bool cache_response(const string &url, time_t request_time,
const vector<string> &headers, const FILE *body);
void update_response(const string &url, time_t request_time,
const vector<string> &headers);
// This is separate from get_cached_response() because often an invalid
// cache entry just needs a header update. That is best left to the HTTP
// Connection code.
bool is_url_valid(const string &url);
// Lock these for reading
vector<string> get_conditional_request_headers(const string &url);
FILE *get_cached_response(const string &url, vector<string> &headers,
string &cacheName);
FILE *get_cached_response(const string &url, vector<string> &headers);
FILE *get_cached_response(const string &url);
void release_cached_response(FILE *response);
void purge_cache();
};
} // namespace libdap
#endif // _http_cache_h
libdap::HTTPCache::get_conditional_request_headers
vector< string > get_conditional_request_headers(const string &url)
Definition: HTTPCache.cc:1249
libdap::HTTPCache::get_cache_control
vector< string > get_cache_control()
Definition: HTTPCache.cc:920
libdap::HTTPCache::set_cache_enabled
void set_cache_enabled(bool mode)
Definition: HTTPCache.cc:635
libdap::HTTPCacheTable
Definition: HTTPCacheTable.h:101
libdap::HTTPCache::is_url_valid
bool is_url_valid(const string &url)
Definition: HTTPCache.cc:1388
libdap::HTTPCache::~HTTPCache
virtual ~HTTPCache()
Definition: HTTPCache.cc:302
libdap::HTTPCache::instance
static HTTPCache * instance(const string &cache_root, bool force=false)
Definition: HTTPCache.cc:129
libdap::HTTPCache::set_max_size
void set_max_size(unsigned long size)
Definition: HTTPCache.cc:724
libdap::HTTPCache::get_cache_root
string get_cache_root() const
Definition: HTTPCache.cc:516
libdap::HTTPCache::get_cached_response
FILE * get_cached_response(const string &url, vector< string > &headers, string &cacheName)
Definition: HTTPCache.cc:1480
libdap::HTTPCache::get_max_entry_size
unsigned long get_max_entry_size() const
Definition: HTTPCache.cc:803
libdap::HTTPCache
Definition: HTTPCache.h:103
libdap::HTTPCache::get_cache_disconnected
CacheDisconnectedMode get_cache_disconnected() const
Definition: HTTPCache.cc:676
libdap
Definition: AlarmHandler.h:35
libdap::HTTPCache::get_default_expiration
int get_default_expiration() const
Definition: HTTPCache.cc:831
libdap::is_hop_by_hop_header
bool is_hop_by_hop_header(const string &header)
Definition: HTTPCache.cc:954
libdap::HTTPCache::purge_cache
void purge_cache()
Definition: HTTPCache.cc:1600
libdap::HTTPCache::get_max_size
unsigned long get_max_size() const
Definition: HTTPCache.cc:758
libdap::HTTPCache::update_response
void update_response(const string &url, time_t request_time, const vector< string > &headers)
Definition: HTTPCache.cc:1319
libdap::HTTPCache::set_cache_control
void set_cache_control(const vector< string > &cc)
Definition: HTTPCache.cc:872
libdap::HTTPCache::cache_response
bool cache_response(const string &url, time_t request_time, const vector< string > &headers, const FILE *body)
Definition: HTTPCache.cc:1156
libdap::CacheDisconnectedMode
CacheDisconnectedMode
Definition: HTTPCacheDisconnectedMode.h:47
libdap::HTTPCacheInterruptHandler
Definition: HTTPCacheInterruptHandler.h:57
libdap::HTTPCache::is_cache_enabled
bool is_cache_enabled() const
Definition: HTTPCache.cc:647
libdap::HTTPCache::set_always_validate
void set_always_validate(bool validate)
Definition: HTTPCache.cc:841
libdap::HTTPCache::set_default_expiration
void set_default_expiration(int exp_time)
Definition: HTTPCache.cc:819
libdap::HTTPCache::release_cached_response
void release_cached_response(FILE *response)
Definition: HTTPCache.cc:1571
libdap::HTTPCache::set_expire_ignored
void set_expire_ignored(bool mode)
Definition: HTTPCache.cc:690
libdap::HTTPCache::get_always_validate
bool get_always_validate() const
Definition: HTTPCache.cc:850
libdap::HTTPCache::set_max_entry_size
void set_max_entry_size(unsigned long size)
Definition: HTTPCache.cc:772
libdap::HTTPCache::set_cache_disconnected
void set_cache_disconnected(CacheDisconnectedMode mode)
Definition: HTTPCache.cc:664