17 #include "pacparser.h"
32 const int retval = vasprintf(&msg, fmt, argp);
42 const bool report_errors) {
48 vector<string> components =
SplitString(pac_proxy,
';');
49 for (
unsigned i = 0; i < components.size(); ++i) {
52 for (
unsigned j = 0; j < components[i].length(); ++j) {
53 if ((components[i][j] !=
' ') && (components[i][j] !=
'\t'))
54 next_proxy.push_back(components[i][j]);
58 if (
HasPrefix(next_proxy,
"SOCKS",
false)) {
60 "no support for SOCKS proxy, skipping %s",
61 next_proxy.substr(5).c_str());
65 if ((next_proxy !=
"DIRECT") && !
HasPrefix(next_proxy,
"PROXY",
false)) {
71 if (
HasPrefix(next_proxy,
"PROXY",
false))
72 next_proxy = next_proxy.substr(5);
74 if (cvmfs_proxy ==
"")
75 cvmfs_proxy = next_proxy;
77 cvmfs_proxy +=
";" + next_proxy;
89 bool retval = pacparser_init();
93 const string pac_string(pac_data, size);
95 retval = pacparser_parse_pac_string(pac_string.c_str());
102 vector<string> host_list;
104 unsigned current_host;
105 download_manager->
GetHostInfo(&host_list, &rtt, ¤t_host);
106 for (
unsigned i = 0; i < host_list.size(); ++i) {
107 const size_t hostname_begin = 7;
108 const size_t hostname_end =
109 host_list[i].find_first_of(
":/", hostname_begin);
110 const size_t hostname_len = (hostname_end == string::npos)
112 : hostname_end - hostname_begin;
113 const string hostname = (hostname_begin > host_list[i].length())
115 : host_list[i].substr(hostname_begin,
117 const string url = host_list[i] +
"/.cvmfspublished";
120 char *pac_proxy = pacparser_find_proxy(url.c_str(), hostname.c_str());
121 if (pac_proxy == NULL) {
125 if (*proxies ==
"") {
127 if (*proxies ==
"") {
129 "no valid proxy found (%s returned from pac file)", pac_proxy);
135 if (*proxies != alt_proxies) {
137 "proxy settings for host %s differ from proxy settings for "
138 "other hosts (%s / %s). Not using proxy setting %s.",
139 host_list[i].c_str(), proxies->c_str(), alt_proxies.c_str(),
140 alt_proxies.c_str());
151 char *http_env = getenv(
"http_proxy");
155 "using HTTP proxy server(s) %s from http_proxy environment",
157 return string(http_env);
160 vector<string> pac_paths;
161 char *pac_env = getenv(
"CVMFS_PAC_URLS");
166 for (
unsigned i = 0; i < pac_paths.size(); ++i) {
167 if (pac_paths[i] ==
"auto") {
173 pac_paths[i].c_str());
177 int retval = download_manager->
Fetch(&download_pac);
180 retval =
ParsePac(reinterpret_cast<char *>(pac_memsink.
data()),
186 "failed to parse pac file %s", pac_paths[i].c_str());
191 "using HTTP proxy server(s) %s from pac file %s",
192 proxies.c_str(), pac_paths[i].c_str());
198 pac_paths[i].c_str());
207 const std::string &path_fallback_cache,
209 if ((cvmfs_proxies ==
"") || (cvmfs_proxies.find(
"auto") == string::npos))
210 return cvmfs_proxies;
213 vector<string> lb_groups =
SplitString(cvmfs_proxies,
';');
214 for (
unsigned i = 0; i < lb_groups.size(); ++i) {
215 if (lb_groups[i] !=
"auto")
218 lb_groups[i] =
AutoProxy(download_manager);
219 if (lb_groups[i].empty())
220 empty_auto =
static_cast<int>(i);
223 if (empty_auto != -1)
224 lb_groups.erase(lb_groups.begin() +
static_cast<unsigned>(empty_auto));
225 string discovered_proxies =
JoinStrings(lb_groups,
";");
227 if (!path_fallback_cache.empty()) {
228 if (empty_auto != -1) {
229 string cached_proxies;
230 const int fd = open(path_fallback_cache.c_str(), O_RDONLY);
236 "using cached proxy settings from %s",
237 path_fallback_cache.c_str());
238 return cached_proxies;
246 "failed to write proxy settings into %s",
247 path_fallback_cache.c_str());
252 return discovered_proxies;
258 FILE *log_output = NULL;
264 fprintf(log_output,
"%s\n", msg);
275 const string proxy_configuration = argv[2];
276 const string host_list = argv[3];
281 const string resolved_proxies =
285 return resolved_proxies ==
"";
void SetHostChain(const std::string &host_list)
CVMFS_EXPORT const LogSource const int mask
CVMFS_EXPORT const LogSource source
string JoinStrings(const vector< string > &strings, const string &joint)
int MainResolveProxyDescription(int argc, char **argv)
assert((mem||(size==0))&&"Out Of Memory")
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
void SetAltLogFunc(void(*fn)(const LogSource source, const int mask, const char *msg))
vector< string > SplitString(const string &str, char delim)
string AutoProxy(DownloadManager *download_manager)
static void AltCvmfsLogger(const LogSource source, const int mask, const char *msg)
bool HasPrefix(const string &str, const string &prefix, const bool ignore_case)
bool SafeReadToString(int fd, std::string *final_result)
const char * kAutoPacLocation
string ResolveProxyDescription(const string &cvmfs_proxies, const std::string &path_fallback_cache, DownloadManager *download_manager)
Failures Fetch(JobInfo *info)
static bool ParsePac(const char *pac_data, const size_t size, DownloadManager *download_manager, string *proxies)
static int PrintPacError(const char *fmt, va_list argp)
void GetHostInfo(std::vector< std::string > *host_chain, std::vector< int > *rtt, unsigned *current_host)
static string PacProxy2Cvmfs(const string &pac_proxy, const bool report_errors)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)