Skip to content

Commit 4a8a09f

Browse files
imscaradhjonmmease
authored andcommitted
Using basic auth for proxy tunnel in streaming API (#1133)
Suppport pulling basic auth credentials from `http_proxy` and `https_proxy` environment variables for proxy tunnel.
1 parent f6bbdae commit 4a8a09f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Diff for: plotly/plotly/chunked_requests/chunked_request.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import ssl
55

66
from six.moves import http_client
7-
from six.moves.urllib.parse import urlparse
7+
from six.moves.urllib.parse import urlparse, unquote
8+
9+
from plotly.api import utils
810

911

1012
class Stream:
@@ -86,6 +88,9 @@ def _get_proxy_config(self):
8688

8789
proxy_server = None
8890
proxy_port = None
91+
proxy_username = None
92+
proxy_password = None
93+
proxy_auth = None
8994
ssl_enabled = self._ssl_enabled
9095

9196
if ssl_enabled:
@@ -99,8 +104,15 @@ def _get_proxy_config(self):
99104
p = urlparse(proxy)
100105
proxy_server = p.hostname
101106
proxy_port = p.port
107+
proxy_username = p.username
108+
proxy_password = p.password
109+
110+
if proxy_username and proxy_password:
111+
username = unquote(proxy_username)
112+
password = unquote(proxy_password)
113+
proxy_auth = utils.basic_auth(username, password)
102114

103-
return proxy_server, proxy_port
115+
return proxy_server, proxy_port, proxy_auth
104116

105117
def _get_ssl_context(self):
106118
"""
@@ -123,7 +135,7 @@ def _connect(self):
123135
port = self._port
124136
headers = self._headers
125137
ssl_enabled = self._ssl_enabled
126-
proxy_server, proxy_port = self._get_proxy_config()
138+
proxy_server, proxy_port, proxy_auth = self._get_proxy_config()
127139

128140
if (proxy_server and proxy_port):
129141
if ssl_enabled:
@@ -135,7 +147,12 @@ def _connect(self):
135147
self._conn = http_client.HTTPConnection(
136148
proxy_server, proxy_port
137149
)
138-
self._conn.set_tunnel(server, port)
150+
151+
tunnel_headers = None
152+
if proxy_auth:
153+
tunnel_headers = {'Proxy-Authorization': proxy_auth}
154+
155+
self._conn.set_tunnel(server, port, headers=tunnel_headers)
139156
else:
140157
if ssl_enabled:
141158
context = self._get_ssl_context()

0 commit comments

Comments
 (0)