]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/proxy.cc
implement indextargets option 'DefaultEnabled'
[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 int Pipes[2] = {-1,-1};
45 if (pipe(Pipes) != 0)
46 return _error->Errno("pipe", "Failed to create Pipe");
47
48 pid_t Process = ExecFork();
49 if (Process == 0)
50 {
51 close(Pipes[0]);
52 dup2(Pipes[1],STDOUT_FILENO);
53 SetCloseExec(STDOUT_FILENO,false);
54
55 std::string foo = URL;
56 const char *Args[4];
57 Args[0] = AutoDetectProxyCmd.c_str();
58 Args[1] = foo.c_str();
59 Args[2] = 0;
60 execv(Args[0],(char **)Args);
61 std::cerr << "Failed to exec method " << Args[0] << std::endl;
62 _exit(100);
63 }
64 char buf[512];
65 int InFd = Pipes[0];
66 close(Pipes[1]);
67 int res = read(InFd, buf, sizeof(buf)-1);
68 ExecWait(Process, "ProxyAutoDetect", true);
69
70 if (res < 0)
71 return _error->Errno("read", "Failed to read");
72 if (res == 0)
73 return _error->Warning("ProxyAutoDetect returned no data");
74
75 // add trailing \0
76 buf[res] = 0;
77
78 if (Debug)
79 std::clog << "auto detect command returned: '" << buf << "'" << std::endl;
80
81 if (strstr(buf, URL.Access.c_str()) == buf)
82 _config->Set("Acquire::"+URL.Access+"::proxy::"+URL.Host, _strstrip(buf));
83
84 return true;
85 }
86 /*}}}*/