Skip to content

Commit 3198097

Browse files
committed
add http(|s)_proxy env support to config tool
1 parent 1368562 commit 3198097

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

s3cmd

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ except ImportError:
6868
# python2 fallback code
6969
from distutils.spawn import find_executable as which
7070

71+
try:
72+
# python 3 support
73+
from urlparse import urlparse
74+
except ImportError:
75+
from urllib.parse import urlparse
76+
7177

7278
def output(message):
7379
sys.stdout.write(message + "\n")
@@ -2342,21 +2348,13 @@ def run_configure(config_file, args):
23422348
("gpg_passphrase", "Encryption password", "Encryption password is used to protect your files from reading\nby unauthorized persons while in transfer to S3"),
23432349
("gpg_command", "Path to GPG program"),
23442350
("use_https", "Use HTTPS protocol", "When using secure HTTPS protocol all communication with Amazon S3\nservers is protected from 3rd party eavesdropping. This method is\nslower than plain HTTP, and can only be proxied with Python 2.7 or newer"),
2345-
("proxy_host", "HTTP Proxy server name", "On some networks all internet access must go through a HTTP proxy.\nTry setting it here if you can't connect to S3 directly"),
2351+
("proxy_host", "username:[email protected]", "HTTP Proxy server name", "On some networks all internet access must go through a HTTP proxy.\nTry setting it here if you can't connect to S3 directly"),
23462352
("proxy_port", "HTTP Proxy server port"),
23472353
]
23482354
## Option-specfic defaults
23492355
if getattr(cfg, "gpg_command") == "":
23502356
setattr(cfg, "gpg_command", which("gpg"))
23512357

2352-
if getattr(cfg, "proxy_host") == "" and os.getenv("http_proxy"):
2353-
autodetected_encoding = locale.getpreferredencoding() or "UTF-8"
2354-
re_match=re.match(r"(http://)?([^:]+):(\d+)",
2355-
unicodise_s(os.getenv("http_proxy"), autodetected_encoding))
2356-
if re_match:
2357-
setattr(cfg, "proxy_host", re_match.groups()[1])
2358-
setattr(cfg, "proxy_port", re_match.groups()[2])
2359-
23602358
try:
23612359
# Support for python3
23622360
# raw_input only exists in py2 and was renamed to input in py3
@@ -2372,12 +2370,16 @@ def run_configure(config_file, args):
23722370
for option in options:
23732371
prompt = option[1]
23742372
## Option-specific handling
2375-
if option[0] == 'proxy_host' and getattr(cfg, 'use_https') == True and sys.hexversion < 0x02070000:
2376-
setattr(cfg, option[0], "")
2377-
continue
2378-
if option[0] == 'proxy_port' and getattr(cfg, 'proxy_host') == "":
2379-
setattr(cfg, option[0], 0)
2380-
continue
2373+
if option[0] == "proxy_host":
2374+
autodetected_encoding = locale.getpreferredencoding() or "UTF-8"
2375+
key = "https_proxy" if getattr(cfg, 'use_https') else "http_proxy"
2376+
url = urlparse(unicodise_s(os.getenv(key), autodetected_encoding))
2377+
if url.port is None:
2378+
port = 80 if url.scheme == "http" else 443 # httplib supports HTTP(S) proxy only
2379+
else:
2380+
port = url.port
2381+
setattr(cfg, "proxy_host", "%s:%s@%s" % (url.username, url.password, url.hostname))
2382+
setattr(cfg, "proxy_port", port)
23812383

23822384
try:
23832385
val = getattr(cfg, option[0])

0 commit comments

Comments
 (0)