diff mbox

[2/3] pwclient: Support proxy configuration

Message ID 1481333552-2294-3-git-send-email-thomas.monjalon@6wind.com
State Superseded
Headers show

Commit Message

Thomas Monjalon Dec. 10, 2016, 1:32 a.m. UTC
The environment variables http_proxy and https_proxy can be used
to configure the HTTP transport.

The TCP connection is made with the proxy host, whereas the original host
is maintained in the HTTP POST URI via "handler" in "send_request".

The send_request() method of xmlrpclib has a different signature
and behaviour in Python 2 and 3.

Fixes #47

Signed-off-by: Thomas Monjalon <thomas.monjalon@6wind.com>
---
 patchwork/bin/pwclient | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Comments

Stephen Finucane Dec. 12, 2016, 12:03 p.m. UTC | #1
On Sat, 2016-12-10 at 02:32 +0100, Thomas Monjalon wrote:
> The environment variables http_proxy and https_proxy can be used
> to configure the HTTP transport.
> 
> The TCP connection is made with the proxy host, whereas the original
> host
> is maintained in the HTTP POST URI via "handler" in "send_request".
> 
> The send_request() method of xmlrpclib has a different signature
> and behaviour in Python 2 and 3.
> 
> Fixes #47
> 
> Signed-off-by: Thomas Monjalon <thomas.monjalon@6wind.com>

Works outside of a proxy for me, and behind a proxy for Sean.

Reviewed-by: Stephen Finucane <stephen@that.guru>
diff mbox

Patch

diff --git a/patchwork/bin/pwclient b/patchwork/bin/pwclient
index 9ec8898..42a7fd0 100755
--- a/patchwork/bin/pwclient
+++ b/patchwork/bin/pwclient
@@ -107,12 +107,24 @@  class Transport(xmlrpclib.SafeTransport):
     def __init__(self, url):
         xmlrpclib.SafeTransport.__init__(self)
         self.credentials = None
+        self.host = None
+        self.proxy = None
+        self.scheme = url.split('://', 1)[0]
         self.https = url.startswith('https')
+        if self.https:
+            self.proxy = os.environ.get('https_proxy')
+        else:
+            self.proxy = os.environ.get('http_proxy')
+        if self.proxy is not None:
+            self.https = self.proxy.startswith('https')
 
     def set_credentials(self, username=None, password=None):
         self.credentials = '%s:%s' % (username, password)
 
     def make_connection(self, host):
+        self.host = host
+        if self.proxy is not None:
+            host = self.proxy.split('://', 1)[-1]
         if self.credentials is not None:
             host = '@'.join([self.credentials, host])
         if self.https:
@@ -120,6 +132,18 @@  class Transport(xmlrpclib.SafeTransport):
         else:
             return xmlrpclib.Transport.make_connection(self, host)
 
+    if sys.version_info[0] == 2:
+
+        def send_request(self, connection, handler, request_body):
+            handler = '%s://%s%s' % (self.scheme, self.host, handler)
+            xmlrpclib.Transport.send_request(self, connection, handler, request_body)
+
+    else: # Python 3
+
+        def send_request(self, host, handler, request_body, debug):
+            handler = '%s://%s%s' % (self.scheme, host, handler)
+            return xmlrpclib.Transport.send_request(self, host, handler, request_body, debug)
+
 
 def project_id_by_name(rpc, linkname):
     """Given a project short name, look up the Project ID."""