17 #include "pacparser.h"
32 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 size_t hostname_begin = 7;
108 size_t hostname_end = host_list[i].find_first_of(
":/", hostname_begin);
109 size_t hostname_len = (hostname_end == string::npos)
111 : hostname_end - hostname_begin;
112 const string hostname = (hostname_begin > host_list[i].length())
114 : host_list[i].substr(hostname_begin,
116 const string url = host_list[i] +
"/.cvmfspublished";
119 char *pac_proxy = pacparser_find_proxy(url.c_str(), hostname.c_str());
120 if (pac_proxy == NULL) {
124 if (*proxies ==
"") {
126 if (*proxies ==
"") {
128 "no valid proxy found (%s returned from pac file)", pac_proxy);
134 if (*proxies != alt_proxies) {
136 "proxy settings for host %s differ from proxy settings for "
137 "other hosts (%s / %s). Not using proxy setting %s.",
138 host_list[i].c_str(), proxies->c_str(), alt_proxies.c_str(),
139 alt_proxies.c_str());
150 char *http_env = getenv(
"http_proxy");
154 "using HTTP proxy server(s) %s from http_proxy environment",
156 return string(http_env);
159 vector<string> pac_paths;
160 char *pac_env = getenv(
"CVMFS_PAC_URLS");
165 for (
unsigned i = 0; i < pac_paths.size(); ++i) {
166 if (pac_paths[i] ==
"auto") {
172 pac_paths[i].c_str());
176 int retval = download_manager->
Fetch(&download_pac);
179 retval =
ParsePac(reinterpret_cast<char *>(pac_memsink.
data()),
185 "failed to parse pac file %s", pac_paths[i].c_str());
190 "using HTTP proxy server(s) %s from pac file %s",
191 proxies.c_str(), pac_paths[i].c_str());
197 pac_paths[i].c_str());
206 const std::string &path_fallback_cache,
208 if ((cvmfs_proxies ==
"") || (cvmfs_proxies.find(
"auto") == string::npos))
209 return cvmfs_proxies;
212 vector<string> lb_groups =
SplitString(cvmfs_proxies,
';');
213 for (
unsigned i = 0; i < lb_groups.size(); ++i) {
214 if (lb_groups[i] !=
"auto")
217 lb_groups[i] =
AutoProxy(download_manager);
218 if (lb_groups[i].empty())
219 empty_auto =
static_cast<int>(i);
222 if (empty_auto != -1)
223 lb_groups.erase(lb_groups.begin() +
static_cast<unsigned>(empty_auto));
224 string discovered_proxies =
JoinStrings(lb_groups,
";");
226 if (!path_fallback_cache.empty()) {
227 if (empty_auto != -1) {
228 string cached_proxies;
229 int fd = open(path_fallback_cache.c_str(), O_RDONLY);
235 "using cached proxy settings from %s",
236 path_fallback_cache.c_str());
237 return cached_proxies;
245 "failed to write proxy settings into %s",
246 path_fallback_cache.c_str());
251 return discovered_proxies;
257 FILE *log_output = NULL;
263 fprintf(log_output,
"%s\n", msg);
274 string proxy_configuration = argv[2];
275 string host_list = argv[3];
284 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,...)