add Acquire::http::ProxyAutoDetect configuration that
authorMichael Vogt <michael.vogt@ubuntu.com>
Sat, 6 Feb 2010 02:34:27 +0000 (18:34 -0800)
committerMichael Vogt <michael.vogt@ubuntu.com>
Sat, 6 Feb 2010 02:34:27 +0000 (18:34 -0800)
can be used to call a external helper to figure out the
proxy configuration and return it to apt via stdout

debian/changelog
methods/http.cc
methods/http.h

index 03936a3..329ea29 100644 (file)
@@ -20,6 +20,9 @@ apt (0.7.25.1) UNRELEASED; urgency=low
   * methods/http.cc:
     - add cache-control headers even if no cache is given to allow
       adding options for intercepting proxies
+    - add Acquire::http::ProxyAutoDetect configuration that 
+      can be used to call a external helper to figure out the 
+      proxy configuration and return it to apt via stdout
 
  -- Michael Vogt <michael.vogt@ubuntu.com>  Fri, 18 Dec 2009 16:54:18 +0100
 
index 2bd5702..b054446 100644 (file)
@@ -1073,7 +1073,11 @@ bool HttpMethod::Configuration(string Message)
    PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth",
                                  PipelineDepth);
    Debug = _config->FindB("Debug::Acquire::http",false);
-   
+   AutoDetectProxyCmd = _config->Find("Acquire::http::ProxyAutoDetect");
+
+   // Get the proxy to use
+   AutoDetectProxy();
+
    return true;
 }
                                                                        /*}}}*/
@@ -1323,6 +1327,49 @@ int HttpMethod::Loop()
    return 0;
 }
                                                                        /*}}}*/
+// HttpMethod::AutoDetectProxy - auto detect proxy                     /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool HttpMethod::AutoDetectProxy()
+{
+   if (AutoDetectProxyCmd.empty())
+      return true;
+
+   if (Debug)
+      clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << endl;
+
+   int Pipes[2] = {-1,-1};
+   if (pipe(Pipes) != 0)
+      return _error->Errno("pipe", "Failed to create Pipe");
+
+   pid_t Process = ExecFork();
+   if (Process == 0)
+   {
+      dup2(Pipes[1],STDOUT_FILENO);
+      SetCloseExec(STDOUT_FILENO,false);
+      
+      const char *Args[2];
+      Args[0] = AutoDetectProxyCmd.c_str();
+      Args[1] = 0;
+      execv(Args[0],(char **)Args);
+      cerr << "Failed to exec method " << Args[0] << endl;
+      _exit(100);
+   }
+   char buf[512];
+   int InFd = Pipes[0];
+   if (read(InFd, buf, sizeof(buf)) < 0)
+      return _error->Errno("read", "Failed to read");
+   ExecWait(Process, "ProxyAutoDetect");
+   
+   if (Debug)
+      clog << "auto detect command returned: '" << buf << "'" << endl;
+
+   if (strstr(buf, "http://") == buf)
+      _config->Set("Acquire::http::proxy", _strstrip(buf));
+
+   return true;
+}
+                                                                       /*}}}*/
 
 int main()
 {
index 13f02ec..ceee36c 100644 (file)
@@ -134,6 +134,7 @@ class HttpMethod : public pkgAcqMethod
    bool Flush(ServerState *Srv);
    bool ServerDie(ServerState *Srv);
    int DealWithHeaders(FetchResult &Res,ServerState *Srv);
+   bool AutoDetectProxy();
 
    virtual bool Fetch(FetchItem *);
    virtual bool Configuration(string Message);
@@ -145,6 +146,7 @@ class HttpMethod : public pkgAcqMethod
    static void SigTerm(int);
    
    string NextURI;
+   string AutoDetectProxyCmd;
    
    public:
    friend class ServerState;