summaryrefslogtreecommitdiff
path: root/lang/python22/patches/patch-af
blob: 5489702ef0b1e932663de67dc883dbdf4714c4e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
$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