00001
00002
00003
00004
00005
00006
00007
00008
00013 #include <iostream>
00014 #include <fstream>
00015
00016 #include "zypp/base/Logger.h"
00017 #include "zypp/base/String.h"
00018 #include "zypp/Pathname.h"
00019
00020 #include "zypp/media/proxyinfo/ProxyInfoSysconfig.h"
00021
00022 using namespace std;
00023 using namespace zypp::base;
00024
00025 namespace zypp {
00026 namespace media {
00027
00028
00029 namespace proxyinfo {
00030 map<string,string> sysconfigRead(const Pathname & _path)
00031 {
00032 DBG << "Load '" << _path << "'" << endl;
00033 map<string,string> ret;
00034
00035 string line;
00036 ifstream in( _path.asString().c_str() );
00037 if ( in.fail() ) {
00038 WAR << "Unable to load '" << _path << "'" << endl;
00039 return ret;
00040 }
00041 while( getline( in, line ) ) {
00042 if ( *line.begin() != '#' ) {
00043 string::size_type pos = line.find( '=', 0 );
00044 if ( pos != string::npos ) {
00045 string key = str::trim( line.substr( 0, pos ) );
00046 string value = str::trim( line.substr( pos + 1, line.length() - pos - 1 ) );
00047 if ( value.length() >= 2 && *(value.begin()) == '"' &&
00048 *(value.rbegin()) == '"' ) {
00049 value = value.substr( 1, value.length() - 2 );
00050 }
00051 if ( value.length() >= 2 && *(value.begin()) == '\'' &&
00052 *(value.rbegin()) == '\'' ) {
00053 value = value.substr( 1, value.length() - 2 );
00054 }
00055 DBG << "KEY: '" << key << "' VALUE: '" << value << "'" << endl;
00056 ret[key] = value;
00057 }
00058 }
00059 }
00060 return ret;
00061 }
00062 }
00063
00064 ProxyInfoSysconfig::ProxyInfoSysconfig(const Pathname & path)
00065 : ProxyInfo::Impl()
00066 {
00067 map<string,string> data = proxyinfo::sysconfigRead(
00068 path.relative()
00069 ? "/etc/sysconfig" + path
00070 : path);
00071 map<string,string>::const_iterator it = data.find("PROXY_ENABLED");
00072 if (it != data.end())
00073 _enabled = it->second != "no";
00074 it = data.find("HTTP_PROXY");
00075 if (it != data.end())
00076 _proxies["http"] = it->second;
00077 it = data.find("HTTPS_PROXY");
00078 if (it != data.end())
00079 _proxies["https"] = it->second;
00080 it = data.find("FTP_PROXY");
00081 if (it != data.end())
00082 _proxies["ftp"] = it->second;
00083 it = data.find("NO_PROXY");
00084 if (it != data.end())
00085 str::split(it->second, std::back_inserter(_no_proxy), ", \t");
00086 }
00087
00088 std::string ProxyInfoSysconfig::proxy(const std::string & protocol_r) const
00089 {
00090 map<string,string>::const_iterator it = _proxies.find(protocol_r);
00091 if (it != _proxies.end())
00092 return it->second;
00093 return "";
00094 }
00095
00096 ProxyInfo::NoProxyIterator ProxyInfoSysconfig::noProxyBegin() const
00097 { return _no_proxy.begin(); }
00098
00099 ProxyInfo::NoProxyIterator ProxyInfoSysconfig::noProxyEnd() const
00100 { return _no_proxy.end(); }
00101
00102 }
00103 }