1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Proxy - Proxy releated functions
7 ##################################################################### */
9 // Include Files /*{{{*/
10 #include<apt-pkg/configuration.h>
11 #include<apt-pkg/error.h>
12 #include<apt-pkg/fileutl.h>
13 #include<apt-pkg/strutl.h>
21 // AutoDetectProxy - auto detect proxy /*{{{*/
22 // ---------------------------------------------------------------------
24 bool AutoDetectProxy(URI
&URL
)
26 // we support both http/https debug options
27 bool Debug
= _config
->FindB("Debug::Acquire::"+URL
.Access
,false);
29 // option is "Acquire::http::Proxy-Auto-Detect" but we allow the old
30 // name without the dash ("-")
31 std::string AutoDetectProxyCmd
= _config
->Find("Acquire::"+URL
.Access
+"::Proxy-Auto-Detect",
32 _config
->Find("Acquire::"+URL
.Access
+"::ProxyAutoDetect"));
34 if (AutoDetectProxyCmd
.empty())
38 std::clog
<< "Using auto proxy detect command: " << AutoDetectProxyCmd
<< std::endl
;
40 int Pipes
[2] = {-1,-1};
42 return _error
->Errno("pipe", "Failed to create Pipe");
44 pid_t Process
= ExecFork();
48 dup2(Pipes
[1],STDOUT_FILENO
);
49 SetCloseExec(STDOUT_FILENO
,false);
51 std::string foo
= URL
;
53 Args
[0] = AutoDetectProxyCmd
.c_str();
54 Args
[1] = foo
.c_str();
56 execv(Args
[0],(char **)Args
);
57 std::cerr
<< "Failed to exec method " << Args
[0] << std::endl
;
63 int res
= read(InFd
, buf
, sizeof(buf
)-1);
64 ExecWait(Process
, "ProxyAutoDetect", true);
67 return _error
->Errno("read", "Failed to read");
69 return _error
->Warning("ProxyAutoDetect returned no data");
75 std::clog
<< "auto detect command returned: '" << buf
<< "'" << std::endl
;
77 if (strstr(buf
, URL
.Access
.c_str()) == buf
)
78 _config
->Set("Acquire::"+URL
.Access
+"::proxy::"+URL
.Host
, _strstrip(buf
));