Skip to content

Commit

Permalink
feat(proxy): allow text/plain PAC file
Browse files Browse the repository at this point in the history
  • Loading branch information
jmkerloch committed Oct 2, 2024
1 parent 508ab54 commit daba8b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
35 changes: 23 additions & 12 deletions qgis_deployment_toolbelt/utils/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,31 @@ def get_proxy_settings(url: str | None = None) -> dict:
)
elif qdt_pac_file := environ.get("QDT_PAC_FILE"):
if qdt_pac_file.startswith(("http",)):
pac = get_pac(qdt_pac_file)
pac = get_pac(
qdt_pac_file,
allowed_content_types=[
"text/plain",
"application/x-ns-proxy-autoconfig",
"application/x-javascript-config",
],
)
else:
with open(qdt_pac_file) as f:
with open(qdt_pac_file, encoding="UTF-8") as f:
pac = PACFile(f.read)

with pac_context_for_url(url=url, pac=pac):
if environ.get("HTTP_PROXY"):
proxy_settings["http"] = environ.get("HTTP_PROXY")
if environ.get("HTTPS_PROXY"):
proxy_settings["https"] = environ.get("HTTPS_PROXY")
logger.info(
f"Proxies settings from environment vars PAC file: {environ.get('QDT_PAC_FILE')}"
f"{proxy_settings}"
)
if pac:
with pac_context_for_url(url=url, pac=pac):
if environ.get("HTTP_PROXY"):
proxy_settings["http"] = environ.get("HTTP_PROXY")
if environ.get("HTTPS_PROXY"):
proxy_settings["https"] = environ.get("HTTPS_PROXY")
logger.info(
f"Proxies settings from environment vars PAC file: {environ.get('QDT_PAC_FILE')}"
f"{proxy_settings}"
)
else:
logger.warning(
f"Invalid PAC file from environment vars PAC file : {environ.get('QDT_PAC_FILE')}. No proxy use."
)
elif getproxies():
proxy_settings = getproxies()
logger.debug(f"Proxies settings found in the OS: {proxy_settings}")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_pac_file(self):

## url
environ["QDT_PAC_FILE"] = (
"http://ieadmin.grandlyon.fr/administration/config/proxy.pac"
"https://raw.githubusercontent.com/Guts/qgis-deployment-cli/refs/heads/feat/pypac_auto_proxy/tests/fixtures/pac/proxy.pac"
)

### QGIS plugin : use proxy
Expand Down

0 comments on commit daba8b6

Please sign in to comment.