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 #include "config.h"
00038 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 
00042 #include <sys/types.h>
00043 #include <sys/stat.h>
00044 #include <unistd.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 RCReader* RCReader::_instance = 0;
00069 
00070 // This variable (instance_control) is used to ensure that in a MT
00071 // environment _instance is correctly initialized. See the get_instance
00072 // method. 08/07/02 jhrg
00073 static pthread_once_t instance_control = PTHREAD_ONCE_INIT;
00074 
00079 bool
00080 RCReader::write_rc_file(const string &pathname)
00081 {
00082     DBG(cerr << "Writing the RC file to " << pathname << endl);
00083     ofstream fpo(pathname.c_str());
00084 
00085     // If the file couldn't be created.  Nothing needs to be done here,
00086     // the program will simply use the defaults.
00087 
00088     if (fpo) {
00089         // This means we just created the file.  We will now save
00090         // the defaults in it for future use.
00091         fpo << "# OPeNDAP client configuation file. See the OPeNDAP" << endl;
00092         fpo << "# users guide for information." << endl;
00093         fpo << "USE_CACHE=" << _dods_use_cache << endl;
00094         fpo << "# Cache and object size are given in megabytes (20 ==> 20Mb)."
00095         << endl;
00096         fpo << "MAX_CACHE_SIZE=" << _dods_cache_max  << endl;
00097         fpo << "MAX_CACHED_OBJ=" << _dods_cached_obj << endl;
00098         fpo << "IGNORE_EXPIRES=" << _dods_ign_expires  << endl;
00099         fpo << "CACHE_ROOT=" << d_cache_root << endl;
00100         fpo << "DEFAULT_EXPIRES=" << _dods_default_expires << endl;
00101         fpo << "ALWAYS_VALIDATE=" << _dods_always_validate << endl;
00102         fpo << "# Request servers compress responses if possible?" << endl;
00103         fpo << "# 1 (yes) or 0 (false)." << endl;
00104         fpo << "DEFLATE=" << _dods_deflate << endl;
00105 
00106         fpo << "# Should SSL certificates and hosts be validated? SSL" << endl;
00107         fpo << "# will only work with signed certificates." << endl;
00108         fpo << "VALIDATE_SSL=" << d_validate_ssl << endl;
00109 
00110         fpo << "# Proxy configuration:" << endl;
00111         fpo << "# PROXY_SERVER=<protocol>,<[username:password@]host[:port]>"
00112         << endl;
00113         if (!d_dods_proxy_server_host.empty()) {
00114             fpo << "PROXY_SERVER=" <<  d_dods_proxy_server_protocol << ","
00115             << (d_dods_proxy_server_userpw.empty()
00116                 ? ""
00117                 : d_dods_proxy_server_userpw + "@")
00118             + d_dods_proxy_server_host
00119             + ":" + long_to_string(d_dods_proxy_server_port) << endl;
00120         }
00121 
00122         fpo << "# NO_PROXY_FOR=<protocol>,<host|domain>" << endl;
00123         if (!d_dods_no_proxy_for_host.empty()) {
00124             fpo << "NO_PROXY_FOR=" << d_dods_no_proxy_for_protocol << ","
00125             << d_dods_no_proxy_for_host << endl;
00126         }
00127 
00128         fpo << "# AIS_DATABASE=<file or url>" << endl;
00129         fpo.close();
00130 
00131         return true;
00132     }
00133 
00134     return false;
00135 }
00136 
00137 bool
00138 RCReader::read_rc_file(const string &pathname)
00139 {
00140     DBG(cerr << "Reading the RC file from " << pathname << endl);
00141 
00142     ifstream fpi(pathname.c_str());
00143     if (fpi) {
00144         // The file exists and we may now begin to parse it.
00145         // Defaults are already stored in the variables, if the correct
00146         // tokens are found in the file then those defaults will be
00147         // overwritten.
00148         char *value;
00149         char *tempstr = new char[1024];;
00150         int tokenlength;
00151         while (true) {
00152             fpi.getline(tempstr, 1023);
00153             if (!fpi.good())
00154                 break;
00155 
00156             value = strchr(tempstr, '=');
00157             if (!value)
00158                 continue;
00159             tokenlength = value - tempstr;
00160             value++;
00161 
00162             if ((strncmp(tempstr, "USE_CACHE", 9) == 0)
00163                 && tokenlength == 9) {
00164                 _dods_use_cache = atoi(value) ? true : false;
00165             }
00166             else if ((strncmp(tempstr, "MAX_CACHE_SIZE", 14) == 0)
00167                      && tokenlength == 14) {
00168                 _dods_cache_max = atoi(value);
00169             }
00170             else if ((strncmp(tempstr, "MAX_CACHED_OBJ", 14) == 0)
00171                      && tokenlength == 14) {
00172                 _dods_cached_obj = atoi(value);
00173             }
00174             else if ((strncmp(tempstr, "IGNORE_EXPIRES", 14) == 0)
00175                      && tokenlength == 14) {
00176                 _dods_ign_expires = atoi(value);
00177             }
00178             else if ((strncmp(tempstr, "DEFLATE", 7) == 0)
00179                      && tokenlength == 7) {
00180                 _dods_deflate = atoi(value) ? true : false;
00181             }
00182             else if ((strncmp(tempstr, "CACHE_ROOT", 10) == 0)
00183                      && tokenlength == 10) {
00184                 d_cache_root = value;
00185                 if (d_cache_root[d_cache_root.length() - 1] != DIR_SEP_CHAR)
00186                     d_cache_root += string(DIR_SEP_STRING);
00187             }
00188             else if ((strncmp(tempstr, "DEFAULT_EXPIRES", 15) == 0)
00189                      && tokenlength == 15) {
00190                 _dods_default_expires = atoi(value);
00191             }
00192             else if ((strncmp(tempstr, "ALWAYS_VALIDATE", 15) == 0)
00193                      && tokenlength == 15) {
00194                 _dods_always_validate = atoi(value);
00195             }
00196             else if ((strncmp(tempstr, "VALIDATE_SSL", 12) == 0)
00197                      && tokenlength == 12) {
00198                 d_validate_ssl = atoi(value);
00199             }
00200             else if (strncmp(tempstr, "AIS_DATABASE", 12) == 0
00201                      && tokenlength == 12) {
00202                 d_ais_database = value;
00203             }
00204             else if ((strncmp(tempstr, "PROXY_SERVER", 12) == 0)
00205                      && tokenlength == 12) {
00206                 // Setup a proxy server for all requests.
00207                 string proxy = value;
00208                 string::size_type comma = proxy.find(',');
00209 
00210                 // Since the protocol is required, the comma *must* be
00211                 // present. We could throw an Error on the malformed line...
00212                 if (comma == string::npos)
00213                     continue;
00214                 d_dods_proxy_server_protocol = proxy.substr(0, comma);
00215                 proxy = proxy.substr(comma + 1);
00216 
00217                 // Break apart into userpw, host and port.
00218                 string::size_type at_sign = proxy.find('@');
00219                 if (at_sign != string::npos) { // has userpw
00220                     d_dods_proxy_server_userpw = proxy.substr(0, at_sign);
00221                     proxy = proxy.substr(at_sign + 1);
00222                 }
00223                 else
00224                     d_dods_proxy_server_userpw = "";
00225 
00226                 // Get host and look for a port number
00227                 string::size_type colon = proxy.find(':');
00228                 if (colon != string::npos) {
00229                     d_dods_proxy_server_host = proxy.substr(0, colon);
00230                     d_dods_proxy_server_port
00231                     = strtol(proxy.substr(colon + 1).c_str(), 0, 0);
00232                 }
00233                 else {
00234                     d_dods_proxy_server_host = proxy;
00235                     // No port given, look at protocol or throw
00236                     if (d_dods_proxy_server_protocol == "http")
00237                         d_dods_proxy_server_port = 80;
00238                     else if (d_dods_proxy_server_protocol == "https")
00239                         d_dods_proxy_server_port = 443;
00240                     else if (d_dods_proxy_server_protocol == "ftp")
00241                         d_dods_proxy_server_port = 21;
00242                     else throw Error("Could not determine the port to use for proxy '"
00243                                          + d_dods_proxy_server_host
00244                                          + ".' Please check your .dodsrc file.");
00245                 }
00246             }
00247             else if ((strncmp(tempstr, "NO_PROXY_FOR", 12) == 0)
00248                      && tokenlength == 12) {
00249                 // Setup a proxy server for all requests.
00250                 string no_proxy = value;
00251                 string::size_type comma = no_proxy.find(',');
00252 
00253                 // Since the protocol is required, the comma *must* be
00254                 // present. We could throw an Error on the malformed line...
00255                 if (comma == string::npos)
00256                     continue;
00257                 d_dods_no_proxy_for_protocol = no_proxy.substr(0, comma);
00258                 d_dods_no_proxy_for_host = no_proxy.substr(comma + 1);
00259                 d_dods_no_proxy_for = true;
00260             }
00261         }
00262 
00263         delete [] tempstr; tempstr = 0;
00264 
00265         fpi.close(); // Close the .dodsrc file. 12/14/99 jhrg
00266 
00267         return true;
00268     }  // End of cache file parsing.
00269 
00270     return false;
00271 }
00272 
00273 // Helper for check_env_var(). This is its main logic, separated out for the
00274 // cases under WIN32 where we don't use an environment variable.  09/19/03
00275 // jhrg
00276 string
00277 RCReader::check_string(string env_var)
00278 {
00279     struct stat stat_info;
00280 
00281     if (stat(env_var.c_str(), &stat_info) != 0)
00282         return "";  // ENV VAR not a file or dir, bail
00283 
00284     if (S_ISREG(stat_info.st_mode))
00285         return env_var;  // ENV VAR is a file, use it
00286 
00287     // ENV VAR is a directory, does it contain .dodsrc? Can we create
00288     // .dodsrc if it's not there?
00289     if (S_ISDIR(stat_info.st_mode)) {
00290         if (*env_var.rbegin() != DIR_SEP_CHAR) // Add trailing / if missing
00291             env_var += DIR_SEP_STRING;
00292         // Trick: set d_cache_root here in case we're going to create the
00293         // .dodsrc later on. If the .dodsrc file exists, its value will
00294         // overwrite this value, if not write_rc_file() will use the correct
00295         // value. 09/19/03 jhrg
00296         d_cache_root = env_var + string(".dods_cache") + DIR_SEP_STRING;
00297         env_var += ".dodsrc";
00298         if (stat(env_var.c_str(), &stat_info) == 0 &&
00299             S_ISREG(stat_info.st_mode))
00300             return env_var; // Found .dodsrc in ENV VAR
00301 
00302         // Didn't find .dodsrc in ENV VAR and ENV VAR is a directory; try to
00303         // create it. Note write_rc_file uses d_cache_root (set above) when
00304         // it creates the RC file's contents.
00305         if (write_rc_file(env_var))
00306             return env_var;
00307     }
00308 
00309     // If we're here, then we've not found or created the RC file.
00310     return "";
00311 }
00312 
00322 string
00323 RCReader::check_env_var(const string &variable_name)
00324 {
00325     char *ev = getenv(variable_name.c_str());
00326     if (!ev || strlen(ev) == 0)
00327         return "";
00328 
00329     return check_string(ev);
00330 }
00331 
00332 RCReader::RCReader() throw(Error)
00333 {
00334     d_rc_file_path = "";
00335     d_cache_root = "";
00336 
00337     // ** Set default values **
00338     // Users must explicitly turn caching on.
00339     _dods_use_cache = false;
00340     _dods_cache_max = 20;
00341     _dods_cached_obj = 5;
00342     _dods_ign_expires = 0;
00343     _dods_default_expires = 86400;
00344     _dods_always_validate = 0;
00345 
00346     _dods_deflate = 0;
00347     d_validate_ssl = 1;
00348 
00349     //flags for PROXY_SERVER=<protocol>,<host url>
00350     d_dods_proxy_server_protocol = "";
00351     d_dods_proxy_server_host = "";
00352     d_dods_proxy_server_port = 0;
00353     d_dods_proxy_server_userpw = "";
00354 
00355     _dods_proxy_server_host_url = ""; // deprecated
00356 
00357     // flags for PROXY_FOR=<regex>,<proxy host url>,<flags>
00358     _dods_proxy_for = false; // true if proxy_for is used.
00359     _dods_proxy_for_regexp = "";
00360     _dods_proxy_for_proxy_host_url = "";
00361     _dods_proxy_for_regexp_flags = 0;
00362 
00363     //flags for NO_PROXY_FOR=<protocol>,<host>,<port>
00364     d_dods_no_proxy_for = false;
00365     d_dods_no_proxy_for_protocol = "";
00366     d_dods_no_proxy_for_host = "";
00367     // default to port 0 if not specified. This means all ports. Using 80
00368     // will fail when the URL does not contain the port number. That's
00369     // probably a bug in libwww. 10/23/2000 jhrg
00370     _dods_no_proxy_for_port = 0;
00371 
00372 #ifdef WIN32
00373     string homedir = string("C:") + string(DIR_SEP_STRING) + string("Dods");
00374     d_rc_file_path = check_string(homedir);
00375     //  Normally, I'd prefer this for WinNT-based systems.
00376     if (d_rc_file_path.empty())
00377         d_rc_file_path = check_env_var("APPDATA");
00378     if (d_rc_file_path.empty())
00379         d_rc_file_path = check_env_var("TEMP");
00380     if (d_rc_file_path.empty())
00381         d_rc_file_path = check_env_var("TMP");
00382 #else
00383     d_rc_file_path = check_env_var("DODS_CONF");
00384     if (d_rc_file_path.empty())
00385         d_rc_file_path = check_env_var("HOME");
00386 #endif
00387 
00388     if (!d_rc_file_path.empty())
00389         read_rc_file(d_rc_file_path);
00390 }
00391 
00392 RCReader::~RCReader()
00393 {}
00394 
00396 void
00397 RCReader::delete_instance()
00398 {
00399     if (RCReader::_instance) {
00400         delete RCReader::_instance;
00401         RCReader::_instance = 0;
00402     }
00403 }
00404 
00406 void
00407 RCReader::initialize_instance()
00408 {
00409     DBG(cerr << "RCReader::initialize_instance() ... ");
00410 
00411     RCReader::_instance = new RCReader;
00412     atexit(RCReader::delete_instance);
00413 
00414     DBGN(cerr << "exiting." << endl);
00415 }
00416 
00417 RCReader*
00418 RCReader::instance()
00419 {
00420     // The instance_control variable is defined at the top of this file.
00421     // 08/07/02 jhrg
00422     pthread_once(&instance_control, initialize_instance);
00423 
00424     return _instance;
00425 }

Generated on Wed Jun 27 12:56:39 2007 for libdap++ by  doxygen 1.4.7