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)
52 return _error
->Error("ProxyAutoDetect command '%s' failed!", AutoDetectProxyCmd
.c_str());
54 if (PipeFd
.ReadLine(buf
, sizeof(buf
)) == nullptr)
55 return _error
->Error("Failed to read in AutoDetectProxy");
57 ExecWait(Child
, "ProxyAutoDetect", true);
58 auto const cleanedbuf
= _strstrip(buf
);
60 if (cleanedbuf
[0] == '\0')
61 return _error
->Warning("ProxyAutoDetect returned no data");
64 std::clog
<< "auto detect command returned: '" << cleanedbuf
<< "'" << std::endl
;
66 if (strstr(cleanedbuf
, URL
.Access
.c_str()) == cleanedbuf
)
67 _config
->Set("Acquire::"+URL
.Access
+"::proxy::"+URL
.Host
, cleanedbuf
);