diff options
author | npmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56> | 2010-01-16 22:51:29 +0000 |
---|---|---|
committer | npmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56> | 2010-01-16 22:51:29 +0000 |
commit | 4f8fd0fec07ad0c21ed64a6b24c223f42f9d4e81 (patch) | |
tree | 0574aaf71bc4c11c3858848500297b9d55806882 /bindings | |
parent | 88c9d0d1705730c0c12a61464ea5f5caad7e756c (diff) | |
download | libproxy-4f8fd0fec07ad0c21ed64a6b24c223f42f9d4e81.tar.gz |
new build system: CMake
git-svn-id: http://libproxy.googlecode.com/svn/trunk@455 c587cffe-e639-0410-9787-d7902ae8ed56
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/AssemblyInfo.cs.in | 33 | ||||
-rw-r--r-- | bindings/CMakeLists.txt | 13 | ||||
-rw-r--r-- | bindings/libproxy-sharp-1.0.pc.in | 8 | ||||
-rw-r--r-- | bindings/libproxy.cs | 79 | ||||
-rw-r--r-- | bindings/libproxy.py | 103 | ||||
-rw-r--r-- | bindings/libproxy.snk | bin | 0 -> 596 bytes |
6 files changed, 236 insertions, 0 deletions
diff --git a/bindings/AssemblyInfo.cs.in b/bindings/AssemblyInfo.cs.in new file mode 100644 index 0000000..cbee88d --- /dev/null +++ b/bindings/AssemblyInfo.cs.in @@ -0,0 +1,33 @@ +/******************************************************************************* + * libproxy - A library for proxy configuration + * Copyright (C) 2008-2009 Dominique Leuenberger <dominique@leuenberger.net> + * + * 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 + ******************************************************************************/ + +using System; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly:AssemblyVersion("@VERSION@")] +[assembly:AssemblyTitle ("LibProxy#")] +[assembly:AssemblyDescription ("A library that provides automatic proxy configuration management.")] +[assembly:AssemblyCopyright ("Copyright (c) 2006-2009 Nathaniel McCallum")] +[assembly:AssemblyCompany ("")] +[assembly:AssemblyDelaySign(false)] +[assembly:AssemblyKeyFile("libproxy.snk")] +[assembly:CLSCompliant(false)] + diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt new file mode 100644 index 0000000..0d9f749 --- /dev/null +++ b/bindings/CMakeLists.txt @@ -0,0 +1,13 @@ +find_package(PythonInterp) + +if(PYTHONINTERP_FOUND) + execute_process(COMMAND + ${PYTHON_EXECUTABLE} + -c "import distutils.sysconfig ; print distutils.sysconfig.get_python_lib(plat_specific=1)" + OUTPUT_VARIABLE pysitepkgdir + OUTPUT_STRIP_TRAILING_WHITESPACE) + install(FILES libproxy.py DESTINATION ${pysitepkgdir}) +endif() + +#TODO c-sharp bindings + diff --git a/bindings/libproxy-sharp-1.0.pc.in b/bindings/libproxy-sharp-1.0.pc.in new file mode 100644 index 0000000..99b1d00 --- /dev/null +++ b/bindings/libproxy-sharp-1.0.pc.in @@ -0,0 +1,8 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=${exec_prefix}/lib + +Name: Libproxy# +Description: Libproxy# - .NET Binding to libproxy +Version: @VERSION@ +Libs: -r:${libdir}/mono/@ASSEMBLY_NAME@/@ASSEMBLY_NAME@.dll diff --git a/bindings/libproxy.cs b/bindings/libproxy.cs new file mode 100644 index 0000000..b2ceeb2 --- /dev/null +++ b/bindings/libproxy.cs @@ -0,0 +1,79 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +namespace libproxy { + using System; + using System.Runtime.InteropServices; + + public class ProxyFactory { + private HandleRef self; + + [DllImport ("proxy")] + private static extern + IntPtr px_proxy_factory_new(); + + [DllImport ("proxy")] + private static extern + IntPtr px_proxy_factory_get_proxies(HandleRef self, string url); + + [DllImport ("proxy")] + private static extern + void px_proxy_factory_free(HandleRef self); + + public ProxyFactory() + { + this.self = new HandleRef(this, px_proxy_factory_new()); + } + + public string[] GetProxies(string url) + { + int count = 0; + + // Get the results + // TODO: If we call both this function and px_proxy_factory_free() + // this crashes, figure out why... + IntPtr array = px_proxy_factory_get_proxies(this.self, url); + + // Count the number of returned strings + while (Marshal.ReadIntPtr(array, count * IntPtr.Size) != IntPtr.Zero) count++; + + // Allocate a correctly sized array + string[] proxies = new string[count]; + + // Fill the response array + for (int i=0 ; i < count ; i++) + { + IntPtr p = Marshal.ReadIntPtr(array, i * IntPtr.Size); + proxies[i] = Marshal.PtrToStringAnsi(p); + Marshal.FreeCoTaskMem(p); // Free the string + } + + // Free the array itself + Marshal.FreeCoTaskMem(array); + + return proxies; + } + + ~ProxyFactory() + { + // TODO: See note above... + px_proxy_factory_free(this.self); + } + } +} diff --git a/bindings/libproxy.py b/bindings/libproxy.py new file mode 100644 index 0000000..5d9b43f --- /dev/null +++ b/bindings/libproxy.py @@ -0,0 +1,103 @@ +############################################################################### +# 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 +############################################################################### + +"A library for proxy configuration and autodetection." + +import ctypes +import ctypes.util + +import sys + +# Load libproxy +if not ctypes.util.find_library("proxy"): + raise ImportError, "Unable to import libproxy!?!?" +_libproxy = ctypes.cdll.LoadLibrary(ctypes.util.find_library("proxy")) +_libproxy.px_proxy_factory_get_proxies.restype = ctypes.POINTER(ctypes.c_void_p) + +class ProxyFactory(object): + """A ProxyFactory object is used to provide potential proxies to use + in order to reach a given URL (via 'getProxy(url)'). + + This instance should be kept around as long as possible as it contains + cached data to increase performance. Memory usage should be minimal (cache + is small) and the cache lifespan is handled automatically. + + Usage is pretty simple: + pf = libproxy.ProxyFactory() + for url in urls: + proxies = pf.getProxy(url) + for proxy in proxies: + if proxy == "direct://": + # Fetch URL without using a proxy + elif proxy.startswith("http://"): + # Fetch URL using an HTTP proxy + elif proxy.startswith("socks://"): + # Fetch URL using a SOCKS proxy + + if fetchSucceeded: + break + """ + + def __init__(self): + self._pf = _libproxy.px_proxy_factory_new() + + def getProxies(self, url): + """Given a URL, returns a list of proxies in priority order to be used + to reach that URL. + + A list of proxy strings is returned. If the first proxy fails, the + second should be tried, etc... In all cases, at least one entry in the + list will be returned. There are no error conditions. + + Regarding performance: this method always blocks and may be called + in a separate thread (is thread-safe). In most cases, the time + required to complete this function call is simply the time required + to read the configuration (e.g from GConf, Kconfig, etc). + + In the case of PAC, if no valid PAC is found in the cache (i.e. + configuration has changed, cache is invalid, etc), the PAC file is + downloaded and inserted into the cache. This is the most expensive + operation as the PAC is retrieved over the network. Once a PAC exists + in the cache, it is merely a JavaScript invocation to evaluate the PAC. + One should note that DNS can be called from within a PAC during + JavaScript invocation. + + In the case of WPAD, WPAD is used to automatically locate a PAC on the + network. Currently, we only use DNS for this, but other methods may + be implemented in the future. Once the PAC is located, normal PAC + performance (described above) applies. + + """ + if type(url) != str: + raise TypeError, "url must be a string!" + + proxies = [] + array = _libproxy.px_proxy_factory_get_proxies(self._pf, url) + i=0 + while array[i]: + proxies.append(str(ctypes.cast(array[i], ctypes.c_char_p).value)) + _libproxy.px_free(array[i]) + i += 1 + _libproxy.px_free(array) + + return proxies + + def __del__(self): + _libproxy.px_proxy_factory_free(self._pf) + diff --git a/bindings/libproxy.snk b/bindings/libproxy.snk Binary files differnew file mode 100644 index 0000000..3dc1731 --- /dev/null +++ b/bindings/libproxy.snk |