libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
HTTPCache.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2002,2008 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#ifndef _http_cache_h
27#define _http_cache_h
28
29#include <pthread.h>
30
31#ifdef WIN32
32#include <io.h> // stat for win32? 09/05/02 jhrg
33#endif
34
35#include <map>
36#include <string>
37#include <vector>
38
39#include "HTTPCacheTable.h" // included for macros
40
41#include "HTTPCacheDisconnectedMode.h"
42// using namespace std;
43
44namespace libdap {
45
46class HTTPCacheTabe;
47
48// This function is exported so the test code can use it too.
49bool is_hop_by_hop_header(const string &header);
50
102class HTTPCache {
103private:
104 string d_cache_root;
105 FILE *d_locked_open_file; // Lock for single process use.
106
107 bool d_cache_enabled;
108 bool d_cache_protected;
109 CacheDisconnectedMode d_cache_disconnected;
110 bool d_expire_ignored;
111 bool d_always_validate;
112
113 unsigned long d_total_size; // How much can we store?
114 unsigned long d_folder_size; // How much of that is meta data?
115 unsigned long d_gc_buffer; // How much memory needed as buffer?
116 unsigned long d_max_entry_size; // Max individual entry size.
117 int d_default_expiration;
118
119 vector<string> d_cache_control;
120 // these are values read from a request-directive Cache-Control header.
121 // Not to be confused with values read from the response or a cached
122 // response (e.g., CacheEntry has a max_age field, too). These fields are
123 // set when the set_cache_control method is called.
124 time_t d_max_age;
125 time_t d_max_stale; // -1: not set, 0:any response, >0 max time.
126 time_t d_min_fresh;
127
128 // Lock non-const methods (also ones that use the STL).
129 pthread_mutex_t d_cache_mutex;
130
131 HTTPCacheTable *d_http_cache_table;
132
133 // d_open_files is used by the interrupt handler to clean up
134 vector<string> d_open_files;
135
136 static HTTPCache *_instance;
137
138 friend class HTTPCacheTest; // Unit tests
139 friend class HTTPConnectTest;
140
141 friend class HTTPCacheInterruptHandler;
142
143 // Private methods
144 HTTPCache(const HTTPCache &);
145 HTTPCache();
146 HTTPCache &operator=(const HTTPCache &);
147
148 HTTPCache(string cache_root, bool force);
149
150 static void delete_instance(); // Run by atexit (hence static)
151
152 void set_cache_root(const string &root = "");
153 void create_cache_root(const string &cache_root);
154
155 // These will go away when the cache can be used by multiple processes.
156 bool get_single_user_lock(bool force = false);
157 void release_single_user_lock();
158
159 bool is_url_in_cache(const string &url);
160
161 // I made these four methods so they could be tested by HTTPCacheTest.
162 // Otherwise they would be static functions. jhrg 10/01/02
163 void write_metadata(const string &cachename, const vector<string> &headers);
164 void read_metadata(const string &cachename, vector<string> &headers);
165 int write_body(const string &cachename, const FILE *src);
166 FILE *open_body(const string &cachename);
167
168 bool stopGC() const;
169 bool startGC() const;
170
171 void perform_garbage_collection();
172 void too_big_gc();
173 void expired_gc();
174 void hits_gc();
175
176public:
177 static HTTPCache *instance(const string &cache_root, bool force = false);
178 virtual ~HTTPCache();
179
180 string get_cache_root() const;
181
182 void set_cache_enabled(bool mode);
183 bool is_cache_enabled() const;
184
187
188 void set_expire_ignored(bool mode);
189 bool is_expire_ignored() const;
190
191 void set_max_size(unsigned long size);
192 unsigned long get_max_size() const;
193
194 void set_max_entry_size(unsigned long size);
195 unsigned long get_max_entry_size() const;
196
197 void set_default_expiration(int exp_time);
198 int get_default_expiration() const;
199
200 void set_always_validate(bool validate);
201 bool get_always_validate() const;
202
203 void set_cache_control(const vector<string> &cc);
204 vector<string> get_cache_control();
205
206 void lock_cache_interface() {
207 DBG(cerr << "Locking interface... ");
208 LOCK(&d_cache_mutex);
209 DBGN(cerr << "Done" << endl);
210 }
211 void unlock_cache_interface() {
212 DBG(cerr << "Unlocking interface... ");
213 UNLOCK(&d_cache_mutex);
214 DBGN(cerr << "Done" << endl);
215 }
216
217 // This must lock for writing
218 bool cache_response(const string &url, time_t request_time, const vector<string> &headers, const FILE *body);
219 void update_response(const string &url, time_t request_time, const vector<string> &headers);
220
221 // This is separate from get_cached_response() because often an invalid
222 // cache entry just needs a header update. That is best left to the HTTP
223 // Connection code.
224 bool is_url_valid(const string &url);
225
226 // Lock these for reading
227 vector<string> get_conditional_request_headers(const string &url);
228 FILE *get_cached_response(const string &url, vector<string> &headers, string &cacheName);
229 FILE *get_cached_response(const string &url, vector<string> &headers);
230 FILE *get_cached_response(const string &url);
231
232 void release_cached_response(FILE *response);
233
234 void purge_cache();
235};
236
237} // namespace libdap
238
239#endif // _http_cache_h
CacheDisconnectedMode get_cache_disconnected() const
Definition HTTPCache.cc:626
bool cache_response(const string &url, time_t request_time, const vector< string > &headers, const FILE *body)
static HTTPCache * instance(const string &cache_root, bool force=false)
Definition HTTPCache.cc:125
vector< string > get_cache_control()
Definition HTTPCache.cc:822
void set_expire_ignored(bool mode)
Definition HTTPCache.cc:636
void set_default_expiration(int exp_time)
Definition HTTPCache.cc:742
string get_cache_root() const
Definition HTTPCache.cc:480
void set_cache_disconnected(CacheDisconnectedMode mode)
Definition HTTPCache.cc:616
void release_cached_response(FILE *response)
vector< string > get_conditional_request_headers(const string &url)
unsigned long get_max_entry_size() const
Definition HTTPCache.cc:730
void set_cache_enabled(bool mode)
Definition HTTPCache.cc:592
unsigned long get_max_size() const
Definition HTTPCache.cc:693
void set_max_entry_size(unsigned long size)
Definition HTTPCache.cc:703
bool get_always_validate() const
Definition HTTPCache.cc:763
int get_default_expiration() const
Definition HTTPCache.cc:752
bool is_url_valid(const string &url)
void set_always_validate(bool validate)
Definition HTTPCache.cc:758
void update_response(const string &url, time_t request_time, const vector< string > &headers)
void set_max_size(unsigned long size)
Definition HTTPCache.cc:664
void set_cache_control(const vector< string > &cc)
Definition HTTPCache.cc:781
virtual ~HTTPCache()
Definition HTTPCache.cc:284
FILE * get_cached_response(const string &url, vector< string > &headers, string &cacheName)
bool is_cache_enabled() const
Definition HTTPCache.cc:602
top level DAP object to house generic methods
Definition AISConnect.cc:30
bool is_hop_by_hop_header(const string &header)
Definition HTTPCache.cc:850