Debian Codegraph @

lib_pypy/_scproxy.py

raw symbols SCIP
1 """Helper methods for urllib to fetch the proxy configuration settings using
2 the SystemConfiguration framework.
3
4 """
5 import sys
6 if sys.platform != 'darwin':
7 raise ModuleNotFoundError('Requires Mac OS X', name='_scproxy')
8
9 from ctypes import c_int32, c_int64, c_void_p, c_char_p, c_int, cdll
10 from ctypes import pointer, create_string_buffer
11 from ctypes.util import find_library
12
13 kCFNumberSInt32Type = 3
14 kCFStringEncodingUTF8 = 134217984
15
16 def _CFSetup():
17 sc = cdll.LoadLibrary(find_library("SystemConfiguration"))
18 cf = cdll.LoadLibrary(find_library("CoreFoundation"))
19 sctable = [
20 ('SCDynamicStoreCopyProxies', [c_void_p], c_void_p),
21 ]
22 cftable = [
23 ('CFArrayGetCount', [c_void_p], c_int64),
24 ('CFArrayGetValueAtIndex', [c_void_p, c_int64], c_void_p),
25 ('CFDictionaryGetValue', [c_void_p, c_void_p], c_void_p),
26 ('CFStringCreateWithCString', [c_void_p, c_char_p, c_int32], c_void_p),
27 ('CFStringGetLength', [c_void_p], c_int32),
28 ('CFStringGetCString', [c_void_p, c_char_p, c_int32, c_int32], c_int32),
29 ('CFNumberGetValue', [c_void_p, c_int, c_void_p], c_int32),
30 ('CFRelease', [c_void_p], None),
31 ]
32 scconst = [
33 'kSCPropNetProxiesExceptionsList',
34 'kSCPropNetProxiesExcludeSimpleHostnames',
35 'kSCPropNetProxiesHTTPEnable',
36 'kSCPropNetProxiesHTTPProxy',
37 'kSCPropNetProxiesHTTPPort',
38 'kSCPropNetProxiesHTTPSEnable',
39 'kSCPropNetProxiesHTTPSProxy',
40 'kSCPropNetProxiesHTTPSPort',
41 'kSCPropNetProxiesFTPEnable',
42 'kSCPropNetProxiesFTPProxy',
43 'kSCPropNetProxiesFTPPort',
44 'kSCPropNetProxiesGopherEnable',
45 'kSCPropNetProxiesGopherProxy',
46 'kSCPropNetProxiesGopherPort',
47 ]
48 class CFProxy(object):
49 def __init__(self):
50 for mod, table in [(sc, sctable), (cf, cftable)]:
51 for fname, argtypes, restype in table:
52 func = getattr(mod, fname)
53 func.argtypes = argtypes
54 func.restype = restype
55 setattr(self, fname, func)
56 for k in scconst:
57 v = None
58 try:
59 v = c_void_p.in_dll(sc, k)
60 except ValueError:
61 v = None
62 setattr(self, k, v)
63 return CFProxy()
64 ffi = _CFSetup()
65
66 def cfstring_to_pystring(value):
67 length = (ffi.CFStringGetLength(value) * 4) + 1
68 buff = create_string_buffer(length)
69 ffi.CFStringGetCString(value, buff, length * 4, kCFStringEncodingUTF8)
70 return str(buff.value, 'utf8')
71
72 def cfnum_to_int32(num):
73 result_ptr = pointer(c_int32(0))
74 ffi.CFNumberGetValue(num, kCFNumberSInt32Type, result_ptr)
75 return result_ptr[0]
76
77 def _get_proxy_settings():
78 result = {'exclude_simple': False}
79 cfdct = ffi.SCDynamicStoreCopyProxies(None)
80 if not cfdct:
81 return result
82 try:
83 k = ffi.kSCPropNetProxiesExcludeSimpleHostnames
84 if k:
85 cfnum = ffi.CFDictionaryGetValue(cfdct, k)
86 if cfnum:
87 result['exclude_simple'] = bool(cfnum_to_int32(cfnum))
88 k = ffi.kSCPropNetProxiesExceptionsList
89 if k:
90 cfarr = ffi.CFDictionaryGetValue(cfdct, k)
91 if cfarr:
92 lst = []
93 for i in range(ffi.CFArrayGetCount(cfarr)):
94 cfstr = ffi.CFArrayGetValueAtIndex(cfarr, i)
95 if cfstr:
96 v = cfstring_to_pystring(cfstr)
97 else:
98 v = None
99 lst.append(v)
100 result['exceptions'] = lst
101 return result
102 finally:
103 ffi.CFRelease(cfdct)
104
105 def _get_proxies():
106 result = {}
107 cfdct = ffi.SCDynamicStoreCopyProxies(None)
108 if not cfdct:
109 return result
110 try:
111 for proto in 'HTTP', 'HTTPS', 'FTP', 'Gopher':
112 enabled_key = getattr(ffi, 'kSCPropNetProxies' + proto + 'Enable')
113 proxy_key = getattr(ffi, 'kSCPropNetProxies' + proto + 'Proxy')
114 port_key = getattr(ffi, 'kSCPropNetProxies' + proto + 'Port')
115 cfnum = ffi.CFDictionaryGetValue(cfdct, enabled_key)
116 if cfnum and cfnum_to_int32(cfnum):
117 cfhoststr = ffi.CFDictionaryGetValue(cfdct, proxy_key)
118 cfportnum = ffi.CFDictionaryGetValue(cfdct, port_key)
119 if cfhoststr:
120 host = cfstring_to_pystring(cfhoststr)
121 if host:
122 if cfportnum:
123 port = cfnum_to_int32(cfportnum)
124 v = 'http://%s:%d' % (host, port)
125 else:
126 v = 'http://%s' % (host,)
127 result[proto.lower()] = v
128 return result
129 finally:
130 ffi.CFRelease(cfdct)