FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Modules/_scproxy.c at main · python/cpython · GitHub
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.7k
(2.7k)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Modules
/
_scproxy.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
258 lines (218 loc) · 6.31 KB
Breadcrumbs
cpython
/
Modules
/
_scproxy.c
Copy path
File metadata and controls
258 lines (218 loc) · 6.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Helper method for urllib to fetch the proxy configuration settings
* using the SystemConfiguration framework.
*/
// Need limited C API version 3.13 for Py_mod_gil
#include
"pyconfig.h"
// Py_GIL_DISABLED
#ifndef
Py_GIL_DISABLED
# define
Py_LIMITED_API
0x030d0000
#endif
#include
<Python.h>
#include
<SystemConfiguration/SystemConfiguration.h>
static
int32_t
cfnum_to_int32
(
CFNumberRef
num
)
{
int32_t
result
;
CFNumberGetValue
(
num
,
kCFNumberSInt32Type
,
&
result
);
return
result
;
}
static
PyObject
*
cfstring_to_pystring
(
CFStringRef
ref
)
{
const
char
*
s
;
s
=
CFStringGetCStringPtr
(
ref
,
kCFStringEncodingUTF8
);
if
(
s
) {
return
PyUnicode_DecodeUTF8
(
s
,
strlen
(
s
),
NULL
);
}
else
{
CFIndex
len
=
CFStringGetLength
(
ref
);
Boolean
ok
;
PyObject
*
result
;
char
*
buf
;
buf
=
PyMem_Malloc
(
len
*
4
);
if
(
buf
==
NULL
) {
PyErr_NoMemory
();
return
NULL
;
}
ok
=
CFStringGetCString
(
ref
,
buf
,
len
*
4
,
kCFStringEncodingUTF8
);
if
(!
ok
) {
PyMem_Free
(
buf
);
return
NULL
;
}
else
{
result
=
PyUnicode_DecodeUTF8
(
buf
,
strlen
(
buf
),
NULL
);
PyMem_Free
(
buf
);
}
return
result
;
}
}
static
PyObject
*
get_proxy_settings
(
PyObject
*
Py_UNUSED
(
mod
),
PyObject
*
Py_UNUSED
(
ignored
))
{
CFDictionaryRef
proxyDict
=
NULL
;
CFNumberRef
aNum
=
NULL
;
CFArrayRef
anArray
=
NULL
;
PyObject
*
result
=
NULL
;
PyObject
*
v
;
int
r
;
Py_BEGIN_ALLOW_THREADS
proxyDict
=
SCDynamicStoreCopyProxies
(
NULL
);
Py_END_ALLOW_THREADS
if
(!
proxyDict
) {
Py_RETURN_NONE
;
}
result
=
PyDict_New
();
if
(
result
==
NULL
) goto
error
;
aNum
=
CFDictionaryGetValue
(
proxyDict
,
kSCPropNetProxiesExcludeSimpleHostnames
);
if
(
aNum
==
NULL
) {
v
=
PyBool_FromLong
(
0
);
}
else
{
v
=
PyBool_FromLong
(
cfnum_to_int32
(
aNum
));
}
if
(
v
==
NULL
) goto
error
;
r
=
PyDict_SetItemString
(
result
,
"exclude_simple"
,
v
);
Py_CLEAR
(
v
);
if
(
r
==
-1
) goto
error
;
anArray
=
CFDictionaryGetValue
(
proxyDict
,
kSCPropNetProxiesExceptionsList
);
if
(
anArray
!=
NULL
) {
CFIndex
len
=
CFArrayGetCount
(
anArray
);
CFIndex
i
;
v
=
PyTuple_New
(
len
);
if
(
v
==
NULL
) goto
error
;
r
=
PyDict_SetItemString
(
result
,
"exceptions"
,
v
);
Py_DECREF
(
v
);
if
(
r
==
-1
) goto
error
;
for
(
i
=
0
;
i
<
len
;
i
++
) {
CFStringRef
aString
=
NULL
;
aString
=
CFArrayGetValueAtIndex
(
anArray
,
i
);
if
(
aString
==
NULL
) {
PyTuple_SetItem
(
v
,
i
,
Py_NewRef
(
Py_None
));
}
else
{
PyObject
*
t
=
cfstring_to_pystring
(
aString
);
if
(!
t
) {
PyTuple_SetItem
(
v
,
i
,
Py_NewRef
(
Py_None
));
}
else
{
PyTuple_SetItem
(
v
,
i
,
t
);
}
}
}
}
CFRelease
(
proxyDict
);
return
result
;
error
:
if
(
proxyDict
)
CFRelease
(
proxyDict
);
Py_XDECREF
(
result
);
return
NULL
;
}
static
int
set_proxy
(
PyObject
*
proxies
,
const
char
*
proto
,
CFDictionaryRef
proxyDict
,
CFStringRef
enabledKey
,
CFStringRef
hostKey
,
CFStringRef
portKey
)
{
CFNumberRef
aNum
;
aNum
=
CFDictionaryGetValue
(
proxyDict
,
enabledKey
);
if
(
aNum
&&
cfnum_to_int32
(
aNum
)) {
CFStringRef
hostString
;
hostString
=
CFDictionaryGetValue
(
proxyDict
,
hostKey
);
aNum
=
CFDictionaryGetValue
(
proxyDict
,
portKey
);
if
(
hostString
) {
int
r
;
PyObject
*
h
=
cfstring_to_pystring
(
hostString
);
PyObject
*
v
;
if
(
h
) {
if
(
aNum
) {
int32_t
port
=
cfnum_to_int32
(
aNum
);
v
=
PyUnicode_FromFormat
(
"http://%U:%ld"
,
h
, (
long
)
port
);
}
else
{
v
=
PyUnicode_FromFormat
(
"http://%U"
,
h
);
}
Py_DECREF
(
h
);
if
(!
v
)
return
-1
;
r
=
PyDict_SetItemString
(
proxies
,
proto
,
v
);
Py_DECREF
(
v
);
return
r
;
}
}
}
return
0
;
}
static
PyObject
*
get_proxies
(
PyObject
*
Py_UNUSED
(
mod
),
PyObject
*
Py_UNUSED
(
ignored
))
{
PyObject
*
result
=
NULL
;
int
r
;
CFDictionaryRef
proxyDict
=
NULL
;
Py_BEGIN_ALLOW_THREADS
proxyDict
=
SCDynamicStoreCopyProxies
(
NULL
);
Py_END_ALLOW_THREADS
if
(
proxyDict
==
NULL) {
return
PyDict_New
();
}
result
=
PyDict_New
();
if
(
result
==
NULL
) goto
error
;
r
=
set_proxy
(
result
,
"http"
,
proxyDict
,
kSCPropNetProxiesHTTPEnable
,
kSCPropNetProxiesHTTPProxy
,
kSCPropNetProxiesHTTPPort
);
if
(
r
==
-1
) goto
error
;
r
=
set_proxy
(
result
,
"https"
,
proxyDict
,
kSCPropNetProxiesHTTPSEnable
,
kSCPropNetProxiesHTTPSProxy
,
kSCPropNetProxiesHTTPSPort
);
if
(
r
==
-1
) goto
error
;
r
=
set_proxy
(
result
,
"ftp"
,
proxyDict
,
kSCPropNetProxiesFTPEnable
,
kSCPropNetProxiesFTPProxy
,
kSCPropNetProxiesFTPPort
);
if
(
r
==
-1
) goto
error
;
r
=
set_proxy
(
result
,
"gopher"
,
proxyDict
,
kSCPropNetProxiesGopherEnable
,
kSCPropNetProxiesGopherProxy
,
kSCPropNetProxiesGopherPort
);
if
(
r
==
-1
) goto
error
;
r
=
set_proxy
(
result
,
"socks"
,
proxyDict
,
kSCPropNetProxiesSOCKSEnable
,
kSCPropNetProxiesSOCKSProxy
,
kSCPropNetProxiesSOCKSPort
);
if
(
r
==
-1
) goto
error
;
CFRelease
(
proxyDict
);
return
result
;
error
:
if
(
proxyDict
)
CFRelease
(
proxyDict
);
Py_XDECREF
(
result
);
return
NULL
;
}
static
PyMethodDef
mod_methods
[]
=
{
{
"_get_proxy_settings"
,
get_proxy_settings
,
METH_NOARGS
,
NULL
,
},
{
"_get_proxies"
,
get_proxies
,
METH_NOARGS
,
NULL
,
},
{
0
,
0
,
0
,
0
}
};
static
PyModuleDef_Slot
_scproxy_slots
[]
=
{
{
Py_mod_multiple_interpreters
,
Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
},
{
Py_mod_gil
,
Py_MOD_GIL_NOT_USED
},
{
0
,
NULL
}
};
static
struct
PyModuleDef
_scproxy_module
=
{
PyModuleDef_HEAD_INIT
,
.
m_name
=
"_scproxy"
,
.
m_size
=
0
,
.
m_methods
=
mod_methods
,
.
m_slots
=
_scproxy_slots
,
};
PyMODINIT_FUNC
PyInit__scproxy
(
void
)
{
return
PyModuleDef_Init
(
&
_scproxy_module
);
}
Back
|
FazBrowse Home
|
New Git URL