CurlConfig.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 
00004 #include "zypp/base/Logger.h"
00005 #include "zypp/base/IOStream.h"
00006 #include "zypp/Pathname.h"
00007 #include "zypp/PathInfo.h"
00008 
00009 #include "zypp/media/CurlConfig.h"
00010 
00011 using namespace std;
00012 
00013 namespace zypp
00014 {
00015   namespace media
00016   {
00017 
00018 
00020   //
00021   //  METHOD NAME : CurlConfig::parseConfig
00022   //  METHOD TYPE : int
00023   //
00024   int CurlConfig::parseConfig(CurlConfig & config, const std::string & filename)
00025   {
00026     Pathname curlrcFile;
00027 
00028     if(filename.empty())
00029     {
00030       // attempts to load .curlrc from the homedir
00031       char *home = getenv("HOME");
00032       if(home)
00033         curlrcFile = string( home ) + string( "/.curlrc" );
00034     }
00035     else
00036       curlrcFile = filename;
00037 
00038     PathInfo h_info(curlrcFile.dirname(), PathInfo::LSTAT);
00039     PathInfo c_info(curlrcFile,           PathInfo::LSTAT);
00040 
00041     if( h_info.isDir()  && h_info.owner() == getuid() &&
00042         c_info.isFile() && c_info.owner() == getuid())
00043     {
00044       MIL << "Going to parse " << curlrcFile << endl;
00045     }
00046     else
00047     {
00048       WAR << "Not allowed to parse '" << curlrcFile
00049           << "': bad file owner" << std::endl;
00050       return 1;
00051     }
00052 
00053     ifstream inp(curlrcFile.c_str());
00054     for(iostr::EachLine in( inp ); in; in.next())
00055     {
00056       string line = str::trim(*in);
00057 
00058       // skip empty lines and comments
00059       if (line.empty())
00060         continue;
00061       switch (line[0])
00062       {
00063       case '#':
00064       case '/':
00065       case '\r':
00066       case '\n':
00067       case '*':
00068       case '\0':
00069         continue;
00070       }
00071 
00072       // DBG << "line " << in.lineNo() << ": " << line << endl; // can't log passwords
00073 
00074       const char * beg = line.c_str();
00075       const char * cur = beg;
00076 
00077 // space, '=' and ':' are all valid separators in curlrc
00078 #define ISSEP(x) (((x)=='=') || ((x) == ':') || isspace(x))
00079 
00080       // skip leading dashes (they are optional)
00081       while (*cur && *cur == '-')
00082         cur++;
00083       beg = cur;
00084 
00085       // skip non-separator characters
00086       while (*cur && !ISSEP(*cur))
00087         cur++;
00088 
00089       string option(beg, cur - beg);
00090 
00091       // skip separator characters
00092       while (*cur && ISSEP(*cur))
00093         cur++;
00094 
00095       // rewind to the end of the line
00096       beg = cur;
00097       while (*cur)
00098         cur++;
00099 
00100       string value(beg, cur - beg);
00101 
00102       DBG << "GOT: " << option << endl;
00103 
00104       if (!value.empty())
00105       {
00106         // quoted parameter
00107         if (value[0] == '\"')
00108         {
00109           // remove the quotes
00110           string::size_type pos = value.rfind('\"');
00111           bool cut_last =
00112             pos == value.size() - 1 && pos > 1 && value[pos-1] != '\\';
00113           value = value.substr(1,
00114               cut_last ? value.size() - 2 : value.size() - 1);
00115 
00116           // replace special characters:
00117           pos = 0;
00118           while ((pos = value.find('\\', pos)) != string::npos)
00119           {
00120             // just erase the backslash if it is found at the end
00121             if (pos == value.size() - 1)
00122             {
00123               value = value.erase(pos, 1);
00124               break;
00125             }
00126 
00127             switch(value[pos+1])
00128             {
00129             case 't':
00130               value = value.replace(pos, 2, "\t");
00131               break;
00132             case 'n':
00133               value = value.replace(pos, 2, "\n");
00134               break;
00135             case 'r':
00136               value = value.replace(pos, 2, "\r");
00137               break;
00138             case 'v':
00139               value = value.replace(pos, 2, "\v");
00140               break;
00141             case '\\':
00142               value = value.erase(pos++, 1);
00143               break;
00144             default:;
00145               value = value.erase(pos, 1);
00146             }
00147           }
00148         }
00149 
00150         // DBG << "PARAM: " << value << endl; // can't log passwords
00151       }
00152 
00153       CurlConfig::setParameter(config, option, value);
00154     } // for EachLine in curlrc
00155 
00156     return 0;
00157   }
00158 
00159 
00161   //
00162   //  METHOD NAME : CurlConfig::setParameter
00163   //  METHOD TYPE : int
00164   //
00165   int CurlConfig::setParameter(CurlConfig & config,
00166                                const std::string & option,
00167                                const std::string & value)
00168   {
00169     if (option == "proxy-user")
00170       config.proxyuserpwd = value;
00171     // add more curl config data here as they become needed
00172     // else if (option == "foo")
00173     else
00174       DBG << "Ignoring option " << option << endl;
00175 
00176     return 0;
00177   }
00178 
00179 
00180   } // namespace media
00181 } // namespace zypp

Generated on Tue Oct 21 02:32:57 2008 for libzypp by  doxygen 1.5.3