$NetBSD: patch-af,v 1.1 2002/10/16 15:51:04 tsarna Exp $

Unofficial patch to support Basic Auth for XML-RPC.

--- Lib/xmlrpclib.py.orig	Tue Oct 15 18:52:10 2002
+++ Lib/xmlrpclib.py	Wed Oct 16 11:44:47 2002
@@ -852,20 +852,55 @@
 
         return self.parse_response(h.getfile())
 
+    ##
+    # Get authorization info from host parameter
+    # Host may be a string, or a (host, x509-dict) tuple; if a string,
+    # it is checked for a 'user:pw@host' format, and a "Basic Auth"
+    # header is created from the 'user:pw' info.
+    #
+    # @return A tuple of: (actual host, base64-encoded Authorization 
+    # header or None, x509 info or empty dictionary)
+
+    def get_host_info(self, host):
+
+        x509 = {}
+        if isinstance(host,tuple):
+            host, x509 = host
+
+        import urllib
+        auth, host = urllib.splituser(host)
+
+        if auth:
+            auth='Basic %s' % auth.encode('base64').strip()
+        else:
+            auth=None
+
+        return host, auth, x509
+
     def getparser(self):
         # get parser and unmarshaller
         return getparser()
 
     def make_connection(self, host):
         # create a HTTP connection object from a host descriptor
+        host, auth, x509 = self.get_host_info(host)
         import httplib
         return httplib.HTTP(host)
 
     def send_request(self, connection, handler, request_body):
         connection.putrequest("POST", handler)
 
+    ##
+    # Send host name (and authorization, if any)
+    #
+    # @param connection Connection handle.
+    # @param host Host object (per get_host_info).
+
     def send_host(self, connection, host):
+        host, auth, x509 = self.get_host_info(host)
         connection.putheader("Host", host)
+        if auth:
+            connection.putheader("Authorization", auth)
 
     def send_user_agent(self, connection):
         connection.putheader("User-Agent", self.user_agent)
@@ -901,11 +936,10 @@
     def make_connection(self, host):
         # create a HTTPS connection object from a host descriptor
         # host may be a string, or a (host, x509-dict) tuple
+
         import httplib
-        if isinstance(host, TupleType):
-            host, x509 = host
-        else:
-            x509 = {}
+        host, auth, x509 = self.get_host_info(host)
+
         try:
             HTTPS = httplib.HTTPS
         except AttributeError:
@@ -914,10 +948,6 @@
         else:
             return apply(HTTPS, (host, None), x509)
 
-    def send_host(self, connection, host):
-        if isinstance(host, TupleType):
-            host, x509 = host
-        connection.putheader("Host", host)
 
 class ServerProxy:
     """uri [,options] -> a logical connection to an XML-RPC server
