RCReader.cc

Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: Jose Garcia <jgarcia@ucar.edu>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 2001,2002
00027 // Please read the full copyright statement in the file COPYRIGHT_URI.
00028 //
00029 // Authors:
00030 // jose Jose Garcia <jgarcia@ucar.edu>
00031 
00037 // #define DODS_DEBUG
00038 #include "config.h"
00039 
00040 #include <cstring>
00041 
00042 #include <unistd.h>  // for stat
00043 #include <sys/types.h>
00044 #include <sys/stat.h>
00045 
00046 #ifdef WIN32
00047 #define FALSE 0
00048 // Win32 does not define F_OK. 08/21/02 jhrg
00049 #define F_OK 0
00050 #define DIR_SEP_STRING "\\"
00051 #define DIR_SEP_CHAR   '\\'
00052 #include <direct.h>
00053 #else
00054 #define DIR_SEP_STRING "/"
00055 #define DIR_SEP_CHAR   '/'
00056 #endif
00057 
00058 #include <pthread.h>
00059 
00060 #include <fstream>
00061 
00062 #include "debug.h"
00063 #include "RCReader.h"
00064 #include "Error.h"
00065 
00066 using namespace std;
00067 
00068 namespace libdap {
00069 
00070 RCReader* RCReader::_instance = 0;
00071 
00072 // This variable (instance_control) is used to ensure that in a MT
00073 // environment _instance is correctly initialized. See the get_instance
00074 // method. 08/07/02 jhrg
00075 static pthread_once_t instance_control = PTHREAD_ONCE_INIT;
00076 
00081 bool
00082 RCReader::write_rc_file(const string &pathname)
00083 {
00084     DBG(cerr << "Writing the RC file to " << pathname << endl);
00085     ofstream fpo(pathname.c_str());
00086 
00087     // If the file couldn't be created.  Nothing needs to be done here,
00088     // the program will simply use the defaults.
00089 
00090     if (fpo) {
00091         // This means we just created the file.  We will now save
00092         // the defaults in it for future use.
00093         fpo << "# OPeNDAP client configuration file. See the OPeNDAP" << endl;
00094         fpo << "# users guide for information." << endl;
00095         fpo << "USE_CACHE=" << _dods_use_cache << endl;
00096         fpo << "# Cache and object size are given in megabytes (20 ==> 20Mb)."
00097         << endl;
00098         fpo << "MAX_CACHE_SIZE=" << _dods_cache_max  << endl;
00099         fpo << "MAX_CACHED_OBJ=" << _dods_cached_obj << endl;
00100         fpo << "IGNORE_EXPIRES=" << _dods_ign_expires  << endl;
00101         fpo << "CACHE_ROOT=" << d_cache_root << endl;
00102         fpo << "DEFAULT_EXPIRES=" << _dods_default_expires << endl;
00103         fpo << "ALWAYS_VALIDATE=" << _dods_always_validate << endl;
00104         fpo << "# Request servers compress responses if possible?" << endl;
00105         fpo << "# 1 (yes) or 0 (false)." << endl;
00106         fpo << "DEFLATE=" << _dods_deflate << endl;
00107 
00108         fpo << "# Should SSL certificates and hosts be validated? SSL" << endl;
00109         fpo << "# will only work with signed certificates." << endl;
00110         fpo << "VALIDATE_SSL=" << d_validate_ssl << endl;
00111 
00112         fpo << "# Proxy configuration (optional parts in []s)." << endl;
00113         fpo << "# You may also use the 'http_proxy' environment variable" 
00114             << endl;
00115         fpo << "# but a value in this file will override that env variable."
00116             << endl;
00117         fpo << "# PROXY_SERVER=[http://][username:password@]host[:port]"
00118         << endl;
00119         if (!d_dods_proxy_server_host.empty()) {
00120             fpo << "PROXY_SERVER=" <<  d_dods_proxy_server_protocol << "://"
00121             << (d_dods_proxy_server_userpw.empty()
00122                 ? ""
00123                 : d_dods_proxy_server_userpw + "@")
00124             + d_dods_proxy_server_host
00125             + ":" + long_to_string(d_dods_proxy_server_port) << endl;
00126         }
00127 
00128         fpo << "# NO_PROXY_FOR=<host|domain>" << endl;
00129         if (!d_dods_no_proxy_for_host.empty()) {
00130             fpo << "NO_PROXY_FOR=" << d_dods_no_proxy_for_host << endl;
00131         }
00132 
00133         fpo << "# AIS_DATABASE=<file or url>" << endl;
00134         fpo.close();
00135 
00136         return true;
00137     }
00138 
00139     return false;
00140 }
00141 
00142 bool
00143 RCReader::read_rc_file(const string &pathname)
00144 {
00145     DBG(cerr << "Reading the RC file from " << pathname << endl);
00146 
00147     ifstream fpi(pathname.c_str());
00148     if (fpi) {
00149         // The file exists and we may now begin to parse it.
00150         // Defaults are already stored in the variables, if the correct
00151         // tokens are found in the file then those defaults will be
00152         // overwritten.
00153         char *value;
00154         char *tempstr = new char[1024];;
00155         int tokenlength;
00156         while (true) {
00157             fpi.getline(tempstr, 1023);
00158             if (!fpi.good())
00159                 break;
00160 
00161             value = strchr(tempstr, '=');
00162             if (!value)
00163                 continue;
00164             tokenlength = value - tempstr;
00165             value++;
00166 
00167             if ((strncmp(tempstr, "USE_CACHE", 9) == 0)
00168                 && tokenlength == 9) {
00169                 _dods_use_cache = atoi(value) ? true : false;
00170             }
00171             else if ((strncmp(tempstr, "MAX_CACHE_SIZE", 14) == 0)
00172                      && tokenlength == 14) {
00173                 _dods_cache_max = atoi(value);
00174             }
00175             else if ((strncmp(tempstr, "MAX_CACHED_OBJ", 14) == 0)
00176                      && tokenlength == 14) {
00177                 _dods_cached_obj = atoi(value);
00178             }
00179             else if ((strncmp(tempstr, "IGNORE_EXPIRES", 14) == 0)
00180                      && tokenlength == 14) {
00181                 _dods_ign_expires = atoi(value);
00182             }
00183             else if ((strncmp(tempstr, "DEFLATE", 7) == 0)
00184                      && tokenlength == 7) {
00185                 _dods_deflate = atoi(value) ? true : false;
00186             }
00187             else if ((strncmp(tempstr, "CACHE_ROOT", 10) == 0)
00188                      && tokenlength == 10) {
00189                 d_cache_root = value;
00190                 if (d_cache_root[d_cache_root.length() - 1] != DIR_SEP_CHAR)
00191                     d_cache_root += string(DIR_SEP_STRING);
00192             }
00193             else if ((strncmp(tempstr, "DEFAULT_EXPIRES", 15) == 0)
00194                      && tokenlength == 15) {
00195                 _dods_default_expires = atoi(value);
00196             }
00197             else if ((strncmp(tempstr, "ALWAYS_VALIDATE", 15) == 0)
00198                      && tokenlength == 15) {
00199                 _dods_always_validate = atoi(value);
00200             }
00201             else if ((strncmp(tempstr, "VALIDATE_SSL", 12) == 0)
00202                      && tokenlength == 12) {
00203                 d_validate_ssl = atoi(value);
00204             }
00205             else if (strncmp(tempstr, "AIS_DATABASE", 12) == 0
00206                      && tokenlength == 12) {
00207                 d_ais_database = value;
00208             }
00209             else if ((strncmp(tempstr, "PROXY_SERVER", 12) == 0)
00210                      && tokenlength == 12) {
00211                 // Setup a proxy server for all requests.
00212                 // The original syntax was <protocol>,<machine> where the
00213                 // machine could also contain the user/pass and port info.
00214                 // Support that but also support machine prefixed by
00215                 // 'http://' with and without the '<protocol>,' prefix. jhrg
00216                 // 4/21/08 (see bug 1095).
00217                 string proxy = value;
00218                 string::size_type comma = proxy.find(',');
00219 
00220                 // Since the <protocol> is now optional, the comma might be
00221                 // here. If it is, check that the protocol given is http.
00222                 if (comma != string::npos) {
00223                     d_dods_proxy_server_protocol = proxy.substr(0, comma);
00224                     downcase(d_dods_proxy_server_protocol);
00225                     if (d_dods_proxy_server_protocol != "http")
00226                         throw Error("The only supported protocol for a proxy server is \"HTTP\". Correct your \".dodsrc\" file.");
00227                     proxy = proxy.substr(comma + 1);
00228                 }
00229                 else {
00230                     d_dods_proxy_server_protocol = "http";
00231                 }
00232 
00233                 // Look for a 'protocol://' prefix; skip if found
00234                 string::size_type protocol = proxy.find("://");
00235                 if (protocol != string::npos) {
00236                     proxy = proxy.substr(protocol + 3);
00237                 }
00238 
00239                 // Break apart into userpw, host and port.
00240                 string::size_type at_sign = proxy.find('@');
00241                 if (at_sign != string::npos) { // has userpw
00242                     d_dods_proxy_server_userpw = proxy.substr(0, at_sign);
00243                     proxy = proxy.substr(at_sign + 1);
00244                 }
00245                 else
00246                     d_dods_proxy_server_userpw = "";
00247 
00248                 // Get host and look for a port number
00249                 string::size_type colon = proxy.find(':');
00250                 if (colon != string::npos) {
00251                     d_dods_proxy_server_host = proxy.substr(0, colon);
00252                     d_dods_proxy_server_port
00253                         = strtol(proxy.substr(colon + 1).c_str(), 0, 0);
00254                 }
00255                 else {
00256                     d_dods_proxy_server_host = proxy;
00257                     d_dods_proxy_server_port = 80;
00258                 }
00259             }
00260             else if ((strncmp(tempstr, "NO_PROXY_FOR", 12) == 0)
00261                      && tokenlength == 12) {
00262                 // Setup a proxy server for all requests.
00263                 string no_proxy = value;
00264                 string::size_type comma = no_proxy.find(',');
00265 
00266                 // Since the protocol is required, the comma *must* be
00267                 // present. We could throw an Error on the malformed line...
00268                 if (comma == string::npos) {
00269                     d_dods_no_proxy_for_protocol = "http";
00270                     d_dods_no_proxy_for_host = no_proxy;
00271                     d_dods_no_proxy_for = true;
00272                 }
00273                 else {
00274                     d_dods_no_proxy_for_protocol = no_proxy.substr(0, comma);
00275                     d_dods_no_proxy_for_host = no_proxy.substr(comma + 1);
00276                     d_dods_no_proxy_for = true;
00277                 }
00278             }
00279         }
00280 
00281         delete [] tempstr; tempstr = 0;
00282 
00283         fpi.close(); // Close the .dodsrc file. 12/14/99 jhrg
00284 
00285         return true;
00286     }  // End of cache file parsing.
00287 
00288     return false;
00289 }
00290 
00291 // Helper for check_env_var(). This is its main logic, separated out for the
00292 // cases under WIN32 where we don't use an environment variable.  09/19/03
00293 // jhrg
00294 string
00295 RCReader::check_string(string env_var)
00296 {
00297         DBG(cerr << "Entering check_string... (" << env_var << ")" << endl);
00298     struct stat stat_info;
00299 
00300         if (stat(env_var.c_str(), &stat_info) != 0) {
00301                 DBG(cerr << "stat returned non-zero" << endl);
00302         return "";  // ENV VAR not a file or dir, bail
00303         }
00304 
00305         if (S_ISREG(stat_info.st_mode)) {
00306                 DBG(cerr << "S_ISREG: " << S_ISREG(stat_info.st_mode) << endl);
00307         return env_var;  // ENV VAR is a file, use it
00308         }
00309 
00310     // ENV VAR is a directory, does it contain .dodsrc? Can we create
00311     // .dodsrc if it's not there?
00312     if (S_ISDIR(stat_info.st_mode)) {
00313                 DBG(cerr << "S_ISDIR: " << S_ISDIR(stat_info.st_mode) << endl);
00314         if (*env_var.rbegin() != DIR_SEP_CHAR) // Add trailing / if missing
00315             env_var += DIR_SEP_STRING;
00316         // Trick: set d_cache_root here in case we're going to create the
00317         // .dodsrc later on. If the .dodsrc file exists, its value will
00318         // overwrite this value, if not write_rc_file() will use the correct
00319         // value. 09/19/03 jhrg
00320         d_cache_root = env_var + string(".dods_cache") + DIR_SEP_STRING;
00321         env_var += ".dodsrc";
00322         if (stat(env_var.c_str(), &stat_info) == 0 &&
00323                         S_ISREG(stat_info.st_mode)) {
00324                         DBG(cerr << "Found .dodsrc in \"" << env_var << "\"" << endl);
00325             return env_var; // Found .dodsrc in ENV VAR
00326                 }
00327 
00328         // Didn't find .dodsrc in ENV VAR and ENV VAR is a directory; try to
00329         // create it. Note write_rc_file uses d_cache_root (set above) when
00330         // it creates the RC file's contents.
00331                 if (write_rc_file(env_var)) {
00332                         DBG(cerr << "Wrote .dodsrc in \"" << env_var << "\"" << endl);
00333             return env_var;
00334                 }
00335     }
00336 
00337     // If we're here, then we've neither found nor created the RC file.
00338         DBG(cerr << "could neither find nor create a .dodsrc file" << endl);
00339     return "";
00340 }
00341 
00351 string
00352 RCReader::check_env_var(const string &variable_name)
00353 {
00354     char *ev = getenv(variable_name.c_str());
00355     if (!ev || strlen(ev) == 0)
00356         return "";
00357 
00358     return check_string(ev);
00359 }
00360 
00361 RCReader::RCReader() throw(Error)
00362 {
00363     d_rc_file_path = "";
00364     d_cache_root = "";
00365 
00366     // ** Set default values **
00367     // Users must explicitly turn caching on.
00368     _dods_use_cache = false;
00369     _dods_cache_max = 20;
00370     _dods_cached_obj = 5;
00371     _dods_ign_expires = 0;
00372     _dods_default_expires = 86400;
00373     _dods_always_validate = 0;
00374 
00375     _dods_deflate = 0;
00376     d_validate_ssl = 1;
00377 
00378     //flags for PROXY_SERVER=<protocol>,<host url>
00379     // New syntax PROXY_SERVER=[http://][user:pw@]host[:port]
00380     d_dods_proxy_server_protocol = "";
00381     d_dods_proxy_server_host = "";
00382     d_dods_proxy_server_port = 0;
00383     d_dods_proxy_server_userpw = "";
00384 
00385     _dods_proxy_server_host_url = ""; // deprecated
00386 
00387     // PROXY_FOR is deprecated.
00388     // flags for PROXY_FOR=<regex>,<proxy host url>,<flags>
00389     _dods_proxy_for = false; // true if proxy_for is used.
00390     _dods_proxy_for_regexp = "";
00391     _dods_proxy_for_proxy_host_url = "";
00392     _dods_proxy_for_regexp_flags = 0;
00393 
00394     //flags for NO_PROXY_FOR=<protocol>,<host>,<port>
00395     // New syntax NO_PROXY_FOR=<host|domain>
00396     d_dods_no_proxy_for = false;
00397     d_dods_no_proxy_for_protocol = ""; // deprecated
00398     d_dods_no_proxy_for_host = "";
00399     // default to port 0 if not specified. This means all ports. Using 80
00400     // will fail when the URL does not contain the port number. That's
00401     // probably a bug in libwww. 10/23/2000 jhrg
00402     _dods_no_proxy_for_port = 0; // deprecated
00403 
00404 #ifdef WIN32
00405     string homedir = string("C:") + string(DIR_SEP_STRING) + string("Dods");
00406     d_rc_file_path = check_string(homedir);
00407     //  Normally, I'd prefer this for WinNT-based systems.
00408     if (d_rc_file_path.empty())
00409         d_rc_file_path = check_env_var("APPDATA");
00410     if (d_rc_file_path.empty())
00411         d_rc_file_path = check_env_var("TEMP");
00412     if (d_rc_file_path.empty())
00413         d_rc_file_path = check_env_var("TMP");
00414 #else
00415     d_rc_file_path = check_env_var("DODS_CONF");
00416     if (d_rc_file_path.empty())
00417         d_rc_file_path = check_env_var("HOME");
00418 #endif
00419         DBG(cerr << "Looking for .dodsrc in: " << d_rc_file_path << endl);
00420 
00421     if (!d_rc_file_path.empty())
00422         read_rc_file(d_rc_file_path);
00423 }
00424 
00425 RCReader::~RCReader()
00426 {}
00427 
00429 void
00430 RCReader::delete_instance()
00431 {
00432     if (RCReader::_instance) {
00433         delete RCReader::_instance;
00434         RCReader::_instance = 0;
00435     }
00436 }
00437 
00439 void
00440 RCReader::initialize_instance()
00441 {
00442     DBGN(cerr << "RCReader::initialize_instance() ... ");
00443 
00444     RCReader::_instance = new RCReader;
00445     atexit(RCReader::delete_instance);
00446 
00447     DBG(cerr << "exiting." << endl);
00448 }
00449 
00450 RCReader*
00451 RCReader::instance()
00452 {
00453         DBG(cerr << "Entring RCReader::instance" << endl);
00454     // The instance_control variable is defined at the top of this file.
00455     // 08/07/02 jhrg
00456     pthread_once(&instance_control, initialize_instance);
00457         
00458         DBG(cerr << "Instance value: " << hex << _instance << dec << endl);
00459     return _instance;
00460 }
00461 
00462 } // namespace libdap

Generated on Tue Jun 10 18:00:31 2008 for libdap++ by  doxygen 1.5.4