]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/proxy.cc
travis: Add coverage testing using codecov.io
[apt.git] / apt-pkg / contrib / proxy.cc
CommitLineData
c6ee61ea
MV
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"
8f858d56 19 /*}}}*/
c6ee61ea 20
8f858d56 21// AutoDetectProxy - auto detect proxy /*{{{*/
c6ee61ea
MV
22// ---------------------------------------------------------------------
23/* */
24bool AutoDetectProxy(URI &URL)
25{
26 // we support both http/https debug options
27 bool Debug = _config->FindB("Debug::Acquire::"+URL.Access,false);
28
d73fe42c
MV
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
c6ee61ea
MV
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
8f858d56
DK
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());
c6ee61ea 53 char buf[512];
9515ed7b 54 bool const goodread = PipeFd.ReadLine(buf, sizeof(buf)) != nullptr;
8f858d56 55 PipeFd.Close();
9515ed7b
DK
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;
8f858d56 62 auto const cleanedbuf = _strstrip(buf);
9515ed7b 63 // We warn about this as the implementor probably meant to use DIRECT instead
8f858d56 64 if (cleanedbuf[0] == '\0')
9515ed7b
DK
65 {
66 _error->Warning("ProxyAutoDetect command returned an empty line");
67 return true;
68 }
c6ee61ea 69
c6ee61ea 70 if (Debug)
8f858d56 71 std::clog << "auto detect command returned: '" << cleanedbuf << "'" << std::endl;
c6ee61ea 72
9515ed7b 73 if (strstr(cleanedbuf, URL.Access.c_str()) == cleanedbuf || strcmp(cleanedbuf, "DIRECT") == 0)
8f858d56 74 _config->Set("Acquire::"+URL.Access+"::proxy::"+URL.Host, cleanedbuf);
c6ee61ea
MV
75
76 return true;
77}
78 /*}}}*/