FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pymanager/src/_native/bits.cpp at main · python/pymanager · GitHub
python
pymanager
Repository navigation
Code
Issues
23
(23)
Pull requests
6
(6)
Actions
Security and quality
1
(1)
Insights
Expand file tree
Breadcrumbs
pymanager
/
src
/
_native
/
bits.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
464 lines (404 loc) · 13.7 KB
Breadcrumbs
pymanager
/
src
/
_native
/
bits.cpp
Copy path
File metadata and controls
464 lines (404 loc) · 13.7 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#
include
<
Python.h
>
#
include
<
windows.h
>
#
include
<
bits.h
>
#
include
<
winhttp.h
>
#
include
<
typeinfo
>
#
include
"
helpers.h
"
#
include
"
proxy.h
"
#
ifdef
BITS_INJECT_ERROR
static
HRESULT
_inject_hr[]
#
else
const
HRESULT
_inject_hr[]
#
endif
= {
S_OK
,
S_OK
,
S_OK
,
S_OK
};
static
PyObject *
error_from_bits_hr
(IBackgroundCopyManager *bcm,
HRESULT
hr,
const
char
*operation)
{
LPWSTR
err;
HRESULT
hr2;
if
(
FAILED
(hr2 = _inject_hr[
2
])
||
FAILED
(hr2 = bcm->
GetErrorDescription
(hr,
LANGIDFROMLCID
(
GetThreadLocale
()), &err))) {
err_SetFromWindowsErrWithMessage
(hr, operation);
err_SetFromWindowsErrWithMessage
(hr2,
"
Retrieving error message
"
);
return
NULL
;
}
size_t
n =
wcslen
(err);
while
(n >
0
&&
isspace
(err[--n])) {
err[n] = L
'
\0
'
;
}
err_SetFromWindowsErrWithMessage
(hr, operation, err);
CoTaskMemFree
(err);
return
NULL
;
}
static
PyObject *
error_from_bits_job
(IBackgroundCopyJob *job)
{
IBackgroundCopyError *error =
NULL
;
BG_ERROR_CONTEXT
context;
HRESULT
hr, hr_error =
S_FALSE
;
LPWSTR
str_error;
PyObject *exc, *val, *tb;
if
(
FAILED
(hr = _inject_hr[
1
])
||
FAILED
(hr = job->
GetError
(&error))
||
FAILED
(hr = error->
GetError
(&context, &hr_error))
) {
if
(error) {
error->
Release
();
}
PyErr_SetString
(PyExc_OSError,
"
Unidentified download error
"
);
err_SetFromWindowsErrWithMessage
(hr,
"
Retrieving download error
"
);
return
NULL
;
}
if
(
FAILED
(hr = _inject_hr[
2
])
||
FAILED
(hr = error->
GetErrorDescription
(
LANGIDFROMLCID
(
GetThreadLocale
()), &str_error))
) {
error->
Release
();
//
PyErr_SetFromWindowsErr(hr_error);
//
err_SetFromWindowsErrWithMessage(hr, "Retrieving error message");
err_SetFromWindowsErrWithMessage
(hr_error,
"
Could not retrieve message
"
);
return
NULL
;
}
size_t
n =
wcslen
(str_error);
while
(n >
0
&&
isspace
(str_error[--n])) {
str_error[n] = L
'
\0
'
;
}
err_SetFromWindowsErrWithMessage
(hr_error,
"
Download error
"
, str_error);
CoTaskMemFree
(str_error);
error->
Release
();
return
NULL
;
}
static
HRESULT
get_job_progress
(IBackgroundCopyJob *job,
int
*progress,
int
*already_complete) {
HRESULT
hr;
BG_JOB_STATE
job_state;
BG_JOB_PROGRESS
job_progress;
if
(
FAILED
(hr = _inject_hr[
0
]) ||
FAILED
(hr = job->
GetState
(&job_state))) {
return
hr;
}
*already_complete =
0
;
switch
(job_state) {
case
BG_JOB_STATE_QUEUED
:
case
BG_JOB_STATE_CONNECTING
:
case
BG_JOB_STATE_CANCELLED
:
*progress =
0
;
break
;
case
BG_JOB_STATE_TRANSFERRED
:
*progress =
100
;
break
;
case
BG_JOB_STATE_ACKNOWLEDGED
:
*progress =
100
;
*already_complete =
1
;
break
;
case
BG_JOB_STATE_TRANSFERRING
:
case
BG_JOB_STATE_SUSPENDED
:
if
(
FAILED
(hr = job->
GetProgress
(&job_progress))) {
return
hr;
}
//
probably an unnecessary sanity check
if
(job_progress.
FilesTransferred
>= job_progress.
FilesTotal
|| job_progress.
BytesTransferred
>= job_progress.
BytesTotal
) {
*progress =
100
;
}
else
if
(job_progress.
BytesTotal
==
BG_SIZE_UNKNOWN
) {
*progress = (job_progress.
FilesTransferred
*
100
) / job_progress.
FilesTotal
;
}
else
{
*progress = (job_progress.
BytesTransferred
*
100
) / job_progress.
BytesTotal
;
}
break
;
case
BG_JOB_STATE_TRANSIENT_ERROR
:
case
BG_JOB_STATE_ERROR
:
return
S_FALSE
;
}
if
(*progress <
0
) {
*progress =
0
;
}
else
if
(*progress >
100
) {
*progress =
100
;
}
return
S_OK
;
}
extern
"
C
"
{
//
Returns a capsule containing the BackgroundCopyManager instance
PyObject *
bits_connect
(PyObject *, PyObject *args, PyObject *kwargs) {
IBackgroundCopyManager *bcm =
NULL
;
HRESULT
hr =
CoCreateInstance
(
__uuidof
(BackgroundCopyManager),
NULL
,
CLSCTX_LOCAL_SERVER
,
__uuidof
(IBackgroundCopyManager),
(
void
**)&bcm
);
if
(
FAILED
(hr)) {
PyErr_SetFromWindowsErr
(hr);
return
NULL
;
}
return
make_capsule
(bcm);
}
//
(conn, job_id) -> job
PyObject *
bits_find_job
(PyObject *, PyObject *args, PyObject *kwargs) {
static
const
char
* keywords[] = {
"
conn
"
,
"
job_id
"
,
NULL
};
IBackgroundCopyManager *bcm =
NULL
;
Py_buffer job_id;
if
(!
PyArg_ParseTupleAndKeywords
(args, kwargs,
"
O&y*:bits_find_job
"
, keywords,
from_capsule<IBackgroundCopyManager>, &bcm, &job_id
)) {
return
NULL
;
}
if
(job_id.
len
<
sizeof
(
GUID
)) {
PyBuffer_Release
(&job_id);
PyErr_SetString
(PyExc_ValueError,
"
'job_id' must be a serialized job ID
"
);
return
NULL
;
}
PyObject *r =
NULL
;
IBackgroundCopyJob *job =
NULL
;
GUID
job_guid = *(
GUID
*)job_id.
buf
;
HRESULT
hr;
if
(
FAILED
(hr = _inject_hr[
0
]) ||
FAILED
(hr = bcm->
GetJob
(job_guid, &job))) {
error_from_bits_hr
(bcm, hr,
"
Getting background download
"
);
}
else
{
r =
make_capsule
(job);
}
PyBuffer_Release
(&job_id);
return
r;
}
//
(conn, job) -> job_id
PyObject *
bits_serialize_job
(PyObject *, PyObject *args, PyObject *kwargs) {
static
const
char
* keywords[] = {
"
conn
"
,
"
job
"
,
NULL
};
IBackgroundCopyManager *bcm =
NULL
;
IBackgroundCopyJob* job =
NULL
;
if
(!
PyArg_ParseTupleAndKeywords
(args, kwargs,
"
O&O&:bits_serialize_job
"
, keywords,
from_capsule<IBackgroundCopyManager>, &bcm, from_capsule<IBackgroundCopyJob>, &job)) {
return
NULL
;
}
PyObject *r =
NULL
;
GUID
job_id;
HRESULT
hr;
if
(
FAILED
(hr = _inject_hr[
0
]) ||
FAILED
(hr = job->
GetId
(&job_id))) {
error_from_bits_hr
(bcm, hr,
"
Getting download job ID
"
);
}
else
{
r =
PyBytes_FromStringAndSize
((
char
*)&job_id,
sizeof
(job_id));
}
return
r;
}
static
HRESULT
_job_setproxy
(IBackgroundCopyJob *job,
const
ProxySettings *settings) {
if
(settings->
mode
==
PROXY_MODE_DIRECT
) {
return
job->
SetProxySettings
(
BG_JOB_PROXY_USAGE_NO_PROXY
,
NULL
,
NULL
);
}
if
(settings->
mode
==
PROXY_MODE_OVERRIDE
) {
return
job->
SetProxySettings
(
BG_JOB_PROXY_USAGE_OVERRIDE
,
settings->
proxy_list
,
NULL
);
}
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
proxy_config = {
0
};
if
(
WinHttpGetIEProxyConfigForCurrentUser
(&proxy_config)) {
if
(proxy_config.
lpszProxy
|| proxy_config.
lpszAutoConfigUrl
|| proxy_config.
fAutoDetect
) {
//
Global settings are present, and so BITS's default behaviour
//
should be fine.
if
(proxy_config.
lpszProxy
) {
GlobalFree
(proxy_config.
lpszProxy
);
}
if
(proxy_config.
lpszProxyBypass
) {
GlobalFree
(proxy_config.
lpszProxyBypass
);
}
if
(proxy_config.
lpszAutoConfigUrl
) {
GlobalFree
(proxy_config.
lpszAutoConfigUrl
);
}
return
S_OK
;
}
}
//
Probably no static settings, so tell BITS to do autodetection.
return
job->
SetProxySettings
(
BG_JOB_PROXY_USAGE_AUTODETECT
,
NULL
,
NULL
);
}
static
HRESULT
_job_setcredentials
(
IBackgroundCopyJob *job,
BG_AUTH_TARGET
target,
wchar_t
*username,
wchar_t
*password
) {
IBackgroundCopyJob2 *job2 =
NULL
;
HRESULT
hr;
BG_AUTH_CREDENTIALS
creds = {
.
Target
= target,
.
Scheme
= username ?
BG_AUTH_SCHEME_BASIC
:
BG_AUTH_SCHEME_NEGOTIATE
,
.
Credentials
= {
.
Basic
= {
.
UserName
= username,
.
Password
= username ? password :
NULL
}
}
};
if
(
FAILED
(hr = _inject_hr[
3
])
||
FAILED
(hr = job->
QueryInterface
(
__uuidof
(IBackgroundCopyJob2), (
void
**)&job2))) {
return
hr;
}
hr = job2->
SetCredentials
(&creds);
job2->
Release
();
return
hr;
}
//
(conn, name, url, path, [username], [password], [proxy_settings]) -> job
PyObject *
bits_begin
(PyObject *, PyObject *args, PyObject *kwargs) {
static
const
char
* keywords[] = {
"
conn
"
,
"
name
"
,
"
url
"
,
"
path
"
,
"
username
"
,
"
password
"
,
"
proxy_settings
"
,
NULL
};
IBackgroundCopyManager *bcm =
NULL
;
wchar_t
*name =
NULL
;
wchar_t
*url =
NULL
;
wchar_t
*path =
NULL
;
wchar_t
*username =
NULL
;
wchar_t
*password =
NULL
;
PyObject *proxy_settings_obj = Py_None;
if
(!
PyArg_ParseTupleAndKeywords
(args, kwargs,
"
O&O&O&O&|O&O&O:bits_begin
"
, keywords,
from_capsule<IBackgroundCopyManager>, &bcm, as_utf16, &name, as_utf16, &url, as_utf16, &path,
as_utf16, &username, as_utf16, &password, &proxy_settings_obj
)) {
return
NULL
;
}
PyObject *r =
NULL
;
GUID
jobId;
IBackgroundCopyJob* job =
NULL
;
ProxySettings proxy_settings = {};
HRESULT
hr;
if
(!
proxy_settings_parse
(proxy_settings_obj, &proxy_settings)) {
goto
done;
}
if
(
FAILED
(hr = _inject_hr[
0
])
||
FAILED
(hr = bcm->
CreateJob
(name,
BG_JOB_TYPE_DOWNLOAD
, &jobId, &job))) {
error_from_bits_hr
(bcm, hr,
"
Creating download job
"
);
goto
done;
}
if
(
FAILED
(hr =
_job_setproxy
(job, &proxy_settings))) {
error_from_bits_hr
(bcm, hr,
"
Setting proxy
"
);
goto
done;
}
//
Explicit proxy credentials come from the configured proxy URL. NULL
//
automatically infers credentials if needed, which remains the default.
if
(
FAILED
(hr =
_job_setcredentials
(
job,
BG_AUTH_TARGET_PROXY
,
proxy_settings.
username
,
proxy_settings.
password
))) {
error_from_bits_hr
(bcm, hr,
"
Setting proxy credentials
"
);
goto
done;
}
if
(
FAILED
(hr =
_job_setcredentials
(job,
BG_AUTH_TARGET_SERVER
, username, password))) {
error_from_bits_hr
(bcm, hr,
"
Adding basic credentials to download job
"
);
goto
done;
}
if
(
FAILED
(hr = job->
AddFile
(url, path))) {
error_from_bits_hr
(bcm, hr,
"
Adding file to download job
"
);
goto
done;
}
if
(
FAILED
(hr = job->
SetPriority
(
BG_JOB_PRIORITY_FOREGROUND
))) {
error_from_bits_hr
(bcm, hr,
"
Setting download job priority
"
);
goto
done;
}
if
(
FAILED
(hr = job->
Resume
())) {
error_from_bits_hr
(bcm, hr,
"
Starting download job
"
);
goto
done;
}
job->
AddRef
();
r =
make_capsule
(job);
done:
if
(job) {
job->
Release
();
}
proxy_settings_clear
(&proxy_settings);
PyMem_Free
(path);
PyMem_Free
(url);
PyMem_Free
(name);
PyMem_Free
(username);
PyMem_Free
(password);
return
r;
}
//
(job)
PyObject *
bits_cancel
(PyObject *, PyObject *args, PyObject *kwargs) {
static
const
char
* keywords[] = {
"
conn
"
,
"
job
"
,
NULL
};
IBackgroundCopyManager *bcm =
NULL
;
IBackgroundCopyJob* job =
NULL
;
if
(!
PyArg_ParseTupleAndKeywords
(args, kwargs,
"
O&O&:bits_cancel
"
, keywords,
from_capsule<IBackgroundCopyManager>, &bcm, from_capsule<IBackgroundCopyJob>, &job)) {
return
NULL
;
}
HRESULT
hr;
if
(
FAILED
(hr = _inject_hr[
0
]) ||
FAILED
(hr = job->
Cancel
())) {
return
error_from_bits_hr
(bcm, hr,
"
Cancelling download job
"
);
}
Py_RETURN_NONE;
}
//
(job) -> int[0..100] or exception
PyObject *
bits_get_progress
(PyObject *, PyObject *args, PyObject *kwargs) {
static
const
char
* keywords[] = {
"
conn
"
,
"
job
"
,
NULL
};
IBackgroundCopyManager *bcm =
NULL
;
IBackgroundCopyJob* job =
NULL
;
if
(!
PyArg_ParseTupleAndKeywords
(args, kwargs,
"
O&O&:bits_get_progress
"
, keywords,
from_capsule<IBackgroundCopyManager>, &bcm, from_capsule<IBackgroundCopyJob>, &job)) {
return
NULL
;
}
PyObject *r =
NULL
;
int
progress =
0
, already_complete =
0
;
HRESULT
hr = _inject_hr[
0
];
if
(!
FAILED
(hr) && hr !=
S_FALSE
) {
hr =
get_job_progress
(job, &progress, &already_complete);
}
if
(hr ==
S_FALSE
) {
return
error_from_bits_job
(job);
}
else
if
(
FAILED
(hr)) {
return
error_from_bits_hr
(bcm, hr,
"
Getting download progress
"
);
}
if
(progress ==
100
&& !already_complete) {
hr = job->
Complete
();
if
(
FAILED
(hr)) {
return
error_from_bits_hr
(bcm, hr,
"
Completing download job
"
);
}
}
return
Py_BuildValue
(
"
i
"
, progress);
}
//
(conn, job, username, password) -> job
PyObject *
bits_retry_with_auth
(PyObject *, PyObject *args, PyObject *kwargs) {
static
const
char
* keywords[] = {
"
conn
"
,
"
job
"
,
"
username
"
,
"
password
"
,
NULL
};
IBackgroundCopyManager *bcm =
NULL
;
IBackgroundCopyJob* job =
NULL
;
wchar_t
*username =
NULL
;
wchar_t
*password =
NULL
;
if
(!
PyArg_ParseTupleAndKeywords
(args, kwargs,
"
O&O&O&O&:bits_retry_with_auth
"
, keywords,
from_capsule<IBackgroundCopyManager>, &bcm, from_capsule<IBackgroundCopyJob>, &job,
as_utf16, &username, as_utf16, &password
)) {
return
NULL
;
}
HRESULT
hr;
PyObject *r =
NULL
;
if
(
FAILED
(hr =
_job_setcredentials
(job,
BG_AUTH_TARGET_SERVER
, username, password))) {
error_from_bits_hr
(bcm, hr,
"
Adding basic credentials to download job
"
);
goto
done;
}
if
(
FAILED
(hr = job->
Resume
())) {
error_from_bits_hr
(bcm, hr,
"
Starting download job
"
);
goto
done;
}
r =
Py_GetConstant
(Py_CONSTANT_NONE);
done:
PyMem_Free
(username);
PyMem_Free
(password);
return
r;
}
#
ifdef
BITS_INJECT_ERROR
PyObject *
bits_inject_error
(PyObject *, PyObject *args, PyObject *kwargs) {
HRESULT
hr;
if
(!
PyArg_ParseTuple
(
args,
"
IIII:bits_inject_error
"
,
//
replace HRESULT for primary operation
&_inject_hr[
0
],
//
replace HRESULT for getting error code
&_inject_hr[
1
],
//
replace HRESULT for getting error text
&_inject_hr[
2
],
//
replace HRESULT for adding credentials to job
&_inject_hr[
3
]
)) {
return
NULL
;
}
Py_RETURN_NONE;
}
#
endif
}
Back
|
FazBrowse Home
|
New Git URL