Skip to content

Commit 9d47bee

Browse files
committed
Add BITS proxy setting
1 parent 14bd587 commit 9d47bee

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/_native/bits.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <Python.h>
22
#include <windows.h>
33
#include <bits.h>
4+
#include <winhttp.h>
45
#include <typeinfo>
56

67
#include "helpers.h"
@@ -204,6 +205,30 @@ PyObject *bits_serialize_job(PyObject *, PyObject *args, PyObject *kwargs) {
204205
}
205206

206207

208+
static HRESULT _job_setproxy(IBackgroundCopyJob *job) {
209+
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxy_config = { 0 };
210+
if (WinHttpGetIEProxyConfigForCurrentUser(&proxy_config)) {
211+
if (proxy_config.lpszProxy || proxy_config.lpszAutoConfigUrl || proxy_config.fAutoDetect) {
212+
// Global settings are present, and so BITS's default behaviour
213+
// should be fine.
214+
if (proxy_config.lpszProxy) {
215+
GlobalFree(proxy_config.lpszProxy);
216+
}
217+
if (proxy_config.lpszProxyBypass) {
218+
GlobalFree(proxy_config.lpszProxyBypass);
219+
}
220+
if (proxy_config.lpszAutoConfigUrl) {
221+
GlobalFree(proxy_config.lpszAutoConfigUrl);
222+
}
223+
return S_OK;
224+
}
225+
}
226+
227+
// Probably no static settings, so tell BITS to do autodetection.
228+
return job->SetProxySettings(BG_JOB_PROXY_USAGE_AUTODETECT, NULL, NULL);
229+
}
230+
231+
207232
static HRESULT _job_setcredentials(IBackgroundCopyJob *job, wchar_t *username, wchar_t *password) {
208233
IBackgroundCopyJob2 *job2 = NULL;
209234
HRESULT hr;
@@ -256,6 +281,10 @@ PyObject *bits_begin(PyObject *, PyObject *args, PyObject *kwargs) {
256281
error_from_bits_hr(bcm, hr, "Creating download job");
257282
goto done;
258283
}
284+
if (FAILED(hr = _job_setproxy(job))) {
285+
error_from_bits_hr(bcm, hr, "Setting proxy");
286+
goto done;
287+
}
259288
if ((username || password) && FAILED(hr = _job_setcredentials(job, username, password))) {
260289
error_from_bits_hr(bcm, hr, "Adding basic credentials to download job");
261290
goto done;

0 commit comments

Comments
 (0)