]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/proxy.cc
give a descriptive error for pipe tries with 'false'
[apt.git] / apt-pkg / contrib / proxy.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Proxy - Proxy releated functions
6
7 ##################################################################### */
8 /*}}}*/
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>
14
15 #include<iostream>
16 #include <unistd.h>
17
18 #include "proxy.h"
19 /*}}}*/
20
21 // AutoDetectProxy - auto detect proxy /*{{{*/
22 // ---------------------------------------------------------------------
23 /* */
24 bool AutoDetectProxy(URI &URL)
25 {
26 // we support both http/https debug options
27 bool Debug = _config->FindB("Debug::Acquire::"+URL.Access,false);
28
29 // the user already explicitly set a proxy for this host
30 if(_config->Find("Acquire::"+URL.Access+"::proxy::"+URL.Host, "") != "")
31 return true;
32
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"));
37
38 if (AutoDetectProxyCmd.empty())
39 return true;
40
41 if (Debug)
42 std::clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << std::endl;
43
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);
49 FileFd PipeFd;
50 pid_t Child;
51 if(Popen(&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
52 return _error->Error("ProxyAutoDetect command '%s' failed!", AutoDetectProxyCmd.c_str());
53 char buf[512];
54 bool const goodread = PipeFd.ReadLine(buf, sizeof(buf)) != nullptr;
55 PipeFd.Close();
56 if (ExecWait(Child, "ProxyAutoDetect") == false)
57 return 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)
61 return true;
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')
65 {
66 _error->Warning("ProxyAutoDetect command returned an empty line");
67 return true;
68 }
69
70 if (Debug)
71 std::clog << "auto detect command returned: '" << cleanedbuf << "'" << std::endl;
72
73 if (strstr(cleanedbuf, URL.Access.c_str()) == cleanedbuf || strcmp(cleanedbuf, "DIRECT") == 0)
74 _config->Set("Acquire::"+URL.Access+"::proxy::"+URL.Host, cleanedbuf);
75
76 return true;
77 }
78 /*}}}*/