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    int Pipes
[2] = {-1,-1}; 
  46       return _error
->Errno("pipe", "Failed to create Pipe"); 
  48    pid_t Process 
= ExecFork(); 
  52       dup2(Pipes
[1],STDOUT_FILENO
); 
  53       SetCloseExec(STDOUT_FILENO
,false); 
  55       std::string foo 
= URL
; 
  57       Args
[0] = AutoDetectProxyCmd
.c_str(); 
  58       Args
[1] = foo
.c_str(); 
  60       execv(Args
[0],(char **)Args
); 
  61       std::cerr 
<< "Failed to exec method " << Args
[0] << std::endl
; 
  67    int res 
= read(InFd
, buf
, sizeof(buf
)-1); 
  68    ExecWait(Process
, "ProxyAutoDetect", true); 
  71       return _error
->Errno("read", "Failed to read"); 
  73       return _error
->Warning("ProxyAutoDetect returned no data"); 
  79       std::clog 
<< "auto detect command returned: '" << buf 
<< "'" << std::endl
; 
  81    if (strstr(buf
, URL
.Access
.c_str()) == buf
) 
  82       _config
->Set("Acquire::"+URL
.Access
+"::proxy::"+URL
.Host
, _strstrip(buf
));