4 #include <zypp-core/base/Logger.h>
5 #include <zypp-core/base/IOStream.h>
6 #include <zypp-core/Pathname.h>
7 #include <zypp-core/fs/PathInfo.h>
9 #include <zypp-curl/CurlConfig>
19 ///////////////////////////////////////////////////////////////////
21 // METHOD NAME : CurlConfig::parseConfig
24 int CurlConfig::parseConfig(CurlConfig & config, const std::string & filename)
30 // attempts to load .curlrc from the homedir
31 char *home = getenv("HOME");
33 curlrcFile = std::string( home ) + std::string( "/.curlrc" );
36 curlrcFile = filename;
38 PathInfo h_info(curlrcFile.dirname(), PathInfo::LSTAT);
39 PathInfo c_info(curlrcFile, PathInfo::LSTAT);
41 if( h_info.isDir() && h_info.userMayRX() &&
42 c_info.isFile() && c_info.userMayR() )
44 MIL << "Going to parse " << curlrcFile << endl;
49 WAR << "Not allowed to parse '" << curlrcFile
50 << "': dir/file owner: " << h_info.owner() << "/" << c_info.owner()
51 << ", process uid: " << getuid()
52 << " (" << (!getlogin_r(buf, 31) ? buf : "") << ")" << std::endl;
57 std::ifstream inp(curlrcFile.c_str());
58 for(iostr::EachLine in( inp ); in; in.next())
60 std::string line = str::trim(*in);
62 // skip empty lines and comments
76 // DBG << "line " << in.lineNo() << ": " << line << endl; // can't log passwords
78 const char * beg = line.c_str();
79 const char * cur = beg;
81 // space, '=' and ':' are all valid separators in curlrc
82 #define ISSEP(x) (((x)=='=') || ((x) == ':') || isspace(x))
84 // skip leading dashes (they are optional)
85 while (*cur && *cur == '-')
89 // skip non-separator characters
90 while (*cur && !ISSEP(*cur))
93 std::string option(beg, cur - beg);
95 // skip separator characters
96 while (*cur && ISSEP(*cur))
99 // rewind to the end of the line
104 std::string value(beg, cur - beg);
106 DBG << "GOT: " << option << endl;
111 if (value[0] == '\"')
114 std::string::size_type pos = value.rfind('\"');
116 pos == value.size() - 1 && pos > 1 && value[pos-1] != '\\';
117 value = value.substr(1,
118 cut_last ? value.size() - 2 : value.size() - 1);
120 // replace special characters:
122 while ((pos = value.find('\\', pos)) != std::string::npos)
124 // just erase the backslash if it is found at the end
125 if (pos == value.size() - 1)
127 value = value.erase(pos, 1);
134 value = value.replace(pos, 2, "\t");
137 value = value.replace(pos, 2, "\n");
140 value = value.replace(pos, 2, "\r");
143 value = value.replace(pos, 2, "\v");
146 value = value.erase(pos++, 1);
149 value = value.erase(pos, 1);
154 // DBG << "PARAM: " << value << endl; // can't log passwords
157 CurlConfig::setParameter(config, option, value);
158 } // for EachLine in curlrc
164 ///////////////////////////////////////////////////////////////////
166 // METHOD NAME : CurlConfig::setParameter
169 int CurlConfig::setParameter(CurlConfig & config,
170 const std::string & option,
171 const std::string & value)
173 if (option == "proxy-user")
174 config.proxyuserpwd = value;
175 // add more curl config data here as they become needed
176 // else if (option == "foo")
178 DBG << "Ignoring option " << option << endl;