1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/*******************************************************************************
* libproxy - A library for proxy configuration
* Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include <vector>
#include <cstring> // For strdup()
#include <iostream> // For cerr
#include <stdexcept> // For exception
#include <typeinfo> // Only for debug messages.
#include "config.hpp"
#include <stdlib.h>
#include <gio/gio.h>
namespace libproxy {
using namespace std;
class proxy_factory {
public:
proxy_factory();
~proxy_factory();
vector<string> get_proxies(const string &url);
private:
bool debug;
GDBusProxy *proxy;
GError **error = NULL;
};
proxy_factory::proxy_factory() {
this->proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL, /* GDBusInterfaceInfo */
"org.libproxy.proxy",
"/org/libproxy/proxy",
"org.libproxy.proxy",
NULL, /* GCancellable */
this->error);
}
proxy_factory::~proxy_factory() {
}
vector<string> proxy_factory::get_proxies(const string &realurl) {
vector<string> response;
GVariant* result;
result = g_dbus_proxy_call_sync (proxy,
"query",
g_variant_new("(s)", realurl.c_str()),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
this->error);
GVariantIter *iter;
gchar *str;
g_variant_get (result, "(as)", &iter);
while (g_variant_iter_loop (iter, "s", &str))
response.push_back(str);
g_variant_iter_free (iter);
do_return:
if (response.size() == 0)
response.push_back("direct://");
return response;
}
struct pxProxyFactory_ {
libproxy::proxy_factory pf;
};
extern "C" DLL_PUBLIC struct pxProxyFactory_ *px_proxy_factory_new(void) {
return new struct pxProxyFactory_;
}
extern "C" DLL_PUBLIC char** px_proxy_factory_get_proxies(struct pxProxyFactory_ *self, const char *url) {
std::vector<std::string> proxies;
char** retval;
// Call the main method
try {
proxies = self->pf.get_proxies(url);
retval = (char**) malloc(sizeof(char*) * (proxies.size() + 1));
if (!retval) return NULL;
} catch (std::exception&) {
// Return NULL on any exception
return NULL;
}
// Copy the results into an array
// Return NULL on memory allocation failure
retval[proxies.size()] = NULL;
for (size_t i=0 ; i < proxies.size() ; i++) {
retval[i] = strdup(proxies[i].c_str());
if (retval[i] == NULL) {
for (int j=i-1 ; j >= 0 ; j--)
free(retval[j]);
free(retval);
return NULL;
}
}
return retval;
}
extern "C" DLL_PUBLIC void px_proxy_factory_free_proxies(char ** const proxies) {
if (!proxies)
return;
for (size_t i = 0; proxies[i]; ++i)
free(proxies[i]);
free(proxies);
}
extern "C" DLL_PUBLIC void px_proxy_factory_free(struct pxProxyFactory_ *self) {
delete self;
}
}
|