/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef PROXY_LINUX
#define PROXY_LINUX

#ifdef HAVE_PROXY
extern "C" {
  #include <proxy.h>
}

#include <iostream> // debug

namespace proxy {

  class Unix: public Interface {

    public:

      Unix(): _factory(px_proxy_factory_new()) {}

      virtual ~Unix() {
        px_proxy_factory_free(_factory);
      }

      //! Implemented using http://code.google.com/p/libproxy/
      virtual List proxies(const std::string& url) {
        List res;
        char** proxies(px_proxy_factory_get_proxies
                       (_factory, const_cast<char*>(url.data())));
        for (int i(0); proxies[i]; ++i) {
          std::string proxy(proxies[i]);
          Type type(proxy.find("http://")==0?HTTP:
                    (proxy.find("socks://")==0?SOCKS:DIRECT));
          if (proxy.find("://")!=std::string::npos)
            proxy=proxy.substr(proxy.find("://")+3);
          std::string::size_type oldpos(0);
          std::string port((oldpos=proxy.rfind(":"))!=std::string::npos
                           ?proxy.substr(oldpos+1):std::string());
          std::string host(proxy.substr(0, proxy.rfind(":")));
          std::cout<<"Proxy found: "<<host<<", port="<<port<<", type"<<type
                   <<std::endl;
          res.push_back(Proxy(type, host, port));
        }
        for (int i(0); proxies[i]; ++i)
          delete proxies[i];
        delete proxies;
        return res;
      }

    private:
      
      pxProxyFactory* _factory;
      
  };
  
}

#endif
#endif
