44 #include <sys/types.h> 51 #define DIR_SEP_STRING "\\" 52 #define DIR_SEP_CHAR '\\' 55 #define DIR_SEP_STRING "/" 56 #define DIR_SEP_CHAR '/' 71 RCReader* RCReader::_instance = 0;
76 static pthread_once_t instance_control = PTHREAD_ONCE_INIT;
83 RCReader::write_rc_file(
const string &pathname)
85 DBG(cerr <<
"Writing the RC file to " << pathname << endl);
86 ofstream fpo(pathname.c_str());
94 fpo <<
"# OPeNDAP client configuration file. See the OPeNDAP" << endl;
95 fpo <<
"# users guide for information." << endl;
96 fpo <<
"USE_CACHE=" << _dods_use_cache << endl;
97 fpo <<
"# Cache and object size are given in megabytes (20 ==> 20Mb)." 99 fpo <<
"MAX_CACHE_SIZE=" << _dods_cache_max << endl;
100 fpo <<
"MAX_CACHED_OBJ=" << _dods_cached_obj << endl;
101 fpo <<
"IGNORE_EXPIRES=" << _dods_ign_expires << endl;
102 fpo <<
"CACHE_ROOT=" << d_cache_root << endl;
103 fpo <<
"DEFAULT_EXPIRES=" << _dods_default_expires << endl;
104 fpo <<
"ALWAYS_VALIDATE=" << _dods_always_validate << endl;
105 fpo <<
"# Request servers compress responses if possible?" << endl;
106 fpo <<
"# 1 (yes) or 0 (false)." << endl;
107 fpo <<
"DEFLATE=" << _dods_deflate << endl;
109 fpo <<
"# Should SSL certificates and hosts be validated? SSL" << endl;
110 fpo <<
"# will only work with signed certificates." << endl;
111 fpo <<
"VALIDATE_SSL=" << d_validate_ssl << endl;
113 fpo <<
"# Proxy configuration (optional parts in []s)." << endl;
114 fpo <<
"# You may also use the 'http_proxy' environment variable" 116 fpo <<
"# but a value in this file will override that env variable." 118 fpo <<
"# PROXY_SERVER=[http://][username:password@]host[:port]" 120 if (!d_dods_proxy_server_host.empty()) {
121 fpo <<
"PROXY_SERVER=" << d_dods_proxy_server_protocol <<
"://" 122 << (d_dods_proxy_server_userpw.empty()
124 : d_dods_proxy_server_userpw +
"@")
125 + d_dods_proxy_server_host
129 fpo <<
"# NO_PROXY_FOR=<host|domain>" << endl;
130 if (!d_dods_no_proxy_for_host.empty()) {
131 fpo <<
"NO_PROXY_FOR=" << d_dods_no_proxy_for_host << endl;
134 fpo <<
"# AIS_DATABASE=<file or url>" << endl;
136 fpo <<
"# COOKIE_JAR=.dods_cookies" << endl;
137 fpo <<
"# The cookie jar is a file that holds cookies sent from" 139 fpo <<
"# servers such as single signon systems. Uncomment this" 141 fpo <<
"# option and provide a file name to activate this feature." 143 fpo <<
"# If the value is a filename, it will be created in this" 145 fpo <<
"# directory; a full pathname can be used to force a specific" 147 fpo <<
"# location." << endl;
157 RCReader::read_rc_file(
const string &pathname)
159 DBG(cerr <<
"Reading the RC file from " << pathname << endl);
161 ifstream fpi(pathname.c_str());
168 char *tempstr =
new char[1024];;
171 fpi.getline(tempstr, 1023);
175 value = strchr(tempstr,
'=');
178 tokenlength = value - tempstr;
181 if ((strncmp(tempstr,
"USE_CACHE", 9) == 0)
182 && tokenlength == 9) {
183 _dods_use_cache = atoi(value) ? true :
false;
185 else if ((strncmp(tempstr,
"MAX_CACHE_SIZE", 14) == 0)
186 && tokenlength == 14) {
187 _dods_cache_max = atoi(value);
189 else if ((strncmp(tempstr,
"MAX_CACHED_OBJ", 14) == 0)
190 && tokenlength == 14) {
191 _dods_cached_obj = atoi(value);
193 else if ((strncmp(tempstr,
"IGNORE_EXPIRES", 14) == 0)
194 && tokenlength == 14) {
195 _dods_ign_expires = atoi(value);
197 else if ((strncmp(tempstr,
"DEFLATE", 7) == 0)
198 && tokenlength == 7) {
199 _dods_deflate = atoi(value) ? true :
false;
201 else if ((strncmp(tempstr,
"CACHE_ROOT", 10) == 0)
202 && tokenlength == 10) {
203 d_cache_root = value;
204 if (d_cache_root[d_cache_root.length() - 1] !=
DIR_SEP_CHAR)
207 else if ((strncmp(tempstr,
"DEFAULT_EXPIRES", 15) == 0)
208 && tokenlength == 15) {
209 _dods_default_expires = atoi(value);
211 else if ((strncmp(tempstr,
"ALWAYS_VALIDATE", 15) == 0)
212 && tokenlength == 15) {
213 _dods_always_validate = atoi(value);
215 else if ((strncmp(tempstr,
"VALIDATE_SSL", 12) == 0)
216 && tokenlength == 12) {
217 d_validate_ssl = atoi(value);
219 else if (strncmp(tempstr,
"AIS_DATABASE", 12) == 0
220 && tokenlength == 12) {
221 d_ais_database = value;
223 else if (strncmp(tempstr,
"COOKIE_JAR", 10) == 0
224 && tokenlength == 10) {
228 if (value[0] ==
'/') {
229 d_cookie_jar = value;
232 d_cookie_jar = d_rc_file_path.substr(0, d_rc_file_path.find(
".dodsrc")) +
string(value);
234 DBG(cerr <<
"set cookie jar to: " << d_cookie_jar << endl);
236 else if ((strncmp(tempstr,
"PROXY_SERVER", 12) == 0)
237 && tokenlength == 12) {
244 string proxy = value;
245 string::size_type comma = proxy.find(
',');
249 if (comma != string::npos) {
250 d_dods_proxy_server_protocol = proxy.substr(0, comma);
251 downcase(d_dods_proxy_server_protocol);
252 if (d_dods_proxy_server_protocol !=
"http")
253 throw Error(
"The only supported protocol for a proxy server is \"HTTP\". Correct your \".dodsrc\" file.");
254 proxy = proxy.substr(comma + 1);
257 d_dods_proxy_server_protocol =
"http";
261 string::size_type protocol = proxy.find(
"://");
262 if (protocol != string::npos) {
263 proxy = proxy.substr(protocol + 3);
267 string::size_type at_sign = proxy.find(
'@');
268 if (at_sign != string::npos) {
269 d_dods_proxy_server_userpw = proxy.substr(0, at_sign);
270 proxy = proxy.substr(at_sign + 1);
273 d_dods_proxy_server_userpw =
"";
276 string::size_type colon = proxy.find(
':');
277 if (colon != string::npos) {
278 d_dods_proxy_server_host = proxy.substr(0, colon);
279 d_dods_proxy_server_port
280 = strtol(proxy.substr(colon + 1).c_str(), 0, 0);
283 d_dods_proxy_server_host = proxy;
284 d_dods_proxy_server_port = 80;
287 else if ((strncmp(tempstr,
"NO_PROXY_FOR", 12) == 0)
288 && tokenlength == 12) {
290 string no_proxy = value;
291 string::size_type comma = no_proxy.find(
',');
295 if (comma == string::npos) {
296 d_dods_no_proxy_for_protocol =
"http";
297 d_dods_no_proxy_for_host = no_proxy;
298 d_dods_no_proxy_for =
true;
301 d_dods_no_proxy_for_protocol = no_proxy.substr(0, comma);
302 d_dods_no_proxy_for_host = no_proxy.substr(comma + 1);
303 d_dods_no_proxy_for =
true;
308 delete [] tempstr; tempstr = 0;
322 RCReader::check_string(
string env_var)
324 DBG(cerr <<
"Entering check_string... (" << env_var <<
")" << endl);
325 struct stat stat_info;
327 if (stat(env_var.c_str(), &stat_info) != 0) {
328 DBG(cerr <<
"stat returned non-zero" << endl);
332 if (S_ISREG(stat_info.st_mode)) {
333 DBG(cerr <<
"S_ISREG: " << S_ISREG(stat_info.st_mode) << endl);
339 if (S_ISDIR(stat_info.st_mode)) {
340 DBG(cerr <<
"S_ISDIR: " << S_ISDIR(stat_info.st_mode) << endl);
348 env_var +=
".dodsrc";
349 if (stat(env_var.c_str(), &stat_info) == 0 &&
350 S_ISREG(stat_info.st_mode)) {
351 DBG(cerr <<
"Found .dodsrc in \"" << env_var <<
"\"" << endl);
358 if (write_rc_file(env_var)) {
359 DBG(cerr <<
"Wrote .dodsrc in \"" << env_var <<
"\"" << endl);
365 DBG(cerr <<
"could neither find nor create a .dodsrc file" << endl);
379 RCReader::check_env_var(
const string &variable_name)
381 char *ev = getenv(variable_name.c_str());
382 if (!ev || strlen(ev) == 0)
385 return check_string(ev);
388 RCReader::RCReader()
throw(Error)
395 _dods_use_cache =
false;
396 _dods_cache_max = 20;
397 _dods_cached_obj = 5;
398 _dods_ign_expires = 0;
399 _dods_default_expires = 86400;
400 _dods_always_validate = 0;
407 d_dods_proxy_server_protocol =
"";
408 d_dods_proxy_server_host =
"";
409 d_dods_proxy_server_port = 0;
410 d_dods_proxy_server_userpw =
"";
412 _dods_proxy_server_host_url =
"";
416 _dods_proxy_for =
false;
417 _dods_proxy_for_regexp =
"";
418 _dods_proxy_for_proxy_host_url =
"";
419 _dods_proxy_for_regexp_flags = 0;
423 d_dods_no_proxy_for =
false;
424 d_dods_no_proxy_for_protocol =
"";
425 d_dods_no_proxy_for_host =
"";
429 _dods_no_proxy_for_port = 0;
434 string homedir = string(
"C:") + string(
DIR_SEP_STRING) + string(
"Dods");
435 d_rc_file_path = check_string(homedir);
436 if (d_rc_file_path.empty()) {
437 homedir = string(
"C:") + string(
DIR_SEP_STRING) + string(
"opendap");
438 d_rc_file_path = check_string(homedir);
441 if (d_rc_file_path.empty())
442 d_rc_file_path = check_env_var(
"APPDATA");
443 if (d_rc_file_path.empty())
444 d_rc_file_path = check_env_var(
"TEMP");
445 if (d_rc_file_path.empty())
446 d_rc_file_path = check_env_var(
"TMP");
448 d_rc_file_path = check_env_var(
"DODS_CONF");
449 if (d_rc_file_path.empty())
450 d_rc_file_path = check_env_var(
"HOME");
452 DBG(cerr <<
"Looking for .dodsrc in: " << d_rc_file_path << endl);
454 if (!d_rc_file_path.empty())
455 read_rc_file(d_rc_file_path);
458 RCReader::~RCReader()
463 RCReader::delete_instance()
465 if (RCReader::_instance) {
466 delete RCReader::_instance;
467 RCReader::_instance = 0;
473 RCReader::initialize_instance()
475 DBGN(cerr <<
"RCReader::initialize_instance() ... ");
477 RCReader::_instance =
new RCReader;
478 atexit(RCReader::delete_instance);
480 DBG(cerr <<
"exiting." << endl);
486 DBG(cerr <<
"Entring RCReader::instance" << endl);
489 pthread_once(&instance_control, initialize_instance);
491 DBG(cerr <<
"Instance value: " << hex << _instance << dec << endl);
string long_to_string(long val, int base)