libproxy implementation for ProxyInfo. Right now it is used if libproxy is present.
[platform/upstream/libzypp.git] / zypp / media / proxyinfo / ProxyInfoLibproxy.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/proxyinfo/ProxyInfoLibproxy.cc
10  *
11 */
12
13 #include <iostream>
14 #include <fstream>
15
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/String.h"
18 #include "zypp/Pathname.h"
19
20 #include "zypp/media/proxyinfo/ProxyInfoLibproxy.h"
21
22 using namespace std;
23 using namespace zypp::base;
24
25 namespace zypp {
26   namespace media {
27
28     ProxyInfoLibproxy::ProxyInfoLibproxy()
29     : ProxyInfo::Impl()
30     {
31       _factory = px_proxy_factory_new();
32       _enabled = !(_factory == NULL);
33     }
34
35     ProxyInfoLibproxy::~ProxyInfoLibproxy()
36     {
37       if (_enabled) {
38         px_proxy_factory_free(_factory);
39         _factory = NULL;
40         _enabled = false;
41       }
42     }
43
44     std::string ProxyInfoLibproxy::proxy(const Url & url_r) const
45     {
46       if (!_enabled)
47         return "";
48
49       const url::ViewOption vopt =
50               url::ViewOption::WITH_SCHEME
51               + url::ViewOption::WITH_HOST
52               + url::ViewOption::WITH_PORT
53               + url::ViewOption::WITH_PATH_NAME;
54
55       char **proxies = px_proxy_factory_get_proxies(_factory,
56                                                     (char *)url_r.asString(vopt).c_str());
57       if (!proxies)
58               return "";
59
60       /* cURL can only handle HTTP proxies, not SOCKS. And can only handle
61          one. So look through the list and find an appropriate one. */
62       char *result = NULL;
63
64       for (int i = 0; proxies[i]; i++) {
65               if (!result &&
66                   !strncmp(proxies[i], "http://", 7))
67                       result = proxies[i];
68               else
69                       free(proxies[i]);
70       }
71       free(proxies);
72
73       if (!result)
74               return "";
75
76       std::string sresult = result;
77       free(result);
78       return sresult;
79     }
80
81     ProxyInfo::NoProxyIterator ProxyInfoLibproxy::noProxyBegin() const
82     { return _no_proxy.begin(); }
83
84     ProxyInfo::NoProxyIterator ProxyInfoLibproxy::noProxyEnd() const
85     { return _no_proxy.end(); }
86
87   } // namespace media
88 } // namespace zypp