/usr/include/libdap/HTTPCacheTable.h is in libdap-dev 3.11.1-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | // -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#ifndef _http_cache_table_h
#define _http_cache_table_h
//#define DODS_DEBUG
#include <pthread.h>
#ifdef WIN32
#include <io.h> // stat for win32? 09/05/02 jhrg
#endif
#include <string>
#include <vector>
#include <map>
#ifndef _http_cache_h
#include "HTTPCache.h"
#endif
#ifndef _error_h
#include "Error.h"
#endif
#ifndef _internalerr_h
#include "InternalErr.h"
#endif
#ifndef _util_h
#include "util.h"
#endif
#ifndef _debug_h
#include "debug.h"
#endif
#define LOCK(m) do { \
int code = pthread_mutex_lock((m)); \
if (code != 0) \
throw InternalErr(__FILE__, __LINE__, "Mutex lock: " + long_to_string(code)); \
} while(0);
#define UNLOCK(m) do { \
int code = pthread_mutex_unlock((m)); \
if (code != 0) \
throw InternalErr(__FILE__, __LINE__, "Mutex unlock: " + long_to_string(code)); \
} while(0);
#define TRYLOCK(m) pthread_mutex_trylock((m))
#define INIT(m) pthread_mutex_init((m), 0)
#define DESTROY(m) pthread_mutex_destroy((m))
using namespace std;
namespace libdap
{
int get_hash(const string &url);
/** The table of entries in the client-side cache. This class maintains a table
of CacheEntries, where one instance of CacheEntry is made for
each item in the cache. When an item is accessed it is either
locked for reading or writing. When locked for reading the entry is
recorded on a list of read-locked entries. The caller must explicitly
free the entry for it to be removed from this list (which is the only
way it can be opened for writing). An entry can be accessed by multiple
readers but only one writer.
@note The CacheEntry class used to contain a lock that was used to ensure
that the entry was locked during any changes to any of its fields. That
has been removed - its now the responsibility of the caller. This change
was made because it's likely the caller will need to lock all of the methods
that operate on a CacheEntry anyway, so the CacheEntry-specific lock was
redundant. */
class HTTPCacheTable {
public:
/** A struct used to store information about responses in the
cache's volatile memory.
About entry locking: An entry is locked using both a mutex and a
counter. The counter keeps track of how many clients are accessing a
given entry while the mutex provides a guarantee that updates to the
counter are MT-safe. In addition, the HTTPCacheTable object maintains a
map which binds the FILE* returned to a client with a given entry.
This way the client can tell the HTTPCacheTable object that it is done
with <code>FILE *response</code> and the class can arrange to update
the lock counter and mutex. */
struct CacheEntry {
private:
string url; // Location
int hash;
int hits; // Hit counts
string cachename;
string etag;
time_t lm; // Last modified
time_t expires;
time_t date; // From the response header.
time_t age;
time_t max_age; // From Cache-Control
unsigned long size; // Size of cached entity body
bool range; // Range is not currently supported. 10/02/02 jhrg
time_t freshness_lifetime;
time_t response_time;
time_t corrected_initial_age;
bool must_revalidate;
bool no_cache; // This field is not saved in the index.
int readers;
pthread_mutex_t d_response_lock; // set if being read
pthread_mutex_t d_response_write_lock; // set if being written
// Allow HTTPCacheTable methods access and the test class, too
friend class HTTPCacheTable;
friend class HTTPCacheTest;
// Allow access by the functors used in HTTPCacheTable
friend class DeleteCacheEntry;
friend class WriteOneCacheEntry;
friend class DeleteExpired;
friend class DeleteByHits;
friend class DeleteBySize;
public:
string get_cachename()
{
return cachename;
}
string get_etag()
{
return etag;
}
time_t get_lm()
{
return lm;
}
time_t get_expires()
{
return expires;
}
time_t get_max_age()
{
return max_age;
}
void set_size(unsigned long sz)
{
size = sz;
}
time_t get_freshness_lifetime()
{
return freshness_lifetime;
}
time_t get_response_time()
{
return response_time;
}
time_t get_corrected_initial_age()
{
return corrected_initial_age;
}
bool get_must_revalidate()
{
return must_revalidate;
}
void set_no_cache(bool state)
{
no_cache = state;
}
bool is_no_cache()
{
return no_cache;
}
void lock_read_response()
{
DBG(cerr << "Try locking read response... (" << hex << &d_response_lock << dec << ") ");
int status = TRYLOCK(&d_response_lock);
if (status != 0 /*&& status == EBUSY*/) {
// If locked, wait for any writers
LOCK(&d_response_write_lock);
UNLOCK(&d_response_write_lock);
};
DBGN(cerr << "Done" << endl);
readers++; // REcord number of readers
}
void unlock_read_response()
{
readers--;
if (readers == 0) {
DBG(cerr << "Unlocking read response... (" << hex << &d_response_lock << dec << ") ");
UNLOCK(&d_response_lock);
DBGN(cerr << "Done" << endl);
}
}
void lock_write_response()
{
DBG(cerr << "locking write response... (" << hex << &d_response_lock << dec << ") ");
LOCK(&d_response_lock);
LOCK(&d_response_write_lock);
DBGN(cerr << "Done" << endl);
}
void unlock_write_response()
{
DBG(cerr << "Unlocking write response... (" << hex << &d_response_lock << dec << ") ");
UNLOCK(&d_response_write_lock);
UNLOCK(&d_response_lock);
DBGN(cerr << "Done" << endl);
}
CacheEntry() :
url(""), hash(-1), hits(0), cachename(""), etag(""), lm(-1), expires(-1), date(-1), age(-1), max_age(-1),
size(0), range(false), freshness_lifetime(0), response_time(0), corrected_initial_age(0),
must_revalidate(false), no_cache(false), readers(0)
{
INIT(&d_response_lock);
INIT(&d_response_write_lock);
}
CacheEntry(const string &u) :
url(u), hash(-1), hits(0), cachename(""), etag(""), lm(-1), expires(-1), date(-1), age(-1), max_age(-1),
size(0), range(false), freshness_lifetime(0), response_time(0), corrected_initial_age(0),
must_revalidate(false), no_cache(false), readers(0)
{
INIT(&d_response_lock);
INIT(&d_response_write_lock);
hash = get_hash(url);
}
};
// Typedefs for CacheTable. A CacheTable is a vector of vectors of
// CacheEntries. The outer vector is accessed using the hash value.
// Entries with matching hashes occupy successive positions in the inner
// vector (that's how hash collisions are resolved). Search the inner
// vector for a specific match.
typedef vector<CacheEntry *> CacheEntries;
typedef CacheEntries::iterator CacheEntriesIter;
typedef CacheEntries **CacheTable;// Array of pointers to CacheEntries
friend class HTTPCacheTest;
private:
CacheTable d_cache_table;
string d_cache_root;
unsigned int d_block_size; // File block size.
unsigned long d_current_size;
string d_cache_index;
int d_new_entries;
map<FILE *, HTTPCacheTable::CacheEntry *> d_locked_entries;
// Make these private to prevent use
HTTPCacheTable(const HTTPCacheTable &)
{
throw InternalErr(__FILE__, __LINE__, "unimplemented");
}
HTTPCacheTable &operator=(const HTTPCacheTable &)
{
throw InternalErr(__FILE__, __LINE__, "unimplemented");
}
HTTPCacheTable()
{
throw InternalErr(__FILE__, __LINE__, "unimplemented");
}
CacheTable &get_cache_table()
{
return d_cache_table;
}
CacheEntry *get_locked_entry_from_cache_table(int hash, const string &url); /*const*/
public:
HTTPCacheTable(const string &cache_root, int block_size);
~HTTPCacheTable();
//@{ @name Accessors/Mutators
unsigned long get_current_size() const
{
return d_current_size;
}
void set_current_size(unsigned long sz)
{
d_current_size = sz;
}
unsigned int get_block_size() const
{
return d_block_size;
}
void set_block_size(unsigned int sz)
{
d_block_size = sz;
}
int get_new_entries() const
{
return d_new_entries;
}
void increment_new_entries()
{
++d_new_entries;
}
string get_cache_root()
{
return d_cache_root;
}
void set_cache_root(const string &cr)
{
d_cache_root = cr;
}
//@}
void delete_expired_entries(time_t time = 0);
void delete_by_hits(int hits);
void delete_by_size(unsigned int size);
void delete_all_entries();
bool cache_index_delete();
bool cache_index_read();
CacheEntry *cache_index_parse_line(const char *line);
void cache_index_write();
string create_hash_directory(int hash);
void create_location(CacheEntry *entry);
void add_entry_to_cache_table(CacheEntry *entry);
void remove_cache_entry(HTTPCacheTable::CacheEntry *entry);
void remove_entry_from_cache_table(const string &url);
CacheEntry *get_locked_entry_from_cache_table(const string &url);
CacheEntry *get_write_locked_entry_from_cache_table(const string &url);
void calculate_time(HTTPCacheTable::CacheEntry *entry, int default_expiration, time_t request_time);
void parse_headers(HTTPCacheTable::CacheEntry *entry, unsigned long max_entry_size, const vector<string> &headers);
// These should move back to HTTPCache
void bind_entry_to_data(CacheEntry *entry, FILE *body);
void uncouple_entry_from_data(FILE *body);
bool is_locked_read_responses();
};
} // namespace libdap
#endif
|