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 // the user already explicitly set a proxy for this host
30 if(_config
->Find("Acquire::"+URL
.Access
+"::proxy::"+URL
.Host
, "") != "")
33 // option is "Acquire::http::Proxy-Auto-Detect" but we allow the old
34 // name without the dash ("-")
35 std::string AutoDetectProxyCmd
= _config
->Find("Acquire::"+URL
.Access
+"::Proxy-Auto-Detect",
36 _config
->Find("Acquire::"+URL
.Access
+"::ProxyAutoDetect"));
38 if (AutoDetectProxyCmd
.empty())
42 std::clog
<< "Using auto proxy detect command: " << AutoDetectProxyCmd
<< std::endl
;
44 std::string
const urlstring
= URL
;
45 std::vector
<const char *> Args
;
46 Args
.push_back(AutoDetectProxyCmd
.c_str());
47 Args
.push_back(urlstring
.c_str());
48 Args
.push_back(nullptr);
51 if(Popen(&Args
[0], PipeFd
, Child
, FileFd::ReadOnly
, false) == false)
52 return _error
->Error("ProxyAutoDetect command '%s' failed!", AutoDetectProxyCmd
.c_str());
54 bool const goodread
= PipeFd
.ReadLine(buf
, sizeof(buf
)) != nullptr;
56 if (ExecWait(Child
, "ProxyAutoDetect") == false)
58 // no output means the detector has no idea which proxy to use
59 // and apt will use the generic proxy settings
60 if (goodread
== false)
62 auto const cleanedbuf
= _strstrip(buf
);
63 // We warn about this as the implementor probably meant to use DIRECT instead
64 if (cleanedbuf
[0] == '\0')
66 _error
->Warning("ProxyAutoDetect command returned an empty line");
71 std::clog
<< "auto detect command returned: '" << cleanedbuf
<< "'" << std::endl
;
73 if (strstr(cleanedbuf
, URL
.Access
.c_str()) == cleanedbuf
|| strcmp(cleanedbuf
, "DIRECT") == 0)
74 _config
->Set("Acquire::"+URL
.Access
+"::proxy::"+URL
.Host
, cleanedbuf
);