ยปCore Development>Code coverage>Modules/_scproxy.c

Python code coverage for Modules/_scproxy.c

#countcontent
1n/a/*
2n/a * Helper method for urllib to fetch the proxy configuration settings
3n/a * using the SystemConfiguration framework.
4n/a */
5n/a#include <Python.h>
6n/a#include <SystemConfiguration/SystemConfiguration.h>
7n/a
8n/astatic int32_t
9n/acfnum_to_int32(CFNumberRef num)
10n/a{
11n/a int32_t result;
12n/a
13n/a CFNumberGetValue(num, kCFNumberSInt32Type, &result);
14n/a return result;
15n/a}
16n/a
17n/astatic PyObject*
18n/acfstring_to_pystring(CFStringRef ref)
19n/a{
20n/a const char* s;
21n/a
22n/a s = CFStringGetCStringPtr(ref, kCFStringEncodingUTF8);
23n/a if (s) {
24n/a return PyUnicode_DecodeUTF8(
25n/a s, strlen(s), NULL);
26n/a
27n/a } else {
28n/a CFIndex len = CFStringGetLength(ref);
29n/a Boolean ok;
30n/a PyObject* result;
31n/a char* buf;
32n/a
33n/a buf = PyMem_Malloc(len*4);
34n/a if (buf == NULL) {
35n/a PyErr_NoMemory();
36n/a return NULL;
37n/a }
38n/a
39n/a ok = CFStringGetCString(ref,
40n/a buf, len * 4,
41n/a kCFStringEncodingUTF8);
42n/a if (!ok) {
43n/a PyMem_Free(buf);
44n/a return NULL;
45n/a } else {
46n/a result = PyUnicode_DecodeUTF8(
47n/a buf, strlen(buf), NULL);
48n/a PyMem_Free(buf);
49n/a }
50n/a return result;
51n/a }
52n/a}
53n/a
54n/a
55n/astatic PyObject*
56n/aget_proxy_settings(PyObject* mod __attribute__((__unused__)))
57n/a{
58n/a CFDictionaryRef proxyDict = NULL;
59n/a CFNumberRef aNum = NULL;
60n/a CFArrayRef anArray = NULL;
61n/a PyObject* result = NULL;
62n/a PyObject* v;
63n/a int r;
64n/a
65n/a proxyDict = SCDynamicStoreCopyProxies(NULL);
66n/a if (!proxyDict) {
67n/a Py_RETURN_NONE;
68n/a }
69n/a
70n/a result = PyDict_New();
71n/a if (result == NULL) goto error;
72n/a
73n/a aNum = CFDictionaryGetValue(proxyDict,
74n/a kSCPropNetProxiesExcludeSimpleHostnames);
75n/a if (aNum == NULL) {
76n/a v = PyBool_FromLong(0);
77n/a } else {
78n/a v = PyBool_FromLong(cfnum_to_int32(aNum));
79n/a }
80n/a
81n/a if (v == NULL) goto error;
82n/a
83n/a r = PyDict_SetItemString(result, "exclude_simple", v);
84n/a Py_DECREF(v); v = NULL;
85n/a if (r == -1) goto error;
86n/a
87n/a anArray = CFDictionaryGetValue(proxyDict,
88n/a kSCPropNetProxiesExceptionsList);
89n/a if (anArray != NULL) {
90n/a CFIndex len = CFArrayGetCount(anArray);
91n/a CFIndex i;
92n/a v = PyTuple_New(len);
93n/a if (v == NULL) goto error;
94n/a
95n/a r = PyDict_SetItemString(result, "exceptions", v);
96n/a Py_DECREF(v);
97n/a if (r == -1) goto error;
98n/a
99n/a for (i = 0; i < len; i++) {
100n/a CFStringRef aString = NULL;
101n/a
102n/a aString = CFArrayGetValueAtIndex(anArray, i);
103n/a if (aString == NULL) {
104n/a PyTuple_SetItem(v, i, Py_None);
105n/a Py_INCREF(Py_None);
106n/a } else {
107n/a PyObject* t = cfstring_to_pystring(aString);
108n/a if (!t) {
109n/a PyTuple_SetItem(v, i, Py_None);
110n/a Py_INCREF(Py_None);
111n/a } else {
112n/a PyTuple_SetItem(v, i, t);
113n/a }
114n/a }
115n/a }
116n/a }
117n/a
118n/a CFRelease(proxyDict);
119n/a return result;
120n/a
121n/aerror:
122n/a if (proxyDict) CFRelease(proxyDict);
123n/a Py_XDECREF(result);
124n/a return NULL;
125n/a}
126n/a
127n/astatic int
128n/aset_proxy(PyObject* proxies, const char* proto, CFDictionaryRef proxyDict,
129n/a CFStringRef enabledKey,
130n/a CFStringRef hostKey, CFStringRef portKey)
131n/a{
132n/a CFNumberRef aNum;
133n/a
134n/a aNum = CFDictionaryGetValue(proxyDict, enabledKey);
135n/a if (aNum && cfnum_to_int32(aNum)) {
136n/a CFStringRef hostString;
137n/a
138n/a hostString = CFDictionaryGetValue(proxyDict, hostKey);
139n/a aNum = CFDictionaryGetValue(proxyDict, portKey);
140n/a
141n/a if (hostString) {
142n/a int r;
143n/a PyObject* h = cfstring_to_pystring(hostString);
144n/a PyObject* v;
145n/a if (h) {
146n/a if (aNum) {
147n/a int32_t port = cfnum_to_int32(aNum);
148n/a v = PyUnicode_FromFormat("http://%U:%ld",
149n/a h, (long)port);
150n/a } else {
151n/a v = PyUnicode_FromFormat("http://%U", h);
152n/a }
153n/a Py_DECREF(h);
154n/a if (!v) return -1;
155n/a r = PyDict_SetItemString(proxies, proto,
156n/a v);
157n/a Py_DECREF(v);
158n/a return r;
159n/a }
160n/a }
161n/a
162n/a }
163n/a return 0;
164n/a}
165n/a
166n/a
167n/a
168n/astatic PyObject*
169n/aget_proxies(PyObject* mod __attribute__((__unused__)))
170n/a{
171n/a PyObject* result = NULL;
172n/a int r;
173n/a CFDictionaryRef proxyDict = NULL;
174n/a
175n/a proxyDict = SCDynamicStoreCopyProxies(NULL);
176n/a if (proxyDict == NULL) {
177n/a return PyDict_New();
178n/a }
179n/a
180n/a result = PyDict_New();
181n/a if (result == NULL) goto error;
182n/a
183n/a r = set_proxy(result, "http", proxyDict,
184n/a kSCPropNetProxiesHTTPEnable,
185n/a kSCPropNetProxiesHTTPProxy,
186n/a kSCPropNetProxiesHTTPPort);
187n/a if (r == -1) goto error;
188n/a r = set_proxy(result, "https", proxyDict,
189n/a kSCPropNetProxiesHTTPSEnable,
190n/a kSCPropNetProxiesHTTPSProxy,
191n/a kSCPropNetProxiesHTTPSPort);
192n/a if (r == -1) goto error;
193n/a r = set_proxy(result, "ftp", proxyDict,
194n/a kSCPropNetProxiesFTPEnable,
195n/a kSCPropNetProxiesFTPProxy,
196n/a kSCPropNetProxiesFTPPort);
197n/a if (r == -1) goto error;
198n/a r = set_proxy(result, "gopher", proxyDict,
199n/a kSCPropNetProxiesGopherEnable,
200n/a kSCPropNetProxiesGopherProxy,
201n/a kSCPropNetProxiesGopherPort);
202n/a if (r == -1) goto error;
203n/a
204n/a CFRelease(proxyDict);
205n/a return result;
206n/aerror:
207n/a if (proxyDict) CFRelease(proxyDict);
208n/a Py_XDECREF(result);
209n/a return NULL;
210n/a}
211n/a
212n/astatic PyMethodDef mod_methods[] = {
213n/a {
214n/a "_get_proxy_settings",
215n/a (PyCFunction)get_proxy_settings,
216n/a METH_NOARGS,
217n/a NULL,
218n/a },
219n/a {
220n/a "_get_proxies",
221n/a (PyCFunction)get_proxies,
222n/a METH_NOARGS,
223n/a NULL,
224n/a },
225n/a { 0, 0, 0, 0 }
226n/a};
227n/a
228n/a
229n/a
230n/astatic struct PyModuleDef mod_module = {
231n/a PyModuleDef_HEAD_INIT,
232n/a "_scproxy",
233n/a NULL,
234n/a -1,
235n/a mod_methods,
236n/a NULL,
237n/a NULL,
238n/a NULL,
239n/a NULL
240n/a};
241n/a
242n/a
243n/a#ifdef __cplusplus
244n/aextern "C" {
245n/a#endif
246n/a
247n/aPyMODINIT_FUNC
248n/aPyInit__scproxy(void)
249n/a{
250n/a return PyModule_Create(&mod_module);
251n/a}
252n/a
253n/a#ifdef __cplusplus
254n/a}
255n/a#endif
256n/a